All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] dma-buf: nuke seqno-fence
@ 2021-07-29  7:03 Christian König
  2021-07-29  7:03 ` [PATCH 2/3] dma-buf: nuke DMA_FENCE_TRACE macros Christian König
                   ` (2 more replies)
  0 siblings, 3 replies; 26+ messages in thread
From: Christian König @ 2021-07-29  7:03 UTC (permalink / raw)
  To: sumit.semwal, gustavo, daniel, linux-media, dri-devel, linaro-mm-sig

Entirely unused.

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/dma-buf/Makefile      |   2 +-
 drivers/dma-buf/seqno-fence.c |  71 ----------------------
 include/linux/seqno-fence.h   | 109 ----------------------------------
 3 files changed, 1 insertion(+), 181 deletions(-)
 delete mode 100644 drivers/dma-buf/seqno-fence.c
 delete mode 100644 include/linux/seqno-fence.h

diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
index 40d81f23cacf..1ef021273a06 100644
--- a/drivers/dma-buf/Makefile
+++ b/drivers/dma-buf/Makefile
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
-	 dma-resv.o seqno-fence.o
+	 dma-resv.o
 obj-$(CONFIG_DMABUF_HEAPS)	+= dma-heap.o
 obj-$(CONFIG_DMABUF_HEAPS)	+= heaps/
 obj-$(CONFIG_SYNC_FILE)		+= sync_file.o
diff --git a/drivers/dma-buf/seqno-fence.c b/drivers/dma-buf/seqno-fence.c
deleted file mode 100644
index bfe14e94c488..000000000000
--- a/drivers/dma-buf/seqno-fence.c
+++ /dev/null
@@ -1,71 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-/*
- * seqno-fence, using a dma-buf to synchronize fencing
- *
- * Copyright (C) 2012 Texas Instruments
- * Copyright (C) 2012-2014 Canonical Ltd
- * Authors:
- *   Rob Clark <robdclark@gmail.com>
- *   Maarten Lankhorst <maarten.lankhorst@canonical.com>
- */
-
-#include <linux/slab.h>
-#include <linux/export.h>
-#include <linux/seqno-fence.h>
-
-static const char *seqno_fence_get_driver_name(struct dma_fence *fence)
-{
-	struct seqno_fence *seqno_fence = to_seqno_fence(fence);
-
-	return seqno_fence->ops->get_driver_name(fence);
-}
-
-static const char *seqno_fence_get_timeline_name(struct dma_fence *fence)
-{
-	struct seqno_fence *seqno_fence = to_seqno_fence(fence);
-
-	return seqno_fence->ops->get_timeline_name(fence);
-}
-
-static bool seqno_enable_signaling(struct dma_fence *fence)
-{
-	struct seqno_fence *seqno_fence = to_seqno_fence(fence);
-
-	return seqno_fence->ops->enable_signaling(fence);
-}
-
-static bool seqno_signaled(struct dma_fence *fence)
-{
-	struct seqno_fence *seqno_fence = to_seqno_fence(fence);
-
-	return seqno_fence->ops->signaled && seqno_fence->ops->signaled(fence);
-}
-
-static void seqno_release(struct dma_fence *fence)
-{
-	struct seqno_fence *f = to_seqno_fence(fence);
-
-	dma_buf_put(f->sync_buf);
-	if (f->ops->release)
-		f->ops->release(fence);
-	else
-		dma_fence_free(&f->base);
-}
-
-static signed long seqno_wait(struct dma_fence *fence, bool intr,
-			      signed long timeout)
-{
-	struct seqno_fence *f = to_seqno_fence(fence);
-
-	return f->ops->wait(fence, intr, timeout);
-}
-
-const struct dma_fence_ops seqno_fence_ops = {
-	.get_driver_name = seqno_fence_get_driver_name,
-	.get_timeline_name = seqno_fence_get_timeline_name,
-	.enable_signaling = seqno_enable_signaling,
-	.signaled = seqno_signaled,
-	.wait = seqno_wait,
-	.release = seqno_release,
-};
-EXPORT_SYMBOL(seqno_fence_ops);
diff --git a/include/linux/seqno-fence.h b/include/linux/seqno-fence.h
deleted file mode 100644
index 3cca2b8fac43..000000000000
--- a/include/linux/seqno-fence.h
+++ /dev/null
@@ -1,109 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-/*
- * seqno-fence, using a dma-buf to synchronize fencing
- *
- * Copyright (C) 2012 Texas Instruments
- * Copyright (C) 2012 Canonical Ltd
- * Authors:
- *   Rob Clark <robdclark@gmail.com>
- *   Maarten Lankhorst <maarten.lankhorst@canonical.com>
- */
-
-#ifndef __LINUX_SEQNO_FENCE_H
-#define __LINUX_SEQNO_FENCE_H
-
-#include <linux/dma-fence.h>
-#include <linux/dma-buf.h>
-
-enum seqno_fence_condition {
-	SEQNO_FENCE_WAIT_GEQUAL,
-	SEQNO_FENCE_WAIT_NONZERO
-};
-
-struct seqno_fence {
-	struct dma_fence base;
-
-	const struct dma_fence_ops *ops;
-	struct dma_buf *sync_buf;
-	uint32_t seqno_ofs;
-	enum seqno_fence_condition condition;
-};
-
-extern const struct dma_fence_ops seqno_fence_ops;
-
-/**
- * to_seqno_fence - cast a fence to a seqno_fence
- * @fence: fence to cast to a seqno_fence
- *
- * Returns NULL if the fence is not a seqno_fence,
- * or the seqno_fence otherwise.
- */
-static inline struct seqno_fence *
-to_seqno_fence(struct dma_fence *fence)
-{
-	if (fence->ops != &seqno_fence_ops)
-		return NULL;
-	return container_of(fence, struct seqno_fence, base);
-}
-
-/**
- * seqno_fence_init - initialize a seqno fence
- * @fence: seqno_fence to initialize
- * @lock: pointer to spinlock to use for fence
- * @sync_buf: buffer containing the memory location to signal on
- * @context: the execution context this fence is a part of
- * @seqno_ofs: the offset within @sync_buf
- * @seqno: the sequence # to signal on
- * @cond: fence wait condition
- * @ops: the fence_ops for operations on this seqno fence
- *
- * This function initializes a struct seqno_fence with passed parameters,
- * and takes a reference on sync_buf which is released on fence destruction.
- *
- * A seqno_fence is a dma_fence which can complete in software when
- * enable_signaling is called, but it also completes when
- * (s32)((sync_buf)[seqno_ofs] - seqno) >= 0 is true
- *
- * The seqno_fence will take a refcount on the sync_buf until it's
- * destroyed, but actual lifetime of sync_buf may be longer if one of the
- * callers take a reference to it.
- *
- * Certain hardware have instructions to insert this type of wait condition
- * in the command stream, so no intervention from software would be needed.
- * This type of fence can be destroyed before completed, however a reference
- * on the sync_buf dma-buf can be taken. It is encouraged to re-use the same
- * dma-buf for sync_buf, since mapping or unmapping the sync_buf to the
- * device's vm can be expensive.
- *
- * It is recommended for creators of seqno_fence to call dma_fence_signal()
- * before destruction. This will prevent possible issues from wraparound at
- * time of issue vs time of check, since users can check dma_fence_is_signaled()
- * before submitting instructions for the hardware to wait on the fence.
- * However, when ops.enable_signaling is not called, it doesn't have to be
- * done as soon as possible, just before there's any real danger of seqno
- * wraparound.
- */
-static inline void
-seqno_fence_init(struct seqno_fence *fence, spinlock_t *lock,
-		 struct dma_buf *sync_buf,  uint32_t context,
-		 uint32_t seqno_ofs, uint32_t seqno,
-		 enum seqno_fence_condition cond,
-		 const struct dma_fence_ops *ops)
-{
-	BUG_ON(!fence || !sync_buf || !ops);
-	BUG_ON(!ops->wait || !ops->enable_signaling ||
-	       !ops->get_driver_name || !ops->get_timeline_name);
-
-	/*
-	 * ops is used in dma_fence_init for get_driver_name, so needs to be
-	 * initialized first
-	 */
-	fence->ops = ops;
-	dma_fence_init(&fence->base, &seqno_fence_ops, lock, context, seqno);
-	get_dma_buf(sync_buf);
-	fence->sync_buf = sync_buf;
-	fence->seqno_ofs = seqno_ofs;
-	fence->condition = cond;
-}
-
-#endif /* __LINUX_SEQNO_FENCE_H */
-- 
2.25.1


^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 2/3] dma-buf: nuke DMA_FENCE_TRACE macros
  2021-07-29  7:03 [PATCH 1/3] dma-buf: nuke seqno-fence Christian König
@ 2021-07-29  7:03 ` Christian König
  2021-07-29  7:22     ` Daniel Vetter
  2021-07-29  7:03 ` [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files Christian König
  2021-07-29  7:24   ` Daniel Vetter
  2 siblings, 1 reply; 26+ messages in thread
From: Christian König @ 2021-07-29  7:03 UTC (permalink / raw)
  To: sumit.semwal, gustavo, daniel, linux-media, dri-devel, linaro-mm-sig

Only the DRM GPU scheduler, radeon and amdgpu where using them and they depend
on a non existing config option to actually emit some code.

Nuke them and clean up the dma_fence_signal* return value.

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/dma-buf/dma-fence.c               | 44 +++++------------------
 drivers/dma-buf/st-dma-fence.c            | 12 ++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 10 +-----
 drivers/gpu/drm/radeon/radeon_fence.c     | 24 +++----------
 drivers/gpu/drm/scheduler/sched_fence.c   | 18 ++--------
 include/linux/dma-fence.h                 | 32 +++--------------
 6 files changed, 23 insertions(+), 117 deletions(-)

diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c
index ce0f5eff575d..21cba0f74e69 100644
--- a/drivers/dma-buf/dma-fence.c
+++ b/drivers/dma-buf/dma-fence.c
@@ -350,12 +350,9 @@ void __dma_fence_might_wait(void)
  *
  * Unlike dma_fence_signal_timestamp(), this function must be called with
  * &dma_fence.lock held.
- *
- * Returns 0 on success and a negative error value when @fence has been
- * signalled already.
  */
-int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
-				      ktime_t timestamp)
+void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
+				       ktime_t timestamp)
 {
 	struct dma_fence_cb *cur, *tmp;
 	struct list_head cb_list;
@@ -364,7 +361,7 @@ int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
 
 	if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
 				      &fence->flags)))
-		return -EINVAL;
+		return;
 
 	/* Stash the cb_list before replacing it with the timestamp */
 	list_replace(&fence->cb_list, &cb_list);
@@ -377,8 +374,6 @@ int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
 		INIT_LIST_HEAD(&cur->node);
 		cur->func(fence, cur);
 	}
-
-	return 0;
 }
 EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
 
@@ -393,23 +388,14 @@ EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
  * can only go from the unsignaled to the signaled state and not back, it will
  * only be effective the first time. Set the timestamp provided as the fence
  * signal timestamp.
- *
- * Returns 0 on success and a negative error value when @fence has been
- * signalled already.
  */
-int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
+void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
 {
 	unsigned long flags;
-	int ret;
-
-	if (!fence)
-		return -EINVAL;
 
 	spin_lock_irqsave(fence->lock, flags);
-	ret = dma_fence_signal_timestamp_locked(fence, timestamp);
+	dma_fence_signal_timestamp_locked(fence, timestamp);
 	spin_unlock_irqrestore(fence->lock, flags);
-
-	return ret;
 }
 EXPORT_SYMBOL(dma_fence_signal_timestamp);
 
@@ -425,13 +411,10 @@ EXPORT_SYMBOL(dma_fence_signal_timestamp);
  *
  * Unlike dma_fence_signal(), this function must be called with &dma_fence.lock
  * held.
- *
- * Returns 0 on success and a negative error value when @fence has been
- * signalled already.
  */
-int dma_fence_signal_locked(struct dma_fence *fence)
+void dma_fence_signal_locked(struct dma_fence *fence)
 {
-	return dma_fence_signal_timestamp_locked(fence, ktime_get());
+	dma_fence_signal_timestamp_locked(fence, ktime_get());
 }
 EXPORT_SYMBOL(dma_fence_signal_locked);
 
@@ -444,28 +427,19 @@ EXPORT_SYMBOL(dma_fence_signal_locked);
  * dma_fence_add_callback(). Can be called multiple times, but since a fence
  * can only go from the unsignaled to the signaled state and not back, it will
  * only be effective the first time.
- *
- * Returns 0 on success and a negative error value when @fence has been
- * signalled already.
  */
-int dma_fence_signal(struct dma_fence *fence)
+void dma_fence_signal(struct dma_fence *fence)
 {
 	unsigned long flags;
-	int ret;
 	bool tmp;
 
-	if (!fence)
-		return -EINVAL;
-
 	tmp = dma_fence_begin_signalling();
 
 	spin_lock_irqsave(fence->lock, flags);
-	ret = dma_fence_signal_timestamp_locked(fence, ktime_get());
+	dma_fence_signal_timestamp_locked(fence, ktime_get());
 	spin_unlock_irqrestore(fence->lock, flags);
 
 	dma_fence_end_signalling(tmp);
-
-	return ret;
 }
 EXPORT_SYMBOL(dma_fence_signal);
 
diff --git a/drivers/dma-buf/st-dma-fence.c b/drivers/dma-buf/st-dma-fence.c
index c8a12d7ad71a..e89001d8873f 100644
--- a/drivers/dma-buf/st-dma-fence.c
+++ b/drivers/dma-buf/st-dma-fence.c
@@ -122,21 +122,13 @@ static int test_signaling(void *arg)
 		goto err_free;
 	}
 
-	if (dma_fence_signal(f)) {
-		pr_err("Fence reported being already signaled\n");
-		goto err_free;
-	}
-
+	dma_fence_signal(f);
 	if (!dma_fence_is_signaled(f)) {
 		pr_err("Fence not reporting signaled\n");
 		goto err_free;
 	}
 
-	if (!dma_fence_signal(f)) {
-		pr_err("Fence reported not being already signaled\n");
-		goto err_free;
-	}
-
+	dma_fence_signal(f);
 	err = 0;
 err_free:
 	dma_fence_put(f);
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
index d4547d195173..397872779c31 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
@@ -246,7 +246,6 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
 	struct amdgpu_device *adev = ring->adev;
 	uint32_t seq, last_seq;
-	int r;
 
 	do {
 		last_seq = atomic_read(&ring->fence_drv.last_seq);
@@ -278,12 +277,7 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
 		if (!fence)
 			continue;
 
-		r = dma_fence_signal(fence);
-		if (!r)
-			DMA_FENCE_TRACE(fence, "signaled from irq context\n");
-		else
-			BUG();
-
+		dma_fence_signal(fence);
 		dma_fence_put(fence);
 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -673,8 +667,6 @@ static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
 	if (!timer_pending(&ring->fence_drv.fallback_timer))
 		amdgpu_fence_schedule_fallback(ring);
 
-	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
-
 	return true;
 }
 
diff --git a/drivers/gpu/drm/radeon/radeon_fence.c b/drivers/gpu/drm/radeon/radeon_fence.c
index 18f2c2e0dfb3..3f351d222cbb 100644
--- a/drivers/gpu/drm/radeon/radeon_fence.c
+++ b/drivers/gpu/drm/radeon/radeon_fence.c
@@ -176,18 +176,11 @@ static int radeon_fence_check_signaled(wait_queue_entry_t *wait, unsigned mode,
 	 */
 	seq = atomic64_read(&fence->rdev->fence_drv[fence->ring].last_seq);
 	if (seq >= fence->seq) {
-		int ret = dma_fence_signal_locked(&fence->base);
-
-		if (!ret)
-			DMA_FENCE_TRACE(&fence->base, "signaled from irq context\n");
-		else
-			DMA_FENCE_TRACE(&fence->base, "was already signaled\n");
-
+		dma_fence_signal_locked(&fence->base);
 		radeon_irq_kms_sw_irq_put(fence->rdev, fence->ring);
 		__remove_wait_queue(&fence->rdev->fence_queue, &fence->fence_wake);
 		dma_fence_put(&fence->base);
-	} else
-		DMA_FENCE_TRACE(&fence->base, "pending\n");
+	}
 	return 0;
 }
 
@@ -422,8 +415,6 @@ static bool radeon_fence_enable_signaling(struct dma_fence *f)
 	fence->fence_wake.func = radeon_fence_check_signaled;
 	__add_wait_queue(&rdev->fence_queue, &fence->fence_wake);
 	dma_fence_get(f);
-
-	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", fence->ring);
 	return true;
 }
 
@@ -441,11 +432,7 @@ bool radeon_fence_signaled(struct radeon_fence *fence)
 		return true;
 
 	if (radeon_fence_seq_signaled(fence->rdev, fence->seq, fence->ring)) {
-		int ret;
-
-		ret = dma_fence_signal(&fence->base);
-		if (!ret)
-			DMA_FENCE_TRACE(&fence->base, "signaled from radeon_fence_signaled\n");
+		dma_fence_signal(&fence->base);
 		return true;
 	}
 	return false;
@@ -550,7 +537,6 @@ long radeon_fence_wait_timeout(struct radeon_fence *fence, bool intr, long timeo
 {
 	uint64_t seq[RADEON_NUM_RINGS] = {};
 	long r;
-	int r_sig;
 
 	/*
 	 * This function should not be called on !radeon fences.
@@ -567,9 +553,7 @@ long radeon_fence_wait_timeout(struct radeon_fence *fence, bool intr, long timeo
 		return r;
 	}
 
-	r_sig = dma_fence_signal(&fence->base);
-	if (!r_sig)
-		DMA_FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
+	dma_fence_signal(&fence->base);
 	return r;
 }
 
diff --git a/drivers/gpu/drm/scheduler/sched_fence.c b/drivers/gpu/drm/scheduler/sched_fence.c
index 69de2c76731f..3736746c47bd 100644
--- a/drivers/gpu/drm/scheduler/sched_fence.c
+++ b/drivers/gpu/drm/scheduler/sched_fence.c
@@ -50,26 +50,12 @@ static void __exit drm_sched_fence_slab_fini(void)
 
 void drm_sched_fence_scheduled(struct drm_sched_fence *fence)
 {
-	int ret = dma_fence_signal(&fence->scheduled);
-
-	if (!ret)
-		DMA_FENCE_TRACE(&fence->scheduled,
-				"signaled from irq context\n");
-	else
-		DMA_FENCE_TRACE(&fence->scheduled,
-				"was already signaled\n");
+	dma_fence_signal(&fence->scheduled);
 }
 
 void drm_sched_fence_finished(struct drm_sched_fence *fence)
 {
-	int ret = dma_fence_signal(&fence->finished);
-
-	if (!ret)
-		DMA_FENCE_TRACE(&fence->finished,
-				"signaled from irq context\n");
-	else
-		DMA_FENCE_TRACE(&fence->finished,
-				"was already signaled\n");
+	dma_fence_signal(&fence->finished);
 }
 
 static const char *drm_sched_fence_get_driver_name(struct dma_fence *fence)
diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
index 6ffb4b2c6371..027db23fd4e3 100644
--- a/include/linux/dma-fence.h
+++ b/include/linux/dma-fence.h
@@ -370,11 +370,11 @@ static inline void dma_fence_end_signalling(bool cookie) {}
 static inline void __dma_fence_might_wait(void) {}
 #endif
 
-int dma_fence_signal(struct dma_fence *fence);
-int dma_fence_signal_locked(struct dma_fence *fence);
-int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp);
-int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
-				      ktime_t timestamp);
+void dma_fence_signal(struct dma_fence *fence);
+void dma_fence_signal_locked(struct dma_fence *fence);
+void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp);
+void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
+				       ktime_t timestamp);
 signed long dma_fence_default_wait(struct dma_fence *fence,
 				   bool intr, signed long timeout);
 int dma_fence_add_callback(struct dma_fence *fence,
@@ -590,26 +590,4 @@ struct dma_fence *dma_fence_get_stub(void);
 struct dma_fence *dma_fence_allocate_private_stub(void);
 u64 dma_fence_context_alloc(unsigned num);
 
-#define DMA_FENCE_TRACE(f, fmt, args...) \
-	do {								\
-		struct dma_fence *__ff = (f);				\
-		if (IS_ENABLED(CONFIG_DMA_FENCE_TRACE))			\
-			pr_info("f %llu#%llu: " fmt,			\
-				__ff->context, __ff->seqno, ##args);	\
-	} while (0)
-
-#define DMA_FENCE_WARN(f, fmt, args...) \
-	do {								\
-		struct dma_fence *__ff = (f);				\
-		pr_warn("f %llu#%llu: " fmt, __ff->context, __ff->seqno,\
-			 ##args);					\
-	} while (0)
-
-#define DMA_FENCE_ERR(f, fmt, args...) \
-	do {								\
-		struct dma_fence *__ff = (f);				\
-		pr_err("f %llu#%llu: " fmt, __ff->context, __ff->seqno,	\
-			##args);					\
-	} while (0)
-
 #endif /* __LINUX_DMA_FENCE_H */
-- 
2.25.1


^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
  2021-07-29  7:03 [PATCH 1/3] dma-buf: nuke seqno-fence Christian König
  2021-07-29  7:03 ` [PATCH 2/3] dma-buf: nuke DMA_FENCE_TRACE macros Christian König
@ 2021-07-29  7:03 ` Christian König
  2021-07-29  7:23     ` Daniel Vetter
  2021-07-29  7:24   ` Daniel Vetter
  2 siblings, 1 reply; 26+ messages in thread
From: Christian König @ 2021-07-29  7:03 UTC (permalink / raw)
  To: sumit.semwal, gustavo, daniel, linux-media, dri-devel, linaro-mm-sig

As we now knew controlling dma_fence synchronization from userspace is
extremely dangerous and can not only deadlock drivers but trivially also the
whole kernel memory management.

Entirely remove this option. We now have in kernel unit tests to exercise the
dma_fence framework and it's containers.

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/dma-buf/Kconfig      |  13 --
 drivers/dma-buf/Makefile     |   1 -
 drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
 drivers/dma-buf/sync_debug.c | 190 ----------------
 drivers/dma-buf/sync_debug.h |  72 ------
 5 files changed, 688 deletions(-)
 delete mode 100644 drivers/dma-buf/sw_sync.c
 delete mode 100644 drivers/dma-buf/sync_debug.c
 delete mode 100644 drivers/dma-buf/sync_debug.h

diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
index 9561e3d2d428..26c53f45616a 100644
--- a/drivers/dma-buf/Kconfig
+++ b/drivers/dma-buf/Kconfig
@@ -17,19 +17,6 @@ config SYNC_FILE
 	  Files fds, to the DRM driver for example. More details at
 	  Documentation/driver-api/sync_file.rst.
 
-config SW_SYNC
-	bool "Sync File Validation Framework"
-	default n
-	depends on SYNC_FILE
-	depends on DEBUG_FS
-	help
-	  A sync object driver that uses a 32bit counter to coordinate
-	  synchronization.  Useful when there is no hardware primitive backing
-	  the synchronization.
-
-	  WARNING: improper use of this can result in deadlocking kernel
-	  drivers from userspace. Intended for test and debug only.
-
 config UDMABUF
 	bool "userspace dmabuf misc driver"
 	default n
diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
index 1ef021273a06..fa3af0953f87 100644
--- a/drivers/dma-buf/Makefile
+++ b/drivers/dma-buf/Makefile
@@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
 obj-$(CONFIG_DMABUF_HEAPS)	+= dma-heap.o
 obj-$(CONFIG_DMABUF_HEAPS)	+= heaps/
 obj-$(CONFIG_SYNC_FILE)		+= sync_file.o
-obj-$(CONFIG_SW_SYNC)		+= sw_sync.o sync_debug.o
 obj-$(CONFIG_UDMABUF)		+= udmabuf.o
 obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
 
diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
deleted file mode 100644
index 348b3a9170fa..000000000000
--- a/drivers/dma-buf/sw_sync.c
+++ /dev/null
@@ -1,412 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-/*
- * Sync File validation framework
- *
- * Copyright (C) 2012 Google, Inc.
- */
-
-#include <linux/file.h>
-#include <linux/fs.h>
-#include <linux/uaccess.h>
-#include <linux/slab.h>
-#include <linux/sync_file.h>
-
-#include "sync_debug.h"
-
-#define CREATE_TRACE_POINTS
-#include "sync_trace.h"
-
-/*
- * SW SYNC validation framework
- *
- * A sync object driver that uses a 32bit counter to coordinate
- * synchronization.  Useful when there is no hardware primitive backing
- * the synchronization.
- *
- * To start the framework just open:
- *
- * <debugfs>/sync/sw_sync
- *
- * That will create a sync timeline, all fences created under this timeline
- * file descriptor will belong to the this timeline.
- *
- * The 'sw_sync' file can be opened many times as to create different
- * timelines.
- *
- * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
- * sw_sync_create_fence_data as parameter.
- *
- * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
- * with the increment as u32. This will update the last signaled value
- * from the timeline and signal any fence that has a seqno smaller or equal
- * to it.
- *
- * struct sw_sync_create_fence_data
- * @value:	the seqno to initialise the fence with
- * @name:	the name of the new sync point
- * @fence:	return the fd of the new sync_file with the created fence
- */
-struct sw_sync_create_fence_data {
-	__u32	value;
-	char	name[32];
-	__s32	fence; /* fd of new fence */
-};
-
-#define SW_SYNC_IOC_MAGIC	'W'
-
-#define SW_SYNC_IOC_CREATE_FENCE	_IOWR(SW_SYNC_IOC_MAGIC, 0,\
-		struct sw_sync_create_fence_data)
-
-#define SW_SYNC_IOC_INC			_IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
-
-static const struct dma_fence_ops timeline_fence_ops;
-
-static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
-{
-	if (fence->ops != &timeline_fence_ops)
-		return NULL;
-	return container_of(fence, struct sync_pt, base);
-}
-
-/**
- * sync_timeline_create() - creates a sync object
- * @name:	sync_timeline name
- *
- * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
- * case of error.
- */
-static struct sync_timeline *sync_timeline_create(const char *name)
-{
-	struct sync_timeline *obj;
-
-	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
-	if (!obj)
-		return NULL;
-
-	kref_init(&obj->kref);
-	obj->context = dma_fence_context_alloc(1);
-	strlcpy(obj->name, name, sizeof(obj->name));
-
-	obj->pt_tree = RB_ROOT;
-	INIT_LIST_HEAD(&obj->pt_list);
-	spin_lock_init(&obj->lock);
-
-	sync_timeline_debug_add(obj);
-
-	return obj;
-}
-
-static void sync_timeline_free(struct kref *kref)
-{
-	struct sync_timeline *obj =
-		container_of(kref, struct sync_timeline, kref);
-
-	sync_timeline_debug_remove(obj);
-
-	kfree(obj);
-}
-
-static void sync_timeline_get(struct sync_timeline *obj)
-{
-	kref_get(&obj->kref);
-}
-
-static void sync_timeline_put(struct sync_timeline *obj)
-{
-	kref_put(&obj->kref, sync_timeline_free);
-}
-
-static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
-{
-	return "sw_sync";
-}
-
-static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
-{
-	struct sync_timeline *parent = dma_fence_parent(fence);
-
-	return parent->name;
-}
-
-static void timeline_fence_release(struct dma_fence *fence)
-{
-	struct sync_pt *pt = dma_fence_to_sync_pt(fence);
-	struct sync_timeline *parent = dma_fence_parent(fence);
-	unsigned long flags;
-
-	spin_lock_irqsave(fence->lock, flags);
-	if (!list_empty(&pt->link)) {
-		list_del(&pt->link);
-		rb_erase(&pt->node, &parent->pt_tree);
-	}
-	spin_unlock_irqrestore(fence->lock, flags);
-
-	sync_timeline_put(parent);
-	dma_fence_free(fence);
-}
-
-static bool timeline_fence_signaled(struct dma_fence *fence)
-{
-	struct sync_timeline *parent = dma_fence_parent(fence);
-
-	return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
-}
-
-static bool timeline_fence_enable_signaling(struct dma_fence *fence)
-{
-	return true;
-}
-
-static void timeline_fence_value_str(struct dma_fence *fence,
-				    char *str, int size)
-{
-	snprintf(str, size, "%lld", fence->seqno);
-}
-
-static void timeline_fence_timeline_value_str(struct dma_fence *fence,
-					     char *str, int size)
-{
-	struct sync_timeline *parent = dma_fence_parent(fence);
-
-	snprintf(str, size, "%d", parent->value);
-}
-
-static const struct dma_fence_ops timeline_fence_ops = {
-	.get_driver_name = timeline_fence_get_driver_name,
-	.get_timeline_name = timeline_fence_get_timeline_name,
-	.enable_signaling = timeline_fence_enable_signaling,
-	.signaled = timeline_fence_signaled,
-	.release = timeline_fence_release,
-	.fence_value_str = timeline_fence_value_str,
-	.timeline_value_str = timeline_fence_timeline_value_str,
-};
-
-/**
- * sync_timeline_signal() - signal a status change on a sync_timeline
- * @obj:	sync_timeline to signal
- * @inc:	num to increment on timeline->value
- *
- * A sync implementation should call this any time one of it's fences
- * has signaled or has an error condition.
- */
-static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
-{
-	struct sync_pt *pt, *next;
-
-	trace_sync_timeline(obj);
-
-	spin_lock_irq(&obj->lock);
-
-	obj->value += inc;
-
-	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
-		if (!timeline_fence_signaled(&pt->base))
-			break;
-
-		list_del_init(&pt->link);
-		rb_erase(&pt->node, &obj->pt_tree);
-
-		/*
-		 * A signal callback may release the last reference to this
-		 * fence, causing it to be freed. That operation has to be
-		 * last to avoid a use after free inside this loop, and must
-		 * be after we remove the fence from the timeline in order to
-		 * prevent deadlocking on timeline->lock inside
-		 * timeline_fence_release().
-		 */
-		dma_fence_signal_locked(&pt->base);
-	}
-
-	spin_unlock_irq(&obj->lock);
-}
-
-/**
- * sync_pt_create() - creates a sync pt
- * @obj:	parent sync_timeline
- * @value:	value of the fence
- *
- * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
- * allocated allowing for implementation specific data to be kept after
- * the generic sync_timeline struct. Returns the sync_pt object or
- * NULL in case of error.
- */
-static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
-				      unsigned int value)
-{
-	struct sync_pt *pt;
-
-	pt = kzalloc(sizeof(*pt), GFP_KERNEL);
-	if (!pt)
-		return NULL;
-
-	sync_timeline_get(obj);
-	dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
-		       obj->context, value);
-	INIT_LIST_HEAD(&pt->link);
-
-	spin_lock_irq(&obj->lock);
-	if (!dma_fence_is_signaled_locked(&pt->base)) {
-		struct rb_node **p = &obj->pt_tree.rb_node;
-		struct rb_node *parent = NULL;
-
-		while (*p) {
-			struct sync_pt *other;
-			int cmp;
-
-			parent = *p;
-			other = rb_entry(parent, typeof(*pt), node);
-			cmp = value - other->base.seqno;
-			if (cmp > 0) {
-				p = &parent->rb_right;
-			} else if (cmp < 0) {
-				p = &parent->rb_left;
-			} else {
-				if (dma_fence_get_rcu(&other->base)) {
-					sync_timeline_put(obj);
-					kfree(pt);
-					pt = other;
-					goto unlock;
-				}
-				p = &parent->rb_left;
-			}
-		}
-		rb_link_node(&pt->node, parent, p);
-		rb_insert_color(&pt->node, &obj->pt_tree);
-
-		parent = rb_next(&pt->node);
-		list_add_tail(&pt->link,
-			      parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
-	}
-unlock:
-	spin_unlock_irq(&obj->lock);
-
-	return pt;
-}
-
-/*
- * *WARNING*
- *
- * improper use of this can result in deadlocking kernel drivers from userspace.
- */
-
-/* opening sw_sync create a new sync obj */
-static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
-{
-	struct sync_timeline *obj;
-	char task_comm[TASK_COMM_LEN];
-
-	get_task_comm(task_comm, current);
-
-	obj = sync_timeline_create(task_comm);
-	if (!obj)
-		return -ENOMEM;
-
-	file->private_data = obj;
-
-	return 0;
-}
-
-static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
-{
-	struct sync_timeline *obj = file->private_data;
-	struct sync_pt *pt, *next;
-
-	spin_lock_irq(&obj->lock);
-
-	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
-		dma_fence_set_error(&pt->base, -ENOENT);
-		dma_fence_signal_locked(&pt->base);
-	}
-
-	spin_unlock_irq(&obj->lock);
-
-	sync_timeline_put(obj);
-	return 0;
-}
-
-static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
-				       unsigned long arg)
-{
-	int fd = get_unused_fd_flags(O_CLOEXEC);
-	int err;
-	struct sync_pt *pt;
-	struct sync_file *sync_file;
-	struct sw_sync_create_fence_data data;
-
-	if (fd < 0)
-		return fd;
-
-	if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
-		err = -EFAULT;
-		goto err;
-	}
-
-	pt = sync_pt_create(obj, data.value);
-	if (!pt) {
-		err = -ENOMEM;
-		goto err;
-	}
-
-	sync_file = sync_file_create(&pt->base);
-	dma_fence_put(&pt->base);
-	if (!sync_file) {
-		err = -ENOMEM;
-		goto err;
-	}
-
-	data.fence = fd;
-	if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
-		fput(sync_file->file);
-		err = -EFAULT;
-		goto err;
-	}
-
-	fd_install(fd, sync_file->file);
-
-	return 0;
-
-err:
-	put_unused_fd(fd);
-	return err;
-}
-
-static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
-{
-	u32 value;
-
-	if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
-		return -EFAULT;
-
-	while (value > INT_MAX)  {
-		sync_timeline_signal(obj, INT_MAX);
-		value -= INT_MAX;
-	}
-
-	sync_timeline_signal(obj, value);
-
-	return 0;
-}
-
-static long sw_sync_ioctl(struct file *file, unsigned int cmd,
-			  unsigned long arg)
-{
-	struct sync_timeline *obj = file->private_data;
-
-	switch (cmd) {
-	case SW_SYNC_IOC_CREATE_FENCE:
-		return sw_sync_ioctl_create_fence(obj, arg);
-
-	case SW_SYNC_IOC_INC:
-		return sw_sync_ioctl_inc(obj, arg);
-
-	default:
-		return -ENOTTY;
-	}
-}
-
-const struct file_operations sw_sync_debugfs_fops = {
-	.open           = sw_sync_debugfs_open,
-	.release        = sw_sync_debugfs_release,
-	.unlocked_ioctl = sw_sync_ioctl,
-	.compat_ioctl	= compat_ptr_ioctl,
-};
diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
deleted file mode 100644
index 101394f16930..000000000000
--- a/drivers/dma-buf/sync_debug.c
+++ /dev/null
@@ -1,190 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-/*
- * Sync File validation framework and debug information
- *
- * Copyright (C) 2012 Google, Inc.
- */
-
-#include <linux/debugfs.h>
-#include "sync_debug.h"
-
-static struct dentry *dbgfs;
-
-static LIST_HEAD(sync_timeline_list_head);
-static DEFINE_SPINLOCK(sync_timeline_list_lock);
-static LIST_HEAD(sync_file_list_head);
-static DEFINE_SPINLOCK(sync_file_list_lock);
-
-void sync_timeline_debug_add(struct sync_timeline *obj)
-{
-	unsigned long flags;
-
-	spin_lock_irqsave(&sync_timeline_list_lock, flags);
-	list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
-	spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
-}
-
-void sync_timeline_debug_remove(struct sync_timeline *obj)
-{
-	unsigned long flags;
-
-	spin_lock_irqsave(&sync_timeline_list_lock, flags);
-	list_del(&obj->sync_timeline_list);
-	spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
-}
-
-void sync_file_debug_add(struct sync_file *sync_file)
-{
-	unsigned long flags;
-
-	spin_lock_irqsave(&sync_file_list_lock, flags);
-	list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
-	spin_unlock_irqrestore(&sync_file_list_lock, flags);
-}
-
-void sync_file_debug_remove(struct sync_file *sync_file)
-{
-	unsigned long flags;
-
-	spin_lock_irqsave(&sync_file_list_lock, flags);
-	list_del(&sync_file->sync_file_list);
-	spin_unlock_irqrestore(&sync_file_list_lock, flags);
-}
-
-static const char *sync_status_str(int status)
-{
-	if (status < 0)
-		return "error";
-
-	if (status > 0)
-		return "signaled";
-
-	return "active";
-}
-
-static void sync_print_fence(struct seq_file *s,
-			     struct dma_fence *fence, bool show)
-{
-	struct sync_timeline *parent = dma_fence_parent(fence);
-	int status;
-
-	status = dma_fence_get_status_locked(fence);
-
-	seq_printf(s, "  %s%sfence %s",
-		   show ? parent->name : "",
-		   show ? "_" : "",
-		   sync_status_str(status));
-
-	if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
-		struct timespec64 ts64 =
-			ktime_to_timespec64(fence->timestamp);
-
-		seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
-	}
-
-	if (fence->ops->timeline_value_str &&
-		fence->ops->fence_value_str) {
-		char value[64];
-		bool success;
-
-		fence->ops->fence_value_str(fence, value, sizeof(value));
-		success = strlen(value);
-
-		if (success) {
-			seq_printf(s, ": %s", value);
-
-			fence->ops->timeline_value_str(fence, value,
-						       sizeof(value));
-
-			if (strlen(value))
-				seq_printf(s, " / %s", value);
-		}
-	}
-
-	seq_putc(s, '\n');
-}
-
-static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
-{
-	struct list_head *pos;
-
-	seq_printf(s, "%s: %d\n", obj->name, obj->value);
-
-	spin_lock_irq(&obj->lock);
-	list_for_each(pos, &obj->pt_list) {
-		struct sync_pt *pt = container_of(pos, struct sync_pt, link);
-		sync_print_fence(s, &pt->base, false);
-	}
-	spin_unlock_irq(&obj->lock);
-}
-
-static void sync_print_sync_file(struct seq_file *s,
-				  struct sync_file *sync_file)
-{
-	char buf[128];
-	int i;
-
-	seq_printf(s, "[%p] %s: %s\n", sync_file,
-		   sync_file_get_name(sync_file, buf, sizeof(buf)),
-		   sync_status_str(dma_fence_get_status(sync_file->fence)));
-
-	if (dma_fence_is_array(sync_file->fence)) {
-		struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
-
-		for (i = 0; i < array->num_fences; ++i)
-			sync_print_fence(s, array->fences[i], true);
-	} else {
-		sync_print_fence(s, sync_file->fence, true);
-	}
-}
-
-static int sync_info_debugfs_show(struct seq_file *s, void *unused)
-{
-	struct list_head *pos;
-
-	seq_puts(s, "objs:\n--------------\n");
-
-	spin_lock_irq(&sync_timeline_list_lock);
-	list_for_each(pos, &sync_timeline_list_head) {
-		struct sync_timeline *obj =
-			container_of(pos, struct sync_timeline,
-				     sync_timeline_list);
-
-		sync_print_obj(s, obj);
-		seq_putc(s, '\n');
-	}
-	spin_unlock_irq(&sync_timeline_list_lock);
-
-	seq_puts(s, "fences:\n--------------\n");
-
-	spin_lock_irq(&sync_file_list_lock);
-	list_for_each(pos, &sync_file_list_head) {
-		struct sync_file *sync_file =
-			container_of(pos, struct sync_file, sync_file_list);
-
-		sync_print_sync_file(s, sync_file);
-		seq_putc(s, '\n');
-	}
-	spin_unlock_irq(&sync_file_list_lock);
-	return 0;
-}
-
-DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
-
-static __init int sync_debugfs_init(void)
-{
-	dbgfs = debugfs_create_dir("sync", NULL);
-
-	/*
-	 * The debugfs files won't ever get removed and thus, there is
-	 * no need to protect it against removal races. The use of
-	 * debugfs_create_file_unsafe() is actually safe here.
-	 */
-	debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
-				   &sync_info_debugfs_fops);
-	debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
-				   &sw_sync_debugfs_fops);
-
-	return 0;
-}
-late_initcall(sync_debugfs_init);
diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
deleted file mode 100644
index 6176e52ba2d7..000000000000
--- a/drivers/dma-buf/sync_debug.h
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Sync File validation framework and debug infomation
- *
- * Copyright (C) 2012 Google, Inc.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- */
-
-#ifndef _LINUX_SYNC_H
-#define _LINUX_SYNC_H
-
-#include <linux/list.h>
-#include <linux/rbtree.h>
-#include <linux/spinlock.h>
-#include <linux/dma-fence.h>
-
-#include <linux/sync_file.h>
-#include <uapi/linux/sync_file.h>
-
-/**
- * struct sync_timeline - sync object
- * @kref:		reference count on fence.
- * @name:		name of the sync_timeline. Useful for debugging
- * @lock:		lock protecting @pt_list and @value
- * @pt_tree:		rbtree of active (unsignaled/errored) sync_pts
- * @pt_list:		list of active (unsignaled/errored) sync_pts
- * @sync_timeline_list:	membership in global sync_timeline_list
- */
-struct sync_timeline {
-	struct kref		kref;
-	char			name[32];
-
-	/* protected by lock */
-	u64			context;
-	int			value;
-
-	struct rb_root		pt_tree;
-	struct list_head	pt_list;
-	spinlock_t		lock;
-
-	struct list_head	sync_timeline_list;
-};
-
-static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
-{
-	return container_of(fence->lock, struct sync_timeline, lock);
-}
-
-/**
- * struct sync_pt - sync_pt object
- * @base: base fence object
- * @link: link on the sync timeline's list
- * @node: node in the sync timeline's tree
- */
-struct sync_pt {
-	struct dma_fence base;
-	struct list_head link;
-	struct rb_node node;
-};
-
-extern const struct file_operations sw_sync_debugfs_fops;
-
-void sync_timeline_debug_add(struct sync_timeline *obj);
-void sync_timeline_debug_remove(struct sync_timeline *obj);
-void sync_file_debug_add(struct sync_file *fence);
-void sync_file_debug_remove(struct sync_file *fence);
-
-#endif /* _LINUX_SYNC_H */
-- 
2.25.1


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 2/3] dma-buf: nuke DMA_FENCE_TRACE macros
  2021-07-29  7:03 ` [PATCH 2/3] dma-buf: nuke DMA_FENCE_TRACE macros Christian König
@ 2021-07-29  7:22     ` Daniel Vetter
  0 siblings, 0 replies; 26+ messages in thread
From: Daniel Vetter @ 2021-07-29  7:22 UTC (permalink / raw)
  To: Christian König
  Cc: sumit.semwal, gustavo, daniel, linux-media, dri-devel, linaro-mm-sig

On Thu, Jul 29, 2021 at 09:03:29AM +0200, Christian König wrote:
> Only the DRM GPU scheduler, radeon and amdgpu where using them and they depend
> on a non existing config option to actually emit some code.
> 
> Nuke them and clean up the dma_fence_signal* return value.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>

So i915 is very much become a horror show of wheel reinventing (and we're
trying to fix that), but there's some legit hand-rolled use-cases for
having the return value of dma_fence_signal. We're not using that right
now because hand-rollled nonsense, but can you perhaps keep that part?

Other pieces lgtm.
-Daniel

> ---
>  drivers/dma-buf/dma-fence.c               | 44 +++++------------------
>  drivers/dma-buf/st-dma-fence.c            | 12 ++-----
>  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 10 +-----
>  drivers/gpu/drm/radeon/radeon_fence.c     | 24 +++----------
>  drivers/gpu/drm/scheduler/sched_fence.c   | 18 ++--------
>  include/linux/dma-fence.h                 | 32 +++--------------
>  6 files changed, 23 insertions(+), 117 deletions(-)
> 
> diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c
> index ce0f5eff575d..21cba0f74e69 100644
> --- a/drivers/dma-buf/dma-fence.c
> +++ b/drivers/dma-buf/dma-fence.c
> @@ -350,12 +350,9 @@ void __dma_fence_might_wait(void)
>   *
>   * Unlike dma_fence_signal_timestamp(), this function must be called with
>   * &dma_fence.lock held.
> - *
> - * Returns 0 on success and a negative error value when @fence has been
> - * signalled already.
>   */
> -int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
> -				      ktime_t timestamp)
> +void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
> +				       ktime_t timestamp)
>  {
>  	struct dma_fence_cb *cur, *tmp;
>  	struct list_head cb_list;
> @@ -364,7 +361,7 @@ int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>  
>  	if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
>  				      &fence->flags)))
> -		return -EINVAL;
> +		return;
>  
>  	/* Stash the cb_list before replacing it with the timestamp */
>  	list_replace(&fence->cb_list, &cb_list);
> @@ -377,8 +374,6 @@ int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>  		INIT_LIST_HEAD(&cur->node);
>  		cur->func(fence, cur);
>  	}
> -
> -	return 0;
>  }
>  EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
>  
> @@ -393,23 +388,14 @@ EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
>   * can only go from the unsignaled to the signaled state and not back, it will
>   * only be effective the first time. Set the timestamp provided as the fence
>   * signal timestamp.
> - *
> - * Returns 0 on success and a negative error value when @fence has been
> - * signalled already.
>   */
> -int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
> +void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
>  {
>  	unsigned long flags;
> -	int ret;
> -
> -	if (!fence)
> -		return -EINVAL;
>  
>  	spin_lock_irqsave(fence->lock, flags);
> -	ret = dma_fence_signal_timestamp_locked(fence, timestamp);
> +	dma_fence_signal_timestamp_locked(fence, timestamp);
>  	spin_unlock_irqrestore(fence->lock, flags);
> -
> -	return ret;
>  }
>  EXPORT_SYMBOL(dma_fence_signal_timestamp);
>  
> @@ -425,13 +411,10 @@ EXPORT_SYMBOL(dma_fence_signal_timestamp);
>   *
>   * Unlike dma_fence_signal(), this function must be called with &dma_fence.lock
>   * held.
> - *
> - * Returns 0 on success and a negative error value when @fence has been
> - * signalled already.
>   */
> -int dma_fence_signal_locked(struct dma_fence *fence)
> +void dma_fence_signal_locked(struct dma_fence *fence)
>  {
> -	return dma_fence_signal_timestamp_locked(fence, ktime_get());
> +	dma_fence_signal_timestamp_locked(fence, ktime_get());
>  }
>  EXPORT_SYMBOL(dma_fence_signal_locked);
>  
> @@ -444,28 +427,19 @@ EXPORT_SYMBOL(dma_fence_signal_locked);
>   * dma_fence_add_callback(). Can be called multiple times, but since a fence
>   * can only go from the unsignaled to the signaled state and not back, it will
>   * only be effective the first time.
> - *
> - * Returns 0 on success and a negative error value when @fence has been
> - * signalled already.
>   */
> -int dma_fence_signal(struct dma_fence *fence)
> +void dma_fence_signal(struct dma_fence *fence)
>  {
>  	unsigned long flags;
> -	int ret;
>  	bool tmp;
>  
> -	if (!fence)
> -		return -EINVAL;
> -
>  	tmp = dma_fence_begin_signalling();
>  
>  	spin_lock_irqsave(fence->lock, flags);
> -	ret = dma_fence_signal_timestamp_locked(fence, ktime_get());
> +	dma_fence_signal_timestamp_locked(fence, ktime_get());
>  	spin_unlock_irqrestore(fence->lock, flags);
>  
>  	dma_fence_end_signalling(tmp);
> -
> -	return ret;
>  }
>  EXPORT_SYMBOL(dma_fence_signal);
>  
> diff --git a/drivers/dma-buf/st-dma-fence.c b/drivers/dma-buf/st-dma-fence.c
> index c8a12d7ad71a..e89001d8873f 100644
> --- a/drivers/dma-buf/st-dma-fence.c
> +++ b/drivers/dma-buf/st-dma-fence.c
> @@ -122,21 +122,13 @@ static int test_signaling(void *arg)
>  		goto err_free;
>  	}
>  
> -	if (dma_fence_signal(f)) {
> -		pr_err("Fence reported being already signaled\n");
> -		goto err_free;
> -	}
> -
> +	dma_fence_signal(f);
>  	if (!dma_fence_is_signaled(f)) {
>  		pr_err("Fence not reporting signaled\n");
>  		goto err_free;
>  	}
>  
> -	if (!dma_fence_signal(f)) {
> -		pr_err("Fence reported not being already signaled\n");
> -		goto err_free;
> -	}
> -
> +	dma_fence_signal(f);
>  	err = 0;
>  err_free:
>  	dma_fence_put(f);
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
> index d4547d195173..397872779c31 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
> @@ -246,7 +246,6 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
>  	struct amdgpu_fence_driver *drv = &ring->fence_drv;
>  	struct amdgpu_device *adev = ring->adev;
>  	uint32_t seq, last_seq;
> -	int r;
>  
>  	do {
>  		last_seq = atomic_read(&ring->fence_drv.last_seq);
> @@ -278,12 +277,7 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
>  		if (!fence)
>  			continue;
>  
> -		r = dma_fence_signal(fence);
> -		if (!r)
> -			DMA_FENCE_TRACE(fence, "signaled from irq context\n");
> -		else
> -			BUG();
> -
> +		dma_fence_signal(fence);
>  		dma_fence_put(fence);
>  		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
>  		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
> @@ -673,8 +667,6 @@ static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
>  	if (!timer_pending(&ring->fence_drv.fallback_timer))
>  		amdgpu_fence_schedule_fallback(ring);
>  
> -	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
> -
>  	return true;
>  }
>  
> diff --git a/drivers/gpu/drm/radeon/radeon_fence.c b/drivers/gpu/drm/radeon/radeon_fence.c
> index 18f2c2e0dfb3..3f351d222cbb 100644
> --- a/drivers/gpu/drm/radeon/radeon_fence.c
> +++ b/drivers/gpu/drm/radeon/radeon_fence.c
> @@ -176,18 +176,11 @@ static int radeon_fence_check_signaled(wait_queue_entry_t *wait, unsigned mode,
>  	 */
>  	seq = atomic64_read(&fence->rdev->fence_drv[fence->ring].last_seq);
>  	if (seq >= fence->seq) {
> -		int ret = dma_fence_signal_locked(&fence->base);
> -
> -		if (!ret)
> -			DMA_FENCE_TRACE(&fence->base, "signaled from irq context\n");
> -		else
> -			DMA_FENCE_TRACE(&fence->base, "was already signaled\n");
> -
> +		dma_fence_signal_locked(&fence->base);
>  		radeon_irq_kms_sw_irq_put(fence->rdev, fence->ring);
>  		__remove_wait_queue(&fence->rdev->fence_queue, &fence->fence_wake);
>  		dma_fence_put(&fence->base);
> -	} else
> -		DMA_FENCE_TRACE(&fence->base, "pending\n");
> +	}
>  	return 0;
>  }
>  
> @@ -422,8 +415,6 @@ static bool radeon_fence_enable_signaling(struct dma_fence *f)
>  	fence->fence_wake.func = radeon_fence_check_signaled;
>  	__add_wait_queue(&rdev->fence_queue, &fence->fence_wake);
>  	dma_fence_get(f);
> -
> -	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", fence->ring);
>  	return true;
>  }
>  
> @@ -441,11 +432,7 @@ bool radeon_fence_signaled(struct radeon_fence *fence)
>  		return true;
>  
>  	if (radeon_fence_seq_signaled(fence->rdev, fence->seq, fence->ring)) {
> -		int ret;
> -
> -		ret = dma_fence_signal(&fence->base);
> -		if (!ret)
> -			DMA_FENCE_TRACE(&fence->base, "signaled from radeon_fence_signaled\n");
> +		dma_fence_signal(&fence->base);
>  		return true;
>  	}
>  	return false;
> @@ -550,7 +537,6 @@ long radeon_fence_wait_timeout(struct radeon_fence *fence, bool intr, long timeo
>  {
>  	uint64_t seq[RADEON_NUM_RINGS] = {};
>  	long r;
> -	int r_sig;
>  
>  	/*
>  	 * This function should not be called on !radeon fences.
> @@ -567,9 +553,7 @@ long radeon_fence_wait_timeout(struct radeon_fence *fence, bool intr, long timeo
>  		return r;
>  	}
>  
> -	r_sig = dma_fence_signal(&fence->base);
> -	if (!r_sig)
> -		DMA_FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
> +	dma_fence_signal(&fence->base);
>  	return r;
>  }
>  
> diff --git a/drivers/gpu/drm/scheduler/sched_fence.c b/drivers/gpu/drm/scheduler/sched_fence.c
> index 69de2c76731f..3736746c47bd 100644
> --- a/drivers/gpu/drm/scheduler/sched_fence.c
> +++ b/drivers/gpu/drm/scheduler/sched_fence.c
> @@ -50,26 +50,12 @@ static void __exit drm_sched_fence_slab_fini(void)
>  
>  void drm_sched_fence_scheduled(struct drm_sched_fence *fence)
>  {
> -	int ret = dma_fence_signal(&fence->scheduled);
> -
> -	if (!ret)
> -		DMA_FENCE_TRACE(&fence->scheduled,
> -				"signaled from irq context\n");
> -	else
> -		DMA_FENCE_TRACE(&fence->scheduled,
> -				"was already signaled\n");
> +	dma_fence_signal(&fence->scheduled);
>  }
>  
>  void drm_sched_fence_finished(struct drm_sched_fence *fence)
>  {
> -	int ret = dma_fence_signal(&fence->finished);
> -
> -	if (!ret)
> -		DMA_FENCE_TRACE(&fence->finished,
> -				"signaled from irq context\n");
> -	else
> -		DMA_FENCE_TRACE(&fence->finished,
> -				"was already signaled\n");
> +	dma_fence_signal(&fence->finished);
>  }
>  
>  static const char *drm_sched_fence_get_driver_name(struct dma_fence *fence)
> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
> index 6ffb4b2c6371..027db23fd4e3 100644
> --- a/include/linux/dma-fence.h
> +++ b/include/linux/dma-fence.h
> @@ -370,11 +370,11 @@ static inline void dma_fence_end_signalling(bool cookie) {}
>  static inline void __dma_fence_might_wait(void) {}
>  #endif
>  
> -int dma_fence_signal(struct dma_fence *fence);
> -int dma_fence_signal_locked(struct dma_fence *fence);
> -int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp);
> -int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
> -				      ktime_t timestamp);
> +void dma_fence_signal(struct dma_fence *fence);
> +void dma_fence_signal_locked(struct dma_fence *fence);
> +void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp);
> +void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
> +				       ktime_t timestamp);
>  signed long dma_fence_default_wait(struct dma_fence *fence,
>  				   bool intr, signed long timeout);
>  int dma_fence_add_callback(struct dma_fence *fence,
> @@ -590,26 +590,4 @@ struct dma_fence *dma_fence_get_stub(void);
>  struct dma_fence *dma_fence_allocate_private_stub(void);
>  u64 dma_fence_context_alloc(unsigned num);
>  
> -#define DMA_FENCE_TRACE(f, fmt, args...) \
> -	do {								\
> -		struct dma_fence *__ff = (f);				\
> -		if (IS_ENABLED(CONFIG_DMA_FENCE_TRACE))			\
> -			pr_info("f %llu#%llu: " fmt,			\
> -				__ff->context, __ff->seqno, ##args);	\
> -	} while (0)
> -
> -#define DMA_FENCE_WARN(f, fmt, args...) \
> -	do {								\
> -		struct dma_fence *__ff = (f);				\
> -		pr_warn("f %llu#%llu: " fmt, __ff->context, __ff->seqno,\
> -			 ##args);					\
> -	} while (0)
> -
> -#define DMA_FENCE_ERR(f, fmt, args...) \
> -	do {								\
> -		struct dma_fence *__ff = (f);				\
> -		pr_err("f %llu#%llu: " fmt, __ff->context, __ff->seqno,	\
> -			##args);					\
> -	} while (0)
> -
>  #endif /* __LINUX_DMA_FENCE_H */
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 2/3] dma-buf: nuke DMA_FENCE_TRACE macros
@ 2021-07-29  7:22     ` Daniel Vetter
  0 siblings, 0 replies; 26+ messages in thread
From: Daniel Vetter @ 2021-07-29  7:22 UTC (permalink / raw)
  To: Christian König; +Cc: gustavo, dri-devel, linaro-mm-sig, linux-media

On Thu, Jul 29, 2021 at 09:03:29AM +0200, Christian König wrote:
> Only the DRM GPU scheduler, radeon and amdgpu where using them and they depend
> on a non existing config option to actually emit some code.
> 
> Nuke them and clean up the dma_fence_signal* return value.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>

So i915 is very much become a horror show of wheel reinventing (and we're
trying to fix that), but there's some legit hand-rolled use-cases for
having the return value of dma_fence_signal. We're not using that right
now because hand-rollled nonsense, but can you perhaps keep that part?

Other pieces lgtm.
-Daniel

> ---
>  drivers/dma-buf/dma-fence.c               | 44 +++++------------------
>  drivers/dma-buf/st-dma-fence.c            | 12 ++-----
>  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 10 +-----
>  drivers/gpu/drm/radeon/radeon_fence.c     | 24 +++----------
>  drivers/gpu/drm/scheduler/sched_fence.c   | 18 ++--------
>  include/linux/dma-fence.h                 | 32 +++--------------
>  6 files changed, 23 insertions(+), 117 deletions(-)
> 
> diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c
> index ce0f5eff575d..21cba0f74e69 100644
> --- a/drivers/dma-buf/dma-fence.c
> +++ b/drivers/dma-buf/dma-fence.c
> @@ -350,12 +350,9 @@ void __dma_fence_might_wait(void)
>   *
>   * Unlike dma_fence_signal_timestamp(), this function must be called with
>   * &dma_fence.lock held.
> - *
> - * Returns 0 on success and a negative error value when @fence has been
> - * signalled already.
>   */
> -int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
> -				      ktime_t timestamp)
> +void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
> +				       ktime_t timestamp)
>  {
>  	struct dma_fence_cb *cur, *tmp;
>  	struct list_head cb_list;
> @@ -364,7 +361,7 @@ int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>  
>  	if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
>  				      &fence->flags)))
> -		return -EINVAL;
> +		return;
>  
>  	/* Stash the cb_list before replacing it with the timestamp */
>  	list_replace(&fence->cb_list, &cb_list);
> @@ -377,8 +374,6 @@ int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>  		INIT_LIST_HEAD(&cur->node);
>  		cur->func(fence, cur);
>  	}
> -
> -	return 0;
>  }
>  EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
>  
> @@ -393,23 +388,14 @@ EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
>   * can only go from the unsignaled to the signaled state and not back, it will
>   * only be effective the first time. Set the timestamp provided as the fence
>   * signal timestamp.
> - *
> - * Returns 0 on success and a negative error value when @fence has been
> - * signalled already.
>   */
> -int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
> +void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
>  {
>  	unsigned long flags;
> -	int ret;
> -
> -	if (!fence)
> -		return -EINVAL;
>  
>  	spin_lock_irqsave(fence->lock, flags);
> -	ret = dma_fence_signal_timestamp_locked(fence, timestamp);
> +	dma_fence_signal_timestamp_locked(fence, timestamp);
>  	spin_unlock_irqrestore(fence->lock, flags);
> -
> -	return ret;
>  }
>  EXPORT_SYMBOL(dma_fence_signal_timestamp);
>  
> @@ -425,13 +411,10 @@ EXPORT_SYMBOL(dma_fence_signal_timestamp);
>   *
>   * Unlike dma_fence_signal(), this function must be called with &dma_fence.lock
>   * held.
> - *
> - * Returns 0 on success and a negative error value when @fence has been
> - * signalled already.
>   */
> -int dma_fence_signal_locked(struct dma_fence *fence)
> +void dma_fence_signal_locked(struct dma_fence *fence)
>  {
> -	return dma_fence_signal_timestamp_locked(fence, ktime_get());
> +	dma_fence_signal_timestamp_locked(fence, ktime_get());
>  }
>  EXPORT_SYMBOL(dma_fence_signal_locked);
>  
> @@ -444,28 +427,19 @@ EXPORT_SYMBOL(dma_fence_signal_locked);
>   * dma_fence_add_callback(). Can be called multiple times, but since a fence
>   * can only go from the unsignaled to the signaled state and not back, it will
>   * only be effective the first time.
> - *
> - * Returns 0 on success and a negative error value when @fence has been
> - * signalled already.
>   */
> -int dma_fence_signal(struct dma_fence *fence)
> +void dma_fence_signal(struct dma_fence *fence)
>  {
>  	unsigned long flags;
> -	int ret;
>  	bool tmp;
>  
> -	if (!fence)
> -		return -EINVAL;
> -
>  	tmp = dma_fence_begin_signalling();
>  
>  	spin_lock_irqsave(fence->lock, flags);
> -	ret = dma_fence_signal_timestamp_locked(fence, ktime_get());
> +	dma_fence_signal_timestamp_locked(fence, ktime_get());
>  	spin_unlock_irqrestore(fence->lock, flags);
>  
>  	dma_fence_end_signalling(tmp);
> -
> -	return ret;
>  }
>  EXPORT_SYMBOL(dma_fence_signal);
>  
> diff --git a/drivers/dma-buf/st-dma-fence.c b/drivers/dma-buf/st-dma-fence.c
> index c8a12d7ad71a..e89001d8873f 100644
> --- a/drivers/dma-buf/st-dma-fence.c
> +++ b/drivers/dma-buf/st-dma-fence.c
> @@ -122,21 +122,13 @@ static int test_signaling(void *arg)
>  		goto err_free;
>  	}
>  
> -	if (dma_fence_signal(f)) {
> -		pr_err("Fence reported being already signaled\n");
> -		goto err_free;
> -	}
> -
> +	dma_fence_signal(f);
>  	if (!dma_fence_is_signaled(f)) {
>  		pr_err("Fence not reporting signaled\n");
>  		goto err_free;
>  	}
>  
> -	if (!dma_fence_signal(f)) {
> -		pr_err("Fence reported not being already signaled\n");
> -		goto err_free;
> -	}
> -
> +	dma_fence_signal(f);
>  	err = 0;
>  err_free:
>  	dma_fence_put(f);
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
> index d4547d195173..397872779c31 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
> @@ -246,7 +246,6 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
>  	struct amdgpu_fence_driver *drv = &ring->fence_drv;
>  	struct amdgpu_device *adev = ring->adev;
>  	uint32_t seq, last_seq;
> -	int r;
>  
>  	do {
>  		last_seq = atomic_read(&ring->fence_drv.last_seq);
> @@ -278,12 +277,7 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
>  		if (!fence)
>  			continue;
>  
> -		r = dma_fence_signal(fence);
> -		if (!r)
> -			DMA_FENCE_TRACE(fence, "signaled from irq context\n");
> -		else
> -			BUG();
> -
> +		dma_fence_signal(fence);
>  		dma_fence_put(fence);
>  		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
>  		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
> @@ -673,8 +667,6 @@ static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
>  	if (!timer_pending(&ring->fence_drv.fallback_timer))
>  		amdgpu_fence_schedule_fallback(ring);
>  
> -	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
> -
>  	return true;
>  }
>  
> diff --git a/drivers/gpu/drm/radeon/radeon_fence.c b/drivers/gpu/drm/radeon/radeon_fence.c
> index 18f2c2e0dfb3..3f351d222cbb 100644
> --- a/drivers/gpu/drm/radeon/radeon_fence.c
> +++ b/drivers/gpu/drm/radeon/radeon_fence.c
> @@ -176,18 +176,11 @@ static int radeon_fence_check_signaled(wait_queue_entry_t *wait, unsigned mode,
>  	 */
>  	seq = atomic64_read(&fence->rdev->fence_drv[fence->ring].last_seq);
>  	if (seq >= fence->seq) {
> -		int ret = dma_fence_signal_locked(&fence->base);
> -
> -		if (!ret)
> -			DMA_FENCE_TRACE(&fence->base, "signaled from irq context\n");
> -		else
> -			DMA_FENCE_TRACE(&fence->base, "was already signaled\n");
> -
> +		dma_fence_signal_locked(&fence->base);
>  		radeon_irq_kms_sw_irq_put(fence->rdev, fence->ring);
>  		__remove_wait_queue(&fence->rdev->fence_queue, &fence->fence_wake);
>  		dma_fence_put(&fence->base);
> -	} else
> -		DMA_FENCE_TRACE(&fence->base, "pending\n");
> +	}
>  	return 0;
>  }
>  
> @@ -422,8 +415,6 @@ static bool radeon_fence_enable_signaling(struct dma_fence *f)
>  	fence->fence_wake.func = radeon_fence_check_signaled;
>  	__add_wait_queue(&rdev->fence_queue, &fence->fence_wake);
>  	dma_fence_get(f);
> -
> -	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", fence->ring);
>  	return true;
>  }
>  
> @@ -441,11 +432,7 @@ bool radeon_fence_signaled(struct radeon_fence *fence)
>  		return true;
>  
>  	if (radeon_fence_seq_signaled(fence->rdev, fence->seq, fence->ring)) {
> -		int ret;
> -
> -		ret = dma_fence_signal(&fence->base);
> -		if (!ret)
> -			DMA_FENCE_TRACE(&fence->base, "signaled from radeon_fence_signaled\n");
> +		dma_fence_signal(&fence->base);
>  		return true;
>  	}
>  	return false;
> @@ -550,7 +537,6 @@ long radeon_fence_wait_timeout(struct radeon_fence *fence, bool intr, long timeo
>  {
>  	uint64_t seq[RADEON_NUM_RINGS] = {};
>  	long r;
> -	int r_sig;
>  
>  	/*
>  	 * This function should not be called on !radeon fences.
> @@ -567,9 +553,7 @@ long radeon_fence_wait_timeout(struct radeon_fence *fence, bool intr, long timeo
>  		return r;
>  	}
>  
> -	r_sig = dma_fence_signal(&fence->base);
> -	if (!r_sig)
> -		DMA_FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
> +	dma_fence_signal(&fence->base);
>  	return r;
>  }
>  
> diff --git a/drivers/gpu/drm/scheduler/sched_fence.c b/drivers/gpu/drm/scheduler/sched_fence.c
> index 69de2c76731f..3736746c47bd 100644
> --- a/drivers/gpu/drm/scheduler/sched_fence.c
> +++ b/drivers/gpu/drm/scheduler/sched_fence.c
> @@ -50,26 +50,12 @@ static void __exit drm_sched_fence_slab_fini(void)
>  
>  void drm_sched_fence_scheduled(struct drm_sched_fence *fence)
>  {
> -	int ret = dma_fence_signal(&fence->scheduled);
> -
> -	if (!ret)
> -		DMA_FENCE_TRACE(&fence->scheduled,
> -				"signaled from irq context\n");
> -	else
> -		DMA_FENCE_TRACE(&fence->scheduled,
> -				"was already signaled\n");
> +	dma_fence_signal(&fence->scheduled);
>  }
>  
>  void drm_sched_fence_finished(struct drm_sched_fence *fence)
>  {
> -	int ret = dma_fence_signal(&fence->finished);
> -
> -	if (!ret)
> -		DMA_FENCE_TRACE(&fence->finished,
> -				"signaled from irq context\n");
> -	else
> -		DMA_FENCE_TRACE(&fence->finished,
> -				"was already signaled\n");
> +	dma_fence_signal(&fence->finished);
>  }
>  
>  static const char *drm_sched_fence_get_driver_name(struct dma_fence *fence)
> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
> index 6ffb4b2c6371..027db23fd4e3 100644
> --- a/include/linux/dma-fence.h
> +++ b/include/linux/dma-fence.h
> @@ -370,11 +370,11 @@ static inline void dma_fence_end_signalling(bool cookie) {}
>  static inline void __dma_fence_might_wait(void) {}
>  #endif
>  
> -int dma_fence_signal(struct dma_fence *fence);
> -int dma_fence_signal_locked(struct dma_fence *fence);
> -int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp);
> -int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
> -				      ktime_t timestamp);
> +void dma_fence_signal(struct dma_fence *fence);
> +void dma_fence_signal_locked(struct dma_fence *fence);
> +void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp);
> +void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
> +				       ktime_t timestamp);
>  signed long dma_fence_default_wait(struct dma_fence *fence,
>  				   bool intr, signed long timeout);
>  int dma_fence_add_callback(struct dma_fence *fence,
> @@ -590,26 +590,4 @@ struct dma_fence *dma_fence_get_stub(void);
>  struct dma_fence *dma_fence_allocate_private_stub(void);
>  u64 dma_fence_context_alloc(unsigned num);
>  
> -#define DMA_FENCE_TRACE(f, fmt, args...) \
> -	do {								\
> -		struct dma_fence *__ff = (f);				\
> -		if (IS_ENABLED(CONFIG_DMA_FENCE_TRACE))			\
> -			pr_info("f %llu#%llu: " fmt,			\
> -				__ff->context, __ff->seqno, ##args);	\
> -	} while (0)
> -
> -#define DMA_FENCE_WARN(f, fmt, args...) \
> -	do {								\
> -		struct dma_fence *__ff = (f);				\
> -		pr_warn("f %llu#%llu: " fmt, __ff->context, __ff->seqno,\
> -			 ##args);					\
> -	} while (0)
> -
> -#define DMA_FENCE_ERR(f, fmt, args...) \
> -	do {								\
> -		struct dma_fence *__ff = (f);				\
> -		pr_err("f %llu#%llu: " fmt, __ff->context, __ff->seqno,	\
> -			##args);					\
> -	} while (0)
> -
>  #endif /* __LINUX_DMA_FENCE_H */
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
  2021-07-29  7:03 ` [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files Christian König
@ 2021-07-29  7:23     ` Daniel Vetter
  0 siblings, 0 replies; 26+ messages in thread
From: Daniel Vetter @ 2021-07-29  7:23 UTC (permalink / raw)
  To: Christian König
  Cc: sumit.semwal, gustavo, daniel, linux-media, dri-devel, linaro-mm-sig

On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
> As we now knew controlling dma_fence synchronization from userspace is
> extremely dangerous and can not only deadlock drivers but trivially also the
> whole kernel memory management.
> 
> Entirely remove this option. We now have in kernel unit tests to exercise the
> dma_fence framework and it's containers.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>

There's also igts for this, and Android heavily uses this. So I'm not sure
we can just nuke it.

I guess what we could do is maybe just taint the kernel if it's ever used?

Also for testing we use vgem now, which enforces a timeout.
-Daniel

> ---
>  drivers/dma-buf/Kconfig      |  13 --
>  drivers/dma-buf/Makefile     |   1 -
>  drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
>  drivers/dma-buf/sync_debug.c | 190 ----------------
>  drivers/dma-buf/sync_debug.h |  72 ------
>  5 files changed, 688 deletions(-)
>  delete mode 100644 drivers/dma-buf/sw_sync.c
>  delete mode 100644 drivers/dma-buf/sync_debug.c
>  delete mode 100644 drivers/dma-buf/sync_debug.h
> 
> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
> index 9561e3d2d428..26c53f45616a 100644
> --- a/drivers/dma-buf/Kconfig
> +++ b/drivers/dma-buf/Kconfig
> @@ -17,19 +17,6 @@ config SYNC_FILE
>  	  Files fds, to the DRM driver for example. More details at
>  	  Documentation/driver-api/sync_file.rst.
>  
> -config SW_SYNC
> -	bool "Sync File Validation Framework"
> -	default n
> -	depends on SYNC_FILE
> -	depends on DEBUG_FS
> -	help
> -	  A sync object driver that uses a 32bit counter to coordinate
> -	  synchronization.  Useful when there is no hardware primitive backing
> -	  the synchronization.
> -
> -	  WARNING: improper use of this can result in deadlocking kernel
> -	  drivers from userspace. Intended for test and debug only.
> -
>  config UDMABUF
>  	bool "userspace dmabuf misc driver"
>  	default n
> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
> index 1ef021273a06..fa3af0953f87 100644
> --- a/drivers/dma-buf/Makefile
> +++ b/drivers/dma-buf/Makefile
> @@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
>  obj-$(CONFIG_DMABUF_HEAPS)	+= dma-heap.o
>  obj-$(CONFIG_DMABUF_HEAPS)	+= heaps/
>  obj-$(CONFIG_SYNC_FILE)		+= sync_file.o
> -obj-$(CONFIG_SW_SYNC)		+= sw_sync.o sync_debug.o
>  obj-$(CONFIG_UDMABUF)		+= udmabuf.o
>  obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
>  
> diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
> deleted file mode 100644
> index 348b3a9170fa..000000000000
> --- a/drivers/dma-buf/sw_sync.c
> +++ /dev/null
> @@ -1,412 +0,0 @@
> -// SPDX-License-Identifier: GPL-2.0-only
> -/*
> - * Sync File validation framework
> - *
> - * Copyright (C) 2012 Google, Inc.
> - */
> -
> -#include <linux/file.h>
> -#include <linux/fs.h>
> -#include <linux/uaccess.h>
> -#include <linux/slab.h>
> -#include <linux/sync_file.h>
> -
> -#include "sync_debug.h"
> -
> -#define CREATE_TRACE_POINTS
> -#include "sync_trace.h"
> -
> -/*
> - * SW SYNC validation framework
> - *
> - * A sync object driver that uses a 32bit counter to coordinate
> - * synchronization.  Useful when there is no hardware primitive backing
> - * the synchronization.
> - *
> - * To start the framework just open:
> - *
> - * <debugfs>/sync/sw_sync
> - *
> - * That will create a sync timeline, all fences created under this timeline
> - * file descriptor will belong to the this timeline.
> - *
> - * The 'sw_sync' file can be opened many times as to create different
> - * timelines.
> - *
> - * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
> - * sw_sync_create_fence_data as parameter.
> - *
> - * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
> - * with the increment as u32. This will update the last signaled value
> - * from the timeline and signal any fence that has a seqno smaller or equal
> - * to it.
> - *
> - * struct sw_sync_create_fence_data
> - * @value:	the seqno to initialise the fence with
> - * @name:	the name of the new sync point
> - * @fence:	return the fd of the new sync_file with the created fence
> - */
> -struct sw_sync_create_fence_data {
> -	__u32	value;
> -	char	name[32];
> -	__s32	fence; /* fd of new fence */
> -};
> -
> -#define SW_SYNC_IOC_MAGIC	'W'
> -
> -#define SW_SYNC_IOC_CREATE_FENCE	_IOWR(SW_SYNC_IOC_MAGIC, 0,\
> -		struct sw_sync_create_fence_data)
> -
> -#define SW_SYNC_IOC_INC			_IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
> -
> -static const struct dma_fence_ops timeline_fence_ops;
> -
> -static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
> -{
> -	if (fence->ops != &timeline_fence_ops)
> -		return NULL;
> -	return container_of(fence, struct sync_pt, base);
> -}
> -
> -/**
> - * sync_timeline_create() - creates a sync object
> - * @name:	sync_timeline name
> - *
> - * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
> - * case of error.
> - */
> -static struct sync_timeline *sync_timeline_create(const char *name)
> -{
> -	struct sync_timeline *obj;
> -
> -	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
> -	if (!obj)
> -		return NULL;
> -
> -	kref_init(&obj->kref);
> -	obj->context = dma_fence_context_alloc(1);
> -	strlcpy(obj->name, name, sizeof(obj->name));
> -
> -	obj->pt_tree = RB_ROOT;
> -	INIT_LIST_HEAD(&obj->pt_list);
> -	spin_lock_init(&obj->lock);
> -
> -	sync_timeline_debug_add(obj);
> -
> -	return obj;
> -}
> -
> -static void sync_timeline_free(struct kref *kref)
> -{
> -	struct sync_timeline *obj =
> -		container_of(kref, struct sync_timeline, kref);
> -
> -	sync_timeline_debug_remove(obj);
> -
> -	kfree(obj);
> -}
> -
> -static void sync_timeline_get(struct sync_timeline *obj)
> -{
> -	kref_get(&obj->kref);
> -}
> -
> -static void sync_timeline_put(struct sync_timeline *obj)
> -{
> -	kref_put(&obj->kref, sync_timeline_free);
> -}
> -
> -static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
> -{
> -	return "sw_sync";
> -}
> -
> -static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
> -{
> -	struct sync_timeline *parent = dma_fence_parent(fence);
> -
> -	return parent->name;
> -}
> -
> -static void timeline_fence_release(struct dma_fence *fence)
> -{
> -	struct sync_pt *pt = dma_fence_to_sync_pt(fence);
> -	struct sync_timeline *parent = dma_fence_parent(fence);
> -	unsigned long flags;
> -
> -	spin_lock_irqsave(fence->lock, flags);
> -	if (!list_empty(&pt->link)) {
> -		list_del(&pt->link);
> -		rb_erase(&pt->node, &parent->pt_tree);
> -	}
> -	spin_unlock_irqrestore(fence->lock, flags);
> -
> -	sync_timeline_put(parent);
> -	dma_fence_free(fence);
> -}
> -
> -static bool timeline_fence_signaled(struct dma_fence *fence)
> -{
> -	struct sync_timeline *parent = dma_fence_parent(fence);
> -
> -	return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
> -}
> -
> -static bool timeline_fence_enable_signaling(struct dma_fence *fence)
> -{
> -	return true;
> -}
> -
> -static void timeline_fence_value_str(struct dma_fence *fence,
> -				    char *str, int size)
> -{
> -	snprintf(str, size, "%lld", fence->seqno);
> -}
> -
> -static void timeline_fence_timeline_value_str(struct dma_fence *fence,
> -					     char *str, int size)
> -{
> -	struct sync_timeline *parent = dma_fence_parent(fence);
> -
> -	snprintf(str, size, "%d", parent->value);
> -}
> -
> -static const struct dma_fence_ops timeline_fence_ops = {
> -	.get_driver_name = timeline_fence_get_driver_name,
> -	.get_timeline_name = timeline_fence_get_timeline_name,
> -	.enable_signaling = timeline_fence_enable_signaling,
> -	.signaled = timeline_fence_signaled,
> -	.release = timeline_fence_release,
> -	.fence_value_str = timeline_fence_value_str,
> -	.timeline_value_str = timeline_fence_timeline_value_str,
> -};
> -
> -/**
> - * sync_timeline_signal() - signal a status change on a sync_timeline
> - * @obj:	sync_timeline to signal
> - * @inc:	num to increment on timeline->value
> - *
> - * A sync implementation should call this any time one of it's fences
> - * has signaled or has an error condition.
> - */
> -static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
> -{
> -	struct sync_pt *pt, *next;
> -
> -	trace_sync_timeline(obj);
> -
> -	spin_lock_irq(&obj->lock);
> -
> -	obj->value += inc;
> -
> -	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> -		if (!timeline_fence_signaled(&pt->base))
> -			break;
> -
> -		list_del_init(&pt->link);
> -		rb_erase(&pt->node, &obj->pt_tree);
> -
> -		/*
> -		 * A signal callback may release the last reference to this
> -		 * fence, causing it to be freed. That operation has to be
> -		 * last to avoid a use after free inside this loop, and must
> -		 * be after we remove the fence from the timeline in order to
> -		 * prevent deadlocking on timeline->lock inside
> -		 * timeline_fence_release().
> -		 */
> -		dma_fence_signal_locked(&pt->base);
> -	}
> -
> -	spin_unlock_irq(&obj->lock);
> -}
> -
> -/**
> - * sync_pt_create() - creates a sync pt
> - * @obj:	parent sync_timeline
> - * @value:	value of the fence
> - *
> - * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
> - * allocated allowing for implementation specific data to be kept after
> - * the generic sync_timeline struct. Returns the sync_pt object or
> - * NULL in case of error.
> - */
> -static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
> -				      unsigned int value)
> -{
> -	struct sync_pt *pt;
> -
> -	pt = kzalloc(sizeof(*pt), GFP_KERNEL);
> -	if (!pt)
> -		return NULL;
> -
> -	sync_timeline_get(obj);
> -	dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
> -		       obj->context, value);
> -	INIT_LIST_HEAD(&pt->link);
> -
> -	spin_lock_irq(&obj->lock);
> -	if (!dma_fence_is_signaled_locked(&pt->base)) {
> -		struct rb_node **p = &obj->pt_tree.rb_node;
> -		struct rb_node *parent = NULL;
> -
> -		while (*p) {
> -			struct sync_pt *other;
> -			int cmp;
> -
> -			parent = *p;
> -			other = rb_entry(parent, typeof(*pt), node);
> -			cmp = value - other->base.seqno;
> -			if (cmp > 0) {
> -				p = &parent->rb_right;
> -			} else if (cmp < 0) {
> -				p = &parent->rb_left;
> -			} else {
> -				if (dma_fence_get_rcu(&other->base)) {
> -					sync_timeline_put(obj);
> -					kfree(pt);
> -					pt = other;
> -					goto unlock;
> -				}
> -				p = &parent->rb_left;
> -			}
> -		}
> -		rb_link_node(&pt->node, parent, p);
> -		rb_insert_color(&pt->node, &obj->pt_tree);
> -
> -		parent = rb_next(&pt->node);
> -		list_add_tail(&pt->link,
> -			      parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
> -	}
> -unlock:
> -	spin_unlock_irq(&obj->lock);
> -
> -	return pt;
> -}
> -
> -/*
> - * *WARNING*
> - *
> - * improper use of this can result in deadlocking kernel drivers from userspace.
> - */
> -
> -/* opening sw_sync create a new sync obj */
> -static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
> -{
> -	struct sync_timeline *obj;
> -	char task_comm[TASK_COMM_LEN];
> -
> -	get_task_comm(task_comm, current);
> -
> -	obj = sync_timeline_create(task_comm);
> -	if (!obj)
> -		return -ENOMEM;
> -
> -	file->private_data = obj;
> -
> -	return 0;
> -}
> -
> -static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
> -{
> -	struct sync_timeline *obj = file->private_data;
> -	struct sync_pt *pt, *next;
> -
> -	spin_lock_irq(&obj->lock);
> -
> -	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> -		dma_fence_set_error(&pt->base, -ENOENT);
> -		dma_fence_signal_locked(&pt->base);
> -	}
> -
> -	spin_unlock_irq(&obj->lock);
> -
> -	sync_timeline_put(obj);
> -	return 0;
> -}
> -
> -static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
> -				       unsigned long arg)
> -{
> -	int fd = get_unused_fd_flags(O_CLOEXEC);
> -	int err;
> -	struct sync_pt *pt;
> -	struct sync_file *sync_file;
> -	struct sw_sync_create_fence_data data;
> -
> -	if (fd < 0)
> -		return fd;
> -
> -	if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
> -		err = -EFAULT;
> -		goto err;
> -	}
> -
> -	pt = sync_pt_create(obj, data.value);
> -	if (!pt) {
> -		err = -ENOMEM;
> -		goto err;
> -	}
> -
> -	sync_file = sync_file_create(&pt->base);
> -	dma_fence_put(&pt->base);
> -	if (!sync_file) {
> -		err = -ENOMEM;
> -		goto err;
> -	}
> -
> -	data.fence = fd;
> -	if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
> -		fput(sync_file->file);
> -		err = -EFAULT;
> -		goto err;
> -	}
> -
> -	fd_install(fd, sync_file->file);
> -
> -	return 0;
> -
> -err:
> -	put_unused_fd(fd);
> -	return err;
> -}
> -
> -static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
> -{
> -	u32 value;
> -
> -	if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
> -		return -EFAULT;
> -
> -	while (value > INT_MAX)  {
> -		sync_timeline_signal(obj, INT_MAX);
> -		value -= INT_MAX;
> -	}
> -
> -	sync_timeline_signal(obj, value);
> -
> -	return 0;
> -}
> -
> -static long sw_sync_ioctl(struct file *file, unsigned int cmd,
> -			  unsigned long arg)
> -{
> -	struct sync_timeline *obj = file->private_data;
> -
> -	switch (cmd) {
> -	case SW_SYNC_IOC_CREATE_FENCE:
> -		return sw_sync_ioctl_create_fence(obj, arg);
> -
> -	case SW_SYNC_IOC_INC:
> -		return sw_sync_ioctl_inc(obj, arg);
> -
> -	default:
> -		return -ENOTTY;
> -	}
> -}
> -
> -const struct file_operations sw_sync_debugfs_fops = {
> -	.open           = sw_sync_debugfs_open,
> -	.release        = sw_sync_debugfs_release,
> -	.unlocked_ioctl = sw_sync_ioctl,
> -	.compat_ioctl	= compat_ptr_ioctl,
> -};
> diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
> deleted file mode 100644
> index 101394f16930..000000000000
> --- a/drivers/dma-buf/sync_debug.c
> +++ /dev/null
> @@ -1,190 +0,0 @@
> -// SPDX-License-Identifier: GPL-2.0-only
> -/*
> - * Sync File validation framework and debug information
> - *
> - * Copyright (C) 2012 Google, Inc.
> - */
> -
> -#include <linux/debugfs.h>
> -#include "sync_debug.h"
> -
> -static struct dentry *dbgfs;
> -
> -static LIST_HEAD(sync_timeline_list_head);
> -static DEFINE_SPINLOCK(sync_timeline_list_lock);
> -static LIST_HEAD(sync_file_list_head);
> -static DEFINE_SPINLOCK(sync_file_list_lock);
> -
> -void sync_timeline_debug_add(struct sync_timeline *obj)
> -{
> -	unsigned long flags;
> -
> -	spin_lock_irqsave(&sync_timeline_list_lock, flags);
> -	list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
> -	spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> -}
> -
> -void sync_timeline_debug_remove(struct sync_timeline *obj)
> -{
> -	unsigned long flags;
> -
> -	spin_lock_irqsave(&sync_timeline_list_lock, flags);
> -	list_del(&obj->sync_timeline_list);
> -	spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> -}
> -
> -void sync_file_debug_add(struct sync_file *sync_file)
> -{
> -	unsigned long flags;
> -
> -	spin_lock_irqsave(&sync_file_list_lock, flags);
> -	list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
> -	spin_unlock_irqrestore(&sync_file_list_lock, flags);
> -}
> -
> -void sync_file_debug_remove(struct sync_file *sync_file)
> -{
> -	unsigned long flags;
> -
> -	spin_lock_irqsave(&sync_file_list_lock, flags);
> -	list_del(&sync_file->sync_file_list);
> -	spin_unlock_irqrestore(&sync_file_list_lock, flags);
> -}
> -
> -static const char *sync_status_str(int status)
> -{
> -	if (status < 0)
> -		return "error";
> -
> -	if (status > 0)
> -		return "signaled";
> -
> -	return "active";
> -}
> -
> -static void sync_print_fence(struct seq_file *s,
> -			     struct dma_fence *fence, bool show)
> -{
> -	struct sync_timeline *parent = dma_fence_parent(fence);
> -	int status;
> -
> -	status = dma_fence_get_status_locked(fence);
> -
> -	seq_printf(s, "  %s%sfence %s",
> -		   show ? parent->name : "",
> -		   show ? "_" : "",
> -		   sync_status_str(status));
> -
> -	if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
> -		struct timespec64 ts64 =
> -			ktime_to_timespec64(fence->timestamp);
> -
> -		seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
> -	}
> -
> -	if (fence->ops->timeline_value_str &&
> -		fence->ops->fence_value_str) {
> -		char value[64];
> -		bool success;
> -
> -		fence->ops->fence_value_str(fence, value, sizeof(value));
> -		success = strlen(value);
> -
> -		if (success) {
> -			seq_printf(s, ": %s", value);
> -
> -			fence->ops->timeline_value_str(fence, value,
> -						       sizeof(value));
> -
> -			if (strlen(value))
> -				seq_printf(s, " / %s", value);
> -		}
> -	}
> -
> -	seq_putc(s, '\n');
> -}
> -
> -static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
> -{
> -	struct list_head *pos;
> -
> -	seq_printf(s, "%s: %d\n", obj->name, obj->value);
> -
> -	spin_lock_irq(&obj->lock);
> -	list_for_each(pos, &obj->pt_list) {
> -		struct sync_pt *pt = container_of(pos, struct sync_pt, link);
> -		sync_print_fence(s, &pt->base, false);
> -	}
> -	spin_unlock_irq(&obj->lock);
> -}
> -
> -static void sync_print_sync_file(struct seq_file *s,
> -				  struct sync_file *sync_file)
> -{
> -	char buf[128];
> -	int i;
> -
> -	seq_printf(s, "[%p] %s: %s\n", sync_file,
> -		   sync_file_get_name(sync_file, buf, sizeof(buf)),
> -		   sync_status_str(dma_fence_get_status(sync_file->fence)));
> -
> -	if (dma_fence_is_array(sync_file->fence)) {
> -		struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
> -
> -		for (i = 0; i < array->num_fences; ++i)
> -			sync_print_fence(s, array->fences[i], true);
> -	} else {
> -		sync_print_fence(s, sync_file->fence, true);
> -	}
> -}
> -
> -static int sync_info_debugfs_show(struct seq_file *s, void *unused)
> -{
> -	struct list_head *pos;
> -
> -	seq_puts(s, "objs:\n--------------\n");
> -
> -	spin_lock_irq(&sync_timeline_list_lock);
> -	list_for_each(pos, &sync_timeline_list_head) {
> -		struct sync_timeline *obj =
> -			container_of(pos, struct sync_timeline,
> -				     sync_timeline_list);
> -
> -		sync_print_obj(s, obj);
> -		seq_putc(s, '\n');
> -	}
> -	spin_unlock_irq(&sync_timeline_list_lock);
> -
> -	seq_puts(s, "fences:\n--------------\n");
> -
> -	spin_lock_irq(&sync_file_list_lock);
> -	list_for_each(pos, &sync_file_list_head) {
> -		struct sync_file *sync_file =
> -			container_of(pos, struct sync_file, sync_file_list);
> -
> -		sync_print_sync_file(s, sync_file);
> -		seq_putc(s, '\n');
> -	}
> -	spin_unlock_irq(&sync_file_list_lock);
> -	return 0;
> -}
> -
> -DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
> -
> -static __init int sync_debugfs_init(void)
> -{
> -	dbgfs = debugfs_create_dir("sync", NULL);
> -
> -	/*
> -	 * The debugfs files won't ever get removed and thus, there is
> -	 * no need to protect it against removal races. The use of
> -	 * debugfs_create_file_unsafe() is actually safe here.
> -	 */
> -	debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
> -				   &sync_info_debugfs_fops);
> -	debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
> -				   &sw_sync_debugfs_fops);
> -
> -	return 0;
> -}
> -late_initcall(sync_debugfs_init);
> diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
> deleted file mode 100644
> index 6176e52ba2d7..000000000000
> --- a/drivers/dma-buf/sync_debug.h
> +++ /dev/null
> @@ -1,72 +0,0 @@
> -/*
> - * Sync File validation framework and debug infomation
> - *
> - * Copyright (C) 2012 Google, Inc.
> - *
> - * This program is distributed in the hope that it will be useful,
> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> - * GNU General Public License for more details.
> - *
> - */
> -
> -#ifndef _LINUX_SYNC_H
> -#define _LINUX_SYNC_H
> -
> -#include <linux/list.h>
> -#include <linux/rbtree.h>
> -#include <linux/spinlock.h>
> -#include <linux/dma-fence.h>
> -
> -#include <linux/sync_file.h>
> -#include <uapi/linux/sync_file.h>
> -
> -/**
> - * struct sync_timeline - sync object
> - * @kref:		reference count on fence.
> - * @name:		name of the sync_timeline. Useful for debugging
> - * @lock:		lock protecting @pt_list and @value
> - * @pt_tree:		rbtree of active (unsignaled/errored) sync_pts
> - * @pt_list:		list of active (unsignaled/errored) sync_pts
> - * @sync_timeline_list:	membership in global sync_timeline_list
> - */
> -struct sync_timeline {
> -	struct kref		kref;
> -	char			name[32];
> -
> -	/* protected by lock */
> -	u64			context;
> -	int			value;
> -
> -	struct rb_root		pt_tree;
> -	struct list_head	pt_list;
> -	spinlock_t		lock;
> -
> -	struct list_head	sync_timeline_list;
> -};
> -
> -static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
> -{
> -	return container_of(fence->lock, struct sync_timeline, lock);
> -}
> -
> -/**
> - * struct sync_pt - sync_pt object
> - * @base: base fence object
> - * @link: link on the sync timeline's list
> - * @node: node in the sync timeline's tree
> - */
> -struct sync_pt {
> -	struct dma_fence base;
> -	struct list_head link;
> -	struct rb_node node;
> -};
> -
> -extern const struct file_operations sw_sync_debugfs_fops;
> -
> -void sync_timeline_debug_add(struct sync_timeline *obj);
> -void sync_timeline_debug_remove(struct sync_timeline *obj);
> -void sync_file_debug_add(struct sync_file *fence);
> -void sync_file_debug_remove(struct sync_file *fence);
> -
> -#endif /* _LINUX_SYNC_H */
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
@ 2021-07-29  7:23     ` Daniel Vetter
  0 siblings, 0 replies; 26+ messages in thread
From: Daniel Vetter @ 2021-07-29  7:23 UTC (permalink / raw)
  To: Christian König; +Cc: gustavo, dri-devel, linaro-mm-sig, linux-media

On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
> As we now knew controlling dma_fence synchronization from userspace is
> extremely dangerous and can not only deadlock drivers but trivially also the
> whole kernel memory management.
> 
> Entirely remove this option. We now have in kernel unit tests to exercise the
> dma_fence framework and it's containers.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>

There's also igts for this, and Android heavily uses this. So I'm not sure
we can just nuke it.

I guess what we could do is maybe just taint the kernel if it's ever used?

Also for testing we use vgem now, which enforces a timeout.
-Daniel

> ---
>  drivers/dma-buf/Kconfig      |  13 --
>  drivers/dma-buf/Makefile     |   1 -
>  drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
>  drivers/dma-buf/sync_debug.c | 190 ----------------
>  drivers/dma-buf/sync_debug.h |  72 ------
>  5 files changed, 688 deletions(-)
>  delete mode 100644 drivers/dma-buf/sw_sync.c
>  delete mode 100644 drivers/dma-buf/sync_debug.c
>  delete mode 100644 drivers/dma-buf/sync_debug.h
> 
> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
> index 9561e3d2d428..26c53f45616a 100644
> --- a/drivers/dma-buf/Kconfig
> +++ b/drivers/dma-buf/Kconfig
> @@ -17,19 +17,6 @@ config SYNC_FILE
>  	  Files fds, to the DRM driver for example. More details at
>  	  Documentation/driver-api/sync_file.rst.
>  
> -config SW_SYNC
> -	bool "Sync File Validation Framework"
> -	default n
> -	depends on SYNC_FILE
> -	depends on DEBUG_FS
> -	help
> -	  A sync object driver that uses a 32bit counter to coordinate
> -	  synchronization.  Useful when there is no hardware primitive backing
> -	  the synchronization.
> -
> -	  WARNING: improper use of this can result in deadlocking kernel
> -	  drivers from userspace. Intended for test and debug only.
> -
>  config UDMABUF
>  	bool "userspace dmabuf misc driver"
>  	default n
> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
> index 1ef021273a06..fa3af0953f87 100644
> --- a/drivers/dma-buf/Makefile
> +++ b/drivers/dma-buf/Makefile
> @@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
>  obj-$(CONFIG_DMABUF_HEAPS)	+= dma-heap.o
>  obj-$(CONFIG_DMABUF_HEAPS)	+= heaps/
>  obj-$(CONFIG_SYNC_FILE)		+= sync_file.o
> -obj-$(CONFIG_SW_SYNC)		+= sw_sync.o sync_debug.o
>  obj-$(CONFIG_UDMABUF)		+= udmabuf.o
>  obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
>  
> diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
> deleted file mode 100644
> index 348b3a9170fa..000000000000
> --- a/drivers/dma-buf/sw_sync.c
> +++ /dev/null
> @@ -1,412 +0,0 @@
> -// SPDX-License-Identifier: GPL-2.0-only
> -/*
> - * Sync File validation framework
> - *
> - * Copyright (C) 2012 Google, Inc.
> - */
> -
> -#include <linux/file.h>
> -#include <linux/fs.h>
> -#include <linux/uaccess.h>
> -#include <linux/slab.h>
> -#include <linux/sync_file.h>
> -
> -#include "sync_debug.h"
> -
> -#define CREATE_TRACE_POINTS
> -#include "sync_trace.h"
> -
> -/*
> - * SW SYNC validation framework
> - *
> - * A sync object driver that uses a 32bit counter to coordinate
> - * synchronization.  Useful when there is no hardware primitive backing
> - * the synchronization.
> - *
> - * To start the framework just open:
> - *
> - * <debugfs>/sync/sw_sync
> - *
> - * That will create a sync timeline, all fences created under this timeline
> - * file descriptor will belong to the this timeline.
> - *
> - * The 'sw_sync' file can be opened many times as to create different
> - * timelines.
> - *
> - * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
> - * sw_sync_create_fence_data as parameter.
> - *
> - * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
> - * with the increment as u32. This will update the last signaled value
> - * from the timeline and signal any fence that has a seqno smaller or equal
> - * to it.
> - *
> - * struct sw_sync_create_fence_data
> - * @value:	the seqno to initialise the fence with
> - * @name:	the name of the new sync point
> - * @fence:	return the fd of the new sync_file with the created fence
> - */
> -struct sw_sync_create_fence_data {
> -	__u32	value;
> -	char	name[32];
> -	__s32	fence; /* fd of new fence */
> -};
> -
> -#define SW_SYNC_IOC_MAGIC	'W'
> -
> -#define SW_SYNC_IOC_CREATE_FENCE	_IOWR(SW_SYNC_IOC_MAGIC, 0,\
> -		struct sw_sync_create_fence_data)
> -
> -#define SW_SYNC_IOC_INC			_IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
> -
> -static const struct dma_fence_ops timeline_fence_ops;
> -
> -static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
> -{
> -	if (fence->ops != &timeline_fence_ops)
> -		return NULL;
> -	return container_of(fence, struct sync_pt, base);
> -}
> -
> -/**
> - * sync_timeline_create() - creates a sync object
> - * @name:	sync_timeline name
> - *
> - * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
> - * case of error.
> - */
> -static struct sync_timeline *sync_timeline_create(const char *name)
> -{
> -	struct sync_timeline *obj;
> -
> -	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
> -	if (!obj)
> -		return NULL;
> -
> -	kref_init(&obj->kref);
> -	obj->context = dma_fence_context_alloc(1);
> -	strlcpy(obj->name, name, sizeof(obj->name));
> -
> -	obj->pt_tree = RB_ROOT;
> -	INIT_LIST_HEAD(&obj->pt_list);
> -	spin_lock_init(&obj->lock);
> -
> -	sync_timeline_debug_add(obj);
> -
> -	return obj;
> -}
> -
> -static void sync_timeline_free(struct kref *kref)
> -{
> -	struct sync_timeline *obj =
> -		container_of(kref, struct sync_timeline, kref);
> -
> -	sync_timeline_debug_remove(obj);
> -
> -	kfree(obj);
> -}
> -
> -static void sync_timeline_get(struct sync_timeline *obj)
> -{
> -	kref_get(&obj->kref);
> -}
> -
> -static void sync_timeline_put(struct sync_timeline *obj)
> -{
> -	kref_put(&obj->kref, sync_timeline_free);
> -}
> -
> -static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
> -{
> -	return "sw_sync";
> -}
> -
> -static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
> -{
> -	struct sync_timeline *parent = dma_fence_parent(fence);
> -
> -	return parent->name;
> -}
> -
> -static void timeline_fence_release(struct dma_fence *fence)
> -{
> -	struct sync_pt *pt = dma_fence_to_sync_pt(fence);
> -	struct sync_timeline *parent = dma_fence_parent(fence);
> -	unsigned long flags;
> -
> -	spin_lock_irqsave(fence->lock, flags);
> -	if (!list_empty(&pt->link)) {
> -		list_del(&pt->link);
> -		rb_erase(&pt->node, &parent->pt_tree);
> -	}
> -	spin_unlock_irqrestore(fence->lock, flags);
> -
> -	sync_timeline_put(parent);
> -	dma_fence_free(fence);
> -}
> -
> -static bool timeline_fence_signaled(struct dma_fence *fence)
> -{
> -	struct sync_timeline *parent = dma_fence_parent(fence);
> -
> -	return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
> -}
> -
> -static bool timeline_fence_enable_signaling(struct dma_fence *fence)
> -{
> -	return true;
> -}
> -
> -static void timeline_fence_value_str(struct dma_fence *fence,
> -				    char *str, int size)
> -{
> -	snprintf(str, size, "%lld", fence->seqno);
> -}
> -
> -static void timeline_fence_timeline_value_str(struct dma_fence *fence,
> -					     char *str, int size)
> -{
> -	struct sync_timeline *parent = dma_fence_parent(fence);
> -
> -	snprintf(str, size, "%d", parent->value);
> -}
> -
> -static const struct dma_fence_ops timeline_fence_ops = {
> -	.get_driver_name = timeline_fence_get_driver_name,
> -	.get_timeline_name = timeline_fence_get_timeline_name,
> -	.enable_signaling = timeline_fence_enable_signaling,
> -	.signaled = timeline_fence_signaled,
> -	.release = timeline_fence_release,
> -	.fence_value_str = timeline_fence_value_str,
> -	.timeline_value_str = timeline_fence_timeline_value_str,
> -};
> -
> -/**
> - * sync_timeline_signal() - signal a status change on a sync_timeline
> - * @obj:	sync_timeline to signal
> - * @inc:	num to increment on timeline->value
> - *
> - * A sync implementation should call this any time one of it's fences
> - * has signaled or has an error condition.
> - */
> -static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
> -{
> -	struct sync_pt *pt, *next;
> -
> -	trace_sync_timeline(obj);
> -
> -	spin_lock_irq(&obj->lock);
> -
> -	obj->value += inc;
> -
> -	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> -		if (!timeline_fence_signaled(&pt->base))
> -			break;
> -
> -		list_del_init(&pt->link);
> -		rb_erase(&pt->node, &obj->pt_tree);
> -
> -		/*
> -		 * A signal callback may release the last reference to this
> -		 * fence, causing it to be freed. That operation has to be
> -		 * last to avoid a use after free inside this loop, and must
> -		 * be after we remove the fence from the timeline in order to
> -		 * prevent deadlocking on timeline->lock inside
> -		 * timeline_fence_release().
> -		 */
> -		dma_fence_signal_locked(&pt->base);
> -	}
> -
> -	spin_unlock_irq(&obj->lock);
> -}
> -
> -/**
> - * sync_pt_create() - creates a sync pt
> - * @obj:	parent sync_timeline
> - * @value:	value of the fence
> - *
> - * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
> - * allocated allowing for implementation specific data to be kept after
> - * the generic sync_timeline struct. Returns the sync_pt object or
> - * NULL in case of error.
> - */
> -static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
> -				      unsigned int value)
> -{
> -	struct sync_pt *pt;
> -
> -	pt = kzalloc(sizeof(*pt), GFP_KERNEL);
> -	if (!pt)
> -		return NULL;
> -
> -	sync_timeline_get(obj);
> -	dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
> -		       obj->context, value);
> -	INIT_LIST_HEAD(&pt->link);
> -
> -	spin_lock_irq(&obj->lock);
> -	if (!dma_fence_is_signaled_locked(&pt->base)) {
> -		struct rb_node **p = &obj->pt_tree.rb_node;
> -		struct rb_node *parent = NULL;
> -
> -		while (*p) {
> -			struct sync_pt *other;
> -			int cmp;
> -
> -			parent = *p;
> -			other = rb_entry(parent, typeof(*pt), node);
> -			cmp = value - other->base.seqno;
> -			if (cmp > 0) {
> -				p = &parent->rb_right;
> -			} else if (cmp < 0) {
> -				p = &parent->rb_left;
> -			} else {
> -				if (dma_fence_get_rcu(&other->base)) {
> -					sync_timeline_put(obj);
> -					kfree(pt);
> -					pt = other;
> -					goto unlock;
> -				}
> -				p = &parent->rb_left;
> -			}
> -		}
> -		rb_link_node(&pt->node, parent, p);
> -		rb_insert_color(&pt->node, &obj->pt_tree);
> -
> -		parent = rb_next(&pt->node);
> -		list_add_tail(&pt->link,
> -			      parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
> -	}
> -unlock:
> -	spin_unlock_irq(&obj->lock);
> -
> -	return pt;
> -}
> -
> -/*
> - * *WARNING*
> - *
> - * improper use of this can result in deadlocking kernel drivers from userspace.
> - */
> -
> -/* opening sw_sync create a new sync obj */
> -static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
> -{
> -	struct sync_timeline *obj;
> -	char task_comm[TASK_COMM_LEN];
> -
> -	get_task_comm(task_comm, current);
> -
> -	obj = sync_timeline_create(task_comm);
> -	if (!obj)
> -		return -ENOMEM;
> -
> -	file->private_data = obj;
> -
> -	return 0;
> -}
> -
> -static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
> -{
> -	struct sync_timeline *obj = file->private_data;
> -	struct sync_pt *pt, *next;
> -
> -	spin_lock_irq(&obj->lock);
> -
> -	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> -		dma_fence_set_error(&pt->base, -ENOENT);
> -		dma_fence_signal_locked(&pt->base);
> -	}
> -
> -	spin_unlock_irq(&obj->lock);
> -
> -	sync_timeline_put(obj);
> -	return 0;
> -}
> -
> -static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
> -				       unsigned long arg)
> -{
> -	int fd = get_unused_fd_flags(O_CLOEXEC);
> -	int err;
> -	struct sync_pt *pt;
> -	struct sync_file *sync_file;
> -	struct sw_sync_create_fence_data data;
> -
> -	if (fd < 0)
> -		return fd;
> -
> -	if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
> -		err = -EFAULT;
> -		goto err;
> -	}
> -
> -	pt = sync_pt_create(obj, data.value);
> -	if (!pt) {
> -		err = -ENOMEM;
> -		goto err;
> -	}
> -
> -	sync_file = sync_file_create(&pt->base);
> -	dma_fence_put(&pt->base);
> -	if (!sync_file) {
> -		err = -ENOMEM;
> -		goto err;
> -	}
> -
> -	data.fence = fd;
> -	if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
> -		fput(sync_file->file);
> -		err = -EFAULT;
> -		goto err;
> -	}
> -
> -	fd_install(fd, sync_file->file);
> -
> -	return 0;
> -
> -err:
> -	put_unused_fd(fd);
> -	return err;
> -}
> -
> -static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
> -{
> -	u32 value;
> -
> -	if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
> -		return -EFAULT;
> -
> -	while (value > INT_MAX)  {
> -		sync_timeline_signal(obj, INT_MAX);
> -		value -= INT_MAX;
> -	}
> -
> -	sync_timeline_signal(obj, value);
> -
> -	return 0;
> -}
> -
> -static long sw_sync_ioctl(struct file *file, unsigned int cmd,
> -			  unsigned long arg)
> -{
> -	struct sync_timeline *obj = file->private_data;
> -
> -	switch (cmd) {
> -	case SW_SYNC_IOC_CREATE_FENCE:
> -		return sw_sync_ioctl_create_fence(obj, arg);
> -
> -	case SW_SYNC_IOC_INC:
> -		return sw_sync_ioctl_inc(obj, arg);
> -
> -	default:
> -		return -ENOTTY;
> -	}
> -}
> -
> -const struct file_operations sw_sync_debugfs_fops = {
> -	.open           = sw_sync_debugfs_open,
> -	.release        = sw_sync_debugfs_release,
> -	.unlocked_ioctl = sw_sync_ioctl,
> -	.compat_ioctl	= compat_ptr_ioctl,
> -};
> diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
> deleted file mode 100644
> index 101394f16930..000000000000
> --- a/drivers/dma-buf/sync_debug.c
> +++ /dev/null
> @@ -1,190 +0,0 @@
> -// SPDX-License-Identifier: GPL-2.0-only
> -/*
> - * Sync File validation framework and debug information
> - *
> - * Copyright (C) 2012 Google, Inc.
> - */
> -
> -#include <linux/debugfs.h>
> -#include "sync_debug.h"
> -
> -static struct dentry *dbgfs;
> -
> -static LIST_HEAD(sync_timeline_list_head);
> -static DEFINE_SPINLOCK(sync_timeline_list_lock);
> -static LIST_HEAD(sync_file_list_head);
> -static DEFINE_SPINLOCK(sync_file_list_lock);
> -
> -void sync_timeline_debug_add(struct sync_timeline *obj)
> -{
> -	unsigned long flags;
> -
> -	spin_lock_irqsave(&sync_timeline_list_lock, flags);
> -	list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
> -	spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> -}
> -
> -void sync_timeline_debug_remove(struct sync_timeline *obj)
> -{
> -	unsigned long flags;
> -
> -	spin_lock_irqsave(&sync_timeline_list_lock, flags);
> -	list_del(&obj->sync_timeline_list);
> -	spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> -}
> -
> -void sync_file_debug_add(struct sync_file *sync_file)
> -{
> -	unsigned long flags;
> -
> -	spin_lock_irqsave(&sync_file_list_lock, flags);
> -	list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
> -	spin_unlock_irqrestore(&sync_file_list_lock, flags);
> -}
> -
> -void sync_file_debug_remove(struct sync_file *sync_file)
> -{
> -	unsigned long flags;
> -
> -	spin_lock_irqsave(&sync_file_list_lock, flags);
> -	list_del(&sync_file->sync_file_list);
> -	spin_unlock_irqrestore(&sync_file_list_lock, flags);
> -}
> -
> -static const char *sync_status_str(int status)
> -{
> -	if (status < 0)
> -		return "error";
> -
> -	if (status > 0)
> -		return "signaled";
> -
> -	return "active";
> -}
> -
> -static void sync_print_fence(struct seq_file *s,
> -			     struct dma_fence *fence, bool show)
> -{
> -	struct sync_timeline *parent = dma_fence_parent(fence);
> -	int status;
> -
> -	status = dma_fence_get_status_locked(fence);
> -
> -	seq_printf(s, "  %s%sfence %s",
> -		   show ? parent->name : "",
> -		   show ? "_" : "",
> -		   sync_status_str(status));
> -
> -	if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
> -		struct timespec64 ts64 =
> -			ktime_to_timespec64(fence->timestamp);
> -
> -		seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
> -	}
> -
> -	if (fence->ops->timeline_value_str &&
> -		fence->ops->fence_value_str) {
> -		char value[64];
> -		bool success;
> -
> -		fence->ops->fence_value_str(fence, value, sizeof(value));
> -		success = strlen(value);
> -
> -		if (success) {
> -			seq_printf(s, ": %s", value);
> -
> -			fence->ops->timeline_value_str(fence, value,
> -						       sizeof(value));
> -
> -			if (strlen(value))
> -				seq_printf(s, " / %s", value);
> -		}
> -	}
> -
> -	seq_putc(s, '\n');
> -}
> -
> -static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
> -{
> -	struct list_head *pos;
> -
> -	seq_printf(s, "%s: %d\n", obj->name, obj->value);
> -
> -	spin_lock_irq(&obj->lock);
> -	list_for_each(pos, &obj->pt_list) {
> -		struct sync_pt *pt = container_of(pos, struct sync_pt, link);
> -		sync_print_fence(s, &pt->base, false);
> -	}
> -	spin_unlock_irq(&obj->lock);
> -}
> -
> -static void sync_print_sync_file(struct seq_file *s,
> -				  struct sync_file *sync_file)
> -{
> -	char buf[128];
> -	int i;
> -
> -	seq_printf(s, "[%p] %s: %s\n", sync_file,
> -		   sync_file_get_name(sync_file, buf, sizeof(buf)),
> -		   sync_status_str(dma_fence_get_status(sync_file->fence)));
> -
> -	if (dma_fence_is_array(sync_file->fence)) {
> -		struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
> -
> -		for (i = 0; i < array->num_fences; ++i)
> -			sync_print_fence(s, array->fences[i], true);
> -	} else {
> -		sync_print_fence(s, sync_file->fence, true);
> -	}
> -}
> -
> -static int sync_info_debugfs_show(struct seq_file *s, void *unused)
> -{
> -	struct list_head *pos;
> -
> -	seq_puts(s, "objs:\n--------------\n");
> -
> -	spin_lock_irq(&sync_timeline_list_lock);
> -	list_for_each(pos, &sync_timeline_list_head) {
> -		struct sync_timeline *obj =
> -			container_of(pos, struct sync_timeline,
> -				     sync_timeline_list);
> -
> -		sync_print_obj(s, obj);
> -		seq_putc(s, '\n');
> -	}
> -	spin_unlock_irq(&sync_timeline_list_lock);
> -
> -	seq_puts(s, "fences:\n--------------\n");
> -
> -	spin_lock_irq(&sync_file_list_lock);
> -	list_for_each(pos, &sync_file_list_head) {
> -		struct sync_file *sync_file =
> -			container_of(pos, struct sync_file, sync_file_list);
> -
> -		sync_print_sync_file(s, sync_file);
> -		seq_putc(s, '\n');
> -	}
> -	spin_unlock_irq(&sync_file_list_lock);
> -	return 0;
> -}
> -
> -DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
> -
> -static __init int sync_debugfs_init(void)
> -{
> -	dbgfs = debugfs_create_dir("sync", NULL);
> -
> -	/*
> -	 * The debugfs files won't ever get removed and thus, there is
> -	 * no need to protect it against removal races. The use of
> -	 * debugfs_create_file_unsafe() is actually safe here.
> -	 */
> -	debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
> -				   &sync_info_debugfs_fops);
> -	debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
> -				   &sw_sync_debugfs_fops);
> -
> -	return 0;
> -}
> -late_initcall(sync_debugfs_init);
> diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
> deleted file mode 100644
> index 6176e52ba2d7..000000000000
> --- a/drivers/dma-buf/sync_debug.h
> +++ /dev/null
> @@ -1,72 +0,0 @@
> -/*
> - * Sync File validation framework and debug infomation
> - *
> - * Copyright (C) 2012 Google, Inc.
> - *
> - * This program is distributed in the hope that it will be useful,
> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> - * GNU General Public License for more details.
> - *
> - */
> -
> -#ifndef _LINUX_SYNC_H
> -#define _LINUX_SYNC_H
> -
> -#include <linux/list.h>
> -#include <linux/rbtree.h>
> -#include <linux/spinlock.h>
> -#include <linux/dma-fence.h>
> -
> -#include <linux/sync_file.h>
> -#include <uapi/linux/sync_file.h>
> -
> -/**
> - * struct sync_timeline - sync object
> - * @kref:		reference count on fence.
> - * @name:		name of the sync_timeline. Useful for debugging
> - * @lock:		lock protecting @pt_list and @value
> - * @pt_tree:		rbtree of active (unsignaled/errored) sync_pts
> - * @pt_list:		list of active (unsignaled/errored) sync_pts
> - * @sync_timeline_list:	membership in global sync_timeline_list
> - */
> -struct sync_timeline {
> -	struct kref		kref;
> -	char			name[32];
> -
> -	/* protected by lock */
> -	u64			context;
> -	int			value;
> -
> -	struct rb_root		pt_tree;
> -	struct list_head	pt_list;
> -	spinlock_t		lock;
> -
> -	struct list_head	sync_timeline_list;
> -};
> -
> -static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
> -{
> -	return container_of(fence->lock, struct sync_timeline, lock);
> -}
> -
> -/**
> - * struct sync_pt - sync_pt object
> - * @base: base fence object
> - * @link: link on the sync timeline's list
> - * @node: node in the sync timeline's tree
> - */
> -struct sync_pt {
> -	struct dma_fence base;
> -	struct list_head link;
> -	struct rb_node node;
> -};
> -
> -extern const struct file_operations sw_sync_debugfs_fops;
> -
> -void sync_timeline_debug_add(struct sync_timeline *obj);
> -void sync_timeline_debug_remove(struct sync_timeline *obj);
> -void sync_file_debug_add(struct sync_file *fence);
> -void sync_file_debug_remove(struct sync_file *fence);
> -
> -#endif /* _LINUX_SYNC_H */
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 1/3] dma-buf: nuke seqno-fence
  2021-07-29  7:03 [PATCH 1/3] dma-buf: nuke seqno-fence Christian König
@ 2021-07-29  7:24   ` Daniel Vetter
  2021-07-29  7:03 ` [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files Christian König
  2021-07-29  7:24   ` Daniel Vetter
  2 siblings, 0 replies; 26+ messages in thread
From: Daniel Vetter @ 2021-07-29  7:24 UTC (permalink / raw)
  To: Christian König
  Cc: sumit.semwal, gustavo, daniel, linux-media, dri-devel, linaro-mm-sig

On Thu, Jul 29, 2021 at 09:03:28AM +0200, Christian König wrote:
> Entirely unused.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>

Acked-by: Daniel Vetter <daniel.vetter@ffwll.ch>

> ---
>  drivers/dma-buf/Makefile      |   2 +-
>  drivers/dma-buf/seqno-fence.c |  71 ----------------------
>  include/linux/seqno-fence.h   | 109 ----------------------------------
>  3 files changed, 1 insertion(+), 181 deletions(-)
>  delete mode 100644 drivers/dma-buf/seqno-fence.c
>  delete mode 100644 include/linux/seqno-fence.h
> 
> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
> index 40d81f23cacf..1ef021273a06 100644
> --- a/drivers/dma-buf/Makefile
> +++ b/drivers/dma-buf/Makefile
> @@ -1,6 +1,6 @@
>  # SPDX-License-Identifier: GPL-2.0-only
>  obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
> -	 dma-resv.o seqno-fence.o
> +	 dma-resv.o
>  obj-$(CONFIG_DMABUF_HEAPS)	+= dma-heap.o
>  obj-$(CONFIG_DMABUF_HEAPS)	+= heaps/
>  obj-$(CONFIG_SYNC_FILE)		+= sync_file.o
> diff --git a/drivers/dma-buf/seqno-fence.c b/drivers/dma-buf/seqno-fence.c
> deleted file mode 100644
> index bfe14e94c488..000000000000
> --- a/drivers/dma-buf/seqno-fence.c
> +++ /dev/null
> @@ -1,71 +0,0 @@
> -// SPDX-License-Identifier: GPL-2.0-only
> -/*
> - * seqno-fence, using a dma-buf to synchronize fencing
> - *
> - * Copyright (C) 2012 Texas Instruments
> - * Copyright (C) 2012-2014 Canonical Ltd
> - * Authors:
> - *   Rob Clark <robdclark@gmail.com>
> - *   Maarten Lankhorst <maarten.lankhorst@canonical.com>
> - */
> -
> -#include <linux/slab.h>
> -#include <linux/export.h>
> -#include <linux/seqno-fence.h>
> -
> -static const char *seqno_fence_get_driver_name(struct dma_fence *fence)
> -{
> -	struct seqno_fence *seqno_fence = to_seqno_fence(fence);
> -
> -	return seqno_fence->ops->get_driver_name(fence);
> -}
> -
> -static const char *seqno_fence_get_timeline_name(struct dma_fence *fence)
> -{
> -	struct seqno_fence *seqno_fence = to_seqno_fence(fence);
> -
> -	return seqno_fence->ops->get_timeline_name(fence);
> -}
> -
> -static bool seqno_enable_signaling(struct dma_fence *fence)
> -{
> -	struct seqno_fence *seqno_fence = to_seqno_fence(fence);
> -
> -	return seqno_fence->ops->enable_signaling(fence);
> -}
> -
> -static bool seqno_signaled(struct dma_fence *fence)
> -{
> -	struct seqno_fence *seqno_fence = to_seqno_fence(fence);
> -
> -	return seqno_fence->ops->signaled && seqno_fence->ops->signaled(fence);
> -}
> -
> -static void seqno_release(struct dma_fence *fence)
> -{
> -	struct seqno_fence *f = to_seqno_fence(fence);
> -
> -	dma_buf_put(f->sync_buf);
> -	if (f->ops->release)
> -		f->ops->release(fence);
> -	else
> -		dma_fence_free(&f->base);
> -}
> -
> -static signed long seqno_wait(struct dma_fence *fence, bool intr,
> -			      signed long timeout)
> -{
> -	struct seqno_fence *f = to_seqno_fence(fence);
> -
> -	return f->ops->wait(fence, intr, timeout);
> -}
> -
> -const struct dma_fence_ops seqno_fence_ops = {
> -	.get_driver_name = seqno_fence_get_driver_name,
> -	.get_timeline_name = seqno_fence_get_timeline_name,
> -	.enable_signaling = seqno_enable_signaling,
> -	.signaled = seqno_signaled,
> -	.wait = seqno_wait,
> -	.release = seqno_release,
> -};
> -EXPORT_SYMBOL(seqno_fence_ops);
> diff --git a/include/linux/seqno-fence.h b/include/linux/seqno-fence.h
> deleted file mode 100644
> index 3cca2b8fac43..000000000000
> --- a/include/linux/seqno-fence.h
> +++ /dev/null
> @@ -1,109 +0,0 @@
> -/* SPDX-License-Identifier: GPL-2.0-only */
> -/*
> - * seqno-fence, using a dma-buf to synchronize fencing
> - *
> - * Copyright (C) 2012 Texas Instruments
> - * Copyright (C) 2012 Canonical Ltd
> - * Authors:
> - *   Rob Clark <robdclark@gmail.com>
> - *   Maarten Lankhorst <maarten.lankhorst@canonical.com>
> - */
> -
> -#ifndef __LINUX_SEQNO_FENCE_H
> -#define __LINUX_SEQNO_FENCE_H
> -
> -#include <linux/dma-fence.h>
> -#include <linux/dma-buf.h>
> -
> -enum seqno_fence_condition {
> -	SEQNO_FENCE_WAIT_GEQUAL,
> -	SEQNO_FENCE_WAIT_NONZERO
> -};
> -
> -struct seqno_fence {
> -	struct dma_fence base;
> -
> -	const struct dma_fence_ops *ops;
> -	struct dma_buf *sync_buf;
> -	uint32_t seqno_ofs;
> -	enum seqno_fence_condition condition;
> -};
> -
> -extern const struct dma_fence_ops seqno_fence_ops;
> -
> -/**
> - * to_seqno_fence - cast a fence to a seqno_fence
> - * @fence: fence to cast to a seqno_fence
> - *
> - * Returns NULL if the fence is not a seqno_fence,
> - * or the seqno_fence otherwise.
> - */
> -static inline struct seqno_fence *
> -to_seqno_fence(struct dma_fence *fence)
> -{
> -	if (fence->ops != &seqno_fence_ops)
> -		return NULL;
> -	return container_of(fence, struct seqno_fence, base);
> -}
> -
> -/**
> - * seqno_fence_init - initialize a seqno fence
> - * @fence: seqno_fence to initialize
> - * @lock: pointer to spinlock to use for fence
> - * @sync_buf: buffer containing the memory location to signal on
> - * @context: the execution context this fence is a part of
> - * @seqno_ofs: the offset within @sync_buf
> - * @seqno: the sequence # to signal on
> - * @cond: fence wait condition
> - * @ops: the fence_ops for operations on this seqno fence
> - *
> - * This function initializes a struct seqno_fence with passed parameters,
> - * and takes a reference on sync_buf which is released on fence destruction.
> - *
> - * A seqno_fence is a dma_fence which can complete in software when
> - * enable_signaling is called, but it also completes when
> - * (s32)((sync_buf)[seqno_ofs] - seqno) >= 0 is true
> - *
> - * The seqno_fence will take a refcount on the sync_buf until it's
> - * destroyed, but actual lifetime of sync_buf may be longer if one of the
> - * callers take a reference to it.
> - *
> - * Certain hardware have instructions to insert this type of wait condition
> - * in the command stream, so no intervention from software would be needed.
> - * This type of fence can be destroyed before completed, however a reference
> - * on the sync_buf dma-buf can be taken. It is encouraged to re-use the same
> - * dma-buf for sync_buf, since mapping or unmapping the sync_buf to the
> - * device's vm can be expensive.
> - *
> - * It is recommended for creators of seqno_fence to call dma_fence_signal()
> - * before destruction. This will prevent possible issues from wraparound at
> - * time of issue vs time of check, since users can check dma_fence_is_signaled()
> - * before submitting instructions for the hardware to wait on the fence.
> - * However, when ops.enable_signaling is not called, it doesn't have to be
> - * done as soon as possible, just before there's any real danger of seqno
> - * wraparound.
> - */
> -static inline void
> -seqno_fence_init(struct seqno_fence *fence, spinlock_t *lock,
> -		 struct dma_buf *sync_buf,  uint32_t context,
> -		 uint32_t seqno_ofs, uint32_t seqno,
> -		 enum seqno_fence_condition cond,
> -		 const struct dma_fence_ops *ops)
> -{
> -	BUG_ON(!fence || !sync_buf || !ops);
> -	BUG_ON(!ops->wait || !ops->enable_signaling ||
> -	       !ops->get_driver_name || !ops->get_timeline_name);
> -
> -	/*
> -	 * ops is used in dma_fence_init for get_driver_name, so needs to be
> -	 * initialized first
> -	 */
> -	fence->ops = ops;
> -	dma_fence_init(&fence->base, &seqno_fence_ops, lock, context, seqno);
> -	get_dma_buf(sync_buf);
> -	fence->sync_buf = sync_buf;
> -	fence->seqno_ofs = seqno_ofs;
> -	fence->condition = cond;
> -}
> -
> -#endif /* __LINUX_SEQNO_FENCE_H */
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 1/3] dma-buf: nuke seqno-fence
@ 2021-07-29  7:24   ` Daniel Vetter
  0 siblings, 0 replies; 26+ messages in thread
From: Daniel Vetter @ 2021-07-29  7:24 UTC (permalink / raw)
  To: Christian König; +Cc: gustavo, dri-devel, linaro-mm-sig, linux-media

On Thu, Jul 29, 2021 at 09:03:28AM +0200, Christian König wrote:
> Entirely unused.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>

Acked-by: Daniel Vetter <daniel.vetter@ffwll.ch>

> ---
>  drivers/dma-buf/Makefile      |   2 +-
>  drivers/dma-buf/seqno-fence.c |  71 ----------------------
>  include/linux/seqno-fence.h   | 109 ----------------------------------
>  3 files changed, 1 insertion(+), 181 deletions(-)
>  delete mode 100644 drivers/dma-buf/seqno-fence.c
>  delete mode 100644 include/linux/seqno-fence.h
> 
> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
> index 40d81f23cacf..1ef021273a06 100644
> --- a/drivers/dma-buf/Makefile
> +++ b/drivers/dma-buf/Makefile
> @@ -1,6 +1,6 @@
>  # SPDX-License-Identifier: GPL-2.0-only
>  obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
> -	 dma-resv.o seqno-fence.o
> +	 dma-resv.o
>  obj-$(CONFIG_DMABUF_HEAPS)	+= dma-heap.o
>  obj-$(CONFIG_DMABUF_HEAPS)	+= heaps/
>  obj-$(CONFIG_SYNC_FILE)		+= sync_file.o
> diff --git a/drivers/dma-buf/seqno-fence.c b/drivers/dma-buf/seqno-fence.c
> deleted file mode 100644
> index bfe14e94c488..000000000000
> --- a/drivers/dma-buf/seqno-fence.c
> +++ /dev/null
> @@ -1,71 +0,0 @@
> -// SPDX-License-Identifier: GPL-2.0-only
> -/*
> - * seqno-fence, using a dma-buf to synchronize fencing
> - *
> - * Copyright (C) 2012 Texas Instruments
> - * Copyright (C) 2012-2014 Canonical Ltd
> - * Authors:
> - *   Rob Clark <robdclark@gmail.com>
> - *   Maarten Lankhorst <maarten.lankhorst@canonical.com>
> - */
> -
> -#include <linux/slab.h>
> -#include <linux/export.h>
> -#include <linux/seqno-fence.h>
> -
> -static const char *seqno_fence_get_driver_name(struct dma_fence *fence)
> -{
> -	struct seqno_fence *seqno_fence = to_seqno_fence(fence);
> -
> -	return seqno_fence->ops->get_driver_name(fence);
> -}
> -
> -static const char *seqno_fence_get_timeline_name(struct dma_fence *fence)
> -{
> -	struct seqno_fence *seqno_fence = to_seqno_fence(fence);
> -
> -	return seqno_fence->ops->get_timeline_name(fence);
> -}
> -
> -static bool seqno_enable_signaling(struct dma_fence *fence)
> -{
> -	struct seqno_fence *seqno_fence = to_seqno_fence(fence);
> -
> -	return seqno_fence->ops->enable_signaling(fence);
> -}
> -
> -static bool seqno_signaled(struct dma_fence *fence)
> -{
> -	struct seqno_fence *seqno_fence = to_seqno_fence(fence);
> -
> -	return seqno_fence->ops->signaled && seqno_fence->ops->signaled(fence);
> -}
> -
> -static void seqno_release(struct dma_fence *fence)
> -{
> -	struct seqno_fence *f = to_seqno_fence(fence);
> -
> -	dma_buf_put(f->sync_buf);
> -	if (f->ops->release)
> -		f->ops->release(fence);
> -	else
> -		dma_fence_free(&f->base);
> -}
> -
> -static signed long seqno_wait(struct dma_fence *fence, bool intr,
> -			      signed long timeout)
> -{
> -	struct seqno_fence *f = to_seqno_fence(fence);
> -
> -	return f->ops->wait(fence, intr, timeout);
> -}
> -
> -const struct dma_fence_ops seqno_fence_ops = {
> -	.get_driver_name = seqno_fence_get_driver_name,
> -	.get_timeline_name = seqno_fence_get_timeline_name,
> -	.enable_signaling = seqno_enable_signaling,
> -	.signaled = seqno_signaled,
> -	.wait = seqno_wait,
> -	.release = seqno_release,
> -};
> -EXPORT_SYMBOL(seqno_fence_ops);
> diff --git a/include/linux/seqno-fence.h b/include/linux/seqno-fence.h
> deleted file mode 100644
> index 3cca2b8fac43..000000000000
> --- a/include/linux/seqno-fence.h
> +++ /dev/null
> @@ -1,109 +0,0 @@
> -/* SPDX-License-Identifier: GPL-2.0-only */
> -/*
> - * seqno-fence, using a dma-buf to synchronize fencing
> - *
> - * Copyright (C) 2012 Texas Instruments
> - * Copyright (C) 2012 Canonical Ltd
> - * Authors:
> - *   Rob Clark <robdclark@gmail.com>
> - *   Maarten Lankhorst <maarten.lankhorst@canonical.com>
> - */
> -
> -#ifndef __LINUX_SEQNO_FENCE_H
> -#define __LINUX_SEQNO_FENCE_H
> -
> -#include <linux/dma-fence.h>
> -#include <linux/dma-buf.h>
> -
> -enum seqno_fence_condition {
> -	SEQNO_FENCE_WAIT_GEQUAL,
> -	SEQNO_FENCE_WAIT_NONZERO
> -};
> -
> -struct seqno_fence {
> -	struct dma_fence base;
> -
> -	const struct dma_fence_ops *ops;
> -	struct dma_buf *sync_buf;
> -	uint32_t seqno_ofs;
> -	enum seqno_fence_condition condition;
> -};
> -
> -extern const struct dma_fence_ops seqno_fence_ops;
> -
> -/**
> - * to_seqno_fence - cast a fence to a seqno_fence
> - * @fence: fence to cast to a seqno_fence
> - *
> - * Returns NULL if the fence is not a seqno_fence,
> - * or the seqno_fence otherwise.
> - */
> -static inline struct seqno_fence *
> -to_seqno_fence(struct dma_fence *fence)
> -{
> -	if (fence->ops != &seqno_fence_ops)
> -		return NULL;
> -	return container_of(fence, struct seqno_fence, base);
> -}
> -
> -/**
> - * seqno_fence_init - initialize a seqno fence
> - * @fence: seqno_fence to initialize
> - * @lock: pointer to spinlock to use for fence
> - * @sync_buf: buffer containing the memory location to signal on
> - * @context: the execution context this fence is a part of
> - * @seqno_ofs: the offset within @sync_buf
> - * @seqno: the sequence # to signal on
> - * @cond: fence wait condition
> - * @ops: the fence_ops for operations on this seqno fence
> - *
> - * This function initializes a struct seqno_fence with passed parameters,
> - * and takes a reference on sync_buf which is released on fence destruction.
> - *
> - * A seqno_fence is a dma_fence which can complete in software when
> - * enable_signaling is called, but it also completes when
> - * (s32)((sync_buf)[seqno_ofs] - seqno) >= 0 is true
> - *
> - * The seqno_fence will take a refcount on the sync_buf until it's
> - * destroyed, but actual lifetime of sync_buf may be longer if one of the
> - * callers take a reference to it.
> - *
> - * Certain hardware have instructions to insert this type of wait condition
> - * in the command stream, so no intervention from software would be needed.
> - * This type of fence can be destroyed before completed, however a reference
> - * on the sync_buf dma-buf can be taken. It is encouraged to re-use the same
> - * dma-buf for sync_buf, since mapping or unmapping the sync_buf to the
> - * device's vm can be expensive.
> - *
> - * It is recommended for creators of seqno_fence to call dma_fence_signal()
> - * before destruction. This will prevent possible issues from wraparound at
> - * time of issue vs time of check, since users can check dma_fence_is_signaled()
> - * before submitting instructions for the hardware to wait on the fence.
> - * However, when ops.enable_signaling is not called, it doesn't have to be
> - * done as soon as possible, just before there's any real danger of seqno
> - * wraparound.
> - */
> -static inline void
> -seqno_fence_init(struct seqno_fence *fence, spinlock_t *lock,
> -		 struct dma_buf *sync_buf,  uint32_t context,
> -		 uint32_t seqno_ofs, uint32_t seqno,
> -		 enum seqno_fence_condition cond,
> -		 const struct dma_fence_ops *ops)
> -{
> -	BUG_ON(!fence || !sync_buf || !ops);
> -	BUG_ON(!ops->wait || !ops->enable_signaling ||
> -	       !ops->get_driver_name || !ops->get_timeline_name);
> -
> -	/*
> -	 * ops is used in dma_fence_init for get_driver_name, so needs to be
> -	 * initialized first
> -	 */
> -	fence->ops = ops;
> -	dma_fence_init(&fence->base, &seqno_fence_ops, lock, context, seqno);
> -	get_dma_buf(sync_buf);
> -	fence->sync_buf = sync_buf;
> -	fence->seqno_ofs = seqno_ofs;
> -	fence->condition = cond;
> -}
> -
> -#endif /* __LINUX_SEQNO_FENCE_H */
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 2/3] dma-buf: nuke DMA_FENCE_TRACE macros
  2021-07-29  7:22     ` Daniel Vetter
@ 2021-07-29  7:24       ` Christian König
  -1 siblings, 0 replies; 26+ messages in thread
From: Christian König @ 2021-07-29  7:24 UTC (permalink / raw)
  To: Daniel Vetter
  Cc: sumit.semwal, gustavo, linux-media, dri-devel, linaro-mm-sig



Am 29.07.21 um 09:22 schrieb Daniel Vetter:
> On Thu, Jul 29, 2021 at 09:03:29AM +0200, Christian König wrote:
>> Only the DRM GPU scheduler, radeon and amdgpu where using them and they depend
>> on a non existing config option to actually emit some code.
>>
>> Nuke them and clean up the dma_fence_signal* return value.
>>
>> Signed-off-by: Christian König <christian.koenig@amd.com>
> So i915 is very much become a horror show of wheel reinventing (and we're
> trying to fix that), but there's some legit hand-rolled use-cases for
> having the return value of dma_fence_signal. We're not using that right
> now because hand-rollled nonsense, but can you perhaps keep that part?

I was considering that for the unit tests as well, but then though "nah, 
not if no drivers uses it".

But can I make that a bool while at it? E.g. true for was signaled and 
false for already signaled like we have for the callbacks?

Thanks,
Christian.

>
> Other pieces lgtm.
> -Daniel
>
>> ---
>>   drivers/dma-buf/dma-fence.c               | 44 +++++------------------
>>   drivers/dma-buf/st-dma-fence.c            | 12 ++-----
>>   drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 10 +-----
>>   drivers/gpu/drm/radeon/radeon_fence.c     | 24 +++----------
>>   drivers/gpu/drm/scheduler/sched_fence.c   | 18 ++--------
>>   include/linux/dma-fence.h                 | 32 +++--------------
>>   6 files changed, 23 insertions(+), 117 deletions(-)
>>
>> diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c
>> index ce0f5eff575d..21cba0f74e69 100644
>> --- a/drivers/dma-buf/dma-fence.c
>> +++ b/drivers/dma-buf/dma-fence.c
>> @@ -350,12 +350,9 @@ void __dma_fence_might_wait(void)
>>    *
>>    * Unlike dma_fence_signal_timestamp(), this function must be called with
>>    * &dma_fence.lock held.
>> - *
>> - * Returns 0 on success and a negative error value when @fence has been
>> - * signalled already.
>>    */
>> -int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>> -				      ktime_t timestamp)
>> +void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>> +				       ktime_t timestamp)
>>   {
>>   	struct dma_fence_cb *cur, *tmp;
>>   	struct list_head cb_list;
>> @@ -364,7 +361,7 @@ int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>>   
>>   	if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
>>   				      &fence->flags)))
>> -		return -EINVAL;
>> +		return;
>>   
>>   	/* Stash the cb_list before replacing it with the timestamp */
>>   	list_replace(&fence->cb_list, &cb_list);
>> @@ -377,8 +374,6 @@ int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>>   		INIT_LIST_HEAD(&cur->node);
>>   		cur->func(fence, cur);
>>   	}
>> -
>> -	return 0;
>>   }
>>   EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
>>   
>> @@ -393,23 +388,14 @@ EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
>>    * can only go from the unsignaled to the signaled state and not back, it will
>>    * only be effective the first time. Set the timestamp provided as the fence
>>    * signal timestamp.
>> - *
>> - * Returns 0 on success and a negative error value when @fence has been
>> - * signalled already.
>>    */
>> -int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
>> +void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
>>   {
>>   	unsigned long flags;
>> -	int ret;
>> -
>> -	if (!fence)
>> -		return -EINVAL;
>>   
>>   	spin_lock_irqsave(fence->lock, flags);
>> -	ret = dma_fence_signal_timestamp_locked(fence, timestamp);
>> +	dma_fence_signal_timestamp_locked(fence, timestamp);
>>   	spin_unlock_irqrestore(fence->lock, flags);
>> -
>> -	return ret;
>>   }
>>   EXPORT_SYMBOL(dma_fence_signal_timestamp);
>>   
>> @@ -425,13 +411,10 @@ EXPORT_SYMBOL(dma_fence_signal_timestamp);
>>    *
>>    * Unlike dma_fence_signal(), this function must be called with &dma_fence.lock
>>    * held.
>> - *
>> - * Returns 0 on success and a negative error value when @fence has been
>> - * signalled already.
>>    */
>> -int dma_fence_signal_locked(struct dma_fence *fence)
>> +void dma_fence_signal_locked(struct dma_fence *fence)
>>   {
>> -	return dma_fence_signal_timestamp_locked(fence, ktime_get());
>> +	dma_fence_signal_timestamp_locked(fence, ktime_get());
>>   }
>>   EXPORT_SYMBOL(dma_fence_signal_locked);
>>   
>> @@ -444,28 +427,19 @@ EXPORT_SYMBOL(dma_fence_signal_locked);
>>    * dma_fence_add_callback(). Can be called multiple times, but since a fence
>>    * can only go from the unsignaled to the signaled state and not back, it will
>>    * only be effective the first time.
>> - *
>> - * Returns 0 on success and a negative error value when @fence has been
>> - * signalled already.
>>    */
>> -int dma_fence_signal(struct dma_fence *fence)
>> +void dma_fence_signal(struct dma_fence *fence)
>>   {
>>   	unsigned long flags;
>> -	int ret;
>>   	bool tmp;
>>   
>> -	if (!fence)
>> -		return -EINVAL;
>> -
>>   	tmp = dma_fence_begin_signalling();
>>   
>>   	spin_lock_irqsave(fence->lock, flags);
>> -	ret = dma_fence_signal_timestamp_locked(fence, ktime_get());
>> +	dma_fence_signal_timestamp_locked(fence, ktime_get());
>>   	spin_unlock_irqrestore(fence->lock, flags);
>>   
>>   	dma_fence_end_signalling(tmp);
>> -
>> -	return ret;
>>   }
>>   EXPORT_SYMBOL(dma_fence_signal);
>>   
>> diff --git a/drivers/dma-buf/st-dma-fence.c b/drivers/dma-buf/st-dma-fence.c
>> index c8a12d7ad71a..e89001d8873f 100644
>> --- a/drivers/dma-buf/st-dma-fence.c
>> +++ b/drivers/dma-buf/st-dma-fence.c
>> @@ -122,21 +122,13 @@ static int test_signaling(void *arg)
>>   		goto err_free;
>>   	}
>>   
>> -	if (dma_fence_signal(f)) {
>> -		pr_err("Fence reported being already signaled\n");
>> -		goto err_free;
>> -	}
>> -
>> +	dma_fence_signal(f);
>>   	if (!dma_fence_is_signaled(f)) {
>>   		pr_err("Fence not reporting signaled\n");
>>   		goto err_free;
>>   	}
>>   
>> -	if (!dma_fence_signal(f)) {
>> -		pr_err("Fence reported not being already signaled\n");
>> -		goto err_free;
>> -	}
>> -
>> +	dma_fence_signal(f);
>>   	err = 0;
>>   err_free:
>>   	dma_fence_put(f);
>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
>> index d4547d195173..397872779c31 100644
>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
>> @@ -246,7 +246,6 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
>>   	struct amdgpu_fence_driver *drv = &ring->fence_drv;
>>   	struct amdgpu_device *adev = ring->adev;
>>   	uint32_t seq, last_seq;
>> -	int r;
>>   
>>   	do {
>>   		last_seq = atomic_read(&ring->fence_drv.last_seq);
>> @@ -278,12 +277,7 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
>>   		if (!fence)
>>   			continue;
>>   
>> -		r = dma_fence_signal(fence);
>> -		if (!r)
>> -			DMA_FENCE_TRACE(fence, "signaled from irq context\n");
>> -		else
>> -			BUG();
>> -
>> +		dma_fence_signal(fence);
>>   		dma_fence_put(fence);
>>   		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
>>   		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
>> @@ -673,8 +667,6 @@ static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
>>   	if (!timer_pending(&ring->fence_drv.fallback_timer))
>>   		amdgpu_fence_schedule_fallback(ring);
>>   
>> -	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
>> -
>>   	return true;
>>   }
>>   
>> diff --git a/drivers/gpu/drm/radeon/radeon_fence.c b/drivers/gpu/drm/radeon/radeon_fence.c
>> index 18f2c2e0dfb3..3f351d222cbb 100644
>> --- a/drivers/gpu/drm/radeon/radeon_fence.c
>> +++ b/drivers/gpu/drm/radeon/radeon_fence.c
>> @@ -176,18 +176,11 @@ static int radeon_fence_check_signaled(wait_queue_entry_t *wait, unsigned mode,
>>   	 */
>>   	seq = atomic64_read(&fence->rdev->fence_drv[fence->ring].last_seq);
>>   	if (seq >= fence->seq) {
>> -		int ret = dma_fence_signal_locked(&fence->base);
>> -
>> -		if (!ret)
>> -			DMA_FENCE_TRACE(&fence->base, "signaled from irq context\n");
>> -		else
>> -			DMA_FENCE_TRACE(&fence->base, "was already signaled\n");
>> -
>> +		dma_fence_signal_locked(&fence->base);
>>   		radeon_irq_kms_sw_irq_put(fence->rdev, fence->ring);
>>   		__remove_wait_queue(&fence->rdev->fence_queue, &fence->fence_wake);
>>   		dma_fence_put(&fence->base);
>> -	} else
>> -		DMA_FENCE_TRACE(&fence->base, "pending\n");
>> +	}
>>   	return 0;
>>   }
>>   
>> @@ -422,8 +415,6 @@ static bool radeon_fence_enable_signaling(struct dma_fence *f)
>>   	fence->fence_wake.func = radeon_fence_check_signaled;
>>   	__add_wait_queue(&rdev->fence_queue, &fence->fence_wake);
>>   	dma_fence_get(f);
>> -
>> -	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", fence->ring);
>>   	return true;
>>   }
>>   
>> @@ -441,11 +432,7 @@ bool radeon_fence_signaled(struct radeon_fence *fence)
>>   		return true;
>>   
>>   	if (radeon_fence_seq_signaled(fence->rdev, fence->seq, fence->ring)) {
>> -		int ret;
>> -
>> -		ret = dma_fence_signal(&fence->base);
>> -		if (!ret)
>> -			DMA_FENCE_TRACE(&fence->base, "signaled from radeon_fence_signaled\n");
>> +		dma_fence_signal(&fence->base);
>>   		return true;
>>   	}
>>   	return false;
>> @@ -550,7 +537,6 @@ long radeon_fence_wait_timeout(struct radeon_fence *fence, bool intr, long timeo
>>   {
>>   	uint64_t seq[RADEON_NUM_RINGS] = {};
>>   	long r;
>> -	int r_sig;
>>   
>>   	/*
>>   	 * This function should not be called on !radeon fences.
>> @@ -567,9 +553,7 @@ long radeon_fence_wait_timeout(struct radeon_fence *fence, bool intr, long timeo
>>   		return r;
>>   	}
>>   
>> -	r_sig = dma_fence_signal(&fence->base);
>> -	if (!r_sig)
>> -		DMA_FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
>> +	dma_fence_signal(&fence->base);
>>   	return r;
>>   }
>>   
>> diff --git a/drivers/gpu/drm/scheduler/sched_fence.c b/drivers/gpu/drm/scheduler/sched_fence.c
>> index 69de2c76731f..3736746c47bd 100644
>> --- a/drivers/gpu/drm/scheduler/sched_fence.c
>> +++ b/drivers/gpu/drm/scheduler/sched_fence.c
>> @@ -50,26 +50,12 @@ static void __exit drm_sched_fence_slab_fini(void)
>>   
>>   void drm_sched_fence_scheduled(struct drm_sched_fence *fence)
>>   {
>> -	int ret = dma_fence_signal(&fence->scheduled);
>> -
>> -	if (!ret)
>> -		DMA_FENCE_TRACE(&fence->scheduled,
>> -				"signaled from irq context\n");
>> -	else
>> -		DMA_FENCE_TRACE(&fence->scheduled,
>> -				"was already signaled\n");
>> +	dma_fence_signal(&fence->scheduled);
>>   }
>>   
>>   void drm_sched_fence_finished(struct drm_sched_fence *fence)
>>   {
>> -	int ret = dma_fence_signal(&fence->finished);
>> -
>> -	if (!ret)
>> -		DMA_FENCE_TRACE(&fence->finished,
>> -				"signaled from irq context\n");
>> -	else
>> -		DMA_FENCE_TRACE(&fence->finished,
>> -				"was already signaled\n");
>> +	dma_fence_signal(&fence->finished);
>>   }
>>   
>>   static const char *drm_sched_fence_get_driver_name(struct dma_fence *fence)
>> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
>> index 6ffb4b2c6371..027db23fd4e3 100644
>> --- a/include/linux/dma-fence.h
>> +++ b/include/linux/dma-fence.h
>> @@ -370,11 +370,11 @@ static inline void dma_fence_end_signalling(bool cookie) {}
>>   static inline void __dma_fence_might_wait(void) {}
>>   #endif
>>   
>> -int dma_fence_signal(struct dma_fence *fence);
>> -int dma_fence_signal_locked(struct dma_fence *fence);
>> -int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp);
>> -int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>> -				      ktime_t timestamp);
>> +void dma_fence_signal(struct dma_fence *fence);
>> +void dma_fence_signal_locked(struct dma_fence *fence);
>> +void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp);
>> +void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>> +				       ktime_t timestamp);
>>   signed long dma_fence_default_wait(struct dma_fence *fence,
>>   				   bool intr, signed long timeout);
>>   int dma_fence_add_callback(struct dma_fence *fence,
>> @@ -590,26 +590,4 @@ struct dma_fence *dma_fence_get_stub(void);
>>   struct dma_fence *dma_fence_allocate_private_stub(void);
>>   u64 dma_fence_context_alloc(unsigned num);
>>   
>> -#define DMA_FENCE_TRACE(f, fmt, args...) \
>> -	do {								\
>> -		struct dma_fence *__ff = (f);				\
>> -		if (IS_ENABLED(CONFIG_DMA_FENCE_TRACE))			\
>> -			pr_info("f %llu#%llu: " fmt,			\
>> -				__ff->context, __ff->seqno, ##args);	\
>> -	} while (0)
>> -
>> -#define DMA_FENCE_WARN(f, fmt, args...) \
>> -	do {								\
>> -		struct dma_fence *__ff = (f);				\
>> -		pr_warn("f %llu#%llu: " fmt, __ff->context, __ff->seqno,\
>> -			 ##args);					\
>> -	} while (0)
>> -
>> -#define DMA_FENCE_ERR(f, fmt, args...) \
>> -	do {								\
>> -		struct dma_fence *__ff = (f);				\
>> -		pr_err("f %llu#%llu: " fmt, __ff->context, __ff->seqno,	\
>> -			##args);					\
>> -	} while (0)
>> -
>>   #endif /* __LINUX_DMA_FENCE_H */
>> -- 
>> 2.25.1
>>


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 2/3] dma-buf: nuke DMA_FENCE_TRACE macros
@ 2021-07-29  7:24       ` Christian König
  0 siblings, 0 replies; 26+ messages in thread
From: Christian König @ 2021-07-29  7:24 UTC (permalink / raw)
  To: Daniel Vetter; +Cc: linaro-mm-sig, gustavo, dri-devel, linux-media



Am 29.07.21 um 09:22 schrieb Daniel Vetter:
> On Thu, Jul 29, 2021 at 09:03:29AM +0200, Christian König wrote:
>> Only the DRM GPU scheduler, radeon and amdgpu where using them and they depend
>> on a non existing config option to actually emit some code.
>>
>> Nuke them and clean up the dma_fence_signal* return value.
>>
>> Signed-off-by: Christian König <christian.koenig@amd.com>
> So i915 is very much become a horror show of wheel reinventing (and we're
> trying to fix that), but there's some legit hand-rolled use-cases for
> having the return value of dma_fence_signal. We're not using that right
> now because hand-rollled nonsense, but can you perhaps keep that part?

I was considering that for the unit tests as well, but then though "nah, 
not if no drivers uses it".

But can I make that a bool while at it? E.g. true for was signaled and 
false for already signaled like we have for the callbacks?

Thanks,
Christian.

>
> Other pieces lgtm.
> -Daniel
>
>> ---
>>   drivers/dma-buf/dma-fence.c               | 44 +++++------------------
>>   drivers/dma-buf/st-dma-fence.c            | 12 ++-----
>>   drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 10 +-----
>>   drivers/gpu/drm/radeon/radeon_fence.c     | 24 +++----------
>>   drivers/gpu/drm/scheduler/sched_fence.c   | 18 ++--------
>>   include/linux/dma-fence.h                 | 32 +++--------------
>>   6 files changed, 23 insertions(+), 117 deletions(-)
>>
>> diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c
>> index ce0f5eff575d..21cba0f74e69 100644
>> --- a/drivers/dma-buf/dma-fence.c
>> +++ b/drivers/dma-buf/dma-fence.c
>> @@ -350,12 +350,9 @@ void __dma_fence_might_wait(void)
>>    *
>>    * Unlike dma_fence_signal_timestamp(), this function must be called with
>>    * &dma_fence.lock held.
>> - *
>> - * Returns 0 on success and a negative error value when @fence has been
>> - * signalled already.
>>    */
>> -int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>> -				      ktime_t timestamp)
>> +void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>> +				       ktime_t timestamp)
>>   {
>>   	struct dma_fence_cb *cur, *tmp;
>>   	struct list_head cb_list;
>> @@ -364,7 +361,7 @@ int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>>   
>>   	if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
>>   				      &fence->flags)))
>> -		return -EINVAL;
>> +		return;
>>   
>>   	/* Stash the cb_list before replacing it with the timestamp */
>>   	list_replace(&fence->cb_list, &cb_list);
>> @@ -377,8 +374,6 @@ int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>>   		INIT_LIST_HEAD(&cur->node);
>>   		cur->func(fence, cur);
>>   	}
>> -
>> -	return 0;
>>   }
>>   EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
>>   
>> @@ -393,23 +388,14 @@ EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
>>    * can only go from the unsignaled to the signaled state and not back, it will
>>    * only be effective the first time. Set the timestamp provided as the fence
>>    * signal timestamp.
>> - *
>> - * Returns 0 on success and a negative error value when @fence has been
>> - * signalled already.
>>    */
>> -int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
>> +void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
>>   {
>>   	unsigned long flags;
>> -	int ret;
>> -
>> -	if (!fence)
>> -		return -EINVAL;
>>   
>>   	spin_lock_irqsave(fence->lock, flags);
>> -	ret = dma_fence_signal_timestamp_locked(fence, timestamp);
>> +	dma_fence_signal_timestamp_locked(fence, timestamp);
>>   	spin_unlock_irqrestore(fence->lock, flags);
>> -
>> -	return ret;
>>   }
>>   EXPORT_SYMBOL(dma_fence_signal_timestamp);
>>   
>> @@ -425,13 +411,10 @@ EXPORT_SYMBOL(dma_fence_signal_timestamp);
>>    *
>>    * Unlike dma_fence_signal(), this function must be called with &dma_fence.lock
>>    * held.
>> - *
>> - * Returns 0 on success and a negative error value when @fence has been
>> - * signalled already.
>>    */
>> -int dma_fence_signal_locked(struct dma_fence *fence)
>> +void dma_fence_signal_locked(struct dma_fence *fence)
>>   {
>> -	return dma_fence_signal_timestamp_locked(fence, ktime_get());
>> +	dma_fence_signal_timestamp_locked(fence, ktime_get());
>>   }
>>   EXPORT_SYMBOL(dma_fence_signal_locked);
>>   
>> @@ -444,28 +427,19 @@ EXPORT_SYMBOL(dma_fence_signal_locked);
>>    * dma_fence_add_callback(). Can be called multiple times, but since a fence
>>    * can only go from the unsignaled to the signaled state and not back, it will
>>    * only be effective the first time.
>> - *
>> - * Returns 0 on success and a negative error value when @fence has been
>> - * signalled already.
>>    */
>> -int dma_fence_signal(struct dma_fence *fence)
>> +void dma_fence_signal(struct dma_fence *fence)
>>   {
>>   	unsigned long flags;
>> -	int ret;
>>   	bool tmp;
>>   
>> -	if (!fence)
>> -		return -EINVAL;
>> -
>>   	tmp = dma_fence_begin_signalling();
>>   
>>   	spin_lock_irqsave(fence->lock, flags);
>> -	ret = dma_fence_signal_timestamp_locked(fence, ktime_get());
>> +	dma_fence_signal_timestamp_locked(fence, ktime_get());
>>   	spin_unlock_irqrestore(fence->lock, flags);
>>   
>>   	dma_fence_end_signalling(tmp);
>> -
>> -	return ret;
>>   }
>>   EXPORT_SYMBOL(dma_fence_signal);
>>   
>> diff --git a/drivers/dma-buf/st-dma-fence.c b/drivers/dma-buf/st-dma-fence.c
>> index c8a12d7ad71a..e89001d8873f 100644
>> --- a/drivers/dma-buf/st-dma-fence.c
>> +++ b/drivers/dma-buf/st-dma-fence.c
>> @@ -122,21 +122,13 @@ static int test_signaling(void *arg)
>>   		goto err_free;
>>   	}
>>   
>> -	if (dma_fence_signal(f)) {
>> -		pr_err("Fence reported being already signaled\n");
>> -		goto err_free;
>> -	}
>> -
>> +	dma_fence_signal(f);
>>   	if (!dma_fence_is_signaled(f)) {
>>   		pr_err("Fence not reporting signaled\n");
>>   		goto err_free;
>>   	}
>>   
>> -	if (!dma_fence_signal(f)) {
>> -		pr_err("Fence reported not being already signaled\n");
>> -		goto err_free;
>> -	}
>> -
>> +	dma_fence_signal(f);
>>   	err = 0;
>>   err_free:
>>   	dma_fence_put(f);
>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
>> index d4547d195173..397872779c31 100644
>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c
>> @@ -246,7 +246,6 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
>>   	struct amdgpu_fence_driver *drv = &ring->fence_drv;
>>   	struct amdgpu_device *adev = ring->adev;
>>   	uint32_t seq, last_seq;
>> -	int r;
>>   
>>   	do {
>>   		last_seq = atomic_read(&ring->fence_drv.last_seq);
>> @@ -278,12 +277,7 @@ bool amdgpu_fence_process(struct amdgpu_ring *ring)
>>   		if (!fence)
>>   			continue;
>>   
>> -		r = dma_fence_signal(fence);
>> -		if (!r)
>> -			DMA_FENCE_TRACE(fence, "signaled from irq context\n");
>> -		else
>> -			BUG();
>> -
>> +		dma_fence_signal(fence);
>>   		dma_fence_put(fence);
>>   		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
>>   		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
>> @@ -673,8 +667,6 @@ static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
>>   	if (!timer_pending(&ring->fence_drv.fallback_timer))
>>   		amdgpu_fence_schedule_fallback(ring);
>>   
>> -	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
>> -
>>   	return true;
>>   }
>>   
>> diff --git a/drivers/gpu/drm/radeon/radeon_fence.c b/drivers/gpu/drm/radeon/radeon_fence.c
>> index 18f2c2e0dfb3..3f351d222cbb 100644
>> --- a/drivers/gpu/drm/radeon/radeon_fence.c
>> +++ b/drivers/gpu/drm/radeon/radeon_fence.c
>> @@ -176,18 +176,11 @@ static int radeon_fence_check_signaled(wait_queue_entry_t *wait, unsigned mode,
>>   	 */
>>   	seq = atomic64_read(&fence->rdev->fence_drv[fence->ring].last_seq);
>>   	if (seq >= fence->seq) {
>> -		int ret = dma_fence_signal_locked(&fence->base);
>> -
>> -		if (!ret)
>> -			DMA_FENCE_TRACE(&fence->base, "signaled from irq context\n");
>> -		else
>> -			DMA_FENCE_TRACE(&fence->base, "was already signaled\n");
>> -
>> +		dma_fence_signal_locked(&fence->base);
>>   		radeon_irq_kms_sw_irq_put(fence->rdev, fence->ring);
>>   		__remove_wait_queue(&fence->rdev->fence_queue, &fence->fence_wake);
>>   		dma_fence_put(&fence->base);
>> -	} else
>> -		DMA_FENCE_TRACE(&fence->base, "pending\n");
>> +	}
>>   	return 0;
>>   }
>>   
>> @@ -422,8 +415,6 @@ static bool radeon_fence_enable_signaling(struct dma_fence *f)
>>   	fence->fence_wake.func = radeon_fence_check_signaled;
>>   	__add_wait_queue(&rdev->fence_queue, &fence->fence_wake);
>>   	dma_fence_get(f);
>> -
>> -	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", fence->ring);
>>   	return true;
>>   }
>>   
>> @@ -441,11 +432,7 @@ bool radeon_fence_signaled(struct radeon_fence *fence)
>>   		return true;
>>   
>>   	if (radeon_fence_seq_signaled(fence->rdev, fence->seq, fence->ring)) {
>> -		int ret;
>> -
>> -		ret = dma_fence_signal(&fence->base);
>> -		if (!ret)
>> -			DMA_FENCE_TRACE(&fence->base, "signaled from radeon_fence_signaled\n");
>> +		dma_fence_signal(&fence->base);
>>   		return true;
>>   	}
>>   	return false;
>> @@ -550,7 +537,6 @@ long radeon_fence_wait_timeout(struct radeon_fence *fence, bool intr, long timeo
>>   {
>>   	uint64_t seq[RADEON_NUM_RINGS] = {};
>>   	long r;
>> -	int r_sig;
>>   
>>   	/*
>>   	 * This function should not be called on !radeon fences.
>> @@ -567,9 +553,7 @@ long radeon_fence_wait_timeout(struct radeon_fence *fence, bool intr, long timeo
>>   		return r;
>>   	}
>>   
>> -	r_sig = dma_fence_signal(&fence->base);
>> -	if (!r_sig)
>> -		DMA_FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
>> +	dma_fence_signal(&fence->base);
>>   	return r;
>>   }
>>   
>> diff --git a/drivers/gpu/drm/scheduler/sched_fence.c b/drivers/gpu/drm/scheduler/sched_fence.c
>> index 69de2c76731f..3736746c47bd 100644
>> --- a/drivers/gpu/drm/scheduler/sched_fence.c
>> +++ b/drivers/gpu/drm/scheduler/sched_fence.c
>> @@ -50,26 +50,12 @@ static void __exit drm_sched_fence_slab_fini(void)
>>   
>>   void drm_sched_fence_scheduled(struct drm_sched_fence *fence)
>>   {
>> -	int ret = dma_fence_signal(&fence->scheduled);
>> -
>> -	if (!ret)
>> -		DMA_FENCE_TRACE(&fence->scheduled,
>> -				"signaled from irq context\n");
>> -	else
>> -		DMA_FENCE_TRACE(&fence->scheduled,
>> -				"was already signaled\n");
>> +	dma_fence_signal(&fence->scheduled);
>>   }
>>   
>>   void drm_sched_fence_finished(struct drm_sched_fence *fence)
>>   {
>> -	int ret = dma_fence_signal(&fence->finished);
>> -
>> -	if (!ret)
>> -		DMA_FENCE_TRACE(&fence->finished,
>> -				"signaled from irq context\n");
>> -	else
>> -		DMA_FENCE_TRACE(&fence->finished,
>> -				"was already signaled\n");
>> +	dma_fence_signal(&fence->finished);
>>   }
>>   
>>   static const char *drm_sched_fence_get_driver_name(struct dma_fence *fence)
>> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
>> index 6ffb4b2c6371..027db23fd4e3 100644
>> --- a/include/linux/dma-fence.h
>> +++ b/include/linux/dma-fence.h
>> @@ -370,11 +370,11 @@ static inline void dma_fence_end_signalling(bool cookie) {}
>>   static inline void __dma_fence_might_wait(void) {}
>>   #endif
>>   
>> -int dma_fence_signal(struct dma_fence *fence);
>> -int dma_fence_signal_locked(struct dma_fence *fence);
>> -int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp);
>> -int dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>> -				      ktime_t timestamp);
>> +void dma_fence_signal(struct dma_fence *fence);
>> +void dma_fence_signal_locked(struct dma_fence *fence);
>> +void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp);
>> +void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
>> +				       ktime_t timestamp);
>>   signed long dma_fence_default_wait(struct dma_fence *fence,
>>   				   bool intr, signed long timeout);
>>   int dma_fence_add_callback(struct dma_fence *fence,
>> @@ -590,26 +590,4 @@ struct dma_fence *dma_fence_get_stub(void);
>>   struct dma_fence *dma_fence_allocate_private_stub(void);
>>   u64 dma_fence_context_alloc(unsigned num);
>>   
>> -#define DMA_FENCE_TRACE(f, fmt, args...) \
>> -	do {								\
>> -		struct dma_fence *__ff = (f);				\
>> -		if (IS_ENABLED(CONFIG_DMA_FENCE_TRACE))			\
>> -			pr_info("f %llu#%llu: " fmt,			\
>> -				__ff->context, __ff->seqno, ##args);	\
>> -	} while (0)
>> -
>> -#define DMA_FENCE_WARN(f, fmt, args...) \
>> -	do {								\
>> -		struct dma_fence *__ff = (f);				\
>> -		pr_warn("f %llu#%llu: " fmt, __ff->context, __ff->seqno,\
>> -			 ##args);					\
>> -	} while (0)
>> -
>> -#define DMA_FENCE_ERR(f, fmt, args...) \
>> -	do {								\
>> -		struct dma_fence *__ff = (f);				\
>> -		pr_err("f %llu#%llu: " fmt, __ff->context, __ff->seqno,	\
>> -			##args);					\
>> -	} while (0)
>> -
>>   #endif /* __LINUX_DMA_FENCE_H */
>> -- 
>> 2.25.1
>>


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
  2021-07-29  7:23     ` Daniel Vetter
@ 2021-07-29  8:38       ` Christian König
  -1 siblings, 0 replies; 26+ messages in thread
From: Christian König @ 2021-07-29  8:38 UTC (permalink / raw)
  To: Daniel Vetter
  Cc: sumit.semwal, gustavo, linux-media, dri-devel, linaro-mm-sig



Am 29.07.21 um 09:23 schrieb Daniel Vetter:
> On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
>> As we now knew controlling dma_fence synchronization from userspace is
>> extremely dangerous and can not only deadlock drivers but trivially also the
>> whole kernel memory management.
>>
>> Entirely remove this option. We now have in kernel unit tests to exercise the
>> dma_fence framework and it's containers.
>>
>> Signed-off-by: Christian König <christian.koenig@amd.com>
> There's also igts for this, and Android heavily uses this. So I'm not sure
> we can just nuke it.

I feared that you would say that.

> I guess what we could do is maybe just taint the kernel if it's ever used?

What should we use then? TAINT_SOFTLOCKUP or TAINT_CRAP? :)

Regards,
Christian.

>
> Also for testing we use vgem now, which enforces a timeout.
> -Daniel
>
>> ---
>>   drivers/dma-buf/Kconfig      |  13 --
>>   drivers/dma-buf/Makefile     |   1 -
>>   drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
>>   drivers/dma-buf/sync_debug.c | 190 ----------------
>>   drivers/dma-buf/sync_debug.h |  72 ------
>>   5 files changed, 688 deletions(-)
>>   delete mode 100644 drivers/dma-buf/sw_sync.c
>>   delete mode 100644 drivers/dma-buf/sync_debug.c
>>   delete mode 100644 drivers/dma-buf/sync_debug.h
>>
>> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
>> index 9561e3d2d428..26c53f45616a 100644
>> --- a/drivers/dma-buf/Kconfig
>> +++ b/drivers/dma-buf/Kconfig
>> @@ -17,19 +17,6 @@ config SYNC_FILE
>>   	  Files fds, to the DRM driver for example. More details at
>>   	  Documentation/driver-api/sync_file.rst.
>>   
>> -config SW_SYNC
>> -	bool "Sync File Validation Framework"
>> -	default n
>> -	depends on SYNC_FILE
>> -	depends on DEBUG_FS
>> -	help
>> -	  A sync object driver that uses a 32bit counter to coordinate
>> -	  synchronization.  Useful when there is no hardware primitive backing
>> -	  the synchronization.
>> -
>> -	  WARNING: improper use of this can result in deadlocking kernel
>> -	  drivers from userspace. Intended for test and debug only.
>> -
>>   config UDMABUF
>>   	bool "userspace dmabuf misc driver"
>>   	default n
>> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
>> index 1ef021273a06..fa3af0953f87 100644
>> --- a/drivers/dma-buf/Makefile
>> +++ b/drivers/dma-buf/Makefile
>> @@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
>>   obj-$(CONFIG_DMABUF_HEAPS)	+= dma-heap.o
>>   obj-$(CONFIG_DMABUF_HEAPS)	+= heaps/
>>   obj-$(CONFIG_SYNC_FILE)		+= sync_file.o
>> -obj-$(CONFIG_SW_SYNC)		+= sw_sync.o sync_debug.o
>>   obj-$(CONFIG_UDMABUF)		+= udmabuf.o
>>   obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
>>   
>> diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
>> deleted file mode 100644
>> index 348b3a9170fa..000000000000
>> --- a/drivers/dma-buf/sw_sync.c
>> +++ /dev/null
>> @@ -1,412 +0,0 @@
>> -// SPDX-License-Identifier: GPL-2.0-only
>> -/*
>> - * Sync File validation framework
>> - *
>> - * Copyright (C) 2012 Google, Inc.
>> - */
>> -
>> -#include <linux/file.h>
>> -#include <linux/fs.h>
>> -#include <linux/uaccess.h>
>> -#include <linux/slab.h>
>> -#include <linux/sync_file.h>
>> -
>> -#include "sync_debug.h"
>> -
>> -#define CREATE_TRACE_POINTS
>> -#include "sync_trace.h"
>> -
>> -/*
>> - * SW SYNC validation framework
>> - *
>> - * A sync object driver that uses a 32bit counter to coordinate
>> - * synchronization.  Useful when there is no hardware primitive backing
>> - * the synchronization.
>> - *
>> - * To start the framework just open:
>> - *
>> - * <debugfs>/sync/sw_sync
>> - *
>> - * That will create a sync timeline, all fences created under this timeline
>> - * file descriptor will belong to the this timeline.
>> - *
>> - * The 'sw_sync' file can be opened many times as to create different
>> - * timelines.
>> - *
>> - * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
>> - * sw_sync_create_fence_data as parameter.
>> - *
>> - * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
>> - * with the increment as u32. This will update the last signaled value
>> - * from the timeline and signal any fence that has a seqno smaller or equal
>> - * to it.
>> - *
>> - * struct sw_sync_create_fence_data
>> - * @value:	the seqno to initialise the fence with
>> - * @name:	the name of the new sync point
>> - * @fence:	return the fd of the new sync_file with the created fence
>> - */
>> -struct sw_sync_create_fence_data {
>> -	__u32	value;
>> -	char	name[32];
>> -	__s32	fence; /* fd of new fence */
>> -};
>> -
>> -#define SW_SYNC_IOC_MAGIC	'W'
>> -
>> -#define SW_SYNC_IOC_CREATE_FENCE	_IOWR(SW_SYNC_IOC_MAGIC, 0,\
>> -		struct sw_sync_create_fence_data)
>> -
>> -#define SW_SYNC_IOC_INC			_IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
>> -
>> -static const struct dma_fence_ops timeline_fence_ops;
>> -
>> -static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
>> -{
>> -	if (fence->ops != &timeline_fence_ops)
>> -		return NULL;
>> -	return container_of(fence, struct sync_pt, base);
>> -}
>> -
>> -/**
>> - * sync_timeline_create() - creates a sync object
>> - * @name:	sync_timeline name
>> - *
>> - * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
>> - * case of error.
>> - */
>> -static struct sync_timeline *sync_timeline_create(const char *name)
>> -{
>> -	struct sync_timeline *obj;
>> -
>> -	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
>> -	if (!obj)
>> -		return NULL;
>> -
>> -	kref_init(&obj->kref);
>> -	obj->context = dma_fence_context_alloc(1);
>> -	strlcpy(obj->name, name, sizeof(obj->name));
>> -
>> -	obj->pt_tree = RB_ROOT;
>> -	INIT_LIST_HEAD(&obj->pt_list);
>> -	spin_lock_init(&obj->lock);
>> -
>> -	sync_timeline_debug_add(obj);
>> -
>> -	return obj;
>> -}
>> -
>> -static void sync_timeline_free(struct kref *kref)
>> -{
>> -	struct sync_timeline *obj =
>> -		container_of(kref, struct sync_timeline, kref);
>> -
>> -	sync_timeline_debug_remove(obj);
>> -
>> -	kfree(obj);
>> -}
>> -
>> -static void sync_timeline_get(struct sync_timeline *obj)
>> -{
>> -	kref_get(&obj->kref);
>> -}
>> -
>> -static void sync_timeline_put(struct sync_timeline *obj)
>> -{
>> -	kref_put(&obj->kref, sync_timeline_free);
>> -}
>> -
>> -static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
>> -{
>> -	return "sw_sync";
>> -}
>> -
>> -static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
>> -{
>> -	struct sync_timeline *parent = dma_fence_parent(fence);
>> -
>> -	return parent->name;
>> -}
>> -
>> -static void timeline_fence_release(struct dma_fence *fence)
>> -{
>> -	struct sync_pt *pt = dma_fence_to_sync_pt(fence);
>> -	struct sync_timeline *parent = dma_fence_parent(fence);
>> -	unsigned long flags;
>> -
>> -	spin_lock_irqsave(fence->lock, flags);
>> -	if (!list_empty(&pt->link)) {
>> -		list_del(&pt->link);
>> -		rb_erase(&pt->node, &parent->pt_tree);
>> -	}
>> -	spin_unlock_irqrestore(fence->lock, flags);
>> -
>> -	sync_timeline_put(parent);
>> -	dma_fence_free(fence);
>> -}
>> -
>> -static bool timeline_fence_signaled(struct dma_fence *fence)
>> -{
>> -	struct sync_timeline *parent = dma_fence_parent(fence);
>> -
>> -	return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
>> -}
>> -
>> -static bool timeline_fence_enable_signaling(struct dma_fence *fence)
>> -{
>> -	return true;
>> -}
>> -
>> -static void timeline_fence_value_str(struct dma_fence *fence,
>> -				    char *str, int size)
>> -{
>> -	snprintf(str, size, "%lld", fence->seqno);
>> -}
>> -
>> -static void timeline_fence_timeline_value_str(struct dma_fence *fence,
>> -					     char *str, int size)
>> -{
>> -	struct sync_timeline *parent = dma_fence_parent(fence);
>> -
>> -	snprintf(str, size, "%d", parent->value);
>> -}
>> -
>> -static const struct dma_fence_ops timeline_fence_ops = {
>> -	.get_driver_name = timeline_fence_get_driver_name,
>> -	.get_timeline_name = timeline_fence_get_timeline_name,
>> -	.enable_signaling = timeline_fence_enable_signaling,
>> -	.signaled = timeline_fence_signaled,
>> -	.release = timeline_fence_release,
>> -	.fence_value_str = timeline_fence_value_str,
>> -	.timeline_value_str = timeline_fence_timeline_value_str,
>> -};
>> -
>> -/**
>> - * sync_timeline_signal() - signal a status change on a sync_timeline
>> - * @obj:	sync_timeline to signal
>> - * @inc:	num to increment on timeline->value
>> - *
>> - * A sync implementation should call this any time one of it's fences
>> - * has signaled or has an error condition.
>> - */
>> -static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
>> -{
>> -	struct sync_pt *pt, *next;
>> -
>> -	trace_sync_timeline(obj);
>> -
>> -	spin_lock_irq(&obj->lock);
>> -
>> -	obj->value += inc;
>> -
>> -	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
>> -		if (!timeline_fence_signaled(&pt->base))
>> -			break;
>> -
>> -		list_del_init(&pt->link);
>> -		rb_erase(&pt->node, &obj->pt_tree);
>> -
>> -		/*
>> -		 * A signal callback may release the last reference to this
>> -		 * fence, causing it to be freed. That operation has to be
>> -		 * last to avoid a use after free inside this loop, and must
>> -		 * be after we remove the fence from the timeline in order to
>> -		 * prevent deadlocking on timeline->lock inside
>> -		 * timeline_fence_release().
>> -		 */
>> -		dma_fence_signal_locked(&pt->base);
>> -	}
>> -
>> -	spin_unlock_irq(&obj->lock);
>> -}
>> -
>> -/**
>> - * sync_pt_create() - creates a sync pt
>> - * @obj:	parent sync_timeline
>> - * @value:	value of the fence
>> - *
>> - * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
>> - * allocated allowing for implementation specific data to be kept after
>> - * the generic sync_timeline struct. Returns the sync_pt object or
>> - * NULL in case of error.
>> - */
>> -static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
>> -				      unsigned int value)
>> -{
>> -	struct sync_pt *pt;
>> -
>> -	pt = kzalloc(sizeof(*pt), GFP_KERNEL);
>> -	if (!pt)
>> -		return NULL;
>> -
>> -	sync_timeline_get(obj);
>> -	dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
>> -		       obj->context, value);
>> -	INIT_LIST_HEAD(&pt->link);
>> -
>> -	spin_lock_irq(&obj->lock);
>> -	if (!dma_fence_is_signaled_locked(&pt->base)) {
>> -		struct rb_node **p = &obj->pt_tree.rb_node;
>> -		struct rb_node *parent = NULL;
>> -
>> -		while (*p) {
>> -			struct sync_pt *other;
>> -			int cmp;
>> -
>> -			parent = *p;
>> -			other = rb_entry(parent, typeof(*pt), node);
>> -			cmp = value - other->base.seqno;
>> -			if (cmp > 0) {
>> -				p = &parent->rb_right;
>> -			} else if (cmp < 0) {
>> -				p = &parent->rb_left;
>> -			} else {
>> -				if (dma_fence_get_rcu(&other->base)) {
>> -					sync_timeline_put(obj);
>> -					kfree(pt);
>> -					pt = other;
>> -					goto unlock;
>> -				}
>> -				p = &parent->rb_left;
>> -			}
>> -		}
>> -		rb_link_node(&pt->node, parent, p);
>> -		rb_insert_color(&pt->node, &obj->pt_tree);
>> -
>> -		parent = rb_next(&pt->node);
>> -		list_add_tail(&pt->link,
>> -			      parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
>> -	}
>> -unlock:
>> -	spin_unlock_irq(&obj->lock);
>> -
>> -	return pt;
>> -}
>> -
>> -/*
>> - * *WARNING*
>> - *
>> - * improper use of this can result in deadlocking kernel drivers from userspace.
>> - */
>> -
>> -/* opening sw_sync create a new sync obj */
>> -static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
>> -{
>> -	struct sync_timeline *obj;
>> -	char task_comm[TASK_COMM_LEN];
>> -
>> -	get_task_comm(task_comm, current);
>> -
>> -	obj = sync_timeline_create(task_comm);
>> -	if (!obj)
>> -		return -ENOMEM;
>> -
>> -	file->private_data = obj;
>> -
>> -	return 0;
>> -}
>> -
>> -static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
>> -{
>> -	struct sync_timeline *obj = file->private_data;
>> -	struct sync_pt *pt, *next;
>> -
>> -	spin_lock_irq(&obj->lock);
>> -
>> -	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
>> -		dma_fence_set_error(&pt->base, -ENOENT);
>> -		dma_fence_signal_locked(&pt->base);
>> -	}
>> -
>> -	spin_unlock_irq(&obj->lock);
>> -
>> -	sync_timeline_put(obj);
>> -	return 0;
>> -}
>> -
>> -static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
>> -				       unsigned long arg)
>> -{
>> -	int fd = get_unused_fd_flags(O_CLOEXEC);
>> -	int err;
>> -	struct sync_pt *pt;
>> -	struct sync_file *sync_file;
>> -	struct sw_sync_create_fence_data data;
>> -
>> -	if (fd < 0)
>> -		return fd;
>> -
>> -	if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
>> -		err = -EFAULT;
>> -		goto err;
>> -	}
>> -
>> -	pt = sync_pt_create(obj, data.value);
>> -	if (!pt) {
>> -		err = -ENOMEM;
>> -		goto err;
>> -	}
>> -
>> -	sync_file = sync_file_create(&pt->base);
>> -	dma_fence_put(&pt->base);
>> -	if (!sync_file) {
>> -		err = -ENOMEM;
>> -		goto err;
>> -	}
>> -
>> -	data.fence = fd;
>> -	if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
>> -		fput(sync_file->file);
>> -		err = -EFAULT;
>> -		goto err;
>> -	}
>> -
>> -	fd_install(fd, sync_file->file);
>> -
>> -	return 0;
>> -
>> -err:
>> -	put_unused_fd(fd);
>> -	return err;
>> -}
>> -
>> -static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
>> -{
>> -	u32 value;
>> -
>> -	if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
>> -		return -EFAULT;
>> -
>> -	while (value > INT_MAX)  {
>> -		sync_timeline_signal(obj, INT_MAX);
>> -		value -= INT_MAX;
>> -	}
>> -
>> -	sync_timeline_signal(obj, value);
>> -
>> -	return 0;
>> -}
>> -
>> -static long sw_sync_ioctl(struct file *file, unsigned int cmd,
>> -			  unsigned long arg)
>> -{
>> -	struct sync_timeline *obj = file->private_data;
>> -
>> -	switch (cmd) {
>> -	case SW_SYNC_IOC_CREATE_FENCE:
>> -		return sw_sync_ioctl_create_fence(obj, arg);
>> -
>> -	case SW_SYNC_IOC_INC:
>> -		return sw_sync_ioctl_inc(obj, arg);
>> -
>> -	default:
>> -		return -ENOTTY;
>> -	}
>> -}
>> -
>> -const struct file_operations sw_sync_debugfs_fops = {
>> -	.open           = sw_sync_debugfs_open,
>> -	.release        = sw_sync_debugfs_release,
>> -	.unlocked_ioctl = sw_sync_ioctl,
>> -	.compat_ioctl	= compat_ptr_ioctl,
>> -};
>> diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
>> deleted file mode 100644
>> index 101394f16930..000000000000
>> --- a/drivers/dma-buf/sync_debug.c
>> +++ /dev/null
>> @@ -1,190 +0,0 @@
>> -// SPDX-License-Identifier: GPL-2.0-only
>> -/*
>> - * Sync File validation framework and debug information
>> - *
>> - * Copyright (C) 2012 Google, Inc.
>> - */
>> -
>> -#include <linux/debugfs.h>
>> -#include "sync_debug.h"
>> -
>> -static struct dentry *dbgfs;
>> -
>> -static LIST_HEAD(sync_timeline_list_head);
>> -static DEFINE_SPINLOCK(sync_timeline_list_lock);
>> -static LIST_HEAD(sync_file_list_head);
>> -static DEFINE_SPINLOCK(sync_file_list_lock);
>> -
>> -void sync_timeline_debug_add(struct sync_timeline *obj)
>> -{
>> -	unsigned long flags;
>> -
>> -	spin_lock_irqsave(&sync_timeline_list_lock, flags);
>> -	list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
>> -	spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
>> -}
>> -
>> -void sync_timeline_debug_remove(struct sync_timeline *obj)
>> -{
>> -	unsigned long flags;
>> -
>> -	spin_lock_irqsave(&sync_timeline_list_lock, flags);
>> -	list_del(&obj->sync_timeline_list);
>> -	spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
>> -}
>> -
>> -void sync_file_debug_add(struct sync_file *sync_file)
>> -{
>> -	unsigned long flags;
>> -
>> -	spin_lock_irqsave(&sync_file_list_lock, flags);
>> -	list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
>> -	spin_unlock_irqrestore(&sync_file_list_lock, flags);
>> -}
>> -
>> -void sync_file_debug_remove(struct sync_file *sync_file)
>> -{
>> -	unsigned long flags;
>> -
>> -	spin_lock_irqsave(&sync_file_list_lock, flags);
>> -	list_del(&sync_file->sync_file_list);
>> -	spin_unlock_irqrestore(&sync_file_list_lock, flags);
>> -}
>> -
>> -static const char *sync_status_str(int status)
>> -{
>> -	if (status < 0)
>> -		return "error";
>> -
>> -	if (status > 0)
>> -		return "signaled";
>> -
>> -	return "active";
>> -}
>> -
>> -static void sync_print_fence(struct seq_file *s,
>> -			     struct dma_fence *fence, bool show)
>> -{
>> -	struct sync_timeline *parent = dma_fence_parent(fence);
>> -	int status;
>> -
>> -	status = dma_fence_get_status_locked(fence);
>> -
>> -	seq_printf(s, "  %s%sfence %s",
>> -		   show ? parent->name : "",
>> -		   show ? "_" : "",
>> -		   sync_status_str(status));
>> -
>> -	if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
>> -		struct timespec64 ts64 =
>> -			ktime_to_timespec64(fence->timestamp);
>> -
>> -		seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
>> -	}
>> -
>> -	if (fence->ops->timeline_value_str &&
>> -		fence->ops->fence_value_str) {
>> -		char value[64];
>> -		bool success;
>> -
>> -		fence->ops->fence_value_str(fence, value, sizeof(value));
>> -		success = strlen(value);
>> -
>> -		if (success) {
>> -			seq_printf(s, ": %s", value);
>> -
>> -			fence->ops->timeline_value_str(fence, value,
>> -						       sizeof(value));
>> -
>> -			if (strlen(value))
>> -				seq_printf(s, " / %s", value);
>> -		}
>> -	}
>> -
>> -	seq_putc(s, '\n');
>> -}
>> -
>> -static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
>> -{
>> -	struct list_head *pos;
>> -
>> -	seq_printf(s, "%s: %d\n", obj->name, obj->value);
>> -
>> -	spin_lock_irq(&obj->lock);
>> -	list_for_each(pos, &obj->pt_list) {
>> -		struct sync_pt *pt = container_of(pos, struct sync_pt, link);
>> -		sync_print_fence(s, &pt->base, false);
>> -	}
>> -	spin_unlock_irq(&obj->lock);
>> -}
>> -
>> -static void sync_print_sync_file(struct seq_file *s,
>> -				  struct sync_file *sync_file)
>> -{
>> -	char buf[128];
>> -	int i;
>> -
>> -	seq_printf(s, "[%p] %s: %s\n", sync_file,
>> -		   sync_file_get_name(sync_file, buf, sizeof(buf)),
>> -		   sync_status_str(dma_fence_get_status(sync_file->fence)));
>> -
>> -	if (dma_fence_is_array(sync_file->fence)) {
>> -		struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
>> -
>> -		for (i = 0; i < array->num_fences; ++i)
>> -			sync_print_fence(s, array->fences[i], true);
>> -	} else {
>> -		sync_print_fence(s, sync_file->fence, true);
>> -	}
>> -}
>> -
>> -static int sync_info_debugfs_show(struct seq_file *s, void *unused)
>> -{
>> -	struct list_head *pos;
>> -
>> -	seq_puts(s, "objs:\n--------------\n");
>> -
>> -	spin_lock_irq(&sync_timeline_list_lock);
>> -	list_for_each(pos, &sync_timeline_list_head) {
>> -		struct sync_timeline *obj =
>> -			container_of(pos, struct sync_timeline,
>> -				     sync_timeline_list);
>> -
>> -		sync_print_obj(s, obj);
>> -		seq_putc(s, '\n');
>> -	}
>> -	spin_unlock_irq(&sync_timeline_list_lock);
>> -
>> -	seq_puts(s, "fences:\n--------------\n");
>> -
>> -	spin_lock_irq(&sync_file_list_lock);
>> -	list_for_each(pos, &sync_file_list_head) {
>> -		struct sync_file *sync_file =
>> -			container_of(pos, struct sync_file, sync_file_list);
>> -
>> -		sync_print_sync_file(s, sync_file);
>> -		seq_putc(s, '\n');
>> -	}
>> -	spin_unlock_irq(&sync_file_list_lock);
>> -	return 0;
>> -}
>> -
>> -DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
>> -
>> -static __init int sync_debugfs_init(void)
>> -{
>> -	dbgfs = debugfs_create_dir("sync", NULL);
>> -
>> -	/*
>> -	 * The debugfs files won't ever get removed and thus, there is
>> -	 * no need to protect it against removal races. The use of
>> -	 * debugfs_create_file_unsafe() is actually safe here.
>> -	 */
>> -	debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
>> -				   &sync_info_debugfs_fops);
>> -	debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
>> -				   &sw_sync_debugfs_fops);
>> -
>> -	return 0;
>> -}
>> -late_initcall(sync_debugfs_init);
>> diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
>> deleted file mode 100644
>> index 6176e52ba2d7..000000000000
>> --- a/drivers/dma-buf/sync_debug.h
>> +++ /dev/null
>> @@ -1,72 +0,0 @@
>> -/*
>> - * Sync File validation framework and debug infomation
>> - *
>> - * Copyright (C) 2012 Google, Inc.
>> - *
>> - * This program is distributed in the hope that it will be useful,
>> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> - * GNU General Public License for more details.
>> - *
>> - */
>> -
>> -#ifndef _LINUX_SYNC_H
>> -#define _LINUX_SYNC_H
>> -
>> -#include <linux/list.h>
>> -#include <linux/rbtree.h>
>> -#include <linux/spinlock.h>
>> -#include <linux/dma-fence.h>
>> -
>> -#include <linux/sync_file.h>
>> -#include <uapi/linux/sync_file.h>
>> -
>> -/**
>> - * struct sync_timeline - sync object
>> - * @kref:		reference count on fence.
>> - * @name:		name of the sync_timeline. Useful for debugging
>> - * @lock:		lock protecting @pt_list and @value
>> - * @pt_tree:		rbtree of active (unsignaled/errored) sync_pts
>> - * @pt_list:		list of active (unsignaled/errored) sync_pts
>> - * @sync_timeline_list:	membership in global sync_timeline_list
>> - */
>> -struct sync_timeline {
>> -	struct kref		kref;
>> -	char			name[32];
>> -
>> -	/* protected by lock */
>> -	u64			context;
>> -	int			value;
>> -
>> -	struct rb_root		pt_tree;
>> -	struct list_head	pt_list;
>> -	spinlock_t		lock;
>> -
>> -	struct list_head	sync_timeline_list;
>> -};
>> -
>> -static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
>> -{
>> -	return container_of(fence->lock, struct sync_timeline, lock);
>> -}
>> -
>> -/**
>> - * struct sync_pt - sync_pt object
>> - * @base: base fence object
>> - * @link: link on the sync timeline's list
>> - * @node: node in the sync timeline's tree
>> - */
>> -struct sync_pt {
>> -	struct dma_fence base;
>> -	struct list_head link;
>> -	struct rb_node node;
>> -};
>> -
>> -extern const struct file_operations sw_sync_debugfs_fops;
>> -
>> -void sync_timeline_debug_add(struct sync_timeline *obj);
>> -void sync_timeline_debug_remove(struct sync_timeline *obj);
>> -void sync_file_debug_add(struct sync_file *fence);
>> -void sync_file_debug_remove(struct sync_file *fence);
>> -
>> -#endif /* _LINUX_SYNC_H */
>> -- 
>> 2.25.1
>>


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
@ 2021-07-29  8:38       ` Christian König
  0 siblings, 0 replies; 26+ messages in thread
From: Christian König @ 2021-07-29  8:38 UTC (permalink / raw)
  To: Daniel Vetter; +Cc: linaro-mm-sig, gustavo, dri-devel, linux-media



Am 29.07.21 um 09:23 schrieb Daniel Vetter:
> On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
>> As we now knew controlling dma_fence synchronization from userspace is
>> extremely dangerous and can not only deadlock drivers but trivially also the
>> whole kernel memory management.
>>
>> Entirely remove this option. We now have in kernel unit tests to exercise the
>> dma_fence framework and it's containers.
>>
>> Signed-off-by: Christian König <christian.koenig@amd.com>
> There's also igts for this, and Android heavily uses this. So I'm not sure
> we can just nuke it.

I feared that you would say that.

> I guess what we could do is maybe just taint the kernel if it's ever used?

What should we use then? TAINT_SOFTLOCKUP or TAINT_CRAP? :)

Regards,
Christian.

>
> Also for testing we use vgem now, which enforces a timeout.
> -Daniel
>
>> ---
>>   drivers/dma-buf/Kconfig      |  13 --
>>   drivers/dma-buf/Makefile     |   1 -
>>   drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
>>   drivers/dma-buf/sync_debug.c | 190 ----------------
>>   drivers/dma-buf/sync_debug.h |  72 ------
>>   5 files changed, 688 deletions(-)
>>   delete mode 100644 drivers/dma-buf/sw_sync.c
>>   delete mode 100644 drivers/dma-buf/sync_debug.c
>>   delete mode 100644 drivers/dma-buf/sync_debug.h
>>
>> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
>> index 9561e3d2d428..26c53f45616a 100644
>> --- a/drivers/dma-buf/Kconfig
>> +++ b/drivers/dma-buf/Kconfig
>> @@ -17,19 +17,6 @@ config SYNC_FILE
>>   	  Files fds, to the DRM driver for example. More details at
>>   	  Documentation/driver-api/sync_file.rst.
>>   
>> -config SW_SYNC
>> -	bool "Sync File Validation Framework"
>> -	default n
>> -	depends on SYNC_FILE
>> -	depends on DEBUG_FS
>> -	help
>> -	  A sync object driver that uses a 32bit counter to coordinate
>> -	  synchronization.  Useful when there is no hardware primitive backing
>> -	  the synchronization.
>> -
>> -	  WARNING: improper use of this can result in deadlocking kernel
>> -	  drivers from userspace. Intended for test and debug only.
>> -
>>   config UDMABUF
>>   	bool "userspace dmabuf misc driver"
>>   	default n
>> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
>> index 1ef021273a06..fa3af0953f87 100644
>> --- a/drivers/dma-buf/Makefile
>> +++ b/drivers/dma-buf/Makefile
>> @@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
>>   obj-$(CONFIG_DMABUF_HEAPS)	+= dma-heap.o
>>   obj-$(CONFIG_DMABUF_HEAPS)	+= heaps/
>>   obj-$(CONFIG_SYNC_FILE)		+= sync_file.o
>> -obj-$(CONFIG_SW_SYNC)		+= sw_sync.o sync_debug.o
>>   obj-$(CONFIG_UDMABUF)		+= udmabuf.o
>>   obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
>>   
>> diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
>> deleted file mode 100644
>> index 348b3a9170fa..000000000000
>> --- a/drivers/dma-buf/sw_sync.c
>> +++ /dev/null
>> @@ -1,412 +0,0 @@
>> -// SPDX-License-Identifier: GPL-2.0-only
>> -/*
>> - * Sync File validation framework
>> - *
>> - * Copyright (C) 2012 Google, Inc.
>> - */
>> -
>> -#include <linux/file.h>
>> -#include <linux/fs.h>
>> -#include <linux/uaccess.h>
>> -#include <linux/slab.h>
>> -#include <linux/sync_file.h>
>> -
>> -#include "sync_debug.h"
>> -
>> -#define CREATE_TRACE_POINTS
>> -#include "sync_trace.h"
>> -
>> -/*
>> - * SW SYNC validation framework
>> - *
>> - * A sync object driver that uses a 32bit counter to coordinate
>> - * synchronization.  Useful when there is no hardware primitive backing
>> - * the synchronization.
>> - *
>> - * To start the framework just open:
>> - *
>> - * <debugfs>/sync/sw_sync
>> - *
>> - * That will create a sync timeline, all fences created under this timeline
>> - * file descriptor will belong to the this timeline.
>> - *
>> - * The 'sw_sync' file can be opened many times as to create different
>> - * timelines.
>> - *
>> - * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
>> - * sw_sync_create_fence_data as parameter.
>> - *
>> - * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
>> - * with the increment as u32. This will update the last signaled value
>> - * from the timeline and signal any fence that has a seqno smaller or equal
>> - * to it.
>> - *
>> - * struct sw_sync_create_fence_data
>> - * @value:	the seqno to initialise the fence with
>> - * @name:	the name of the new sync point
>> - * @fence:	return the fd of the new sync_file with the created fence
>> - */
>> -struct sw_sync_create_fence_data {
>> -	__u32	value;
>> -	char	name[32];
>> -	__s32	fence; /* fd of new fence */
>> -};
>> -
>> -#define SW_SYNC_IOC_MAGIC	'W'
>> -
>> -#define SW_SYNC_IOC_CREATE_FENCE	_IOWR(SW_SYNC_IOC_MAGIC, 0,\
>> -		struct sw_sync_create_fence_data)
>> -
>> -#define SW_SYNC_IOC_INC			_IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
>> -
>> -static const struct dma_fence_ops timeline_fence_ops;
>> -
>> -static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
>> -{
>> -	if (fence->ops != &timeline_fence_ops)
>> -		return NULL;
>> -	return container_of(fence, struct sync_pt, base);
>> -}
>> -
>> -/**
>> - * sync_timeline_create() - creates a sync object
>> - * @name:	sync_timeline name
>> - *
>> - * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
>> - * case of error.
>> - */
>> -static struct sync_timeline *sync_timeline_create(const char *name)
>> -{
>> -	struct sync_timeline *obj;
>> -
>> -	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
>> -	if (!obj)
>> -		return NULL;
>> -
>> -	kref_init(&obj->kref);
>> -	obj->context = dma_fence_context_alloc(1);
>> -	strlcpy(obj->name, name, sizeof(obj->name));
>> -
>> -	obj->pt_tree = RB_ROOT;
>> -	INIT_LIST_HEAD(&obj->pt_list);
>> -	spin_lock_init(&obj->lock);
>> -
>> -	sync_timeline_debug_add(obj);
>> -
>> -	return obj;
>> -}
>> -
>> -static void sync_timeline_free(struct kref *kref)
>> -{
>> -	struct sync_timeline *obj =
>> -		container_of(kref, struct sync_timeline, kref);
>> -
>> -	sync_timeline_debug_remove(obj);
>> -
>> -	kfree(obj);
>> -}
>> -
>> -static void sync_timeline_get(struct sync_timeline *obj)
>> -{
>> -	kref_get(&obj->kref);
>> -}
>> -
>> -static void sync_timeline_put(struct sync_timeline *obj)
>> -{
>> -	kref_put(&obj->kref, sync_timeline_free);
>> -}
>> -
>> -static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
>> -{
>> -	return "sw_sync";
>> -}
>> -
>> -static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
>> -{
>> -	struct sync_timeline *parent = dma_fence_parent(fence);
>> -
>> -	return parent->name;
>> -}
>> -
>> -static void timeline_fence_release(struct dma_fence *fence)
>> -{
>> -	struct sync_pt *pt = dma_fence_to_sync_pt(fence);
>> -	struct sync_timeline *parent = dma_fence_parent(fence);
>> -	unsigned long flags;
>> -
>> -	spin_lock_irqsave(fence->lock, flags);
>> -	if (!list_empty(&pt->link)) {
>> -		list_del(&pt->link);
>> -		rb_erase(&pt->node, &parent->pt_tree);
>> -	}
>> -	spin_unlock_irqrestore(fence->lock, flags);
>> -
>> -	sync_timeline_put(parent);
>> -	dma_fence_free(fence);
>> -}
>> -
>> -static bool timeline_fence_signaled(struct dma_fence *fence)
>> -{
>> -	struct sync_timeline *parent = dma_fence_parent(fence);
>> -
>> -	return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
>> -}
>> -
>> -static bool timeline_fence_enable_signaling(struct dma_fence *fence)
>> -{
>> -	return true;
>> -}
>> -
>> -static void timeline_fence_value_str(struct dma_fence *fence,
>> -				    char *str, int size)
>> -{
>> -	snprintf(str, size, "%lld", fence->seqno);
>> -}
>> -
>> -static void timeline_fence_timeline_value_str(struct dma_fence *fence,
>> -					     char *str, int size)
>> -{
>> -	struct sync_timeline *parent = dma_fence_parent(fence);
>> -
>> -	snprintf(str, size, "%d", parent->value);
>> -}
>> -
>> -static const struct dma_fence_ops timeline_fence_ops = {
>> -	.get_driver_name = timeline_fence_get_driver_name,
>> -	.get_timeline_name = timeline_fence_get_timeline_name,
>> -	.enable_signaling = timeline_fence_enable_signaling,
>> -	.signaled = timeline_fence_signaled,
>> -	.release = timeline_fence_release,
>> -	.fence_value_str = timeline_fence_value_str,
>> -	.timeline_value_str = timeline_fence_timeline_value_str,
>> -};
>> -
>> -/**
>> - * sync_timeline_signal() - signal a status change on a sync_timeline
>> - * @obj:	sync_timeline to signal
>> - * @inc:	num to increment on timeline->value
>> - *
>> - * A sync implementation should call this any time one of it's fences
>> - * has signaled or has an error condition.
>> - */
>> -static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
>> -{
>> -	struct sync_pt *pt, *next;
>> -
>> -	trace_sync_timeline(obj);
>> -
>> -	spin_lock_irq(&obj->lock);
>> -
>> -	obj->value += inc;
>> -
>> -	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
>> -		if (!timeline_fence_signaled(&pt->base))
>> -			break;
>> -
>> -		list_del_init(&pt->link);
>> -		rb_erase(&pt->node, &obj->pt_tree);
>> -
>> -		/*
>> -		 * A signal callback may release the last reference to this
>> -		 * fence, causing it to be freed. That operation has to be
>> -		 * last to avoid a use after free inside this loop, and must
>> -		 * be after we remove the fence from the timeline in order to
>> -		 * prevent deadlocking on timeline->lock inside
>> -		 * timeline_fence_release().
>> -		 */
>> -		dma_fence_signal_locked(&pt->base);
>> -	}
>> -
>> -	spin_unlock_irq(&obj->lock);
>> -}
>> -
>> -/**
>> - * sync_pt_create() - creates a sync pt
>> - * @obj:	parent sync_timeline
>> - * @value:	value of the fence
>> - *
>> - * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
>> - * allocated allowing for implementation specific data to be kept after
>> - * the generic sync_timeline struct. Returns the sync_pt object or
>> - * NULL in case of error.
>> - */
>> -static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
>> -				      unsigned int value)
>> -{
>> -	struct sync_pt *pt;
>> -
>> -	pt = kzalloc(sizeof(*pt), GFP_KERNEL);
>> -	if (!pt)
>> -		return NULL;
>> -
>> -	sync_timeline_get(obj);
>> -	dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
>> -		       obj->context, value);
>> -	INIT_LIST_HEAD(&pt->link);
>> -
>> -	spin_lock_irq(&obj->lock);
>> -	if (!dma_fence_is_signaled_locked(&pt->base)) {
>> -		struct rb_node **p = &obj->pt_tree.rb_node;
>> -		struct rb_node *parent = NULL;
>> -
>> -		while (*p) {
>> -			struct sync_pt *other;
>> -			int cmp;
>> -
>> -			parent = *p;
>> -			other = rb_entry(parent, typeof(*pt), node);
>> -			cmp = value - other->base.seqno;
>> -			if (cmp > 0) {
>> -				p = &parent->rb_right;
>> -			} else if (cmp < 0) {
>> -				p = &parent->rb_left;
>> -			} else {
>> -				if (dma_fence_get_rcu(&other->base)) {
>> -					sync_timeline_put(obj);
>> -					kfree(pt);
>> -					pt = other;
>> -					goto unlock;
>> -				}
>> -				p = &parent->rb_left;
>> -			}
>> -		}
>> -		rb_link_node(&pt->node, parent, p);
>> -		rb_insert_color(&pt->node, &obj->pt_tree);
>> -
>> -		parent = rb_next(&pt->node);
>> -		list_add_tail(&pt->link,
>> -			      parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
>> -	}
>> -unlock:
>> -	spin_unlock_irq(&obj->lock);
>> -
>> -	return pt;
>> -}
>> -
>> -/*
>> - * *WARNING*
>> - *
>> - * improper use of this can result in deadlocking kernel drivers from userspace.
>> - */
>> -
>> -/* opening sw_sync create a new sync obj */
>> -static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
>> -{
>> -	struct sync_timeline *obj;
>> -	char task_comm[TASK_COMM_LEN];
>> -
>> -	get_task_comm(task_comm, current);
>> -
>> -	obj = sync_timeline_create(task_comm);
>> -	if (!obj)
>> -		return -ENOMEM;
>> -
>> -	file->private_data = obj;
>> -
>> -	return 0;
>> -}
>> -
>> -static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
>> -{
>> -	struct sync_timeline *obj = file->private_data;
>> -	struct sync_pt *pt, *next;
>> -
>> -	spin_lock_irq(&obj->lock);
>> -
>> -	list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
>> -		dma_fence_set_error(&pt->base, -ENOENT);
>> -		dma_fence_signal_locked(&pt->base);
>> -	}
>> -
>> -	spin_unlock_irq(&obj->lock);
>> -
>> -	sync_timeline_put(obj);
>> -	return 0;
>> -}
>> -
>> -static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
>> -				       unsigned long arg)
>> -{
>> -	int fd = get_unused_fd_flags(O_CLOEXEC);
>> -	int err;
>> -	struct sync_pt *pt;
>> -	struct sync_file *sync_file;
>> -	struct sw_sync_create_fence_data data;
>> -
>> -	if (fd < 0)
>> -		return fd;
>> -
>> -	if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
>> -		err = -EFAULT;
>> -		goto err;
>> -	}
>> -
>> -	pt = sync_pt_create(obj, data.value);
>> -	if (!pt) {
>> -		err = -ENOMEM;
>> -		goto err;
>> -	}
>> -
>> -	sync_file = sync_file_create(&pt->base);
>> -	dma_fence_put(&pt->base);
>> -	if (!sync_file) {
>> -		err = -ENOMEM;
>> -		goto err;
>> -	}
>> -
>> -	data.fence = fd;
>> -	if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
>> -		fput(sync_file->file);
>> -		err = -EFAULT;
>> -		goto err;
>> -	}
>> -
>> -	fd_install(fd, sync_file->file);
>> -
>> -	return 0;
>> -
>> -err:
>> -	put_unused_fd(fd);
>> -	return err;
>> -}
>> -
>> -static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
>> -{
>> -	u32 value;
>> -
>> -	if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
>> -		return -EFAULT;
>> -
>> -	while (value > INT_MAX)  {
>> -		sync_timeline_signal(obj, INT_MAX);
>> -		value -= INT_MAX;
>> -	}
>> -
>> -	sync_timeline_signal(obj, value);
>> -
>> -	return 0;
>> -}
>> -
>> -static long sw_sync_ioctl(struct file *file, unsigned int cmd,
>> -			  unsigned long arg)
>> -{
>> -	struct sync_timeline *obj = file->private_data;
>> -
>> -	switch (cmd) {
>> -	case SW_SYNC_IOC_CREATE_FENCE:
>> -		return sw_sync_ioctl_create_fence(obj, arg);
>> -
>> -	case SW_SYNC_IOC_INC:
>> -		return sw_sync_ioctl_inc(obj, arg);
>> -
>> -	default:
>> -		return -ENOTTY;
>> -	}
>> -}
>> -
>> -const struct file_operations sw_sync_debugfs_fops = {
>> -	.open           = sw_sync_debugfs_open,
>> -	.release        = sw_sync_debugfs_release,
>> -	.unlocked_ioctl = sw_sync_ioctl,
>> -	.compat_ioctl	= compat_ptr_ioctl,
>> -};
>> diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
>> deleted file mode 100644
>> index 101394f16930..000000000000
>> --- a/drivers/dma-buf/sync_debug.c
>> +++ /dev/null
>> @@ -1,190 +0,0 @@
>> -// SPDX-License-Identifier: GPL-2.0-only
>> -/*
>> - * Sync File validation framework and debug information
>> - *
>> - * Copyright (C) 2012 Google, Inc.
>> - */
>> -
>> -#include <linux/debugfs.h>
>> -#include "sync_debug.h"
>> -
>> -static struct dentry *dbgfs;
>> -
>> -static LIST_HEAD(sync_timeline_list_head);
>> -static DEFINE_SPINLOCK(sync_timeline_list_lock);
>> -static LIST_HEAD(sync_file_list_head);
>> -static DEFINE_SPINLOCK(sync_file_list_lock);
>> -
>> -void sync_timeline_debug_add(struct sync_timeline *obj)
>> -{
>> -	unsigned long flags;
>> -
>> -	spin_lock_irqsave(&sync_timeline_list_lock, flags);
>> -	list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
>> -	spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
>> -}
>> -
>> -void sync_timeline_debug_remove(struct sync_timeline *obj)
>> -{
>> -	unsigned long flags;
>> -
>> -	spin_lock_irqsave(&sync_timeline_list_lock, flags);
>> -	list_del(&obj->sync_timeline_list);
>> -	spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
>> -}
>> -
>> -void sync_file_debug_add(struct sync_file *sync_file)
>> -{
>> -	unsigned long flags;
>> -
>> -	spin_lock_irqsave(&sync_file_list_lock, flags);
>> -	list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
>> -	spin_unlock_irqrestore(&sync_file_list_lock, flags);
>> -}
>> -
>> -void sync_file_debug_remove(struct sync_file *sync_file)
>> -{
>> -	unsigned long flags;
>> -
>> -	spin_lock_irqsave(&sync_file_list_lock, flags);
>> -	list_del(&sync_file->sync_file_list);
>> -	spin_unlock_irqrestore(&sync_file_list_lock, flags);
>> -}
>> -
>> -static const char *sync_status_str(int status)
>> -{
>> -	if (status < 0)
>> -		return "error";
>> -
>> -	if (status > 0)
>> -		return "signaled";
>> -
>> -	return "active";
>> -}
>> -
>> -static void sync_print_fence(struct seq_file *s,
>> -			     struct dma_fence *fence, bool show)
>> -{
>> -	struct sync_timeline *parent = dma_fence_parent(fence);
>> -	int status;
>> -
>> -	status = dma_fence_get_status_locked(fence);
>> -
>> -	seq_printf(s, "  %s%sfence %s",
>> -		   show ? parent->name : "",
>> -		   show ? "_" : "",
>> -		   sync_status_str(status));
>> -
>> -	if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
>> -		struct timespec64 ts64 =
>> -			ktime_to_timespec64(fence->timestamp);
>> -
>> -		seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
>> -	}
>> -
>> -	if (fence->ops->timeline_value_str &&
>> -		fence->ops->fence_value_str) {
>> -		char value[64];
>> -		bool success;
>> -
>> -		fence->ops->fence_value_str(fence, value, sizeof(value));
>> -		success = strlen(value);
>> -
>> -		if (success) {
>> -			seq_printf(s, ": %s", value);
>> -
>> -			fence->ops->timeline_value_str(fence, value,
>> -						       sizeof(value));
>> -
>> -			if (strlen(value))
>> -				seq_printf(s, " / %s", value);
>> -		}
>> -	}
>> -
>> -	seq_putc(s, '\n');
>> -}
>> -
>> -static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
>> -{
>> -	struct list_head *pos;
>> -
>> -	seq_printf(s, "%s: %d\n", obj->name, obj->value);
>> -
>> -	spin_lock_irq(&obj->lock);
>> -	list_for_each(pos, &obj->pt_list) {
>> -		struct sync_pt *pt = container_of(pos, struct sync_pt, link);
>> -		sync_print_fence(s, &pt->base, false);
>> -	}
>> -	spin_unlock_irq(&obj->lock);
>> -}
>> -
>> -static void sync_print_sync_file(struct seq_file *s,
>> -				  struct sync_file *sync_file)
>> -{
>> -	char buf[128];
>> -	int i;
>> -
>> -	seq_printf(s, "[%p] %s: %s\n", sync_file,
>> -		   sync_file_get_name(sync_file, buf, sizeof(buf)),
>> -		   sync_status_str(dma_fence_get_status(sync_file->fence)));
>> -
>> -	if (dma_fence_is_array(sync_file->fence)) {
>> -		struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
>> -
>> -		for (i = 0; i < array->num_fences; ++i)
>> -			sync_print_fence(s, array->fences[i], true);
>> -	} else {
>> -		sync_print_fence(s, sync_file->fence, true);
>> -	}
>> -}
>> -
>> -static int sync_info_debugfs_show(struct seq_file *s, void *unused)
>> -{
>> -	struct list_head *pos;
>> -
>> -	seq_puts(s, "objs:\n--------------\n");
>> -
>> -	spin_lock_irq(&sync_timeline_list_lock);
>> -	list_for_each(pos, &sync_timeline_list_head) {
>> -		struct sync_timeline *obj =
>> -			container_of(pos, struct sync_timeline,
>> -				     sync_timeline_list);
>> -
>> -		sync_print_obj(s, obj);
>> -		seq_putc(s, '\n');
>> -	}
>> -	spin_unlock_irq(&sync_timeline_list_lock);
>> -
>> -	seq_puts(s, "fences:\n--------------\n");
>> -
>> -	spin_lock_irq(&sync_file_list_lock);
>> -	list_for_each(pos, &sync_file_list_head) {
>> -		struct sync_file *sync_file =
>> -			container_of(pos, struct sync_file, sync_file_list);
>> -
>> -		sync_print_sync_file(s, sync_file);
>> -		seq_putc(s, '\n');
>> -	}
>> -	spin_unlock_irq(&sync_file_list_lock);
>> -	return 0;
>> -}
>> -
>> -DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
>> -
>> -static __init int sync_debugfs_init(void)
>> -{
>> -	dbgfs = debugfs_create_dir("sync", NULL);
>> -
>> -	/*
>> -	 * The debugfs files won't ever get removed and thus, there is
>> -	 * no need to protect it against removal races. The use of
>> -	 * debugfs_create_file_unsafe() is actually safe here.
>> -	 */
>> -	debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
>> -				   &sync_info_debugfs_fops);
>> -	debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
>> -				   &sw_sync_debugfs_fops);
>> -
>> -	return 0;
>> -}
>> -late_initcall(sync_debugfs_init);
>> diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
>> deleted file mode 100644
>> index 6176e52ba2d7..000000000000
>> --- a/drivers/dma-buf/sync_debug.h
>> +++ /dev/null
>> @@ -1,72 +0,0 @@
>> -/*
>> - * Sync File validation framework and debug infomation
>> - *
>> - * Copyright (C) 2012 Google, Inc.
>> - *
>> - * This program is distributed in the hope that it will be useful,
>> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> - * GNU General Public License for more details.
>> - *
>> - */
>> -
>> -#ifndef _LINUX_SYNC_H
>> -#define _LINUX_SYNC_H
>> -
>> -#include <linux/list.h>
>> -#include <linux/rbtree.h>
>> -#include <linux/spinlock.h>
>> -#include <linux/dma-fence.h>
>> -
>> -#include <linux/sync_file.h>
>> -#include <uapi/linux/sync_file.h>
>> -
>> -/**
>> - * struct sync_timeline - sync object
>> - * @kref:		reference count on fence.
>> - * @name:		name of the sync_timeline. Useful for debugging
>> - * @lock:		lock protecting @pt_list and @value
>> - * @pt_tree:		rbtree of active (unsignaled/errored) sync_pts
>> - * @pt_list:		list of active (unsignaled/errored) sync_pts
>> - * @sync_timeline_list:	membership in global sync_timeline_list
>> - */
>> -struct sync_timeline {
>> -	struct kref		kref;
>> -	char			name[32];
>> -
>> -	/* protected by lock */
>> -	u64			context;
>> -	int			value;
>> -
>> -	struct rb_root		pt_tree;
>> -	struct list_head	pt_list;
>> -	spinlock_t		lock;
>> -
>> -	struct list_head	sync_timeline_list;
>> -};
>> -
>> -static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
>> -{
>> -	return container_of(fence->lock, struct sync_timeline, lock);
>> -}
>> -
>> -/**
>> - * struct sync_pt - sync_pt object
>> - * @base: base fence object
>> - * @link: link on the sync timeline's list
>> - * @node: node in the sync timeline's tree
>> - */
>> -struct sync_pt {
>> -	struct dma_fence base;
>> -	struct list_head link;
>> -	struct rb_node node;
>> -};
>> -
>> -extern const struct file_operations sw_sync_debugfs_fops;
>> -
>> -void sync_timeline_debug_add(struct sync_timeline *obj);
>> -void sync_timeline_debug_remove(struct sync_timeline *obj);
>> -void sync_file_debug_add(struct sync_file *fence);
>> -void sync_file_debug_remove(struct sync_file *fence);
>> -
>> -#endif /* _LINUX_SYNC_H */
>> -- 
>> 2.25.1
>>


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
  2021-07-29  8:38       ` Christian König
@ 2021-07-29  9:03         ` Daniel Vetter
  -1 siblings, 0 replies; 26+ messages in thread
From: Daniel Vetter @ 2021-07-29  9:03 UTC (permalink / raw)
  To: Christian König
  Cc: Sumit Semwal, Gustavo Padovan,
	open list:DMA BUFFER SHARING FRAMEWORK, dri-devel,
	moderated list:DMA BUFFER SHARING FRAMEWORK

On Thu, Jul 29, 2021 at 10:38 AM Christian König
<ckoenig.leichtzumerken@gmail.com> wrote:
> Am 29.07.21 um 09:23 schrieb Daniel Vetter:
> > On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
> >> As we now knew controlling dma_fence synchronization from userspace is
> >> extremely dangerous and can not only deadlock drivers but trivially also the
> >> whole kernel memory management.
> >>
> >> Entirely remove this option. We now have in kernel unit tests to exercise the
> >> dma_fence framework and it's containers.
> >>
> >> Signed-off-by: Christian König <christian.koenig@amd.com>
> > There's also igts for this, and Android heavily uses this. So I'm not sure
> > we can just nuke it.
>
> I feared that you would say that.
>
> > I guess what we could do is maybe just taint the kernel if it's ever used?
>
> What should we use then? TAINT_SOFTLOCKUP or TAINT_CRAP? :)

I think the cleanest solution would be to install a 10s timer on each
sw fence, and TAINT_SOFTLOOKUP if it fails to signal. That's similar
to what we do with vgem, except vgem then just steps in with a
dma_fence_signal() instead of tainting the kernel.

I think that should keep everyone happy, because if your Android has a
fence stuck for more than 10s it's busted.

Note that with hwc1 the indefinite fence was actually the model, when
you did a page_flip you got an out-fence back which will only be
signalled when the new buffer _stopped_ being used for scanout. Which
generally depends upon the next page-flip happening (except for manual
mode display, which only scan out once, if you ignore that the
kernel's supposed to restore the display after suspend/resume, which
android did). hwc2 fixed this, so a 10s timeout-to-taint should be
fine with Android people.

Also since we do have the igt/sw_sync.c test intel-gfx-ci should be
able to check that code even.
-Daniel

> Regards,
> Christian.
>
> >
> > Also for testing we use vgem now, which enforces a timeout.
> > -Daniel
> >
> >> ---
> >>   drivers/dma-buf/Kconfig      |  13 --
> >>   drivers/dma-buf/Makefile     |   1 -
> >>   drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
> >>   drivers/dma-buf/sync_debug.c | 190 ----------------
> >>   drivers/dma-buf/sync_debug.h |  72 ------
> >>   5 files changed, 688 deletions(-)
> >>   delete mode 100644 drivers/dma-buf/sw_sync.c
> >>   delete mode 100644 drivers/dma-buf/sync_debug.c
> >>   delete mode 100644 drivers/dma-buf/sync_debug.h
> >>
> >> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
> >> index 9561e3d2d428..26c53f45616a 100644
> >> --- a/drivers/dma-buf/Kconfig
> >> +++ b/drivers/dma-buf/Kconfig
> >> @@ -17,19 +17,6 @@ config SYNC_FILE
> >>        Files fds, to the DRM driver for example. More details at
> >>        Documentation/driver-api/sync_file.rst.
> >>
> >> -config SW_SYNC
> >> -    bool "Sync File Validation Framework"
> >> -    default n
> >> -    depends on SYNC_FILE
> >> -    depends on DEBUG_FS
> >> -    help
> >> -      A sync object driver that uses a 32bit counter to coordinate
> >> -      synchronization.  Useful when there is no hardware primitive backing
> >> -      the synchronization.
> >> -
> >> -      WARNING: improper use of this can result in deadlocking kernel
> >> -      drivers from userspace. Intended for test and debug only.
> >> -
> >>   config UDMABUF
> >>      bool "userspace dmabuf misc driver"
> >>      default n
> >> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
> >> index 1ef021273a06..fa3af0953f87 100644
> >> --- a/drivers/dma-buf/Makefile
> >> +++ b/drivers/dma-buf/Makefile
> >> @@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
> >>   obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o
> >>   obj-$(CONFIG_DMABUF_HEAPS) += heaps/
> >>   obj-$(CONFIG_SYNC_FILE)            += sync_file.o
> >> -obj-$(CONFIG_SW_SYNC)               += sw_sync.o sync_debug.o
> >>   obj-$(CONFIG_UDMABUF)              += udmabuf.o
> >>   obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
> >>
> >> diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
> >> deleted file mode 100644
> >> index 348b3a9170fa..000000000000
> >> --- a/drivers/dma-buf/sw_sync.c
> >> +++ /dev/null
> >> @@ -1,412 +0,0 @@
> >> -// SPDX-License-Identifier: GPL-2.0-only
> >> -/*
> >> - * Sync File validation framework
> >> - *
> >> - * Copyright (C) 2012 Google, Inc.
> >> - */
> >> -
> >> -#include <linux/file.h>
> >> -#include <linux/fs.h>
> >> -#include <linux/uaccess.h>
> >> -#include <linux/slab.h>
> >> -#include <linux/sync_file.h>
> >> -
> >> -#include "sync_debug.h"
> >> -
> >> -#define CREATE_TRACE_POINTS
> >> -#include "sync_trace.h"
> >> -
> >> -/*
> >> - * SW SYNC validation framework
> >> - *
> >> - * A sync object driver that uses a 32bit counter to coordinate
> >> - * synchronization.  Useful when there is no hardware primitive backing
> >> - * the synchronization.
> >> - *
> >> - * To start the framework just open:
> >> - *
> >> - * <debugfs>/sync/sw_sync
> >> - *
> >> - * That will create a sync timeline, all fences created under this timeline
> >> - * file descriptor will belong to the this timeline.
> >> - *
> >> - * The 'sw_sync' file can be opened many times as to create different
> >> - * timelines.
> >> - *
> >> - * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
> >> - * sw_sync_create_fence_data as parameter.
> >> - *
> >> - * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
> >> - * with the increment as u32. This will update the last signaled value
> >> - * from the timeline and signal any fence that has a seqno smaller or equal
> >> - * to it.
> >> - *
> >> - * struct sw_sync_create_fence_data
> >> - * @value:  the seqno to initialise the fence with
> >> - * @name:   the name of the new sync point
> >> - * @fence:  return the fd of the new sync_file with the created fence
> >> - */
> >> -struct sw_sync_create_fence_data {
> >> -    __u32   value;
> >> -    char    name[32];
> >> -    __s32   fence; /* fd of new fence */
> >> -};
> >> -
> >> -#define SW_SYNC_IOC_MAGIC   'W'
> >> -
> >> -#define SW_SYNC_IOC_CREATE_FENCE    _IOWR(SW_SYNC_IOC_MAGIC, 0,\
> >> -            struct sw_sync_create_fence_data)
> >> -
> >> -#define SW_SYNC_IOC_INC                     _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
> >> -
> >> -static const struct dma_fence_ops timeline_fence_ops;
> >> -
> >> -static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
> >> -{
> >> -    if (fence->ops != &timeline_fence_ops)
> >> -            return NULL;
> >> -    return container_of(fence, struct sync_pt, base);
> >> -}
> >> -
> >> -/**
> >> - * sync_timeline_create() - creates a sync object
> >> - * @name:   sync_timeline name
> >> - *
> >> - * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
> >> - * case of error.
> >> - */
> >> -static struct sync_timeline *sync_timeline_create(const char *name)
> >> -{
> >> -    struct sync_timeline *obj;
> >> -
> >> -    obj = kzalloc(sizeof(*obj), GFP_KERNEL);
> >> -    if (!obj)
> >> -            return NULL;
> >> -
> >> -    kref_init(&obj->kref);
> >> -    obj->context = dma_fence_context_alloc(1);
> >> -    strlcpy(obj->name, name, sizeof(obj->name));
> >> -
> >> -    obj->pt_tree = RB_ROOT;
> >> -    INIT_LIST_HEAD(&obj->pt_list);
> >> -    spin_lock_init(&obj->lock);
> >> -
> >> -    sync_timeline_debug_add(obj);
> >> -
> >> -    return obj;
> >> -}
> >> -
> >> -static void sync_timeline_free(struct kref *kref)
> >> -{
> >> -    struct sync_timeline *obj =
> >> -            container_of(kref, struct sync_timeline, kref);
> >> -
> >> -    sync_timeline_debug_remove(obj);
> >> -
> >> -    kfree(obj);
> >> -}
> >> -
> >> -static void sync_timeline_get(struct sync_timeline *obj)
> >> -{
> >> -    kref_get(&obj->kref);
> >> -}
> >> -
> >> -static void sync_timeline_put(struct sync_timeline *obj)
> >> -{
> >> -    kref_put(&obj->kref, sync_timeline_free);
> >> -}
> >> -
> >> -static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
> >> -{
> >> -    return "sw_sync";
> >> -}
> >> -
> >> -static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
> >> -{
> >> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >> -
> >> -    return parent->name;
> >> -}
> >> -
> >> -static void timeline_fence_release(struct dma_fence *fence)
> >> -{
> >> -    struct sync_pt *pt = dma_fence_to_sync_pt(fence);
> >> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >> -    unsigned long flags;
> >> -
> >> -    spin_lock_irqsave(fence->lock, flags);
> >> -    if (!list_empty(&pt->link)) {
> >> -            list_del(&pt->link);
> >> -            rb_erase(&pt->node, &parent->pt_tree);
> >> -    }
> >> -    spin_unlock_irqrestore(fence->lock, flags);
> >> -
> >> -    sync_timeline_put(parent);
> >> -    dma_fence_free(fence);
> >> -}
> >> -
> >> -static bool timeline_fence_signaled(struct dma_fence *fence)
> >> -{
> >> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >> -
> >> -    return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
> >> -}
> >> -
> >> -static bool timeline_fence_enable_signaling(struct dma_fence *fence)
> >> -{
> >> -    return true;
> >> -}
> >> -
> >> -static void timeline_fence_value_str(struct dma_fence *fence,
> >> -                                char *str, int size)
> >> -{
> >> -    snprintf(str, size, "%lld", fence->seqno);
> >> -}
> >> -
> >> -static void timeline_fence_timeline_value_str(struct dma_fence *fence,
> >> -                                         char *str, int size)
> >> -{
> >> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >> -
> >> -    snprintf(str, size, "%d", parent->value);
> >> -}
> >> -
> >> -static const struct dma_fence_ops timeline_fence_ops = {
> >> -    .get_driver_name = timeline_fence_get_driver_name,
> >> -    .get_timeline_name = timeline_fence_get_timeline_name,
> >> -    .enable_signaling = timeline_fence_enable_signaling,
> >> -    .signaled = timeline_fence_signaled,
> >> -    .release = timeline_fence_release,
> >> -    .fence_value_str = timeline_fence_value_str,
> >> -    .timeline_value_str = timeline_fence_timeline_value_str,
> >> -};
> >> -
> >> -/**
> >> - * sync_timeline_signal() - signal a status change on a sync_timeline
> >> - * @obj:    sync_timeline to signal
> >> - * @inc:    num to increment on timeline->value
> >> - *
> >> - * A sync implementation should call this any time one of it's fences
> >> - * has signaled or has an error condition.
> >> - */
> >> -static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
> >> -{
> >> -    struct sync_pt *pt, *next;
> >> -
> >> -    trace_sync_timeline(obj);
> >> -
> >> -    spin_lock_irq(&obj->lock);
> >> -
> >> -    obj->value += inc;
> >> -
> >> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> >> -            if (!timeline_fence_signaled(&pt->base))
> >> -                    break;
> >> -
> >> -            list_del_init(&pt->link);
> >> -            rb_erase(&pt->node, &obj->pt_tree);
> >> -
> >> -            /*
> >> -             * A signal callback may release the last reference to this
> >> -             * fence, causing it to be freed. That operation has to be
> >> -             * last to avoid a use after free inside this loop, and must
> >> -             * be after we remove the fence from the timeline in order to
> >> -             * prevent deadlocking on timeline->lock inside
> >> -             * timeline_fence_release().
> >> -             */
> >> -            dma_fence_signal_locked(&pt->base);
> >> -    }
> >> -
> >> -    spin_unlock_irq(&obj->lock);
> >> -}
> >> -
> >> -/**
> >> - * sync_pt_create() - creates a sync pt
> >> - * @obj:    parent sync_timeline
> >> - * @value:  value of the fence
> >> - *
> >> - * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
> >> - * allocated allowing for implementation specific data to be kept after
> >> - * the generic sync_timeline struct. Returns the sync_pt object or
> >> - * NULL in case of error.
> >> - */
> >> -static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
> >> -                                  unsigned int value)
> >> -{
> >> -    struct sync_pt *pt;
> >> -
> >> -    pt = kzalloc(sizeof(*pt), GFP_KERNEL);
> >> -    if (!pt)
> >> -            return NULL;
> >> -
> >> -    sync_timeline_get(obj);
> >> -    dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
> >> -                   obj->context, value);
> >> -    INIT_LIST_HEAD(&pt->link);
> >> -
> >> -    spin_lock_irq(&obj->lock);
> >> -    if (!dma_fence_is_signaled_locked(&pt->base)) {
> >> -            struct rb_node **p = &obj->pt_tree.rb_node;
> >> -            struct rb_node *parent = NULL;
> >> -
> >> -            while (*p) {
> >> -                    struct sync_pt *other;
> >> -                    int cmp;
> >> -
> >> -                    parent = *p;
> >> -                    other = rb_entry(parent, typeof(*pt), node);
> >> -                    cmp = value - other->base.seqno;
> >> -                    if (cmp > 0) {
> >> -                            p = &parent->rb_right;
> >> -                    } else if (cmp < 0) {
> >> -                            p = &parent->rb_left;
> >> -                    } else {
> >> -                            if (dma_fence_get_rcu(&other->base)) {
> >> -                                    sync_timeline_put(obj);
> >> -                                    kfree(pt);
> >> -                                    pt = other;
> >> -                                    goto unlock;
> >> -                            }
> >> -                            p = &parent->rb_left;
> >> -                    }
> >> -            }
> >> -            rb_link_node(&pt->node, parent, p);
> >> -            rb_insert_color(&pt->node, &obj->pt_tree);
> >> -
> >> -            parent = rb_next(&pt->node);
> >> -            list_add_tail(&pt->link,
> >> -                          parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
> >> -    }
> >> -unlock:
> >> -    spin_unlock_irq(&obj->lock);
> >> -
> >> -    return pt;
> >> -}
> >> -
> >> -/*
> >> - * *WARNING*
> >> - *
> >> - * improper use of this can result in deadlocking kernel drivers from userspace.
> >> - */
> >> -
> >> -/* opening sw_sync create a new sync obj */
> >> -static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
> >> -{
> >> -    struct sync_timeline *obj;
> >> -    char task_comm[TASK_COMM_LEN];
> >> -
> >> -    get_task_comm(task_comm, current);
> >> -
> >> -    obj = sync_timeline_create(task_comm);
> >> -    if (!obj)
> >> -            return -ENOMEM;
> >> -
> >> -    file->private_data = obj;
> >> -
> >> -    return 0;
> >> -}
> >> -
> >> -static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
> >> -{
> >> -    struct sync_timeline *obj = file->private_data;
> >> -    struct sync_pt *pt, *next;
> >> -
> >> -    spin_lock_irq(&obj->lock);
> >> -
> >> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> >> -            dma_fence_set_error(&pt->base, -ENOENT);
> >> -            dma_fence_signal_locked(&pt->base);
> >> -    }
> >> -
> >> -    spin_unlock_irq(&obj->lock);
> >> -
> >> -    sync_timeline_put(obj);
> >> -    return 0;
> >> -}
> >> -
> >> -static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
> >> -                                   unsigned long arg)
> >> -{
> >> -    int fd = get_unused_fd_flags(O_CLOEXEC);
> >> -    int err;
> >> -    struct sync_pt *pt;
> >> -    struct sync_file *sync_file;
> >> -    struct sw_sync_create_fence_data data;
> >> -
> >> -    if (fd < 0)
> >> -            return fd;
> >> -
> >> -    if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
> >> -            err = -EFAULT;
> >> -            goto err;
> >> -    }
> >> -
> >> -    pt = sync_pt_create(obj, data.value);
> >> -    if (!pt) {
> >> -            err = -ENOMEM;
> >> -            goto err;
> >> -    }
> >> -
> >> -    sync_file = sync_file_create(&pt->base);
> >> -    dma_fence_put(&pt->base);
> >> -    if (!sync_file) {
> >> -            err = -ENOMEM;
> >> -            goto err;
> >> -    }
> >> -
> >> -    data.fence = fd;
> >> -    if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
> >> -            fput(sync_file->file);
> >> -            err = -EFAULT;
> >> -            goto err;
> >> -    }
> >> -
> >> -    fd_install(fd, sync_file->file);
> >> -
> >> -    return 0;
> >> -
> >> -err:
> >> -    put_unused_fd(fd);
> >> -    return err;
> >> -}
> >> -
> >> -static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
> >> -{
> >> -    u32 value;
> >> -
> >> -    if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
> >> -            return -EFAULT;
> >> -
> >> -    while (value > INT_MAX)  {
> >> -            sync_timeline_signal(obj, INT_MAX);
> >> -            value -= INT_MAX;
> >> -    }
> >> -
> >> -    sync_timeline_signal(obj, value);
> >> -
> >> -    return 0;
> >> -}
> >> -
> >> -static long sw_sync_ioctl(struct file *file, unsigned int cmd,
> >> -                      unsigned long arg)
> >> -{
> >> -    struct sync_timeline *obj = file->private_data;
> >> -
> >> -    switch (cmd) {
> >> -    case SW_SYNC_IOC_CREATE_FENCE:
> >> -            return sw_sync_ioctl_create_fence(obj, arg);
> >> -
> >> -    case SW_SYNC_IOC_INC:
> >> -            return sw_sync_ioctl_inc(obj, arg);
> >> -
> >> -    default:
> >> -            return -ENOTTY;
> >> -    }
> >> -}
> >> -
> >> -const struct file_operations sw_sync_debugfs_fops = {
> >> -    .open           = sw_sync_debugfs_open,
> >> -    .release        = sw_sync_debugfs_release,
> >> -    .unlocked_ioctl = sw_sync_ioctl,
> >> -    .compat_ioctl   = compat_ptr_ioctl,
> >> -};
> >> diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
> >> deleted file mode 100644
> >> index 101394f16930..000000000000
> >> --- a/drivers/dma-buf/sync_debug.c
> >> +++ /dev/null
> >> @@ -1,190 +0,0 @@
> >> -// SPDX-License-Identifier: GPL-2.0-only
> >> -/*
> >> - * Sync File validation framework and debug information
> >> - *
> >> - * Copyright (C) 2012 Google, Inc.
> >> - */
> >> -
> >> -#include <linux/debugfs.h>
> >> -#include "sync_debug.h"
> >> -
> >> -static struct dentry *dbgfs;
> >> -
> >> -static LIST_HEAD(sync_timeline_list_head);
> >> -static DEFINE_SPINLOCK(sync_timeline_list_lock);
> >> -static LIST_HEAD(sync_file_list_head);
> >> -static DEFINE_SPINLOCK(sync_file_list_lock);
> >> -
> >> -void sync_timeline_debug_add(struct sync_timeline *obj)
> >> -{
> >> -    unsigned long flags;
> >> -
> >> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
> >> -    list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
> >> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> >> -}
> >> -
> >> -void sync_timeline_debug_remove(struct sync_timeline *obj)
> >> -{
> >> -    unsigned long flags;
> >> -
> >> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
> >> -    list_del(&obj->sync_timeline_list);
> >> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> >> -}
> >> -
> >> -void sync_file_debug_add(struct sync_file *sync_file)
> >> -{
> >> -    unsigned long flags;
> >> -
> >> -    spin_lock_irqsave(&sync_file_list_lock, flags);
> >> -    list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
> >> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
> >> -}
> >> -
> >> -void sync_file_debug_remove(struct sync_file *sync_file)
> >> -{
> >> -    unsigned long flags;
> >> -
> >> -    spin_lock_irqsave(&sync_file_list_lock, flags);
> >> -    list_del(&sync_file->sync_file_list);
> >> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
> >> -}
> >> -
> >> -static const char *sync_status_str(int status)
> >> -{
> >> -    if (status < 0)
> >> -            return "error";
> >> -
> >> -    if (status > 0)
> >> -            return "signaled";
> >> -
> >> -    return "active";
> >> -}
> >> -
> >> -static void sync_print_fence(struct seq_file *s,
> >> -                         struct dma_fence *fence, bool show)
> >> -{
> >> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >> -    int status;
> >> -
> >> -    status = dma_fence_get_status_locked(fence);
> >> -
> >> -    seq_printf(s, "  %s%sfence %s",
> >> -               show ? parent->name : "",
> >> -               show ? "_" : "",
> >> -               sync_status_str(status));
> >> -
> >> -    if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
> >> -            struct timespec64 ts64 =
> >> -                    ktime_to_timespec64(fence->timestamp);
> >> -
> >> -            seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
> >> -    }
> >> -
> >> -    if (fence->ops->timeline_value_str &&
> >> -            fence->ops->fence_value_str) {
> >> -            char value[64];
> >> -            bool success;
> >> -
> >> -            fence->ops->fence_value_str(fence, value, sizeof(value));
> >> -            success = strlen(value);
> >> -
> >> -            if (success) {
> >> -                    seq_printf(s, ": %s", value);
> >> -
> >> -                    fence->ops->timeline_value_str(fence, value,
> >> -                                                   sizeof(value));
> >> -
> >> -                    if (strlen(value))
> >> -                            seq_printf(s, " / %s", value);
> >> -            }
> >> -    }
> >> -
> >> -    seq_putc(s, '\n');
> >> -}
> >> -
> >> -static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
> >> -{
> >> -    struct list_head *pos;
> >> -
> >> -    seq_printf(s, "%s: %d\n", obj->name, obj->value);
> >> -
> >> -    spin_lock_irq(&obj->lock);
> >> -    list_for_each(pos, &obj->pt_list) {
> >> -            struct sync_pt *pt = container_of(pos, struct sync_pt, link);
> >> -            sync_print_fence(s, &pt->base, false);
> >> -    }
> >> -    spin_unlock_irq(&obj->lock);
> >> -}
> >> -
> >> -static void sync_print_sync_file(struct seq_file *s,
> >> -                              struct sync_file *sync_file)
> >> -{
> >> -    char buf[128];
> >> -    int i;
> >> -
> >> -    seq_printf(s, "[%p] %s: %s\n", sync_file,
> >> -               sync_file_get_name(sync_file, buf, sizeof(buf)),
> >> -               sync_status_str(dma_fence_get_status(sync_file->fence)));
> >> -
> >> -    if (dma_fence_is_array(sync_file->fence)) {
> >> -            struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
> >> -
> >> -            for (i = 0; i < array->num_fences; ++i)
> >> -                    sync_print_fence(s, array->fences[i], true);
> >> -    } else {
> >> -            sync_print_fence(s, sync_file->fence, true);
> >> -    }
> >> -}
> >> -
> >> -static int sync_info_debugfs_show(struct seq_file *s, void *unused)
> >> -{
> >> -    struct list_head *pos;
> >> -
> >> -    seq_puts(s, "objs:\n--------------\n");
> >> -
> >> -    spin_lock_irq(&sync_timeline_list_lock);
> >> -    list_for_each(pos, &sync_timeline_list_head) {
> >> -            struct sync_timeline *obj =
> >> -                    container_of(pos, struct sync_timeline,
> >> -                                 sync_timeline_list);
> >> -
> >> -            sync_print_obj(s, obj);
> >> -            seq_putc(s, '\n');
> >> -    }
> >> -    spin_unlock_irq(&sync_timeline_list_lock);
> >> -
> >> -    seq_puts(s, "fences:\n--------------\n");
> >> -
> >> -    spin_lock_irq(&sync_file_list_lock);
> >> -    list_for_each(pos, &sync_file_list_head) {
> >> -            struct sync_file *sync_file =
> >> -                    container_of(pos, struct sync_file, sync_file_list);
> >> -
> >> -            sync_print_sync_file(s, sync_file);
> >> -            seq_putc(s, '\n');
> >> -    }
> >> -    spin_unlock_irq(&sync_file_list_lock);
> >> -    return 0;
> >> -}
> >> -
> >> -DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
> >> -
> >> -static __init int sync_debugfs_init(void)
> >> -{
> >> -    dbgfs = debugfs_create_dir("sync", NULL);
> >> -
> >> -    /*
> >> -     * The debugfs files won't ever get removed and thus, there is
> >> -     * no need to protect it against removal races. The use of
> >> -     * debugfs_create_file_unsafe() is actually safe here.
> >> -     */
> >> -    debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
> >> -                               &sync_info_debugfs_fops);
> >> -    debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
> >> -                               &sw_sync_debugfs_fops);
> >> -
> >> -    return 0;
> >> -}
> >> -late_initcall(sync_debugfs_init);
> >> diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
> >> deleted file mode 100644
> >> index 6176e52ba2d7..000000000000
> >> --- a/drivers/dma-buf/sync_debug.h
> >> +++ /dev/null
> >> @@ -1,72 +0,0 @@
> >> -/*
> >> - * Sync File validation framework and debug infomation
> >> - *
> >> - * Copyright (C) 2012 Google, Inc.
> >> - *
> >> - * This program is distributed in the hope that it will be useful,
> >> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> >> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> >> - * GNU General Public License for more details.
> >> - *
> >> - */
> >> -
> >> -#ifndef _LINUX_SYNC_H
> >> -#define _LINUX_SYNC_H
> >> -
> >> -#include <linux/list.h>
> >> -#include <linux/rbtree.h>
> >> -#include <linux/spinlock.h>
> >> -#include <linux/dma-fence.h>
> >> -
> >> -#include <linux/sync_file.h>
> >> -#include <uapi/linux/sync_file.h>
> >> -
> >> -/**
> >> - * struct sync_timeline - sync object
> >> - * @kref:           reference count on fence.
> >> - * @name:           name of the sync_timeline. Useful for debugging
> >> - * @lock:           lock protecting @pt_list and @value
> >> - * @pt_tree:                rbtree of active (unsignaled/errored) sync_pts
> >> - * @pt_list:                list of active (unsignaled/errored) sync_pts
> >> - * @sync_timeline_list:     membership in global sync_timeline_list
> >> - */
> >> -struct sync_timeline {
> >> -    struct kref             kref;
> >> -    char                    name[32];
> >> -
> >> -    /* protected by lock */
> >> -    u64                     context;
> >> -    int                     value;
> >> -
> >> -    struct rb_root          pt_tree;
> >> -    struct list_head        pt_list;
> >> -    spinlock_t              lock;
> >> -
> >> -    struct list_head        sync_timeline_list;
> >> -};
> >> -
> >> -static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
> >> -{
> >> -    return container_of(fence->lock, struct sync_timeline, lock);
> >> -}
> >> -
> >> -/**
> >> - * struct sync_pt - sync_pt object
> >> - * @base: base fence object
> >> - * @link: link on the sync timeline's list
> >> - * @node: node in the sync timeline's tree
> >> - */
> >> -struct sync_pt {
> >> -    struct dma_fence base;
> >> -    struct list_head link;
> >> -    struct rb_node node;
> >> -};
> >> -
> >> -extern const struct file_operations sw_sync_debugfs_fops;
> >> -
> >> -void sync_timeline_debug_add(struct sync_timeline *obj);
> >> -void sync_timeline_debug_remove(struct sync_timeline *obj);
> >> -void sync_file_debug_add(struct sync_file *fence);
> >> -void sync_file_debug_remove(struct sync_file *fence);
> >> -
> >> -#endif /* _LINUX_SYNC_H */
> >> --
> >> 2.25.1
> >>
>


-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
@ 2021-07-29  9:03         ` Daniel Vetter
  0 siblings, 0 replies; 26+ messages in thread
From: Daniel Vetter @ 2021-07-29  9:03 UTC (permalink / raw)
  To: Christian König
  Cc: moderated list:DMA BUFFER SHARING FRAMEWORK, Gustavo Padovan,
	dri-devel, open list:DMA BUFFER SHARING FRAMEWORK

On Thu, Jul 29, 2021 at 10:38 AM Christian König
<ckoenig.leichtzumerken@gmail.com> wrote:
> Am 29.07.21 um 09:23 schrieb Daniel Vetter:
> > On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
> >> As we now knew controlling dma_fence synchronization from userspace is
> >> extremely dangerous and can not only deadlock drivers but trivially also the
> >> whole kernel memory management.
> >>
> >> Entirely remove this option. We now have in kernel unit tests to exercise the
> >> dma_fence framework and it's containers.
> >>
> >> Signed-off-by: Christian König <christian.koenig@amd.com>
> > There's also igts for this, and Android heavily uses this. So I'm not sure
> > we can just nuke it.
>
> I feared that you would say that.
>
> > I guess what we could do is maybe just taint the kernel if it's ever used?
>
> What should we use then? TAINT_SOFTLOCKUP or TAINT_CRAP? :)

I think the cleanest solution would be to install a 10s timer on each
sw fence, and TAINT_SOFTLOOKUP if it fails to signal. That's similar
to what we do with vgem, except vgem then just steps in with a
dma_fence_signal() instead of tainting the kernel.

I think that should keep everyone happy, because if your Android has a
fence stuck for more than 10s it's busted.

Note that with hwc1 the indefinite fence was actually the model, when
you did a page_flip you got an out-fence back which will only be
signalled when the new buffer _stopped_ being used for scanout. Which
generally depends upon the next page-flip happening (except for manual
mode display, which only scan out once, if you ignore that the
kernel's supposed to restore the display after suspend/resume, which
android did). hwc2 fixed this, so a 10s timeout-to-taint should be
fine with Android people.

Also since we do have the igt/sw_sync.c test intel-gfx-ci should be
able to check that code even.
-Daniel

> Regards,
> Christian.
>
> >
> > Also for testing we use vgem now, which enforces a timeout.
> > -Daniel
> >
> >> ---
> >>   drivers/dma-buf/Kconfig      |  13 --
> >>   drivers/dma-buf/Makefile     |   1 -
> >>   drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
> >>   drivers/dma-buf/sync_debug.c | 190 ----------------
> >>   drivers/dma-buf/sync_debug.h |  72 ------
> >>   5 files changed, 688 deletions(-)
> >>   delete mode 100644 drivers/dma-buf/sw_sync.c
> >>   delete mode 100644 drivers/dma-buf/sync_debug.c
> >>   delete mode 100644 drivers/dma-buf/sync_debug.h
> >>
> >> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
> >> index 9561e3d2d428..26c53f45616a 100644
> >> --- a/drivers/dma-buf/Kconfig
> >> +++ b/drivers/dma-buf/Kconfig
> >> @@ -17,19 +17,6 @@ config SYNC_FILE
> >>        Files fds, to the DRM driver for example. More details at
> >>        Documentation/driver-api/sync_file.rst.
> >>
> >> -config SW_SYNC
> >> -    bool "Sync File Validation Framework"
> >> -    default n
> >> -    depends on SYNC_FILE
> >> -    depends on DEBUG_FS
> >> -    help
> >> -      A sync object driver that uses a 32bit counter to coordinate
> >> -      synchronization.  Useful when there is no hardware primitive backing
> >> -      the synchronization.
> >> -
> >> -      WARNING: improper use of this can result in deadlocking kernel
> >> -      drivers from userspace. Intended for test and debug only.
> >> -
> >>   config UDMABUF
> >>      bool "userspace dmabuf misc driver"
> >>      default n
> >> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
> >> index 1ef021273a06..fa3af0953f87 100644
> >> --- a/drivers/dma-buf/Makefile
> >> +++ b/drivers/dma-buf/Makefile
> >> @@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
> >>   obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o
> >>   obj-$(CONFIG_DMABUF_HEAPS) += heaps/
> >>   obj-$(CONFIG_SYNC_FILE)            += sync_file.o
> >> -obj-$(CONFIG_SW_SYNC)               += sw_sync.o sync_debug.o
> >>   obj-$(CONFIG_UDMABUF)              += udmabuf.o
> >>   obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
> >>
> >> diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
> >> deleted file mode 100644
> >> index 348b3a9170fa..000000000000
> >> --- a/drivers/dma-buf/sw_sync.c
> >> +++ /dev/null
> >> @@ -1,412 +0,0 @@
> >> -// SPDX-License-Identifier: GPL-2.0-only
> >> -/*
> >> - * Sync File validation framework
> >> - *
> >> - * Copyright (C) 2012 Google, Inc.
> >> - */
> >> -
> >> -#include <linux/file.h>
> >> -#include <linux/fs.h>
> >> -#include <linux/uaccess.h>
> >> -#include <linux/slab.h>
> >> -#include <linux/sync_file.h>
> >> -
> >> -#include "sync_debug.h"
> >> -
> >> -#define CREATE_TRACE_POINTS
> >> -#include "sync_trace.h"
> >> -
> >> -/*
> >> - * SW SYNC validation framework
> >> - *
> >> - * A sync object driver that uses a 32bit counter to coordinate
> >> - * synchronization.  Useful when there is no hardware primitive backing
> >> - * the synchronization.
> >> - *
> >> - * To start the framework just open:
> >> - *
> >> - * <debugfs>/sync/sw_sync
> >> - *
> >> - * That will create a sync timeline, all fences created under this timeline
> >> - * file descriptor will belong to the this timeline.
> >> - *
> >> - * The 'sw_sync' file can be opened many times as to create different
> >> - * timelines.
> >> - *
> >> - * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
> >> - * sw_sync_create_fence_data as parameter.
> >> - *
> >> - * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
> >> - * with the increment as u32. This will update the last signaled value
> >> - * from the timeline and signal any fence that has a seqno smaller or equal
> >> - * to it.
> >> - *
> >> - * struct sw_sync_create_fence_data
> >> - * @value:  the seqno to initialise the fence with
> >> - * @name:   the name of the new sync point
> >> - * @fence:  return the fd of the new sync_file with the created fence
> >> - */
> >> -struct sw_sync_create_fence_data {
> >> -    __u32   value;
> >> -    char    name[32];
> >> -    __s32   fence; /* fd of new fence */
> >> -};
> >> -
> >> -#define SW_SYNC_IOC_MAGIC   'W'
> >> -
> >> -#define SW_SYNC_IOC_CREATE_FENCE    _IOWR(SW_SYNC_IOC_MAGIC, 0,\
> >> -            struct sw_sync_create_fence_data)
> >> -
> >> -#define SW_SYNC_IOC_INC                     _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
> >> -
> >> -static const struct dma_fence_ops timeline_fence_ops;
> >> -
> >> -static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
> >> -{
> >> -    if (fence->ops != &timeline_fence_ops)
> >> -            return NULL;
> >> -    return container_of(fence, struct sync_pt, base);
> >> -}
> >> -
> >> -/**
> >> - * sync_timeline_create() - creates a sync object
> >> - * @name:   sync_timeline name
> >> - *
> >> - * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
> >> - * case of error.
> >> - */
> >> -static struct sync_timeline *sync_timeline_create(const char *name)
> >> -{
> >> -    struct sync_timeline *obj;
> >> -
> >> -    obj = kzalloc(sizeof(*obj), GFP_KERNEL);
> >> -    if (!obj)
> >> -            return NULL;
> >> -
> >> -    kref_init(&obj->kref);
> >> -    obj->context = dma_fence_context_alloc(1);
> >> -    strlcpy(obj->name, name, sizeof(obj->name));
> >> -
> >> -    obj->pt_tree = RB_ROOT;
> >> -    INIT_LIST_HEAD(&obj->pt_list);
> >> -    spin_lock_init(&obj->lock);
> >> -
> >> -    sync_timeline_debug_add(obj);
> >> -
> >> -    return obj;
> >> -}
> >> -
> >> -static void sync_timeline_free(struct kref *kref)
> >> -{
> >> -    struct sync_timeline *obj =
> >> -            container_of(kref, struct sync_timeline, kref);
> >> -
> >> -    sync_timeline_debug_remove(obj);
> >> -
> >> -    kfree(obj);
> >> -}
> >> -
> >> -static void sync_timeline_get(struct sync_timeline *obj)
> >> -{
> >> -    kref_get(&obj->kref);
> >> -}
> >> -
> >> -static void sync_timeline_put(struct sync_timeline *obj)
> >> -{
> >> -    kref_put(&obj->kref, sync_timeline_free);
> >> -}
> >> -
> >> -static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
> >> -{
> >> -    return "sw_sync";
> >> -}
> >> -
> >> -static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
> >> -{
> >> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >> -
> >> -    return parent->name;
> >> -}
> >> -
> >> -static void timeline_fence_release(struct dma_fence *fence)
> >> -{
> >> -    struct sync_pt *pt = dma_fence_to_sync_pt(fence);
> >> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >> -    unsigned long flags;
> >> -
> >> -    spin_lock_irqsave(fence->lock, flags);
> >> -    if (!list_empty(&pt->link)) {
> >> -            list_del(&pt->link);
> >> -            rb_erase(&pt->node, &parent->pt_tree);
> >> -    }
> >> -    spin_unlock_irqrestore(fence->lock, flags);
> >> -
> >> -    sync_timeline_put(parent);
> >> -    dma_fence_free(fence);
> >> -}
> >> -
> >> -static bool timeline_fence_signaled(struct dma_fence *fence)
> >> -{
> >> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >> -
> >> -    return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
> >> -}
> >> -
> >> -static bool timeline_fence_enable_signaling(struct dma_fence *fence)
> >> -{
> >> -    return true;
> >> -}
> >> -
> >> -static void timeline_fence_value_str(struct dma_fence *fence,
> >> -                                char *str, int size)
> >> -{
> >> -    snprintf(str, size, "%lld", fence->seqno);
> >> -}
> >> -
> >> -static void timeline_fence_timeline_value_str(struct dma_fence *fence,
> >> -                                         char *str, int size)
> >> -{
> >> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >> -
> >> -    snprintf(str, size, "%d", parent->value);
> >> -}
> >> -
> >> -static const struct dma_fence_ops timeline_fence_ops = {
> >> -    .get_driver_name = timeline_fence_get_driver_name,
> >> -    .get_timeline_name = timeline_fence_get_timeline_name,
> >> -    .enable_signaling = timeline_fence_enable_signaling,
> >> -    .signaled = timeline_fence_signaled,
> >> -    .release = timeline_fence_release,
> >> -    .fence_value_str = timeline_fence_value_str,
> >> -    .timeline_value_str = timeline_fence_timeline_value_str,
> >> -};
> >> -
> >> -/**
> >> - * sync_timeline_signal() - signal a status change on a sync_timeline
> >> - * @obj:    sync_timeline to signal
> >> - * @inc:    num to increment on timeline->value
> >> - *
> >> - * A sync implementation should call this any time one of it's fences
> >> - * has signaled or has an error condition.
> >> - */
> >> -static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
> >> -{
> >> -    struct sync_pt *pt, *next;
> >> -
> >> -    trace_sync_timeline(obj);
> >> -
> >> -    spin_lock_irq(&obj->lock);
> >> -
> >> -    obj->value += inc;
> >> -
> >> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> >> -            if (!timeline_fence_signaled(&pt->base))
> >> -                    break;
> >> -
> >> -            list_del_init(&pt->link);
> >> -            rb_erase(&pt->node, &obj->pt_tree);
> >> -
> >> -            /*
> >> -             * A signal callback may release the last reference to this
> >> -             * fence, causing it to be freed. That operation has to be
> >> -             * last to avoid a use after free inside this loop, and must
> >> -             * be after we remove the fence from the timeline in order to
> >> -             * prevent deadlocking on timeline->lock inside
> >> -             * timeline_fence_release().
> >> -             */
> >> -            dma_fence_signal_locked(&pt->base);
> >> -    }
> >> -
> >> -    spin_unlock_irq(&obj->lock);
> >> -}
> >> -
> >> -/**
> >> - * sync_pt_create() - creates a sync pt
> >> - * @obj:    parent sync_timeline
> >> - * @value:  value of the fence
> >> - *
> >> - * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
> >> - * allocated allowing for implementation specific data to be kept after
> >> - * the generic sync_timeline struct. Returns the sync_pt object or
> >> - * NULL in case of error.
> >> - */
> >> -static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
> >> -                                  unsigned int value)
> >> -{
> >> -    struct sync_pt *pt;
> >> -
> >> -    pt = kzalloc(sizeof(*pt), GFP_KERNEL);
> >> -    if (!pt)
> >> -            return NULL;
> >> -
> >> -    sync_timeline_get(obj);
> >> -    dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
> >> -                   obj->context, value);
> >> -    INIT_LIST_HEAD(&pt->link);
> >> -
> >> -    spin_lock_irq(&obj->lock);
> >> -    if (!dma_fence_is_signaled_locked(&pt->base)) {
> >> -            struct rb_node **p = &obj->pt_tree.rb_node;
> >> -            struct rb_node *parent = NULL;
> >> -
> >> -            while (*p) {
> >> -                    struct sync_pt *other;
> >> -                    int cmp;
> >> -
> >> -                    parent = *p;
> >> -                    other = rb_entry(parent, typeof(*pt), node);
> >> -                    cmp = value - other->base.seqno;
> >> -                    if (cmp > 0) {
> >> -                            p = &parent->rb_right;
> >> -                    } else if (cmp < 0) {
> >> -                            p = &parent->rb_left;
> >> -                    } else {
> >> -                            if (dma_fence_get_rcu(&other->base)) {
> >> -                                    sync_timeline_put(obj);
> >> -                                    kfree(pt);
> >> -                                    pt = other;
> >> -                                    goto unlock;
> >> -                            }
> >> -                            p = &parent->rb_left;
> >> -                    }
> >> -            }
> >> -            rb_link_node(&pt->node, parent, p);
> >> -            rb_insert_color(&pt->node, &obj->pt_tree);
> >> -
> >> -            parent = rb_next(&pt->node);
> >> -            list_add_tail(&pt->link,
> >> -                          parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
> >> -    }
> >> -unlock:
> >> -    spin_unlock_irq(&obj->lock);
> >> -
> >> -    return pt;
> >> -}
> >> -
> >> -/*
> >> - * *WARNING*
> >> - *
> >> - * improper use of this can result in deadlocking kernel drivers from userspace.
> >> - */
> >> -
> >> -/* opening sw_sync create a new sync obj */
> >> -static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
> >> -{
> >> -    struct sync_timeline *obj;
> >> -    char task_comm[TASK_COMM_LEN];
> >> -
> >> -    get_task_comm(task_comm, current);
> >> -
> >> -    obj = sync_timeline_create(task_comm);
> >> -    if (!obj)
> >> -            return -ENOMEM;
> >> -
> >> -    file->private_data = obj;
> >> -
> >> -    return 0;
> >> -}
> >> -
> >> -static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
> >> -{
> >> -    struct sync_timeline *obj = file->private_data;
> >> -    struct sync_pt *pt, *next;
> >> -
> >> -    spin_lock_irq(&obj->lock);
> >> -
> >> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> >> -            dma_fence_set_error(&pt->base, -ENOENT);
> >> -            dma_fence_signal_locked(&pt->base);
> >> -    }
> >> -
> >> -    spin_unlock_irq(&obj->lock);
> >> -
> >> -    sync_timeline_put(obj);
> >> -    return 0;
> >> -}
> >> -
> >> -static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
> >> -                                   unsigned long arg)
> >> -{
> >> -    int fd = get_unused_fd_flags(O_CLOEXEC);
> >> -    int err;
> >> -    struct sync_pt *pt;
> >> -    struct sync_file *sync_file;
> >> -    struct sw_sync_create_fence_data data;
> >> -
> >> -    if (fd < 0)
> >> -            return fd;
> >> -
> >> -    if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
> >> -            err = -EFAULT;
> >> -            goto err;
> >> -    }
> >> -
> >> -    pt = sync_pt_create(obj, data.value);
> >> -    if (!pt) {
> >> -            err = -ENOMEM;
> >> -            goto err;
> >> -    }
> >> -
> >> -    sync_file = sync_file_create(&pt->base);
> >> -    dma_fence_put(&pt->base);
> >> -    if (!sync_file) {
> >> -            err = -ENOMEM;
> >> -            goto err;
> >> -    }
> >> -
> >> -    data.fence = fd;
> >> -    if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
> >> -            fput(sync_file->file);
> >> -            err = -EFAULT;
> >> -            goto err;
> >> -    }
> >> -
> >> -    fd_install(fd, sync_file->file);
> >> -
> >> -    return 0;
> >> -
> >> -err:
> >> -    put_unused_fd(fd);
> >> -    return err;
> >> -}
> >> -
> >> -static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
> >> -{
> >> -    u32 value;
> >> -
> >> -    if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
> >> -            return -EFAULT;
> >> -
> >> -    while (value > INT_MAX)  {
> >> -            sync_timeline_signal(obj, INT_MAX);
> >> -            value -= INT_MAX;
> >> -    }
> >> -
> >> -    sync_timeline_signal(obj, value);
> >> -
> >> -    return 0;
> >> -}
> >> -
> >> -static long sw_sync_ioctl(struct file *file, unsigned int cmd,
> >> -                      unsigned long arg)
> >> -{
> >> -    struct sync_timeline *obj = file->private_data;
> >> -
> >> -    switch (cmd) {
> >> -    case SW_SYNC_IOC_CREATE_FENCE:
> >> -            return sw_sync_ioctl_create_fence(obj, arg);
> >> -
> >> -    case SW_SYNC_IOC_INC:
> >> -            return sw_sync_ioctl_inc(obj, arg);
> >> -
> >> -    default:
> >> -            return -ENOTTY;
> >> -    }
> >> -}
> >> -
> >> -const struct file_operations sw_sync_debugfs_fops = {
> >> -    .open           = sw_sync_debugfs_open,
> >> -    .release        = sw_sync_debugfs_release,
> >> -    .unlocked_ioctl = sw_sync_ioctl,
> >> -    .compat_ioctl   = compat_ptr_ioctl,
> >> -};
> >> diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
> >> deleted file mode 100644
> >> index 101394f16930..000000000000
> >> --- a/drivers/dma-buf/sync_debug.c
> >> +++ /dev/null
> >> @@ -1,190 +0,0 @@
> >> -// SPDX-License-Identifier: GPL-2.0-only
> >> -/*
> >> - * Sync File validation framework and debug information
> >> - *
> >> - * Copyright (C) 2012 Google, Inc.
> >> - */
> >> -
> >> -#include <linux/debugfs.h>
> >> -#include "sync_debug.h"
> >> -
> >> -static struct dentry *dbgfs;
> >> -
> >> -static LIST_HEAD(sync_timeline_list_head);
> >> -static DEFINE_SPINLOCK(sync_timeline_list_lock);
> >> -static LIST_HEAD(sync_file_list_head);
> >> -static DEFINE_SPINLOCK(sync_file_list_lock);
> >> -
> >> -void sync_timeline_debug_add(struct sync_timeline *obj)
> >> -{
> >> -    unsigned long flags;
> >> -
> >> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
> >> -    list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
> >> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> >> -}
> >> -
> >> -void sync_timeline_debug_remove(struct sync_timeline *obj)
> >> -{
> >> -    unsigned long flags;
> >> -
> >> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
> >> -    list_del(&obj->sync_timeline_list);
> >> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> >> -}
> >> -
> >> -void sync_file_debug_add(struct sync_file *sync_file)
> >> -{
> >> -    unsigned long flags;
> >> -
> >> -    spin_lock_irqsave(&sync_file_list_lock, flags);
> >> -    list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
> >> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
> >> -}
> >> -
> >> -void sync_file_debug_remove(struct sync_file *sync_file)
> >> -{
> >> -    unsigned long flags;
> >> -
> >> -    spin_lock_irqsave(&sync_file_list_lock, flags);
> >> -    list_del(&sync_file->sync_file_list);
> >> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
> >> -}
> >> -
> >> -static const char *sync_status_str(int status)
> >> -{
> >> -    if (status < 0)
> >> -            return "error";
> >> -
> >> -    if (status > 0)
> >> -            return "signaled";
> >> -
> >> -    return "active";
> >> -}
> >> -
> >> -static void sync_print_fence(struct seq_file *s,
> >> -                         struct dma_fence *fence, bool show)
> >> -{
> >> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >> -    int status;
> >> -
> >> -    status = dma_fence_get_status_locked(fence);
> >> -
> >> -    seq_printf(s, "  %s%sfence %s",
> >> -               show ? parent->name : "",
> >> -               show ? "_" : "",
> >> -               sync_status_str(status));
> >> -
> >> -    if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
> >> -            struct timespec64 ts64 =
> >> -                    ktime_to_timespec64(fence->timestamp);
> >> -
> >> -            seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
> >> -    }
> >> -
> >> -    if (fence->ops->timeline_value_str &&
> >> -            fence->ops->fence_value_str) {
> >> -            char value[64];
> >> -            bool success;
> >> -
> >> -            fence->ops->fence_value_str(fence, value, sizeof(value));
> >> -            success = strlen(value);
> >> -
> >> -            if (success) {
> >> -                    seq_printf(s, ": %s", value);
> >> -
> >> -                    fence->ops->timeline_value_str(fence, value,
> >> -                                                   sizeof(value));
> >> -
> >> -                    if (strlen(value))
> >> -                            seq_printf(s, " / %s", value);
> >> -            }
> >> -    }
> >> -
> >> -    seq_putc(s, '\n');
> >> -}
> >> -
> >> -static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
> >> -{
> >> -    struct list_head *pos;
> >> -
> >> -    seq_printf(s, "%s: %d\n", obj->name, obj->value);
> >> -
> >> -    spin_lock_irq(&obj->lock);
> >> -    list_for_each(pos, &obj->pt_list) {
> >> -            struct sync_pt *pt = container_of(pos, struct sync_pt, link);
> >> -            sync_print_fence(s, &pt->base, false);
> >> -    }
> >> -    spin_unlock_irq(&obj->lock);
> >> -}
> >> -
> >> -static void sync_print_sync_file(struct seq_file *s,
> >> -                              struct sync_file *sync_file)
> >> -{
> >> -    char buf[128];
> >> -    int i;
> >> -
> >> -    seq_printf(s, "[%p] %s: %s\n", sync_file,
> >> -               sync_file_get_name(sync_file, buf, sizeof(buf)),
> >> -               sync_status_str(dma_fence_get_status(sync_file->fence)));
> >> -
> >> -    if (dma_fence_is_array(sync_file->fence)) {
> >> -            struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
> >> -
> >> -            for (i = 0; i < array->num_fences; ++i)
> >> -                    sync_print_fence(s, array->fences[i], true);
> >> -    } else {
> >> -            sync_print_fence(s, sync_file->fence, true);
> >> -    }
> >> -}
> >> -
> >> -static int sync_info_debugfs_show(struct seq_file *s, void *unused)
> >> -{
> >> -    struct list_head *pos;
> >> -
> >> -    seq_puts(s, "objs:\n--------------\n");
> >> -
> >> -    spin_lock_irq(&sync_timeline_list_lock);
> >> -    list_for_each(pos, &sync_timeline_list_head) {
> >> -            struct sync_timeline *obj =
> >> -                    container_of(pos, struct sync_timeline,
> >> -                                 sync_timeline_list);
> >> -
> >> -            sync_print_obj(s, obj);
> >> -            seq_putc(s, '\n');
> >> -    }
> >> -    spin_unlock_irq(&sync_timeline_list_lock);
> >> -
> >> -    seq_puts(s, "fences:\n--------------\n");
> >> -
> >> -    spin_lock_irq(&sync_file_list_lock);
> >> -    list_for_each(pos, &sync_file_list_head) {
> >> -            struct sync_file *sync_file =
> >> -                    container_of(pos, struct sync_file, sync_file_list);
> >> -
> >> -            sync_print_sync_file(s, sync_file);
> >> -            seq_putc(s, '\n');
> >> -    }
> >> -    spin_unlock_irq(&sync_file_list_lock);
> >> -    return 0;
> >> -}
> >> -
> >> -DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
> >> -
> >> -static __init int sync_debugfs_init(void)
> >> -{
> >> -    dbgfs = debugfs_create_dir("sync", NULL);
> >> -
> >> -    /*
> >> -     * The debugfs files won't ever get removed and thus, there is
> >> -     * no need to protect it against removal races. The use of
> >> -     * debugfs_create_file_unsafe() is actually safe here.
> >> -     */
> >> -    debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
> >> -                               &sync_info_debugfs_fops);
> >> -    debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
> >> -                               &sw_sync_debugfs_fops);
> >> -
> >> -    return 0;
> >> -}
> >> -late_initcall(sync_debugfs_init);
> >> diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
> >> deleted file mode 100644
> >> index 6176e52ba2d7..000000000000
> >> --- a/drivers/dma-buf/sync_debug.h
> >> +++ /dev/null
> >> @@ -1,72 +0,0 @@
> >> -/*
> >> - * Sync File validation framework and debug infomation
> >> - *
> >> - * Copyright (C) 2012 Google, Inc.
> >> - *
> >> - * This program is distributed in the hope that it will be useful,
> >> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> >> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> >> - * GNU General Public License for more details.
> >> - *
> >> - */
> >> -
> >> -#ifndef _LINUX_SYNC_H
> >> -#define _LINUX_SYNC_H
> >> -
> >> -#include <linux/list.h>
> >> -#include <linux/rbtree.h>
> >> -#include <linux/spinlock.h>
> >> -#include <linux/dma-fence.h>
> >> -
> >> -#include <linux/sync_file.h>
> >> -#include <uapi/linux/sync_file.h>
> >> -
> >> -/**
> >> - * struct sync_timeline - sync object
> >> - * @kref:           reference count on fence.
> >> - * @name:           name of the sync_timeline. Useful for debugging
> >> - * @lock:           lock protecting @pt_list and @value
> >> - * @pt_tree:                rbtree of active (unsignaled/errored) sync_pts
> >> - * @pt_list:                list of active (unsignaled/errored) sync_pts
> >> - * @sync_timeline_list:     membership in global sync_timeline_list
> >> - */
> >> -struct sync_timeline {
> >> -    struct kref             kref;
> >> -    char                    name[32];
> >> -
> >> -    /* protected by lock */
> >> -    u64                     context;
> >> -    int                     value;
> >> -
> >> -    struct rb_root          pt_tree;
> >> -    struct list_head        pt_list;
> >> -    spinlock_t              lock;
> >> -
> >> -    struct list_head        sync_timeline_list;
> >> -};
> >> -
> >> -static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
> >> -{
> >> -    return container_of(fence->lock, struct sync_timeline, lock);
> >> -}
> >> -
> >> -/**
> >> - * struct sync_pt - sync_pt object
> >> - * @base: base fence object
> >> - * @link: link on the sync timeline's list
> >> - * @node: node in the sync timeline's tree
> >> - */
> >> -struct sync_pt {
> >> -    struct dma_fence base;
> >> -    struct list_head link;
> >> -    struct rb_node node;
> >> -};
> >> -
> >> -extern const struct file_operations sw_sync_debugfs_fops;
> >> -
> >> -void sync_timeline_debug_add(struct sync_timeline *obj);
> >> -void sync_timeline_debug_remove(struct sync_timeline *obj);
> >> -void sync_file_debug_add(struct sync_file *fence);
> >> -void sync_file_debug_remove(struct sync_file *fence);
> >> -
> >> -#endif /* _LINUX_SYNC_H */
> >> --
> >> 2.25.1
> >>
>


-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
  2021-07-29  9:03         ` Daniel Vetter
@ 2021-07-29 10:21           ` Christian König
  -1 siblings, 0 replies; 26+ messages in thread
From: Christian König @ 2021-07-29 10:21 UTC (permalink / raw)
  To: Daniel Vetter
  Cc: Sumit Semwal, Gustavo Padovan,
	open list:DMA BUFFER SHARING FRAMEWORK, dri-devel,
	moderated list:DMA BUFFER SHARING FRAMEWORK

Am 29.07.21 um 11:03 schrieb Daniel Vetter:
> On Thu, Jul 29, 2021 at 10:38 AM Christian König
> <ckoenig.leichtzumerken@gmail.com> wrote:
>> Am 29.07.21 um 09:23 schrieb Daniel Vetter:
>>> On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
>>>> As we now knew controlling dma_fence synchronization from userspace is
>>>> extremely dangerous and can not only deadlock drivers but trivially also the
>>>> whole kernel memory management.
>>>>
>>>> Entirely remove this option. We now have in kernel unit tests to exercise the
>>>> dma_fence framework and it's containers.
>>>>
>>>> Signed-off-by: Christian König <christian.koenig@amd.com>
>>> There's also igts for this, and Android heavily uses this. So I'm not sure
>>> we can just nuke it.
>> I feared that you would say that.
>>
>>> I guess what we could do is maybe just taint the kernel if it's ever used?
>> What should we use then? TAINT_SOFTLOCKUP or TAINT_CRAP? :)
> I think the cleanest solution would be to install a 10s timer on each
> sw fence, and TAINT_SOFTLOOKUP if it fails to signal. That's similar
> to what we do with vgem, except vgem then just steps in with a
> dma_fence_signal() instead of tainting the kernel.

Well vgem is only for testing as far as I know.

When Android is using this for a production environment I think we 
should be a bit harder and explicitly note that this is not a good idea 
in sw_sync_debugfs_open().

> I think that should keep everyone happy, because if your Android has a
> fence stuck for more than 10s it's busted.

Well when they are using userptrs they are busted as soon as they have a 
fence stuck for much less time.

Regards,
Christian.

>
> Note that with hwc1 the indefinite fence was actually the model, when
> you did a page_flip you got an out-fence back which will only be
> signalled when the new buffer _stopped_ being used for scanout. Which
> generally depends upon the next page-flip happening (except for manual
> mode display, which only scan out once, if you ignore that the
> kernel's supposed to restore the display after suspend/resume, which
> android did). hwc2 fixed this, so a 10s timeout-to-taint should be
> fine with Android people.
>
> Also since we do have the igt/sw_sync.c test intel-gfx-ci should be
> able to check that code even.
> -Daniel
>
>> Regards,
>> Christian.
>>
>>> Also for testing we use vgem now, which enforces a timeout.
>>> -Daniel
>>>
>>>> ---
>>>>    drivers/dma-buf/Kconfig      |  13 --
>>>>    drivers/dma-buf/Makefile     |   1 -
>>>>    drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
>>>>    drivers/dma-buf/sync_debug.c | 190 ----------------
>>>>    drivers/dma-buf/sync_debug.h |  72 ------
>>>>    5 files changed, 688 deletions(-)
>>>>    delete mode 100644 drivers/dma-buf/sw_sync.c
>>>>    delete mode 100644 drivers/dma-buf/sync_debug.c
>>>>    delete mode 100644 drivers/dma-buf/sync_debug.h
>>>>
>>>> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
>>>> index 9561e3d2d428..26c53f45616a 100644
>>>> --- a/drivers/dma-buf/Kconfig
>>>> +++ b/drivers/dma-buf/Kconfig
>>>> @@ -17,19 +17,6 @@ config SYNC_FILE
>>>>         Files fds, to the DRM driver for example. More details at
>>>>         Documentation/driver-api/sync_file.rst.
>>>>
>>>> -config SW_SYNC
>>>> -    bool "Sync File Validation Framework"
>>>> -    default n
>>>> -    depends on SYNC_FILE
>>>> -    depends on DEBUG_FS
>>>> -    help
>>>> -      A sync object driver that uses a 32bit counter to coordinate
>>>> -      synchronization.  Useful when there is no hardware primitive backing
>>>> -      the synchronization.
>>>> -
>>>> -      WARNING: improper use of this can result in deadlocking kernel
>>>> -      drivers from userspace. Intended for test and debug only.
>>>> -
>>>>    config UDMABUF
>>>>       bool "userspace dmabuf misc driver"
>>>>       default n
>>>> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
>>>> index 1ef021273a06..fa3af0953f87 100644
>>>> --- a/drivers/dma-buf/Makefile
>>>> +++ b/drivers/dma-buf/Makefile
>>>> @@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
>>>>    obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o
>>>>    obj-$(CONFIG_DMABUF_HEAPS) += heaps/
>>>>    obj-$(CONFIG_SYNC_FILE)            += sync_file.o
>>>> -obj-$(CONFIG_SW_SYNC)               += sw_sync.o sync_debug.o
>>>>    obj-$(CONFIG_UDMABUF)              += udmabuf.o
>>>>    obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
>>>>
>>>> diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
>>>> deleted file mode 100644
>>>> index 348b3a9170fa..000000000000
>>>> --- a/drivers/dma-buf/sw_sync.c
>>>> +++ /dev/null
>>>> @@ -1,412 +0,0 @@
>>>> -// SPDX-License-Identifier: GPL-2.0-only
>>>> -/*
>>>> - * Sync File validation framework
>>>> - *
>>>> - * Copyright (C) 2012 Google, Inc.
>>>> - */
>>>> -
>>>> -#include <linux/file.h>
>>>> -#include <linux/fs.h>
>>>> -#include <linux/uaccess.h>
>>>> -#include <linux/slab.h>
>>>> -#include <linux/sync_file.h>
>>>> -
>>>> -#include "sync_debug.h"
>>>> -
>>>> -#define CREATE_TRACE_POINTS
>>>> -#include "sync_trace.h"
>>>> -
>>>> -/*
>>>> - * SW SYNC validation framework
>>>> - *
>>>> - * A sync object driver that uses a 32bit counter to coordinate
>>>> - * synchronization.  Useful when there is no hardware primitive backing
>>>> - * the synchronization.
>>>> - *
>>>> - * To start the framework just open:
>>>> - *
>>>> - * <debugfs>/sync/sw_sync
>>>> - *
>>>> - * That will create a sync timeline, all fences created under this timeline
>>>> - * file descriptor will belong to the this timeline.
>>>> - *
>>>> - * The 'sw_sync' file can be opened many times as to create different
>>>> - * timelines.
>>>> - *
>>>> - * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
>>>> - * sw_sync_create_fence_data as parameter.
>>>> - *
>>>> - * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
>>>> - * with the increment as u32. This will update the last signaled value
>>>> - * from the timeline and signal any fence that has a seqno smaller or equal
>>>> - * to it.
>>>> - *
>>>> - * struct sw_sync_create_fence_data
>>>> - * @value:  the seqno to initialise the fence with
>>>> - * @name:   the name of the new sync point
>>>> - * @fence:  return the fd of the new sync_file with the created fence
>>>> - */
>>>> -struct sw_sync_create_fence_data {
>>>> -    __u32   value;
>>>> -    char    name[32];
>>>> -    __s32   fence; /* fd of new fence */
>>>> -};
>>>> -
>>>> -#define SW_SYNC_IOC_MAGIC   'W'
>>>> -
>>>> -#define SW_SYNC_IOC_CREATE_FENCE    _IOWR(SW_SYNC_IOC_MAGIC, 0,\
>>>> -            struct sw_sync_create_fence_data)
>>>> -
>>>> -#define SW_SYNC_IOC_INC                     _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
>>>> -
>>>> -static const struct dma_fence_ops timeline_fence_ops;
>>>> -
>>>> -static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
>>>> -{
>>>> -    if (fence->ops != &timeline_fence_ops)
>>>> -            return NULL;
>>>> -    return container_of(fence, struct sync_pt, base);
>>>> -}
>>>> -
>>>> -/**
>>>> - * sync_timeline_create() - creates a sync object
>>>> - * @name:   sync_timeline name
>>>> - *
>>>> - * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
>>>> - * case of error.
>>>> - */
>>>> -static struct sync_timeline *sync_timeline_create(const char *name)
>>>> -{
>>>> -    struct sync_timeline *obj;
>>>> -
>>>> -    obj = kzalloc(sizeof(*obj), GFP_KERNEL);
>>>> -    if (!obj)
>>>> -            return NULL;
>>>> -
>>>> -    kref_init(&obj->kref);
>>>> -    obj->context = dma_fence_context_alloc(1);
>>>> -    strlcpy(obj->name, name, sizeof(obj->name));
>>>> -
>>>> -    obj->pt_tree = RB_ROOT;
>>>> -    INIT_LIST_HEAD(&obj->pt_list);
>>>> -    spin_lock_init(&obj->lock);
>>>> -
>>>> -    sync_timeline_debug_add(obj);
>>>> -
>>>> -    return obj;
>>>> -}
>>>> -
>>>> -static void sync_timeline_free(struct kref *kref)
>>>> -{
>>>> -    struct sync_timeline *obj =
>>>> -            container_of(kref, struct sync_timeline, kref);
>>>> -
>>>> -    sync_timeline_debug_remove(obj);
>>>> -
>>>> -    kfree(obj);
>>>> -}
>>>> -
>>>> -static void sync_timeline_get(struct sync_timeline *obj)
>>>> -{
>>>> -    kref_get(&obj->kref);
>>>> -}
>>>> -
>>>> -static void sync_timeline_put(struct sync_timeline *obj)
>>>> -{
>>>> -    kref_put(&obj->kref, sync_timeline_free);
>>>> -}
>>>> -
>>>> -static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
>>>> -{
>>>> -    return "sw_sync";
>>>> -}
>>>> -
>>>> -static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
>>>> -{
>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>> -
>>>> -    return parent->name;
>>>> -}
>>>> -
>>>> -static void timeline_fence_release(struct dma_fence *fence)
>>>> -{
>>>> -    struct sync_pt *pt = dma_fence_to_sync_pt(fence);
>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>> -    unsigned long flags;
>>>> -
>>>> -    spin_lock_irqsave(fence->lock, flags);
>>>> -    if (!list_empty(&pt->link)) {
>>>> -            list_del(&pt->link);
>>>> -            rb_erase(&pt->node, &parent->pt_tree);
>>>> -    }
>>>> -    spin_unlock_irqrestore(fence->lock, flags);
>>>> -
>>>> -    sync_timeline_put(parent);
>>>> -    dma_fence_free(fence);
>>>> -}
>>>> -
>>>> -static bool timeline_fence_signaled(struct dma_fence *fence)
>>>> -{
>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>> -
>>>> -    return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
>>>> -}
>>>> -
>>>> -static bool timeline_fence_enable_signaling(struct dma_fence *fence)
>>>> -{
>>>> -    return true;
>>>> -}
>>>> -
>>>> -static void timeline_fence_value_str(struct dma_fence *fence,
>>>> -                                char *str, int size)
>>>> -{
>>>> -    snprintf(str, size, "%lld", fence->seqno);
>>>> -}
>>>> -
>>>> -static void timeline_fence_timeline_value_str(struct dma_fence *fence,
>>>> -                                         char *str, int size)
>>>> -{
>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>> -
>>>> -    snprintf(str, size, "%d", parent->value);
>>>> -}
>>>> -
>>>> -static const struct dma_fence_ops timeline_fence_ops = {
>>>> -    .get_driver_name = timeline_fence_get_driver_name,
>>>> -    .get_timeline_name = timeline_fence_get_timeline_name,
>>>> -    .enable_signaling = timeline_fence_enable_signaling,
>>>> -    .signaled = timeline_fence_signaled,
>>>> -    .release = timeline_fence_release,
>>>> -    .fence_value_str = timeline_fence_value_str,
>>>> -    .timeline_value_str = timeline_fence_timeline_value_str,
>>>> -};
>>>> -
>>>> -/**
>>>> - * sync_timeline_signal() - signal a status change on a sync_timeline
>>>> - * @obj:    sync_timeline to signal
>>>> - * @inc:    num to increment on timeline->value
>>>> - *
>>>> - * A sync implementation should call this any time one of it's fences
>>>> - * has signaled or has an error condition.
>>>> - */
>>>> -static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
>>>> -{
>>>> -    struct sync_pt *pt, *next;
>>>> -
>>>> -    trace_sync_timeline(obj);
>>>> -
>>>> -    spin_lock_irq(&obj->lock);
>>>> -
>>>> -    obj->value += inc;
>>>> -
>>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
>>>> -            if (!timeline_fence_signaled(&pt->base))
>>>> -                    break;
>>>> -
>>>> -            list_del_init(&pt->link);
>>>> -            rb_erase(&pt->node, &obj->pt_tree);
>>>> -
>>>> -            /*
>>>> -             * A signal callback may release the last reference to this
>>>> -             * fence, causing it to be freed. That operation has to be
>>>> -             * last to avoid a use after free inside this loop, and must
>>>> -             * be after we remove the fence from the timeline in order to
>>>> -             * prevent deadlocking on timeline->lock inside
>>>> -             * timeline_fence_release().
>>>> -             */
>>>> -            dma_fence_signal_locked(&pt->base);
>>>> -    }
>>>> -
>>>> -    spin_unlock_irq(&obj->lock);
>>>> -}
>>>> -
>>>> -/**
>>>> - * sync_pt_create() - creates a sync pt
>>>> - * @obj:    parent sync_timeline
>>>> - * @value:  value of the fence
>>>> - *
>>>> - * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
>>>> - * allocated allowing for implementation specific data to be kept after
>>>> - * the generic sync_timeline struct. Returns the sync_pt object or
>>>> - * NULL in case of error.
>>>> - */
>>>> -static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
>>>> -                                  unsigned int value)
>>>> -{
>>>> -    struct sync_pt *pt;
>>>> -
>>>> -    pt = kzalloc(sizeof(*pt), GFP_KERNEL);
>>>> -    if (!pt)
>>>> -            return NULL;
>>>> -
>>>> -    sync_timeline_get(obj);
>>>> -    dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
>>>> -                   obj->context, value);
>>>> -    INIT_LIST_HEAD(&pt->link);
>>>> -
>>>> -    spin_lock_irq(&obj->lock);
>>>> -    if (!dma_fence_is_signaled_locked(&pt->base)) {
>>>> -            struct rb_node **p = &obj->pt_tree.rb_node;
>>>> -            struct rb_node *parent = NULL;
>>>> -
>>>> -            while (*p) {
>>>> -                    struct sync_pt *other;
>>>> -                    int cmp;
>>>> -
>>>> -                    parent = *p;
>>>> -                    other = rb_entry(parent, typeof(*pt), node);
>>>> -                    cmp = value - other->base.seqno;
>>>> -                    if (cmp > 0) {
>>>> -                            p = &parent->rb_right;
>>>> -                    } else if (cmp < 0) {
>>>> -                            p = &parent->rb_left;
>>>> -                    } else {
>>>> -                            if (dma_fence_get_rcu(&other->base)) {
>>>> -                                    sync_timeline_put(obj);
>>>> -                                    kfree(pt);
>>>> -                                    pt = other;
>>>> -                                    goto unlock;
>>>> -                            }
>>>> -                            p = &parent->rb_left;
>>>> -                    }
>>>> -            }
>>>> -            rb_link_node(&pt->node, parent, p);
>>>> -            rb_insert_color(&pt->node, &obj->pt_tree);
>>>> -
>>>> -            parent = rb_next(&pt->node);
>>>> -            list_add_tail(&pt->link,
>>>> -                          parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
>>>> -    }
>>>> -unlock:
>>>> -    spin_unlock_irq(&obj->lock);
>>>> -
>>>> -    return pt;
>>>> -}
>>>> -
>>>> -/*
>>>> - * *WARNING*
>>>> - *
>>>> - * improper use of this can result in deadlocking kernel drivers from userspace.
>>>> - */
>>>> -
>>>> -/* opening sw_sync create a new sync obj */
>>>> -static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
>>>> -{
>>>> -    struct sync_timeline *obj;
>>>> -    char task_comm[TASK_COMM_LEN];
>>>> -
>>>> -    get_task_comm(task_comm, current);
>>>> -
>>>> -    obj = sync_timeline_create(task_comm);
>>>> -    if (!obj)
>>>> -            return -ENOMEM;
>>>> -
>>>> -    file->private_data = obj;
>>>> -
>>>> -    return 0;
>>>> -}
>>>> -
>>>> -static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
>>>> -{
>>>> -    struct sync_timeline *obj = file->private_data;
>>>> -    struct sync_pt *pt, *next;
>>>> -
>>>> -    spin_lock_irq(&obj->lock);
>>>> -
>>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
>>>> -            dma_fence_set_error(&pt->base, -ENOENT);
>>>> -            dma_fence_signal_locked(&pt->base);
>>>> -    }
>>>> -
>>>> -    spin_unlock_irq(&obj->lock);
>>>> -
>>>> -    sync_timeline_put(obj);
>>>> -    return 0;
>>>> -}
>>>> -
>>>> -static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
>>>> -                                   unsigned long arg)
>>>> -{
>>>> -    int fd = get_unused_fd_flags(O_CLOEXEC);
>>>> -    int err;
>>>> -    struct sync_pt *pt;
>>>> -    struct sync_file *sync_file;
>>>> -    struct sw_sync_create_fence_data data;
>>>> -
>>>> -    if (fd < 0)
>>>> -            return fd;
>>>> -
>>>> -    if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
>>>> -            err = -EFAULT;
>>>> -            goto err;
>>>> -    }
>>>> -
>>>> -    pt = sync_pt_create(obj, data.value);
>>>> -    if (!pt) {
>>>> -            err = -ENOMEM;
>>>> -            goto err;
>>>> -    }
>>>> -
>>>> -    sync_file = sync_file_create(&pt->base);
>>>> -    dma_fence_put(&pt->base);
>>>> -    if (!sync_file) {
>>>> -            err = -ENOMEM;
>>>> -            goto err;
>>>> -    }
>>>> -
>>>> -    data.fence = fd;
>>>> -    if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
>>>> -            fput(sync_file->file);
>>>> -            err = -EFAULT;
>>>> -            goto err;
>>>> -    }
>>>> -
>>>> -    fd_install(fd, sync_file->file);
>>>> -
>>>> -    return 0;
>>>> -
>>>> -err:
>>>> -    put_unused_fd(fd);
>>>> -    return err;
>>>> -}
>>>> -
>>>> -static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
>>>> -{
>>>> -    u32 value;
>>>> -
>>>> -    if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
>>>> -            return -EFAULT;
>>>> -
>>>> -    while (value > INT_MAX)  {
>>>> -            sync_timeline_signal(obj, INT_MAX);
>>>> -            value -= INT_MAX;
>>>> -    }
>>>> -
>>>> -    sync_timeline_signal(obj, value);
>>>> -
>>>> -    return 0;
>>>> -}
>>>> -
>>>> -static long sw_sync_ioctl(struct file *file, unsigned int cmd,
>>>> -                      unsigned long arg)
>>>> -{
>>>> -    struct sync_timeline *obj = file->private_data;
>>>> -
>>>> -    switch (cmd) {
>>>> -    case SW_SYNC_IOC_CREATE_FENCE:
>>>> -            return sw_sync_ioctl_create_fence(obj, arg);
>>>> -
>>>> -    case SW_SYNC_IOC_INC:
>>>> -            return sw_sync_ioctl_inc(obj, arg);
>>>> -
>>>> -    default:
>>>> -            return -ENOTTY;
>>>> -    }
>>>> -}
>>>> -
>>>> -const struct file_operations sw_sync_debugfs_fops = {
>>>> -    .open           = sw_sync_debugfs_open,
>>>> -    .release        = sw_sync_debugfs_release,
>>>> -    .unlocked_ioctl = sw_sync_ioctl,
>>>> -    .compat_ioctl   = compat_ptr_ioctl,
>>>> -};
>>>> diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
>>>> deleted file mode 100644
>>>> index 101394f16930..000000000000
>>>> --- a/drivers/dma-buf/sync_debug.c
>>>> +++ /dev/null
>>>> @@ -1,190 +0,0 @@
>>>> -// SPDX-License-Identifier: GPL-2.0-only
>>>> -/*
>>>> - * Sync File validation framework and debug information
>>>> - *
>>>> - * Copyright (C) 2012 Google, Inc.
>>>> - */
>>>> -
>>>> -#include <linux/debugfs.h>
>>>> -#include "sync_debug.h"
>>>> -
>>>> -static struct dentry *dbgfs;
>>>> -
>>>> -static LIST_HEAD(sync_timeline_list_head);
>>>> -static DEFINE_SPINLOCK(sync_timeline_list_lock);
>>>> -static LIST_HEAD(sync_file_list_head);
>>>> -static DEFINE_SPINLOCK(sync_file_list_lock);
>>>> -
>>>> -void sync_timeline_debug_add(struct sync_timeline *obj)
>>>> -{
>>>> -    unsigned long flags;
>>>> -
>>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
>>>> -    list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
>>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
>>>> -}
>>>> -
>>>> -void sync_timeline_debug_remove(struct sync_timeline *obj)
>>>> -{
>>>> -    unsigned long flags;
>>>> -
>>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
>>>> -    list_del(&obj->sync_timeline_list);
>>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
>>>> -}
>>>> -
>>>> -void sync_file_debug_add(struct sync_file *sync_file)
>>>> -{
>>>> -    unsigned long flags;
>>>> -
>>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
>>>> -    list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
>>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
>>>> -}
>>>> -
>>>> -void sync_file_debug_remove(struct sync_file *sync_file)
>>>> -{
>>>> -    unsigned long flags;
>>>> -
>>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
>>>> -    list_del(&sync_file->sync_file_list);
>>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
>>>> -}
>>>> -
>>>> -static const char *sync_status_str(int status)
>>>> -{
>>>> -    if (status < 0)
>>>> -            return "error";
>>>> -
>>>> -    if (status > 0)
>>>> -            return "signaled";
>>>> -
>>>> -    return "active";
>>>> -}
>>>> -
>>>> -static void sync_print_fence(struct seq_file *s,
>>>> -                         struct dma_fence *fence, bool show)
>>>> -{
>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>> -    int status;
>>>> -
>>>> -    status = dma_fence_get_status_locked(fence);
>>>> -
>>>> -    seq_printf(s, "  %s%sfence %s",
>>>> -               show ? parent->name : "",
>>>> -               show ? "_" : "",
>>>> -               sync_status_str(status));
>>>> -
>>>> -    if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
>>>> -            struct timespec64 ts64 =
>>>> -                    ktime_to_timespec64(fence->timestamp);
>>>> -
>>>> -            seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
>>>> -    }
>>>> -
>>>> -    if (fence->ops->timeline_value_str &&
>>>> -            fence->ops->fence_value_str) {
>>>> -            char value[64];
>>>> -            bool success;
>>>> -
>>>> -            fence->ops->fence_value_str(fence, value, sizeof(value));
>>>> -            success = strlen(value);
>>>> -
>>>> -            if (success) {
>>>> -                    seq_printf(s, ": %s", value);
>>>> -
>>>> -                    fence->ops->timeline_value_str(fence, value,
>>>> -                                                   sizeof(value));
>>>> -
>>>> -                    if (strlen(value))
>>>> -                            seq_printf(s, " / %s", value);
>>>> -            }
>>>> -    }
>>>> -
>>>> -    seq_putc(s, '\n');
>>>> -}
>>>> -
>>>> -static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
>>>> -{
>>>> -    struct list_head *pos;
>>>> -
>>>> -    seq_printf(s, "%s: %d\n", obj->name, obj->value);
>>>> -
>>>> -    spin_lock_irq(&obj->lock);
>>>> -    list_for_each(pos, &obj->pt_list) {
>>>> -            struct sync_pt *pt = container_of(pos, struct sync_pt, link);
>>>> -            sync_print_fence(s, &pt->base, false);
>>>> -    }
>>>> -    spin_unlock_irq(&obj->lock);
>>>> -}
>>>> -
>>>> -static void sync_print_sync_file(struct seq_file *s,
>>>> -                              struct sync_file *sync_file)
>>>> -{
>>>> -    char buf[128];
>>>> -    int i;
>>>> -
>>>> -    seq_printf(s, "[%p] %s: %s\n", sync_file,
>>>> -               sync_file_get_name(sync_file, buf, sizeof(buf)),
>>>> -               sync_status_str(dma_fence_get_status(sync_file->fence)));
>>>> -
>>>> -    if (dma_fence_is_array(sync_file->fence)) {
>>>> -            struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
>>>> -
>>>> -            for (i = 0; i < array->num_fences; ++i)
>>>> -                    sync_print_fence(s, array->fences[i], true);
>>>> -    } else {
>>>> -            sync_print_fence(s, sync_file->fence, true);
>>>> -    }
>>>> -}
>>>> -
>>>> -static int sync_info_debugfs_show(struct seq_file *s, void *unused)
>>>> -{
>>>> -    struct list_head *pos;
>>>> -
>>>> -    seq_puts(s, "objs:\n--------------\n");
>>>> -
>>>> -    spin_lock_irq(&sync_timeline_list_lock);
>>>> -    list_for_each(pos, &sync_timeline_list_head) {
>>>> -            struct sync_timeline *obj =
>>>> -                    container_of(pos, struct sync_timeline,
>>>> -                                 sync_timeline_list);
>>>> -
>>>> -            sync_print_obj(s, obj);
>>>> -            seq_putc(s, '\n');
>>>> -    }
>>>> -    spin_unlock_irq(&sync_timeline_list_lock);
>>>> -
>>>> -    seq_puts(s, "fences:\n--------------\n");
>>>> -
>>>> -    spin_lock_irq(&sync_file_list_lock);
>>>> -    list_for_each(pos, &sync_file_list_head) {
>>>> -            struct sync_file *sync_file =
>>>> -                    container_of(pos, struct sync_file, sync_file_list);
>>>> -
>>>> -            sync_print_sync_file(s, sync_file);
>>>> -            seq_putc(s, '\n');
>>>> -    }
>>>> -    spin_unlock_irq(&sync_file_list_lock);
>>>> -    return 0;
>>>> -}
>>>> -
>>>> -DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
>>>> -
>>>> -static __init int sync_debugfs_init(void)
>>>> -{
>>>> -    dbgfs = debugfs_create_dir("sync", NULL);
>>>> -
>>>> -    /*
>>>> -     * The debugfs files won't ever get removed and thus, there is
>>>> -     * no need to protect it against removal races. The use of
>>>> -     * debugfs_create_file_unsafe() is actually safe here.
>>>> -     */
>>>> -    debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
>>>> -                               &sync_info_debugfs_fops);
>>>> -    debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
>>>> -                               &sw_sync_debugfs_fops);
>>>> -
>>>> -    return 0;
>>>> -}
>>>> -late_initcall(sync_debugfs_init);
>>>> diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
>>>> deleted file mode 100644
>>>> index 6176e52ba2d7..000000000000
>>>> --- a/drivers/dma-buf/sync_debug.h
>>>> +++ /dev/null
>>>> @@ -1,72 +0,0 @@
>>>> -/*
>>>> - * Sync File validation framework and debug infomation
>>>> - *
>>>> - * Copyright (C) 2012 Google, Inc.
>>>> - *
>>>> - * This program is distributed in the hope that it will be useful,
>>>> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
>>>> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>>>> - * GNU General Public License for more details.
>>>> - *
>>>> - */
>>>> -
>>>> -#ifndef _LINUX_SYNC_H
>>>> -#define _LINUX_SYNC_H
>>>> -
>>>> -#include <linux/list.h>
>>>> -#include <linux/rbtree.h>
>>>> -#include <linux/spinlock.h>
>>>> -#include <linux/dma-fence.h>
>>>> -
>>>> -#include <linux/sync_file.h>
>>>> -#include <uapi/linux/sync_file.h>
>>>> -
>>>> -/**
>>>> - * struct sync_timeline - sync object
>>>> - * @kref:           reference count on fence.
>>>> - * @name:           name of the sync_timeline. Useful for debugging
>>>> - * @lock:           lock protecting @pt_list and @value
>>>> - * @pt_tree:                rbtree of active (unsignaled/errored) sync_pts
>>>> - * @pt_list:                list of active (unsignaled/errored) sync_pts
>>>> - * @sync_timeline_list:     membership in global sync_timeline_list
>>>> - */
>>>> -struct sync_timeline {
>>>> -    struct kref             kref;
>>>> -    char                    name[32];
>>>> -
>>>> -    /* protected by lock */
>>>> -    u64                     context;
>>>> -    int                     value;
>>>> -
>>>> -    struct rb_root          pt_tree;
>>>> -    struct list_head        pt_list;
>>>> -    spinlock_t              lock;
>>>> -
>>>> -    struct list_head        sync_timeline_list;
>>>> -};
>>>> -
>>>> -static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
>>>> -{
>>>> -    return container_of(fence->lock, struct sync_timeline, lock);
>>>> -}
>>>> -
>>>> -/**
>>>> - * struct sync_pt - sync_pt object
>>>> - * @base: base fence object
>>>> - * @link: link on the sync timeline's list
>>>> - * @node: node in the sync timeline's tree
>>>> - */
>>>> -struct sync_pt {
>>>> -    struct dma_fence base;
>>>> -    struct list_head link;
>>>> -    struct rb_node node;
>>>> -};
>>>> -
>>>> -extern const struct file_operations sw_sync_debugfs_fops;
>>>> -
>>>> -void sync_timeline_debug_add(struct sync_timeline *obj);
>>>> -void sync_timeline_debug_remove(struct sync_timeline *obj);
>>>> -void sync_file_debug_add(struct sync_file *fence);
>>>> -void sync_file_debug_remove(struct sync_file *fence);
>>>> -
>>>> -#endif /* _LINUX_SYNC_H */
>>>> --
>>>> 2.25.1
>>>>
>


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
@ 2021-07-29 10:21           ` Christian König
  0 siblings, 0 replies; 26+ messages in thread
From: Christian König @ 2021-07-29 10:21 UTC (permalink / raw)
  To: Daniel Vetter
  Cc: moderated list:DMA BUFFER SHARING FRAMEWORK, Gustavo Padovan,
	dri-devel, open list:DMA BUFFER SHARING FRAMEWORK

Am 29.07.21 um 11:03 schrieb Daniel Vetter:
> On Thu, Jul 29, 2021 at 10:38 AM Christian König
> <ckoenig.leichtzumerken@gmail.com> wrote:
>> Am 29.07.21 um 09:23 schrieb Daniel Vetter:
>>> On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
>>>> As we now knew controlling dma_fence synchronization from userspace is
>>>> extremely dangerous and can not only deadlock drivers but trivially also the
>>>> whole kernel memory management.
>>>>
>>>> Entirely remove this option. We now have in kernel unit tests to exercise the
>>>> dma_fence framework and it's containers.
>>>>
>>>> Signed-off-by: Christian König <christian.koenig@amd.com>
>>> There's also igts for this, and Android heavily uses this. So I'm not sure
>>> we can just nuke it.
>> I feared that you would say that.
>>
>>> I guess what we could do is maybe just taint the kernel if it's ever used?
>> What should we use then? TAINT_SOFTLOCKUP or TAINT_CRAP? :)
> I think the cleanest solution would be to install a 10s timer on each
> sw fence, and TAINT_SOFTLOOKUP if it fails to signal. That's similar
> to what we do with vgem, except vgem then just steps in with a
> dma_fence_signal() instead of tainting the kernel.

Well vgem is only for testing as far as I know.

When Android is using this for a production environment I think we 
should be a bit harder and explicitly note that this is not a good idea 
in sw_sync_debugfs_open().

> I think that should keep everyone happy, because if your Android has a
> fence stuck for more than 10s it's busted.

Well when they are using userptrs they are busted as soon as they have a 
fence stuck for much less time.

Regards,
Christian.

>
> Note that with hwc1 the indefinite fence was actually the model, when
> you did a page_flip you got an out-fence back which will only be
> signalled when the new buffer _stopped_ being used for scanout. Which
> generally depends upon the next page-flip happening (except for manual
> mode display, which only scan out once, if you ignore that the
> kernel's supposed to restore the display after suspend/resume, which
> android did). hwc2 fixed this, so a 10s timeout-to-taint should be
> fine with Android people.
>
> Also since we do have the igt/sw_sync.c test intel-gfx-ci should be
> able to check that code even.
> -Daniel
>
>> Regards,
>> Christian.
>>
>>> Also for testing we use vgem now, which enforces a timeout.
>>> -Daniel
>>>
>>>> ---
>>>>    drivers/dma-buf/Kconfig      |  13 --
>>>>    drivers/dma-buf/Makefile     |   1 -
>>>>    drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
>>>>    drivers/dma-buf/sync_debug.c | 190 ----------------
>>>>    drivers/dma-buf/sync_debug.h |  72 ------
>>>>    5 files changed, 688 deletions(-)
>>>>    delete mode 100644 drivers/dma-buf/sw_sync.c
>>>>    delete mode 100644 drivers/dma-buf/sync_debug.c
>>>>    delete mode 100644 drivers/dma-buf/sync_debug.h
>>>>
>>>> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
>>>> index 9561e3d2d428..26c53f45616a 100644
>>>> --- a/drivers/dma-buf/Kconfig
>>>> +++ b/drivers/dma-buf/Kconfig
>>>> @@ -17,19 +17,6 @@ config SYNC_FILE
>>>>         Files fds, to the DRM driver for example. More details at
>>>>         Documentation/driver-api/sync_file.rst.
>>>>
>>>> -config SW_SYNC
>>>> -    bool "Sync File Validation Framework"
>>>> -    default n
>>>> -    depends on SYNC_FILE
>>>> -    depends on DEBUG_FS
>>>> -    help
>>>> -      A sync object driver that uses a 32bit counter to coordinate
>>>> -      synchronization.  Useful when there is no hardware primitive backing
>>>> -      the synchronization.
>>>> -
>>>> -      WARNING: improper use of this can result in deadlocking kernel
>>>> -      drivers from userspace. Intended for test and debug only.
>>>> -
>>>>    config UDMABUF
>>>>       bool "userspace dmabuf misc driver"
>>>>       default n
>>>> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
>>>> index 1ef021273a06..fa3af0953f87 100644
>>>> --- a/drivers/dma-buf/Makefile
>>>> +++ b/drivers/dma-buf/Makefile
>>>> @@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
>>>>    obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o
>>>>    obj-$(CONFIG_DMABUF_HEAPS) += heaps/
>>>>    obj-$(CONFIG_SYNC_FILE)            += sync_file.o
>>>> -obj-$(CONFIG_SW_SYNC)               += sw_sync.o sync_debug.o
>>>>    obj-$(CONFIG_UDMABUF)              += udmabuf.o
>>>>    obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
>>>>
>>>> diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
>>>> deleted file mode 100644
>>>> index 348b3a9170fa..000000000000
>>>> --- a/drivers/dma-buf/sw_sync.c
>>>> +++ /dev/null
>>>> @@ -1,412 +0,0 @@
>>>> -// SPDX-License-Identifier: GPL-2.0-only
>>>> -/*
>>>> - * Sync File validation framework
>>>> - *
>>>> - * Copyright (C) 2012 Google, Inc.
>>>> - */
>>>> -
>>>> -#include <linux/file.h>
>>>> -#include <linux/fs.h>
>>>> -#include <linux/uaccess.h>
>>>> -#include <linux/slab.h>
>>>> -#include <linux/sync_file.h>
>>>> -
>>>> -#include "sync_debug.h"
>>>> -
>>>> -#define CREATE_TRACE_POINTS
>>>> -#include "sync_trace.h"
>>>> -
>>>> -/*
>>>> - * SW SYNC validation framework
>>>> - *
>>>> - * A sync object driver that uses a 32bit counter to coordinate
>>>> - * synchronization.  Useful when there is no hardware primitive backing
>>>> - * the synchronization.
>>>> - *
>>>> - * To start the framework just open:
>>>> - *
>>>> - * <debugfs>/sync/sw_sync
>>>> - *
>>>> - * That will create a sync timeline, all fences created under this timeline
>>>> - * file descriptor will belong to the this timeline.
>>>> - *
>>>> - * The 'sw_sync' file can be opened many times as to create different
>>>> - * timelines.
>>>> - *
>>>> - * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
>>>> - * sw_sync_create_fence_data as parameter.
>>>> - *
>>>> - * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
>>>> - * with the increment as u32. This will update the last signaled value
>>>> - * from the timeline and signal any fence that has a seqno smaller or equal
>>>> - * to it.
>>>> - *
>>>> - * struct sw_sync_create_fence_data
>>>> - * @value:  the seqno to initialise the fence with
>>>> - * @name:   the name of the new sync point
>>>> - * @fence:  return the fd of the new sync_file with the created fence
>>>> - */
>>>> -struct sw_sync_create_fence_data {
>>>> -    __u32   value;
>>>> -    char    name[32];
>>>> -    __s32   fence; /* fd of new fence */
>>>> -};
>>>> -
>>>> -#define SW_SYNC_IOC_MAGIC   'W'
>>>> -
>>>> -#define SW_SYNC_IOC_CREATE_FENCE    _IOWR(SW_SYNC_IOC_MAGIC, 0,\
>>>> -            struct sw_sync_create_fence_data)
>>>> -
>>>> -#define SW_SYNC_IOC_INC                     _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
>>>> -
>>>> -static const struct dma_fence_ops timeline_fence_ops;
>>>> -
>>>> -static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
>>>> -{
>>>> -    if (fence->ops != &timeline_fence_ops)
>>>> -            return NULL;
>>>> -    return container_of(fence, struct sync_pt, base);
>>>> -}
>>>> -
>>>> -/**
>>>> - * sync_timeline_create() - creates a sync object
>>>> - * @name:   sync_timeline name
>>>> - *
>>>> - * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
>>>> - * case of error.
>>>> - */
>>>> -static struct sync_timeline *sync_timeline_create(const char *name)
>>>> -{
>>>> -    struct sync_timeline *obj;
>>>> -
>>>> -    obj = kzalloc(sizeof(*obj), GFP_KERNEL);
>>>> -    if (!obj)
>>>> -            return NULL;
>>>> -
>>>> -    kref_init(&obj->kref);
>>>> -    obj->context = dma_fence_context_alloc(1);
>>>> -    strlcpy(obj->name, name, sizeof(obj->name));
>>>> -
>>>> -    obj->pt_tree = RB_ROOT;
>>>> -    INIT_LIST_HEAD(&obj->pt_list);
>>>> -    spin_lock_init(&obj->lock);
>>>> -
>>>> -    sync_timeline_debug_add(obj);
>>>> -
>>>> -    return obj;
>>>> -}
>>>> -
>>>> -static void sync_timeline_free(struct kref *kref)
>>>> -{
>>>> -    struct sync_timeline *obj =
>>>> -            container_of(kref, struct sync_timeline, kref);
>>>> -
>>>> -    sync_timeline_debug_remove(obj);
>>>> -
>>>> -    kfree(obj);
>>>> -}
>>>> -
>>>> -static void sync_timeline_get(struct sync_timeline *obj)
>>>> -{
>>>> -    kref_get(&obj->kref);
>>>> -}
>>>> -
>>>> -static void sync_timeline_put(struct sync_timeline *obj)
>>>> -{
>>>> -    kref_put(&obj->kref, sync_timeline_free);
>>>> -}
>>>> -
>>>> -static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
>>>> -{
>>>> -    return "sw_sync";
>>>> -}
>>>> -
>>>> -static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
>>>> -{
>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>> -
>>>> -    return parent->name;
>>>> -}
>>>> -
>>>> -static void timeline_fence_release(struct dma_fence *fence)
>>>> -{
>>>> -    struct sync_pt *pt = dma_fence_to_sync_pt(fence);
>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>> -    unsigned long flags;
>>>> -
>>>> -    spin_lock_irqsave(fence->lock, flags);
>>>> -    if (!list_empty(&pt->link)) {
>>>> -            list_del(&pt->link);
>>>> -            rb_erase(&pt->node, &parent->pt_tree);
>>>> -    }
>>>> -    spin_unlock_irqrestore(fence->lock, flags);
>>>> -
>>>> -    sync_timeline_put(parent);
>>>> -    dma_fence_free(fence);
>>>> -}
>>>> -
>>>> -static bool timeline_fence_signaled(struct dma_fence *fence)
>>>> -{
>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>> -
>>>> -    return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
>>>> -}
>>>> -
>>>> -static bool timeline_fence_enable_signaling(struct dma_fence *fence)
>>>> -{
>>>> -    return true;
>>>> -}
>>>> -
>>>> -static void timeline_fence_value_str(struct dma_fence *fence,
>>>> -                                char *str, int size)
>>>> -{
>>>> -    snprintf(str, size, "%lld", fence->seqno);
>>>> -}
>>>> -
>>>> -static void timeline_fence_timeline_value_str(struct dma_fence *fence,
>>>> -                                         char *str, int size)
>>>> -{
>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>> -
>>>> -    snprintf(str, size, "%d", parent->value);
>>>> -}
>>>> -
>>>> -static const struct dma_fence_ops timeline_fence_ops = {
>>>> -    .get_driver_name = timeline_fence_get_driver_name,
>>>> -    .get_timeline_name = timeline_fence_get_timeline_name,
>>>> -    .enable_signaling = timeline_fence_enable_signaling,
>>>> -    .signaled = timeline_fence_signaled,
>>>> -    .release = timeline_fence_release,
>>>> -    .fence_value_str = timeline_fence_value_str,
>>>> -    .timeline_value_str = timeline_fence_timeline_value_str,
>>>> -};
>>>> -
>>>> -/**
>>>> - * sync_timeline_signal() - signal a status change on a sync_timeline
>>>> - * @obj:    sync_timeline to signal
>>>> - * @inc:    num to increment on timeline->value
>>>> - *
>>>> - * A sync implementation should call this any time one of it's fences
>>>> - * has signaled or has an error condition.
>>>> - */
>>>> -static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
>>>> -{
>>>> -    struct sync_pt *pt, *next;
>>>> -
>>>> -    trace_sync_timeline(obj);
>>>> -
>>>> -    spin_lock_irq(&obj->lock);
>>>> -
>>>> -    obj->value += inc;
>>>> -
>>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
>>>> -            if (!timeline_fence_signaled(&pt->base))
>>>> -                    break;
>>>> -
>>>> -            list_del_init(&pt->link);
>>>> -            rb_erase(&pt->node, &obj->pt_tree);
>>>> -
>>>> -            /*
>>>> -             * A signal callback may release the last reference to this
>>>> -             * fence, causing it to be freed. That operation has to be
>>>> -             * last to avoid a use after free inside this loop, and must
>>>> -             * be after we remove the fence from the timeline in order to
>>>> -             * prevent deadlocking on timeline->lock inside
>>>> -             * timeline_fence_release().
>>>> -             */
>>>> -            dma_fence_signal_locked(&pt->base);
>>>> -    }
>>>> -
>>>> -    spin_unlock_irq(&obj->lock);
>>>> -}
>>>> -
>>>> -/**
>>>> - * sync_pt_create() - creates a sync pt
>>>> - * @obj:    parent sync_timeline
>>>> - * @value:  value of the fence
>>>> - *
>>>> - * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
>>>> - * allocated allowing for implementation specific data to be kept after
>>>> - * the generic sync_timeline struct. Returns the sync_pt object or
>>>> - * NULL in case of error.
>>>> - */
>>>> -static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
>>>> -                                  unsigned int value)
>>>> -{
>>>> -    struct sync_pt *pt;
>>>> -
>>>> -    pt = kzalloc(sizeof(*pt), GFP_KERNEL);
>>>> -    if (!pt)
>>>> -            return NULL;
>>>> -
>>>> -    sync_timeline_get(obj);
>>>> -    dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
>>>> -                   obj->context, value);
>>>> -    INIT_LIST_HEAD(&pt->link);
>>>> -
>>>> -    spin_lock_irq(&obj->lock);
>>>> -    if (!dma_fence_is_signaled_locked(&pt->base)) {
>>>> -            struct rb_node **p = &obj->pt_tree.rb_node;
>>>> -            struct rb_node *parent = NULL;
>>>> -
>>>> -            while (*p) {
>>>> -                    struct sync_pt *other;
>>>> -                    int cmp;
>>>> -
>>>> -                    parent = *p;
>>>> -                    other = rb_entry(parent, typeof(*pt), node);
>>>> -                    cmp = value - other->base.seqno;
>>>> -                    if (cmp > 0) {
>>>> -                            p = &parent->rb_right;
>>>> -                    } else if (cmp < 0) {
>>>> -                            p = &parent->rb_left;
>>>> -                    } else {
>>>> -                            if (dma_fence_get_rcu(&other->base)) {
>>>> -                                    sync_timeline_put(obj);
>>>> -                                    kfree(pt);
>>>> -                                    pt = other;
>>>> -                                    goto unlock;
>>>> -                            }
>>>> -                            p = &parent->rb_left;
>>>> -                    }
>>>> -            }
>>>> -            rb_link_node(&pt->node, parent, p);
>>>> -            rb_insert_color(&pt->node, &obj->pt_tree);
>>>> -
>>>> -            parent = rb_next(&pt->node);
>>>> -            list_add_tail(&pt->link,
>>>> -                          parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
>>>> -    }
>>>> -unlock:
>>>> -    spin_unlock_irq(&obj->lock);
>>>> -
>>>> -    return pt;
>>>> -}
>>>> -
>>>> -/*
>>>> - * *WARNING*
>>>> - *
>>>> - * improper use of this can result in deadlocking kernel drivers from userspace.
>>>> - */
>>>> -
>>>> -/* opening sw_sync create a new sync obj */
>>>> -static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
>>>> -{
>>>> -    struct sync_timeline *obj;
>>>> -    char task_comm[TASK_COMM_LEN];
>>>> -
>>>> -    get_task_comm(task_comm, current);
>>>> -
>>>> -    obj = sync_timeline_create(task_comm);
>>>> -    if (!obj)
>>>> -            return -ENOMEM;
>>>> -
>>>> -    file->private_data = obj;
>>>> -
>>>> -    return 0;
>>>> -}
>>>> -
>>>> -static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
>>>> -{
>>>> -    struct sync_timeline *obj = file->private_data;
>>>> -    struct sync_pt *pt, *next;
>>>> -
>>>> -    spin_lock_irq(&obj->lock);
>>>> -
>>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
>>>> -            dma_fence_set_error(&pt->base, -ENOENT);
>>>> -            dma_fence_signal_locked(&pt->base);
>>>> -    }
>>>> -
>>>> -    spin_unlock_irq(&obj->lock);
>>>> -
>>>> -    sync_timeline_put(obj);
>>>> -    return 0;
>>>> -}
>>>> -
>>>> -static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
>>>> -                                   unsigned long arg)
>>>> -{
>>>> -    int fd = get_unused_fd_flags(O_CLOEXEC);
>>>> -    int err;
>>>> -    struct sync_pt *pt;
>>>> -    struct sync_file *sync_file;
>>>> -    struct sw_sync_create_fence_data data;
>>>> -
>>>> -    if (fd < 0)
>>>> -            return fd;
>>>> -
>>>> -    if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
>>>> -            err = -EFAULT;
>>>> -            goto err;
>>>> -    }
>>>> -
>>>> -    pt = sync_pt_create(obj, data.value);
>>>> -    if (!pt) {
>>>> -            err = -ENOMEM;
>>>> -            goto err;
>>>> -    }
>>>> -
>>>> -    sync_file = sync_file_create(&pt->base);
>>>> -    dma_fence_put(&pt->base);
>>>> -    if (!sync_file) {
>>>> -            err = -ENOMEM;
>>>> -            goto err;
>>>> -    }
>>>> -
>>>> -    data.fence = fd;
>>>> -    if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
>>>> -            fput(sync_file->file);
>>>> -            err = -EFAULT;
>>>> -            goto err;
>>>> -    }
>>>> -
>>>> -    fd_install(fd, sync_file->file);
>>>> -
>>>> -    return 0;
>>>> -
>>>> -err:
>>>> -    put_unused_fd(fd);
>>>> -    return err;
>>>> -}
>>>> -
>>>> -static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
>>>> -{
>>>> -    u32 value;
>>>> -
>>>> -    if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
>>>> -            return -EFAULT;
>>>> -
>>>> -    while (value > INT_MAX)  {
>>>> -            sync_timeline_signal(obj, INT_MAX);
>>>> -            value -= INT_MAX;
>>>> -    }
>>>> -
>>>> -    sync_timeline_signal(obj, value);
>>>> -
>>>> -    return 0;
>>>> -}
>>>> -
>>>> -static long sw_sync_ioctl(struct file *file, unsigned int cmd,
>>>> -                      unsigned long arg)
>>>> -{
>>>> -    struct sync_timeline *obj = file->private_data;
>>>> -
>>>> -    switch (cmd) {
>>>> -    case SW_SYNC_IOC_CREATE_FENCE:
>>>> -            return sw_sync_ioctl_create_fence(obj, arg);
>>>> -
>>>> -    case SW_SYNC_IOC_INC:
>>>> -            return sw_sync_ioctl_inc(obj, arg);
>>>> -
>>>> -    default:
>>>> -            return -ENOTTY;
>>>> -    }
>>>> -}
>>>> -
>>>> -const struct file_operations sw_sync_debugfs_fops = {
>>>> -    .open           = sw_sync_debugfs_open,
>>>> -    .release        = sw_sync_debugfs_release,
>>>> -    .unlocked_ioctl = sw_sync_ioctl,
>>>> -    .compat_ioctl   = compat_ptr_ioctl,
>>>> -};
>>>> diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
>>>> deleted file mode 100644
>>>> index 101394f16930..000000000000
>>>> --- a/drivers/dma-buf/sync_debug.c
>>>> +++ /dev/null
>>>> @@ -1,190 +0,0 @@
>>>> -// SPDX-License-Identifier: GPL-2.0-only
>>>> -/*
>>>> - * Sync File validation framework and debug information
>>>> - *
>>>> - * Copyright (C) 2012 Google, Inc.
>>>> - */
>>>> -
>>>> -#include <linux/debugfs.h>
>>>> -#include "sync_debug.h"
>>>> -
>>>> -static struct dentry *dbgfs;
>>>> -
>>>> -static LIST_HEAD(sync_timeline_list_head);
>>>> -static DEFINE_SPINLOCK(sync_timeline_list_lock);
>>>> -static LIST_HEAD(sync_file_list_head);
>>>> -static DEFINE_SPINLOCK(sync_file_list_lock);
>>>> -
>>>> -void sync_timeline_debug_add(struct sync_timeline *obj)
>>>> -{
>>>> -    unsigned long flags;
>>>> -
>>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
>>>> -    list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
>>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
>>>> -}
>>>> -
>>>> -void sync_timeline_debug_remove(struct sync_timeline *obj)
>>>> -{
>>>> -    unsigned long flags;
>>>> -
>>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
>>>> -    list_del(&obj->sync_timeline_list);
>>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
>>>> -}
>>>> -
>>>> -void sync_file_debug_add(struct sync_file *sync_file)
>>>> -{
>>>> -    unsigned long flags;
>>>> -
>>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
>>>> -    list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
>>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
>>>> -}
>>>> -
>>>> -void sync_file_debug_remove(struct sync_file *sync_file)
>>>> -{
>>>> -    unsigned long flags;
>>>> -
>>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
>>>> -    list_del(&sync_file->sync_file_list);
>>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
>>>> -}
>>>> -
>>>> -static const char *sync_status_str(int status)
>>>> -{
>>>> -    if (status < 0)
>>>> -            return "error";
>>>> -
>>>> -    if (status > 0)
>>>> -            return "signaled";
>>>> -
>>>> -    return "active";
>>>> -}
>>>> -
>>>> -static void sync_print_fence(struct seq_file *s,
>>>> -                         struct dma_fence *fence, bool show)
>>>> -{
>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>> -    int status;
>>>> -
>>>> -    status = dma_fence_get_status_locked(fence);
>>>> -
>>>> -    seq_printf(s, "  %s%sfence %s",
>>>> -               show ? parent->name : "",
>>>> -               show ? "_" : "",
>>>> -               sync_status_str(status));
>>>> -
>>>> -    if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
>>>> -            struct timespec64 ts64 =
>>>> -                    ktime_to_timespec64(fence->timestamp);
>>>> -
>>>> -            seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
>>>> -    }
>>>> -
>>>> -    if (fence->ops->timeline_value_str &&
>>>> -            fence->ops->fence_value_str) {
>>>> -            char value[64];
>>>> -            bool success;
>>>> -
>>>> -            fence->ops->fence_value_str(fence, value, sizeof(value));
>>>> -            success = strlen(value);
>>>> -
>>>> -            if (success) {
>>>> -                    seq_printf(s, ": %s", value);
>>>> -
>>>> -                    fence->ops->timeline_value_str(fence, value,
>>>> -                                                   sizeof(value));
>>>> -
>>>> -                    if (strlen(value))
>>>> -                            seq_printf(s, " / %s", value);
>>>> -            }
>>>> -    }
>>>> -
>>>> -    seq_putc(s, '\n');
>>>> -}
>>>> -
>>>> -static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
>>>> -{
>>>> -    struct list_head *pos;
>>>> -
>>>> -    seq_printf(s, "%s: %d\n", obj->name, obj->value);
>>>> -
>>>> -    spin_lock_irq(&obj->lock);
>>>> -    list_for_each(pos, &obj->pt_list) {
>>>> -            struct sync_pt *pt = container_of(pos, struct sync_pt, link);
>>>> -            sync_print_fence(s, &pt->base, false);
>>>> -    }
>>>> -    spin_unlock_irq(&obj->lock);
>>>> -}
>>>> -
>>>> -static void sync_print_sync_file(struct seq_file *s,
>>>> -                              struct sync_file *sync_file)
>>>> -{
>>>> -    char buf[128];
>>>> -    int i;
>>>> -
>>>> -    seq_printf(s, "[%p] %s: %s\n", sync_file,
>>>> -               sync_file_get_name(sync_file, buf, sizeof(buf)),
>>>> -               sync_status_str(dma_fence_get_status(sync_file->fence)));
>>>> -
>>>> -    if (dma_fence_is_array(sync_file->fence)) {
>>>> -            struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
>>>> -
>>>> -            for (i = 0; i < array->num_fences; ++i)
>>>> -                    sync_print_fence(s, array->fences[i], true);
>>>> -    } else {
>>>> -            sync_print_fence(s, sync_file->fence, true);
>>>> -    }
>>>> -}
>>>> -
>>>> -static int sync_info_debugfs_show(struct seq_file *s, void *unused)
>>>> -{
>>>> -    struct list_head *pos;
>>>> -
>>>> -    seq_puts(s, "objs:\n--------------\n");
>>>> -
>>>> -    spin_lock_irq(&sync_timeline_list_lock);
>>>> -    list_for_each(pos, &sync_timeline_list_head) {
>>>> -            struct sync_timeline *obj =
>>>> -                    container_of(pos, struct sync_timeline,
>>>> -                                 sync_timeline_list);
>>>> -
>>>> -            sync_print_obj(s, obj);
>>>> -            seq_putc(s, '\n');
>>>> -    }
>>>> -    spin_unlock_irq(&sync_timeline_list_lock);
>>>> -
>>>> -    seq_puts(s, "fences:\n--------------\n");
>>>> -
>>>> -    spin_lock_irq(&sync_file_list_lock);
>>>> -    list_for_each(pos, &sync_file_list_head) {
>>>> -            struct sync_file *sync_file =
>>>> -                    container_of(pos, struct sync_file, sync_file_list);
>>>> -
>>>> -            sync_print_sync_file(s, sync_file);
>>>> -            seq_putc(s, '\n');
>>>> -    }
>>>> -    spin_unlock_irq(&sync_file_list_lock);
>>>> -    return 0;
>>>> -}
>>>> -
>>>> -DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
>>>> -
>>>> -static __init int sync_debugfs_init(void)
>>>> -{
>>>> -    dbgfs = debugfs_create_dir("sync", NULL);
>>>> -
>>>> -    /*
>>>> -     * The debugfs files won't ever get removed and thus, there is
>>>> -     * no need to protect it against removal races. The use of
>>>> -     * debugfs_create_file_unsafe() is actually safe here.
>>>> -     */
>>>> -    debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
>>>> -                               &sync_info_debugfs_fops);
>>>> -    debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
>>>> -                               &sw_sync_debugfs_fops);
>>>> -
>>>> -    return 0;
>>>> -}
>>>> -late_initcall(sync_debugfs_init);
>>>> diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
>>>> deleted file mode 100644
>>>> index 6176e52ba2d7..000000000000
>>>> --- a/drivers/dma-buf/sync_debug.h
>>>> +++ /dev/null
>>>> @@ -1,72 +0,0 @@
>>>> -/*
>>>> - * Sync File validation framework and debug infomation
>>>> - *
>>>> - * Copyright (C) 2012 Google, Inc.
>>>> - *
>>>> - * This program is distributed in the hope that it will be useful,
>>>> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
>>>> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>>>> - * GNU General Public License for more details.
>>>> - *
>>>> - */
>>>> -
>>>> -#ifndef _LINUX_SYNC_H
>>>> -#define _LINUX_SYNC_H
>>>> -
>>>> -#include <linux/list.h>
>>>> -#include <linux/rbtree.h>
>>>> -#include <linux/spinlock.h>
>>>> -#include <linux/dma-fence.h>
>>>> -
>>>> -#include <linux/sync_file.h>
>>>> -#include <uapi/linux/sync_file.h>
>>>> -
>>>> -/**
>>>> - * struct sync_timeline - sync object
>>>> - * @kref:           reference count on fence.
>>>> - * @name:           name of the sync_timeline. Useful for debugging
>>>> - * @lock:           lock protecting @pt_list and @value
>>>> - * @pt_tree:                rbtree of active (unsignaled/errored) sync_pts
>>>> - * @pt_list:                list of active (unsignaled/errored) sync_pts
>>>> - * @sync_timeline_list:     membership in global sync_timeline_list
>>>> - */
>>>> -struct sync_timeline {
>>>> -    struct kref             kref;
>>>> -    char                    name[32];
>>>> -
>>>> -    /* protected by lock */
>>>> -    u64                     context;
>>>> -    int                     value;
>>>> -
>>>> -    struct rb_root          pt_tree;
>>>> -    struct list_head        pt_list;
>>>> -    spinlock_t              lock;
>>>> -
>>>> -    struct list_head        sync_timeline_list;
>>>> -};
>>>> -
>>>> -static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
>>>> -{
>>>> -    return container_of(fence->lock, struct sync_timeline, lock);
>>>> -}
>>>> -
>>>> -/**
>>>> - * struct sync_pt - sync_pt object
>>>> - * @base: base fence object
>>>> - * @link: link on the sync timeline's list
>>>> - * @node: node in the sync timeline's tree
>>>> - */
>>>> -struct sync_pt {
>>>> -    struct dma_fence base;
>>>> -    struct list_head link;
>>>> -    struct rb_node node;
>>>> -};
>>>> -
>>>> -extern const struct file_operations sw_sync_debugfs_fops;
>>>> -
>>>> -void sync_timeline_debug_add(struct sync_timeline *obj);
>>>> -void sync_timeline_debug_remove(struct sync_timeline *obj);
>>>> -void sync_file_debug_add(struct sync_file *fence);
>>>> -void sync_file_debug_remove(struct sync_file *fence);
>>>> -
>>>> -#endif /* _LINUX_SYNC_H */
>>>> --
>>>> 2.25.1
>>>>
>


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
  2021-07-29 10:21           ` Christian König
@ 2021-07-29 11:59             ` Daniel Vetter
  -1 siblings, 0 replies; 26+ messages in thread
From: Daniel Vetter @ 2021-07-29 11:59 UTC (permalink / raw)
  To: Christian König
  Cc: Sumit Semwal, Gustavo Padovan,
	open list:DMA BUFFER SHARING FRAMEWORK, dri-devel,
	moderated list:DMA BUFFER SHARING FRAMEWORK

On Thu, Jul 29, 2021 at 12:21 PM Christian König
<ckoenig.leichtzumerken@gmail.com> wrote:
> Am 29.07.21 um 11:03 schrieb Daniel Vetter:
> > On Thu, Jul 29, 2021 at 10:38 AM Christian König
> > <ckoenig.leichtzumerken@gmail.com> wrote:
> >> Am 29.07.21 um 09:23 schrieb Daniel Vetter:
> >>> On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
> >>>> As we now knew controlling dma_fence synchronization from userspace is
> >>>> extremely dangerous and can not only deadlock drivers but trivially also the
> >>>> whole kernel memory management.
> >>>>
> >>>> Entirely remove this option. We now have in kernel unit tests to exercise the
> >>>> dma_fence framework and it's containers.
> >>>>
> >>>> Signed-off-by: Christian König <christian.koenig@amd.com>
> >>> There's also igts for this, and Android heavily uses this. So I'm not sure
> >>> we can just nuke it.
> >> I feared that you would say that.
> >>
> >>> I guess what we could do is maybe just taint the kernel if it's ever used?
> >> What should we use then? TAINT_SOFTLOCKUP or TAINT_CRAP? :)
> > I think the cleanest solution would be to install a 10s timer on each
> > sw fence, and TAINT_SOFTLOOKUP if it fails to signal. That's similar
> > to what we do with vgem, except vgem then just steps in with a
> > dma_fence_signal() instead of tainting the kernel.
>
> Well vgem is only for testing as far as I know.

I think CrOS is using it to shovel buffers around between process, as
a convenient allocator for sw rendering dma-buf. But they don't use
the dma-fence part afaik.

We can't taint in vgem fences though because our CI reboots on any
taint (or lockdep splat) and a few others, so that would make it
useless for testing. Maybe we should have the fence part of vgem
behind a separate Kconfig?

> When Android is using this for a production environment I think we
> should be a bit harder and explicitly note that this is not a good idea
> in sw_sync_debugfs_open().
>
> > I think that should keep everyone happy, because if your Android has a
> > fence stuck for more than 10s it's busted.
>
> Well when they are using userptrs they are busted as soon as they have a
> fence stuck for much less time.

Hm right I forgot that you can't guarantee that it will complete, ever :-)

Otoh I'm not sure the unconditional taint is very nice to the android
folks, they'll probably just patch it out and then it looses it
usefulness. taint on timeout otoh makes sure that normally it's not
hit, but anytime you die in userptr or shrinker there will be a taint
before anything else (since 10s is much shorter than the "stuck in
non-interruptible sleep" warning).
-Daniel

> Regards,
> Christian.
>
> >
> > Note that with hwc1 the indefinite fence was actually the model, when
> > you did a page_flip you got an out-fence back which will only be
> > signalled when the new buffer _stopped_ being used for scanout. Which
> > generally depends upon the next page-flip happening (except for manual
> > mode display, which only scan out once, if you ignore that the
> > kernel's supposed to restore the display after suspend/resume, which
> > android did). hwc2 fixed this, so a 10s timeout-to-taint should be
> > fine with Android people.
> >
> > Also since we do have the igt/sw_sync.c test intel-gfx-ci should be
> > able to check that code even.
> > -Daniel
> >
> >> Regards,
> >> Christian.
> >>
> >>> Also for testing we use vgem now, which enforces a timeout.
> >>> -Daniel
> >>>
> >>>> ---
> >>>>    drivers/dma-buf/Kconfig      |  13 --
> >>>>    drivers/dma-buf/Makefile     |   1 -
> >>>>    drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
> >>>>    drivers/dma-buf/sync_debug.c | 190 ----------------
> >>>>    drivers/dma-buf/sync_debug.h |  72 ------
> >>>>    5 files changed, 688 deletions(-)
> >>>>    delete mode 100644 drivers/dma-buf/sw_sync.c
> >>>>    delete mode 100644 drivers/dma-buf/sync_debug.c
> >>>>    delete mode 100644 drivers/dma-buf/sync_debug.h
> >>>>
> >>>> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
> >>>> index 9561e3d2d428..26c53f45616a 100644
> >>>> --- a/drivers/dma-buf/Kconfig
> >>>> +++ b/drivers/dma-buf/Kconfig
> >>>> @@ -17,19 +17,6 @@ config SYNC_FILE
> >>>>         Files fds, to the DRM driver for example. More details at
> >>>>         Documentation/driver-api/sync_file.rst.
> >>>>
> >>>> -config SW_SYNC
> >>>> -    bool "Sync File Validation Framework"
> >>>> -    default n
> >>>> -    depends on SYNC_FILE
> >>>> -    depends on DEBUG_FS
> >>>> -    help
> >>>> -      A sync object driver that uses a 32bit counter to coordinate
> >>>> -      synchronization.  Useful when there is no hardware primitive backing
> >>>> -      the synchronization.
> >>>> -
> >>>> -      WARNING: improper use of this can result in deadlocking kernel
> >>>> -      drivers from userspace. Intended for test and debug only.
> >>>> -
> >>>>    config UDMABUF
> >>>>       bool "userspace dmabuf misc driver"
> >>>>       default n
> >>>> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
> >>>> index 1ef021273a06..fa3af0953f87 100644
> >>>> --- a/drivers/dma-buf/Makefile
> >>>> +++ b/drivers/dma-buf/Makefile
> >>>> @@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
> >>>>    obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o
> >>>>    obj-$(CONFIG_DMABUF_HEAPS) += heaps/
> >>>>    obj-$(CONFIG_SYNC_FILE)            += sync_file.o
> >>>> -obj-$(CONFIG_SW_SYNC)               += sw_sync.o sync_debug.o
> >>>>    obj-$(CONFIG_UDMABUF)              += udmabuf.o
> >>>>    obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
> >>>>
> >>>> diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
> >>>> deleted file mode 100644
> >>>> index 348b3a9170fa..000000000000
> >>>> --- a/drivers/dma-buf/sw_sync.c
> >>>> +++ /dev/null
> >>>> @@ -1,412 +0,0 @@
> >>>> -// SPDX-License-Identifier: GPL-2.0-only
> >>>> -/*
> >>>> - * Sync File validation framework
> >>>> - *
> >>>> - * Copyright (C) 2012 Google, Inc.
> >>>> - */
> >>>> -
> >>>> -#include <linux/file.h>
> >>>> -#include <linux/fs.h>
> >>>> -#include <linux/uaccess.h>
> >>>> -#include <linux/slab.h>
> >>>> -#include <linux/sync_file.h>
> >>>> -
> >>>> -#include "sync_debug.h"
> >>>> -
> >>>> -#define CREATE_TRACE_POINTS
> >>>> -#include "sync_trace.h"
> >>>> -
> >>>> -/*
> >>>> - * SW SYNC validation framework
> >>>> - *
> >>>> - * A sync object driver that uses a 32bit counter to coordinate
> >>>> - * synchronization.  Useful when there is no hardware primitive backing
> >>>> - * the synchronization.
> >>>> - *
> >>>> - * To start the framework just open:
> >>>> - *
> >>>> - * <debugfs>/sync/sw_sync
> >>>> - *
> >>>> - * That will create a sync timeline, all fences created under this timeline
> >>>> - * file descriptor will belong to the this timeline.
> >>>> - *
> >>>> - * The 'sw_sync' file can be opened many times as to create different
> >>>> - * timelines.
> >>>> - *
> >>>> - * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
> >>>> - * sw_sync_create_fence_data as parameter.
> >>>> - *
> >>>> - * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
> >>>> - * with the increment as u32. This will update the last signaled value
> >>>> - * from the timeline and signal any fence that has a seqno smaller or equal
> >>>> - * to it.
> >>>> - *
> >>>> - * struct sw_sync_create_fence_data
> >>>> - * @value:  the seqno to initialise the fence with
> >>>> - * @name:   the name of the new sync point
> >>>> - * @fence:  return the fd of the new sync_file with the created fence
> >>>> - */
> >>>> -struct sw_sync_create_fence_data {
> >>>> -    __u32   value;
> >>>> -    char    name[32];
> >>>> -    __s32   fence; /* fd of new fence */
> >>>> -};
> >>>> -
> >>>> -#define SW_SYNC_IOC_MAGIC   'W'
> >>>> -
> >>>> -#define SW_SYNC_IOC_CREATE_FENCE    _IOWR(SW_SYNC_IOC_MAGIC, 0,\
> >>>> -            struct sw_sync_create_fence_data)
> >>>> -
> >>>> -#define SW_SYNC_IOC_INC                     _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
> >>>> -
> >>>> -static const struct dma_fence_ops timeline_fence_ops;
> >>>> -
> >>>> -static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
> >>>> -{
> >>>> -    if (fence->ops != &timeline_fence_ops)
> >>>> -            return NULL;
> >>>> -    return container_of(fence, struct sync_pt, base);
> >>>> -}
> >>>> -
> >>>> -/**
> >>>> - * sync_timeline_create() - creates a sync object
> >>>> - * @name:   sync_timeline name
> >>>> - *
> >>>> - * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
> >>>> - * case of error.
> >>>> - */
> >>>> -static struct sync_timeline *sync_timeline_create(const char *name)
> >>>> -{
> >>>> -    struct sync_timeline *obj;
> >>>> -
> >>>> -    obj = kzalloc(sizeof(*obj), GFP_KERNEL);
> >>>> -    if (!obj)
> >>>> -            return NULL;
> >>>> -
> >>>> -    kref_init(&obj->kref);
> >>>> -    obj->context = dma_fence_context_alloc(1);
> >>>> -    strlcpy(obj->name, name, sizeof(obj->name));
> >>>> -
> >>>> -    obj->pt_tree = RB_ROOT;
> >>>> -    INIT_LIST_HEAD(&obj->pt_list);
> >>>> -    spin_lock_init(&obj->lock);
> >>>> -
> >>>> -    sync_timeline_debug_add(obj);
> >>>> -
> >>>> -    return obj;
> >>>> -}
> >>>> -
> >>>> -static void sync_timeline_free(struct kref *kref)
> >>>> -{
> >>>> -    struct sync_timeline *obj =
> >>>> -            container_of(kref, struct sync_timeline, kref);
> >>>> -
> >>>> -    sync_timeline_debug_remove(obj);
> >>>> -
> >>>> -    kfree(obj);
> >>>> -}
> >>>> -
> >>>> -static void sync_timeline_get(struct sync_timeline *obj)
> >>>> -{
> >>>> -    kref_get(&obj->kref);
> >>>> -}
> >>>> -
> >>>> -static void sync_timeline_put(struct sync_timeline *obj)
> >>>> -{
> >>>> -    kref_put(&obj->kref, sync_timeline_free);
> >>>> -}
> >>>> -
> >>>> -static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
> >>>> -{
> >>>> -    return "sw_sync";
> >>>> -}
> >>>> -
> >>>> -static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
> >>>> -{
> >>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>> -
> >>>> -    return parent->name;
> >>>> -}
> >>>> -
> >>>> -static void timeline_fence_release(struct dma_fence *fence)
> >>>> -{
> >>>> -    struct sync_pt *pt = dma_fence_to_sync_pt(fence);
> >>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>> -    unsigned long flags;
> >>>> -
> >>>> -    spin_lock_irqsave(fence->lock, flags);
> >>>> -    if (!list_empty(&pt->link)) {
> >>>> -            list_del(&pt->link);
> >>>> -            rb_erase(&pt->node, &parent->pt_tree);
> >>>> -    }
> >>>> -    spin_unlock_irqrestore(fence->lock, flags);
> >>>> -
> >>>> -    sync_timeline_put(parent);
> >>>> -    dma_fence_free(fence);
> >>>> -}
> >>>> -
> >>>> -static bool timeline_fence_signaled(struct dma_fence *fence)
> >>>> -{
> >>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>> -
> >>>> -    return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
> >>>> -}
> >>>> -
> >>>> -static bool timeline_fence_enable_signaling(struct dma_fence *fence)
> >>>> -{
> >>>> -    return true;
> >>>> -}
> >>>> -
> >>>> -static void timeline_fence_value_str(struct dma_fence *fence,
> >>>> -                                char *str, int size)
> >>>> -{
> >>>> -    snprintf(str, size, "%lld", fence->seqno);
> >>>> -}
> >>>> -
> >>>> -static void timeline_fence_timeline_value_str(struct dma_fence *fence,
> >>>> -                                         char *str, int size)
> >>>> -{
> >>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>> -
> >>>> -    snprintf(str, size, "%d", parent->value);
> >>>> -}
> >>>> -
> >>>> -static const struct dma_fence_ops timeline_fence_ops = {
> >>>> -    .get_driver_name = timeline_fence_get_driver_name,
> >>>> -    .get_timeline_name = timeline_fence_get_timeline_name,
> >>>> -    .enable_signaling = timeline_fence_enable_signaling,
> >>>> -    .signaled = timeline_fence_signaled,
> >>>> -    .release = timeline_fence_release,
> >>>> -    .fence_value_str = timeline_fence_value_str,
> >>>> -    .timeline_value_str = timeline_fence_timeline_value_str,
> >>>> -};
> >>>> -
> >>>> -/**
> >>>> - * sync_timeline_signal() - signal a status change on a sync_timeline
> >>>> - * @obj:    sync_timeline to signal
> >>>> - * @inc:    num to increment on timeline->value
> >>>> - *
> >>>> - * A sync implementation should call this any time one of it's fences
> >>>> - * has signaled or has an error condition.
> >>>> - */
> >>>> -static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
> >>>> -{
> >>>> -    struct sync_pt *pt, *next;
> >>>> -
> >>>> -    trace_sync_timeline(obj);
> >>>> -
> >>>> -    spin_lock_irq(&obj->lock);
> >>>> -
> >>>> -    obj->value += inc;
> >>>> -
> >>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> >>>> -            if (!timeline_fence_signaled(&pt->base))
> >>>> -                    break;
> >>>> -
> >>>> -            list_del_init(&pt->link);
> >>>> -            rb_erase(&pt->node, &obj->pt_tree);
> >>>> -
> >>>> -            /*
> >>>> -             * A signal callback may release the last reference to this
> >>>> -             * fence, causing it to be freed. That operation has to be
> >>>> -             * last to avoid a use after free inside this loop, and must
> >>>> -             * be after we remove the fence from the timeline in order to
> >>>> -             * prevent deadlocking on timeline->lock inside
> >>>> -             * timeline_fence_release().
> >>>> -             */
> >>>> -            dma_fence_signal_locked(&pt->base);
> >>>> -    }
> >>>> -
> >>>> -    spin_unlock_irq(&obj->lock);
> >>>> -}
> >>>> -
> >>>> -/**
> >>>> - * sync_pt_create() - creates a sync pt
> >>>> - * @obj:    parent sync_timeline
> >>>> - * @value:  value of the fence
> >>>> - *
> >>>> - * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
> >>>> - * allocated allowing for implementation specific data to be kept after
> >>>> - * the generic sync_timeline struct. Returns the sync_pt object or
> >>>> - * NULL in case of error.
> >>>> - */
> >>>> -static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
> >>>> -                                  unsigned int value)
> >>>> -{
> >>>> -    struct sync_pt *pt;
> >>>> -
> >>>> -    pt = kzalloc(sizeof(*pt), GFP_KERNEL);
> >>>> -    if (!pt)
> >>>> -            return NULL;
> >>>> -
> >>>> -    sync_timeline_get(obj);
> >>>> -    dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
> >>>> -                   obj->context, value);
> >>>> -    INIT_LIST_HEAD(&pt->link);
> >>>> -
> >>>> -    spin_lock_irq(&obj->lock);
> >>>> -    if (!dma_fence_is_signaled_locked(&pt->base)) {
> >>>> -            struct rb_node **p = &obj->pt_tree.rb_node;
> >>>> -            struct rb_node *parent = NULL;
> >>>> -
> >>>> -            while (*p) {
> >>>> -                    struct sync_pt *other;
> >>>> -                    int cmp;
> >>>> -
> >>>> -                    parent = *p;
> >>>> -                    other = rb_entry(parent, typeof(*pt), node);
> >>>> -                    cmp = value - other->base.seqno;
> >>>> -                    if (cmp > 0) {
> >>>> -                            p = &parent->rb_right;
> >>>> -                    } else if (cmp < 0) {
> >>>> -                            p = &parent->rb_left;
> >>>> -                    } else {
> >>>> -                            if (dma_fence_get_rcu(&other->base)) {
> >>>> -                                    sync_timeline_put(obj);
> >>>> -                                    kfree(pt);
> >>>> -                                    pt = other;
> >>>> -                                    goto unlock;
> >>>> -                            }
> >>>> -                            p = &parent->rb_left;
> >>>> -                    }
> >>>> -            }
> >>>> -            rb_link_node(&pt->node, parent, p);
> >>>> -            rb_insert_color(&pt->node, &obj->pt_tree);
> >>>> -
> >>>> -            parent = rb_next(&pt->node);
> >>>> -            list_add_tail(&pt->link,
> >>>> -                          parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
> >>>> -    }
> >>>> -unlock:
> >>>> -    spin_unlock_irq(&obj->lock);
> >>>> -
> >>>> -    return pt;
> >>>> -}
> >>>> -
> >>>> -/*
> >>>> - * *WARNING*
> >>>> - *
> >>>> - * improper use of this can result in deadlocking kernel drivers from userspace.
> >>>> - */
> >>>> -
> >>>> -/* opening sw_sync create a new sync obj */
> >>>> -static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
> >>>> -{
> >>>> -    struct sync_timeline *obj;
> >>>> -    char task_comm[TASK_COMM_LEN];
> >>>> -
> >>>> -    get_task_comm(task_comm, current);
> >>>> -
> >>>> -    obj = sync_timeline_create(task_comm);
> >>>> -    if (!obj)
> >>>> -            return -ENOMEM;
> >>>> -
> >>>> -    file->private_data = obj;
> >>>> -
> >>>> -    return 0;
> >>>> -}
> >>>> -
> >>>> -static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
> >>>> -{
> >>>> -    struct sync_timeline *obj = file->private_data;
> >>>> -    struct sync_pt *pt, *next;
> >>>> -
> >>>> -    spin_lock_irq(&obj->lock);
> >>>> -
> >>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> >>>> -            dma_fence_set_error(&pt->base, -ENOENT);
> >>>> -            dma_fence_signal_locked(&pt->base);
> >>>> -    }
> >>>> -
> >>>> -    spin_unlock_irq(&obj->lock);
> >>>> -
> >>>> -    sync_timeline_put(obj);
> >>>> -    return 0;
> >>>> -}
> >>>> -
> >>>> -static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
> >>>> -                                   unsigned long arg)
> >>>> -{
> >>>> -    int fd = get_unused_fd_flags(O_CLOEXEC);
> >>>> -    int err;
> >>>> -    struct sync_pt *pt;
> >>>> -    struct sync_file *sync_file;
> >>>> -    struct sw_sync_create_fence_data data;
> >>>> -
> >>>> -    if (fd < 0)
> >>>> -            return fd;
> >>>> -
> >>>> -    if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
> >>>> -            err = -EFAULT;
> >>>> -            goto err;
> >>>> -    }
> >>>> -
> >>>> -    pt = sync_pt_create(obj, data.value);
> >>>> -    if (!pt) {
> >>>> -            err = -ENOMEM;
> >>>> -            goto err;
> >>>> -    }
> >>>> -
> >>>> -    sync_file = sync_file_create(&pt->base);
> >>>> -    dma_fence_put(&pt->base);
> >>>> -    if (!sync_file) {
> >>>> -            err = -ENOMEM;
> >>>> -            goto err;
> >>>> -    }
> >>>> -
> >>>> -    data.fence = fd;
> >>>> -    if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
> >>>> -            fput(sync_file->file);
> >>>> -            err = -EFAULT;
> >>>> -            goto err;
> >>>> -    }
> >>>> -
> >>>> -    fd_install(fd, sync_file->file);
> >>>> -
> >>>> -    return 0;
> >>>> -
> >>>> -err:
> >>>> -    put_unused_fd(fd);
> >>>> -    return err;
> >>>> -}
> >>>> -
> >>>> -static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
> >>>> -{
> >>>> -    u32 value;
> >>>> -
> >>>> -    if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
> >>>> -            return -EFAULT;
> >>>> -
> >>>> -    while (value > INT_MAX)  {
> >>>> -            sync_timeline_signal(obj, INT_MAX);
> >>>> -            value -= INT_MAX;
> >>>> -    }
> >>>> -
> >>>> -    sync_timeline_signal(obj, value);
> >>>> -
> >>>> -    return 0;
> >>>> -}
> >>>> -
> >>>> -static long sw_sync_ioctl(struct file *file, unsigned int cmd,
> >>>> -                      unsigned long arg)
> >>>> -{
> >>>> -    struct sync_timeline *obj = file->private_data;
> >>>> -
> >>>> -    switch (cmd) {
> >>>> -    case SW_SYNC_IOC_CREATE_FENCE:
> >>>> -            return sw_sync_ioctl_create_fence(obj, arg);
> >>>> -
> >>>> -    case SW_SYNC_IOC_INC:
> >>>> -            return sw_sync_ioctl_inc(obj, arg);
> >>>> -
> >>>> -    default:
> >>>> -            return -ENOTTY;
> >>>> -    }
> >>>> -}
> >>>> -
> >>>> -const struct file_operations sw_sync_debugfs_fops = {
> >>>> -    .open           = sw_sync_debugfs_open,
> >>>> -    .release        = sw_sync_debugfs_release,
> >>>> -    .unlocked_ioctl = sw_sync_ioctl,
> >>>> -    .compat_ioctl   = compat_ptr_ioctl,
> >>>> -};
> >>>> diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
> >>>> deleted file mode 100644
> >>>> index 101394f16930..000000000000
> >>>> --- a/drivers/dma-buf/sync_debug.c
> >>>> +++ /dev/null
> >>>> @@ -1,190 +0,0 @@
> >>>> -// SPDX-License-Identifier: GPL-2.0-only
> >>>> -/*
> >>>> - * Sync File validation framework and debug information
> >>>> - *
> >>>> - * Copyright (C) 2012 Google, Inc.
> >>>> - */
> >>>> -
> >>>> -#include <linux/debugfs.h>
> >>>> -#include "sync_debug.h"
> >>>> -
> >>>> -static struct dentry *dbgfs;
> >>>> -
> >>>> -static LIST_HEAD(sync_timeline_list_head);
> >>>> -static DEFINE_SPINLOCK(sync_timeline_list_lock);
> >>>> -static LIST_HEAD(sync_file_list_head);
> >>>> -static DEFINE_SPINLOCK(sync_file_list_lock);
> >>>> -
> >>>> -void sync_timeline_debug_add(struct sync_timeline *obj)
> >>>> -{
> >>>> -    unsigned long flags;
> >>>> -
> >>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
> >>>> -    list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
> >>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> >>>> -}
> >>>> -
> >>>> -void sync_timeline_debug_remove(struct sync_timeline *obj)
> >>>> -{
> >>>> -    unsigned long flags;
> >>>> -
> >>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
> >>>> -    list_del(&obj->sync_timeline_list);
> >>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> >>>> -}
> >>>> -
> >>>> -void sync_file_debug_add(struct sync_file *sync_file)
> >>>> -{
> >>>> -    unsigned long flags;
> >>>> -
> >>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
> >>>> -    list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
> >>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
> >>>> -}
> >>>> -
> >>>> -void sync_file_debug_remove(struct sync_file *sync_file)
> >>>> -{
> >>>> -    unsigned long flags;
> >>>> -
> >>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
> >>>> -    list_del(&sync_file->sync_file_list);
> >>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
> >>>> -}
> >>>> -
> >>>> -static const char *sync_status_str(int status)
> >>>> -{
> >>>> -    if (status < 0)
> >>>> -            return "error";
> >>>> -
> >>>> -    if (status > 0)
> >>>> -            return "signaled";
> >>>> -
> >>>> -    return "active";
> >>>> -}
> >>>> -
> >>>> -static void sync_print_fence(struct seq_file *s,
> >>>> -                         struct dma_fence *fence, bool show)
> >>>> -{
> >>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>> -    int status;
> >>>> -
> >>>> -    status = dma_fence_get_status_locked(fence);
> >>>> -
> >>>> -    seq_printf(s, "  %s%sfence %s",
> >>>> -               show ? parent->name : "",
> >>>> -               show ? "_" : "",
> >>>> -               sync_status_str(status));
> >>>> -
> >>>> -    if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
> >>>> -            struct timespec64 ts64 =
> >>>> -                    ktime_to_timespec64(fence->timestamp);
> >>>> -
> >>>> -            seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
> >>>> -    }
> >>>> -
> >>>> -    if (fence->ops->timeline_value_str &&
> >>>> -            fence->ops->fence_value_str) {
> >>>> -            char value[64];
> >>>> -            bool success;
> >>>> -
> >>>> -            fence->ops->fence_value_str(fence, value, sizeof(value));
> >>>> -            success = strlen(value);
> >>>> -
> >>>> -            if (success) {
> >>>> -                    seq_printf(s, ": %s", value);
> >>>> -
> >>>> -                    fence->ops->timeline_value_str(fence, value,
> >>>> -                                                   sizeof(value));
> >>>> -
> >>>> -                    if (strlen(value))
> >>>> -                            seq_printf(s, " / %s", value);
> >>>> -            }
> >>>> -    }
> >>>> -
> >>>> -    seq_putc(s, '\n');
> >>>> -}
> >>>> -
> >>>> -static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
> >>>> -{
> >>>> -    struct list_head *pos;
> >>>> -
> >>>> -    seq_printf(s, "%s: %d\n", obj->name, obj->value);
> >>>> -
> >>>> -    spin_lock_irq(&obj->lock);
> >>>> -    list_for_each(pos, &obj->pt_list) {
> >>>> -            struct sync_pt *pt = container_of(pos, struct sync_pt, link);
> >>>> -            sync_print_fence(s, &pt->base, false);
> >>>> -    }
> >>>> -    spin_unlock_irq(&obj->lock);
> >>>> -}
> >>>> -
> >>>> -static void sync_print_sync_file(struct seq_file *s,
> >>>> -                              struct sync_file *sync_file)
> >>>> -{
> >>>> -    char buf[128];
> >>>> -    int i;
> >>>> -
> >>>> -    seq_printf(s, "[%p] %s: %s\n", sync_file,
> >>>> -               sync_file_get_name(sync_file, buf, sizeof(buf)),
> >>>> -               sync_status_str(dma_fence_get_status(sync_file->fence)));
> >>>> -
> >>>> -    if (dma_fence_is_array(sync_file->fence)) {
> >>>> -            struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
> >>>> -
> >>>> -            for (i = 0; i < array->num_fences; ++i)
> >>>> -                    sync_print_fence(s, array->fences[i], true);
> >>>> -    } else {
> >>>> -            sync_print_fence(s, sync_file->fence, true);
> >>>> -    }
> >>>> -}
> >>>> -
> >>>> -static int sync_info_debugfs_show(struct seq_file *s, void *unused)
> >>>> -{
> >>>> -    struct list_head *pos;
> >>>> -
> >>>> -    seq_puts(s, "objs:\n--------------\n");
> >>>> -
> >>>> -    spin_lock_irq(&sync_timeline_list_lock);
> >>>> -    list_for_each(pos, &sync_timeline_list_head) {
> >>>> -            struct sync_timeline *obj =
> >>>> -                    container_of(pos, struct sync_timeline,
> >>>> -                                 sync_timeline_list);
> >>>> -
> >>>> -            sync_print_obj(s, obj);
> >>>> -            seq_putc(s, '\n');
> >>>> -    }
> >>>> -    spin_unlock_irq(&sync_timeline_list_lock);
> >>>> -
> >>>> -    seq_puts(s, "fences:\n--------------\n");
> >>>> -
> >>>> -    spin_lock_irq(&sync_file_list_lock);
> >>>> -    list_for_each(pos, &sync_file_list_head) {
> >>>> -            struct sync_file *sync_file =
> >>>> -                    container_of(pos, struct sync_file, sync_file_list);
> >>>> -
> >>>> -            sync_print_sync_file(s, sync_file);
> >>>> -            seq_putc(s, '\n');
> >>>> -    }
> >>>> -    spin_unlock_irq(&sync_file_list_lock);
> >>>> -    return 0;
> >>>> -}
> >>>> -
> >>>> -DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
> >>>> -
> >>>> -static __init int sync_debugfs_init(void)
> >>>> -{
> >>>> -    dbgfs = debugfs_create_dir("sync", NULL);
> >>>> -
> >>>> -    /*
> >>>> -     * The debugfs files won't ever get removed and thus, there is
> >>>> -     * no need to protect it against removal races. The use of
> >>>> -     * debugfs_create_file_unsafe() is actually safe here.
> >>>> -     */
> >>>> -    debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
> >>>> -                               &sync_info_debugfs_fops);
> >>>> -    debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
> >>>> -                               &sw_sync_debugfs_fops);
> >>>> -
> >>>> -    return 0;
> >>>> -}
> >>>> -late_initcall(sync_debugfs_init);
> >>>> diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
> >>>> deleted file mode 100644
> >>>> index 6176e52ba2d7..000000000000
> >>>> --- a/drivers/dma-buf/sync_debug.h
> >>>> +++ /dev/null
> >>>> @@ -1,72 +0,0 @@
> >>>> -/*
> >>>> - * Sync File validation framework and debug infomation
> >>>> - *
> >>>> - * Copyright (C) 2012 Google, Inc.
> >>>> - *
> >>>> - * This program is distributed in the hope that it will be useful,
> >>>> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> >>>> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> >>>> - * GNU General Public License for more details.
> >>>> - *
> >>>> - */
> >>>> -
> >>>> -#ifndef _LINUX_SYNC_H
> >>>> -#define _LINUX_SYNC_H
> >>>> -
> >>>> -#include <linux/list.h>
> >>>> -#include <linux/rbtree.h>
> >>>> -#include <linux/spinlock.h>
> >>>> -#include <linux/dma-fence.h>
> >>>> -
> >>>> -#include <linux/sync_file.h>
> >>>> -#include <uapi/linux/sync_file.h>
> >>>> -
> >>>> -/**
> >>>> - * struct sync_timeline - sync object
> >>>> - * @kref:           reference count on fence.
> >>>> - * @name:           name of the sync_timeline. Useful for debugging
> >>>> - * @lock:           lock protecting @pt_list and @value
> >>>> - * @pt_tree:                rbtree of active (unsignaled/errored) sync_pts
> >>>> - * @pt_list:                list of active (unsignaled/errored) sync_pts
> >>>> - * @sync_timeline_list:     membership in global sync_timeline_list
> >>>> - */
> >>>> -struct sync_timeline {
> >>>> -    struct kref             kref;
> >>>> -    char                    name[32];
> >>>> -
> >>>> -    /* protected by lock */
> >>>> -    u64                     context;
> >>>> -    int                     value;
> >>>> -
> >>>> -    struct rb_root          pt_tree;
> >>>> -    struct list_head        pt_list;
> >>>> -    spinlock_t              lock;
> >>>> -
> >>>> -    struct list_head        sync_timeline_list;
> >>>> -};
> >>>> -
> >>>> -static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
> >>>> -{
> >>>> -    return container_of(fence->lock, struct sync_timeline, lock);
> >>>> -}
> >>>> -
> >>>> -/**
> >>>> - * struct sync_pt - sync_pt object
> >>>> - * @base: base fence object
> >>>> - * @link: link on the sync timeline's list
> >>>> - * @node: node in the sync timeline's tree
> >>>> - */
> >>>> -struct sync_pt {
> >>>> -    struct dma_fence base;
> >>>> -    struct list_head link;
> >>>> -    struct rb_node node;
> >>>> -};
> >>>> -
> >>>> -extern const struct file_operations sw_sync_debugfs_fops;
> >>>> -
> >>>> -void sync_timeline_debug_add(struct sync_timeline *obj);
> >>>> -void sync_timeline_debug_remove(struct sync_timeline *obj);
> >>>> -void sync_file_debug_add(struct sync_file *fence);
> >>>> -void sync_file_debug_remove(struct sync_file *fence);
> >>>> -
> >>>> -#endif /* _LINUX_SYNC_H */
> >>>> --
> >>>> 2.25.1
> >>>>
> >
>


-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
@ 2021-07-29 11:59             ` Daniel Vetter
  0 siblings, 0 replies; 26+ messages in thread
From: Daniel Vetter @ 2021-07-29 11:59 UTC (permalink / raw)
  To: Christian König
  Cc: moderated list:DMA BUFFER SHARING FRAMEWORK, Gustavo Padovan,
	dri-devel, open list:DMA BUFFER SHARING FRAMEWORK

On Thu, Jul 29, 2021 at 12:21 PM Christian König
<ckoenig.leichtzumerken@gmail.com> wrote:
> Am 29.07.21 um 11:03 schrieb Daniel Vetter:
> > On Thu, Jul 29, 2021 at 10:38 AM Christian König
> > <ckoenig.leichtzumerken@gmail.com> wrote:
> >> Am 29.07.21 um 09:23 schrieb Daniel Vetter:
> >>> On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
> >>>> As we now knew controlling dma_fence synchronization from userspace is
> >>>> extremely dangerous and can not only deadlock drivers but trivially also the
> >>>> whole kernel memory management.
> >>>>
> >>>> Entirely remove this option. We now have in kernel unit tests to exercise the
> >>>> dma_fence framework and it's containers.
> >>>>
> >>>> Signed-off-by: Christian König <christian.koenig@amd.com>
> >>> There's also igts for this, and Android heavily uses this. So I'm not sure
> >>> we can just nuke it.
> >> I feared that you would say that.
> >>
> >>> I guess what we could do is maybe just taint the kernel if it's ever used?
> >> What should we use then? TAINT_SOFTLOCKUP or TAINT_CRAP? :)
> > I think the cleanest solution would be to install a 10s timer on each
> > sw fence, and TAINT_SOFTLOOKUP if it fails to signal. That's similar
> > to what we do with vgem, except vgem then just steps in with a
> > dma_fence_signal() instead of tainting the kernel.
>
> Well vgem is only for testing as far as I know.

I think CrOS is using it to shovel buffers around between process, as
a convenient allocator for sw rendering dma-buf. But they don't use
the dma-fence part afaik.

We can't taint in vgem fences though because our CI reboots on any
taint (or lockdep splat) and a few others, so that would make it
useless for testing. Maybe we should have the fence part of vgem
behind a separate Kconfig?

> When Android is using this for a production environment I think we
> should be a bit harder and explicitly note that this is not a good idea
> in sw_sync_debugfs_open().
>
> > I think that should keep everyone happy, because if your Android has a
> > fence stuck for more than 10s it's busted.
>
> Well when they are using userptrs they are busted as soon as they have a
> fence stuck for much less time.

Hm right I forgot that you can't guarantee that it will complete, ever :-)

Otoh I'm not sure the unconditional taint is very nice to the android
folks, they'll probably just patch it out and then it looses it
usefulness. taint on timeout otoh makes sure that normally it's not
hit, but anytime you die in userptr or shrinker there will be a taint
before anything else (since 10s is much shorter than the "stuck in
non-interruptible sleep" warning).
-Daniel

> Regards,
> Christian.
>
> >
> > Note that with hwc1 the indefinite fence was actually the model, when
> > you did a page_flip you got an out-fence back which will only be
> > signalled when the new buffer _stopped_ being used for scanout. Which
> > generally depends upon the next page-flip happening (except for manual
> > mode display, which only scan out once, if you ignore that the
> > kernel's supposed to restore the display after suspend/resume, which
> > android did). hwc2 fixed this, so a 10s timeout-to-taint should be
> > fine with Android people.
> >
> > Also since we do have the igt/sw_sync.c test intel-gfx-ci should be
> > able to check that code even.
> > -Daniel
> >
> >> Regards,
> >> Christian.
> >>
> >>> Also for testing we use vgem now, which enforces a timeout.
> >>> -Daniel
> >>>
> >>>> ---
> >>>>    drivers/dma-buf/Kconfig      |  13 --
> >>>>    drivers/dma-buf/Makefile     |   1 -
> >>>>    drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
> >>>>    drivers/dma-buf/sync_debug.c | 190 ----------------
> >>>>    drivers/dma-buf/sync_debug.h |  72 ------
> >>>>    5 files changed, 688 deletions(-)
> >>>>    delete mode 100644 drivers/dma-buf/sw_sync.c
> >>>>    delete mode 100644 drivers/dma-buf/sync_debug.c
> >>>>    delete mode 100644 drivers/dma-buf/sync_debug.h
> >>>>
> >>>> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
> >>>> index 9561e3d2d428..26c53f45616a 100644
> >>>> --- a/drivers/dma-buf/Kconfig
> >>>> +++ b/drivers/dma-buf/Kconfig
> >>>> @@ -17,19 +17,6 @@ config SYNC_FILE
> >>>>         Files fds, to the DRM driver for example. More details at
> >>>>         Documentation/driver-api/sync_file.rst.
> >>>>
> >>>> -config SW_SYNC
> >>>> -    bool "Sync File Validation Framework"
> >>>> -    default n
> >>>> -    depends on SYNC_FILE
> >>>> -    depends on DEBUG_FS
> >>>> -    help
> >>>> -      A sync object driver that uses a 32bit counter to coordinate
> >>>> -      synchronization.  Useful when there is no hardware primitive backing
> >>>> -      the synchronization.
> >>>> -
> >>>> -      WARNING: improper use of this can result in deadlocking kernel
> >>>> -      drivers from userspace. Intended for test and debug only.
> >>>> -
> >>>>    config UDMABUF
> >>>>       bool "userspace dmabuf misc driver"
> >>>>       default n
> >>>> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
> >>>> index 1ef021273a06..fa3af0953f87 100644
> >>>> --- a/drivers/dma-buf/Makefile
> >>>> +++ b/drivers/dma-buf/Makefile
> >>>> @@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
> >>>>    obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o
> >>>>    obj-$(CONFIG_DMABUF_HEAPS) += heaps/
> >>>>    obj-$(CONFIG_SYNC_FILE)            += sync_file.o
> >>>> -obj-$(CONFIG_SW_SYNC)               += sw_sync.o sync_debug.o
> >>>>    obj-$(CONFIG_UDMABUF)              += udmabuf.o
> >>>>    obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
> >>>>
> >>>> diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
> >>>> deleted file mode 100644
> >>>> index 348b3a9170fa..000000000000
> >>>> --- a/drivers/dma-buf/sw_sync.c
> >>>> +++ /dev/null
> >>>> @@ -1,412 +0,0 @@
> >>>> -// SPDX-License-Identifier: GPL-2.0-only
> >>>> -/*
> >>>> - * Sync File validation framework
> >>>> - *
> >>>> - * Copyright (C) 2012 Google, Inc.
> >>>> - */
> >>>> -
> >>>> -#include <linux/file.h>
> >>>> -#include <linux/fs.h>
> >>>> -#include <linux/uaccess.h>
> >>>> -#include <linux/slab.h>
> >>>> -#include <linux/sync_file.h>
> >>>> -
> >>>> -#include "sync_debug.h"
> >>>> -
> >>>> -#define CREATE_TRACE_POINTS
> >>>> -#include "sync_trace.h"
> >>>> -
> >>>> -/*
> >>>> - * SW SYNC validation framework
> >>>> - *
> >>>> - * A sync object driver that uses a 32bit counter to coordinate
> >>>> - * synchronization.  Useful when there is no hardware primitive backing
> >>>> - * the synchronization.
> >>>> - *
> >>>> - * To start the framework just open:
> >>>> - *
> >>>> - * <debugfs>/sync/sw_sync
> >>>> - *
> >>>> - * That will create a sync timeline, all fences created under this timeline
> >>>> - * file descriptor will belong to the this timeline.
> >>>> - *
> >>>> - * The 'sw_sync' file can be opened many times as to create different
> >>>> - * timelines.
> >>>> - *
> >>>> - * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
> >>>> - * sw_sync_create_fence_data as parameter.
> >>>> - *
> >>>> - * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
> >>>> - * with the increment as u32. This will update the last signaled value
> >>>> - * from the timeline and signal any fence that has a seqno smaller or equal
> >>>> - * to it.
> >>>> - *
> >>>> - * struct sw_sync_create_fence_data
> >>>> - * @value:  the seqno to initialise the fence with
> >>>> - * @name:   the name of the new sync point
> >>>> - * @fence:  return the fd of the new sync_file with the created fence
> >>>> - */
> >>>> -struct sw_sync_create_fence_data {
> >>>> -    __u32   value;
> >>>> -    char    name[32];
> >>>> -    __s32   fence; /* fd of new fence */
> >>>> -};
> >>>> -
> >>>> -#define SW_SYNC_IOC_MAGIC   'W'
> >>>> -
> >>>> -#define SW_SYNC_IOC_CREATE_FENCE    _IOWR(SW_SYNC_IOC_MAGIC, 0,\
> >>>> -            struct sw_sync_create_fence_data)
> >>>> -
> >>>> -#define SW_SYNC_IOC_INC                     _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
> >>>> -
> >>>> -static const struct dma_fence_ops timeline_fence_ops;
> >>>> -
> >>>> -static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
> >>>> -{
> >>>> -    if (fence->ops != &timeline_fence_ops)
> >>>> -            return NULL;
> >>>> -    return container_of(fence, struct sync_pt, base);
> >>>> -}
> >>>> -
> >>>> -/**
> >>>> - * sync_timeline_create() - creates a sync object
> >>>> - * @name:   sync_timeline name
> >>>> - *
> >>>> - * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
> >>>> - * case of error.
> >>>> - */
> >>>> -static struct sync_timeline *sync_timeline_create(const char *name)
> >>>> -{
> >>>> -    struct sync_timeline *obj;
> >>>> -
> >>>> -    obj = kzalloc(sizeof(*obj), GFP_KERNEL);
> >>>> -    if (!obj)
> >>>> -            return NULL;
> >>>> -
> >>>> -    kref_init(&obj->kref);
> >>>> -    obj->context = dma_fence_context_alloc(1);
> >>>> -    strlcpy(obj->name, name, sizeof(obj->name));
> >>>> -
> >>>> -    obj->pt_tree = RB_ROOT;
> >>>> -    INIT_LIST_HEAD(&obj->pt_list);
> >>>> -    spin_lock_init(&obj->lock);
> >>>> -
> >>>> -    sync_timeline_debug_add(obj);
> >>>> -
> >>>> -    return obj;
> >>>> -}
> >>>> -
> >>>> -static void sync_timeline_free(struct kref *kref)
> >>>> -{
> >>>> -    struct sync_timeline *obj =
> >>>> -            container_of(kref, struct sync_timeline, kref);
> >>>> -
> >>>> -    sync_timeline_debug_remove(obj);
> >>>> -
> >>>> -    kfree(obj);
> >>>> -}
> >>>> -
> >>>> -static void sync_timeline_get(struct sync_timeline *obj)
> >>>> -{
> >>>> -    kref_get(&obj->kref);
> >>>> -}
> >>>> -
> >>>> -static void sync_timeline_put(struct sync_timeline *obj)
> >>>> -{
> >>>> -    kref_put(&obj->kref, sync_timeline_free);
> >>>> -}
> >>>> -
> >>>> -static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
> >>>> -{
> >>>> -    return "sw_sync";
> >>>> -}
> >>>> -
> >>>> -static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
> >>>> -{
> >>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>> -
> >>>> -    return parent->name;
> >>>> -}
> >>>> -
> >>>> -static void timeline_fence_release(struct dma_fence *fence)
> >>>> -{
> >>>> -    struct sync_pt *pt = dma_fence_to_sync_pt(fence);
> >>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>> -    unsigned long flags;
> >>>> -
> >>>> -    spin_lock_irqsave(fence->lock, flags);
> >>>> -    if (!list_empty(&pt->link)) {
> >>>> -            list_del(&pt->link);
> >>>> -            rb_erase(&pt->node, &parent->pt_tree);
> >>>> -    }
> >>>> -    spin_unlock_irqrestore(fence->lock, flags);
> >>>> -
> >>>> -    sync_timeline_put(parent);
> >>>> -    dma_fence_free(fence);
> >>>> -}
> >>>> -
> >>>> -static bool timeline_fence_signaled(struct dma_fence *fence)
> >>>> -{
> >>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>> -
> >>>> -    return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
> >>>> -}
> >>>> -
> >>>> -static bool timeline_fence_enable_signaling(struct dma_fence *fence)
> >>>> -{
> >>>> -    return true;
> >>>> -}
> >>>> -
> >>>> -static void timeline_fence_value_str(struct dma_fence *fence,
> >>>> -                                char *str, int size)
> >>>> -{
> >>>> -    snprintf(str, size, "%lld", fence->seqno);
> >>>> -}
> >>>> -
> >>>> -static void timeline_fence_timeline_value_str(struct dma_fence *fence,
> >>>> -                                         char *str, int size)
> >>>> -{
> >>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>> -
> >>>> -    snprintf(str, size, "%d", parent->value);
> >>>> -}
> >>>> -
> >>>> -static const struct dma_fence_ops timeline_fence_ops = {
> >>>> -    .get_driver_name = timeline_fence_get_driver_name,
> >>>> -    .get_timeline_name = timeline_fence_get_timeline_name,
> >>>> -    .enable_signaling = timeline_fence_enable_signaling,
> >>>> -    .signaled = timeline_fence_signaled,
> >>>> -    .release = timeline_fence_release,
> >>>> -    .fence_value_str = timeline_fence_value_str,
> >>>> -    .timeline_value_str = timeline_fence_timeline_value_str,
> >>>> -};
> >>>> -
> >>>> -/**
> >>>> - * sync_timeline_signal() - signal a status change on a sync_timeline
> >>>> - * @obj:    sync_timeline to signal
> >>>> - * @inc:    num to increment on timeline->value
> >>>> - *
> >>>> - * A sync implementation should call this any time one of it's fences
> >>>> - * has signaled or has an error condition.
> >>>> - */
> >>>> -static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
> >>>> -{
> >>>> -    struct sync_pt *pt, *next;
> >>>> -
> >>>> -    trace_sync_timeline(obj);
> >>>> -
> >>>> -    spin_lock_irq(&obj->lock);
> >>>> -
> >>>> -    obj->value += inc;
> >>>> -
> >>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> >>>> -            if (!timeline_fence_signaled(&pt->base))
> >>>> -                    break;
> >>>> -
> >>>> -            list_del_init(&pt->link);
> >>>> -            rb_erase(&pt->node, &obj->pt_tree);
> >>>> -
> >>>> -            /*
> >>>> -             * A signal callback may release the last reference to this
> >>>> -             * fence, causing it to be freed. That operation has to be
> >>>> -             * last to avoid a use after free inside this loop, and must
> >>>> -             * be after we remove the fence from the timeline in order to
> >>>> -             * prevent deadlocking on timeline->lock inside
> >>>> -             * timeline_fence_release().
> >>>> -             */
> >>>> -            dma_fence_signal_locked(&pt->base);
> >>>> -    }
> >>>> -
> >>>> -    spin_unlock_irq(&obj->lock);
> >>>> -}
> >>>> -
> >>>> -/**
> >>>> - * sync_pt_create() - creates a sync pt
> >>>> - * @obj:    parent sync_timeline
> >>>> - * @value:  value of the fence
> >>>> - *
> >>>> - * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
> >>>> - * allocated allowing for implementation specific data to be kept after
> >>>> - * the generic sync_timeline struct. Returns the sync_pt object or
> >>>> - * NULL in case of error.
> >>>> - */
> >>>> -static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
> >>>> -                                  unsigned int value)
> >>>> -{
> >>>> -    struct sync_pt *pt;
> >>>> -
> >>>> -    pt = kzalloc(sizeof(*pt), GFP_KERNEL);
> >>>> -    if (!pt)
> >>>> -            return NULL;
> >>>> -
> >>>> -    sync_timeline_get(obj);
> >>>> -    dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
> >>>> -                   obj->context, value);
> >>>> -    INIT_LIST_HEAD(&pt->link);
> >>>> -
> >>>> -    spin_lock_irq(&obj->lock);
> >>>> -    if (!dma_fence_is_signaled_locked(&pt->base)) {
> >>>> -            struct rb_node **p = &obj->pt_tree.rb_node;
> >>>> -            struct rb_node *parent = NULL;
> >>>> -
> >>>> -            while (*p) {
> >>>> -                    struct sync_pt *other;
> >>>> -                    int cmp;
> >>>> -
> >>>> -                    parent = *p;
> >>>> -                    other = rb_entry(parent, typeof(*pt), node);
> >>>> -                    cmp = value - other->base.seqno;
> >>>> -                    if (cmp > 0) {
> >>>> -                            p = &parent->rb_right;
> >>>> -                    } else if (cmp < 0) {
> >>>> -                            p = &parent->rb_left;
> >>>> -                    } else {
> >>>> -                            if (dma_fence_get_rcu(&other->base)) {
> >>>> -                                    sync_timeline_put(obj);
> >>>> -                                    kfree(pt);
> >>>> -                                    pt = other;
> >>>> -                                    goto unlock;
> >>>> -                            }
> >>>> -                            p = &parent->rb_left;
> >>>> -                    }
> >>>> -            }
> >>>> -            rb_link_node(&pt->node, parent, p);
> >>>> -            rb_insert_color(&pt->node, &obj->pt_tree);
> >>>> -
> >>>> -            parent = rb_next(&pt->node);
> >>>> -            list_add_tail(&pt->link,
> >>>> -                          parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
> >>>> -    }
> >>>> -unlock:
> >>>> -    spin_unlock_irq(&obj->lock);
> >>>> -
> >>>> -    return pt;
> >>>> -}
> >>>> -
> >>>> -/*
> >>>> - * *WARNING*
> >>>> - *
> >>>> - * improper use of this can result in deadlocking kernel drivers from userspace.
> >>>> - */
> >>>> -
> >>>> -/* opening sw_sync create a new sync obj */
> >>>> -static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
> >>>> -{
> >>>> -    struct sync_timeline *obj;
> >>>> -    char task_comm[TASK_COMM_LEN];
> >>>> -
> >>>> -    get_task_comm(task_comm, current);
> >>>> -
> >>>> -    obj = sync_timeline_create(task_comm);
> >>>> -    if (!obj)
> >>>> -            return -ENOMEM;
> >>>> -
> >>>> -    file->private_data = obj;
> >>>> -
> >>>> -    return 0;
> >>>> -}
> >>>> -
> >>>> -static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
> >>>> -{
> >>>> -    struct sync_timeline *obj = file->private_data;
> >>>> -    struct sync_pt *pt, *next;
> >>>> -
> >>>> -    spin_lock_irq(&obj->lock);
> >>>> -
> >>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> >>>> -            dma_fence_set_error(&pt->base, -ENOENT);
> >>>> -            dma_fence_signal_locked(&pt->base);
> >>>> -    }
> >>>> -
> >>>> -    spin_unlock_irq(&obj->lock);
> >>>> -
> >>>> -    sync_timeline_put(obj);
> >>>> -    return 0;
> >>>> -}
> >>>> -
> >>>> -static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
> >>>> -                                   unsigned long arg)
> >>>> -{
> >>>> -    int fd = get_unused_fd_flags(O_CLOEXEC);
> >>>> -    int err;
> >>>> -    struct sync_pt *pt;
> >>>> -    struct sync_file *sync_file;
> >>>> -    struct sw_sync_create_fence_data data;
> >>>> -
> >>>> -    if (fd < 0)
> >>>> -            return fd;
> >>>> -
> >>>> -    if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
> >>>> -            err = -EFAULT;
> >>>> -            goto err;
> >>>> -    }
> >>>> -
> >>>> -    pt = sync_pt_create(obj, data.value);
> >>>> -    if (!pt) {
> >>>> -            err = -ENOMEM;
> >>>> -            goto err;
> >>>> -    }
> >>>> -
> >>>> -    sync_file = sync_file_create(&pt->base);
> >>>> -    dma_fence_put(&pt->base);
> >>>> -    if (!sync_file) {
> >>>> -            err = -ENOMEM;
> >>>> -            goto err;
> >>>> -    }
> >>>> -
> >>>> -    data.fence = fd;
> >>>> -    if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
> >>>> -            fput(sync_file->file);
> >>>> -            err = -EFAULT;
> >>>> -            goto err;
> >>>> -    }
> >>>> -
> >>>> -    fd_install(fd, sync_file->file);
> >>>> -
> >>>> -    return 0;
> >>>> -
> >>>> -err:
> >>>> -    put_unused_fd(fd);
> >>>> -    return err;
> >>>> -}
> >>>> -
> >>>> -static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
> >>>> -{
> >>>> -    u32 value;
> >>>> -
> >>>> -    if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
> >>>> -            return -EFAULT;
> >>>> -
> >>>> -    while (value > INT_MAX)  {
> >>>> -            sync_timeline_signal(obj, INT_MAX);
> >>>> -            value -= INT_MAX;
> >>>> -    }
> >>>> -
> >>>> -    sync_timeline_signal(obj, value);
> >>>> -
> >>>> -    return 0;
> >>>> -}
> >>>> -
> >>>> -static long sw_sync_ioctl(struct file *file, unsigned int cmd,
> >>>> -                      unsigned long arg)
> >>>> -{
> >>>> -    struct sync_timeline *obj = file->private_data;
> >>>> -
> >>>> -    switch (cmd) {
> >>>> -    case SW_SYNC_IOC_CREATE_FENCE:
> >>>> -            return sw_sync_ioctl_create_fence(obj, arg);
> >>>> -
> >>>> -    case SW_SYNC_IOC_INC:
> >>>> -            return sw_sync_ioctl_inc(obj, arg);
> >>>> -
> >>>> -    default:
> >>>> -            return -ENOTTY;
> >>>> -    }
> >>>> -}
> >>>> -
> >>>> -const struct file_operations sw_sync_debugfs_fops = {
> >>>> -    .open           = sw_sync_debugfs_open,
> >>>> -    .release        = sw_sync_debugfs_release,
> >>>> -    .unlocked_ioctl = sw_sync_ioctl,
> >>>> -    .compat_ioctl   = compat_ptr_ioctl,
> >>>> -};
> >>>> diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
> >>>> deleted file mode 100644
> >>>> index 101394f16930..000000000000
> >>>> --- a/drivers/dma-buf/sync_debug.c
> >>>> +++ /dev/null
> >>>> @@ -1,190 +0,0 @@
> >>>> -// SPDX-License-Identifier: GPL-2.0-only
> >>>> -/*
> >>>> - * Sync File validation framework and debug information
> >>>> - *
> >>>> - * Copyright (C) 2012 Google, Inc.
> >>>> - */
> >>>> -
> >>>> -#include <linux/debugfs.h>
> >>>> -#include "sync_debug.h"
> >>>> -
> >>>> -static struct dentry *dbgfs;
> >>>> -
> >>>> -static LIST_HEAD(sync_timeline_list_head);
> >>>> -static DEFINE_SPINLOCK(sync_timeline_list_lock);
> >>>> -static LIST_HEAD(sync_file_list_head);
> >>>> -static DEFINE_SPINLOCK(sync_file_list_lock);
> >>>> -
> >>>> -void sync_timeline_debug_add(struct sync_timeline *obj)
> >>>> -{
> >>>> -    unsigned long flags;
> >>>> -
> >>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
> >>>> -    list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
> >>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> >>>> -}
> >>>> -
> >>>> -void sync_timeline_debug_remove(struct sync_timeline *obj)
> >>>> -{
> >>>> -    unsigned long flags;
> >>>> -
> >>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
> >>>> -    list_del(&obj->sync_timeline_list);
> >>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> >>>> -}
> >>>> -
> >>>> -void sync_file_debug_add(struct sync_file *sync_file)
> >>>> -{
> >>>> -    unsigned long flags;
> >>>> -
> >>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
> >>>> -    list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
> >>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
> >>>> -}
> >>>> -
> >>>> -void sync_file_debug_remove(struct sync_file *sync_file)
> >>>> -{
> >>>> -    unsigned long flags;
> >>>> -
> >>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
> >>>> -    list_del(&sync_file->sync_file_list);
> >>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
> >>>> -}
> >>>> -
> >>>> -static const char *sync_status_str(int status)
> >>>> -{
> >>>> -    if (status < 0)
> >>>> -            return "error";
> >>>> -
> >>>> -    if (status > 0)
> >>>> -            return "signaled";
> >>>> -
> >>>> -    return "active";
> >>>> -}
> >>>> -
> >>>> -static void sync_print_fence(struct seq_file *s,
> >>>> -                         struct dma_fence *fence, bool show)
> >>>> -{
> >>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>> -    int status;
> >>>> -
> >>>> -    status = dma_fence_get_status_locked(fence);
> >>>> -
> >>>> -    seq_printf(s, "  %s%sfence %s",
> >>>> -               show ? parent->name : "",
> >>>> -               show ? "_" : "",
> >>>> -               sync_status_str(status));
> >>>> -
> >>>> -    if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
> >>>> -            struct timespec64 ts64 =
> >>>> -                    ktime_to_timespec64(fence->timestamp);
> >>>> -
> >>>> -            seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
> >>>> -    }
> >>>> -
> >>>> -    if (fence->ops->timeline_value_str &&
> >>>> -            fence->ops->fence_value_str) {
> >>>> -            char value[64];
> >>>> -            bool success;
> >>>> -
> >>>> -            fence->ops->fence_value_str(fence, value, sizeof(value));
> >>>> -            success = strlen(value);
> >>>> -
> >>>> -            if (success) {
> >>>> -                    seq_printf(s, ": %s", value);
> >>>> -
> >>>> -                    fence->ops->timeline_value_str(fence, value,
> >>>> -                                                   sizeof(value));
> >>>> -
> >>>> -                    if (strlen(value))
> >>>> -                            seq_printf(s, " / %s", value);
> >>>> -            }
> >>>> -    }
> >>>> -
> >>>> -    seq_putc(s, '\n');
> >>>> -}
> >>>> -
> >>>> -static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
> >>>> -{
> >>>> -    struct list_head *pos;
> >>>> -
> >>>> -    seq_printf(s, "%s: %d\n", obj->name, obj->value);
> >>>> -
> >>>> -    spin_lock_irq(&obj->lock);
> >>>> -    list_for_each(pos, &obj->pt_list) {
> >>>> -            struct sync_pt *pt = container_of(pos, struct sync_pt, link);
> >>>> -            sync_print_fence(s, &pt->base, false);
> >>>> -    }
> >>>> -    spin_unlock_irq(&obj->lock);
> >>>> -}
> >>>> -
> >>>> -static void sync_print_sync_file(struct seq_file *s,
> >>>> -                              struct sync_file *sync_file)
> >>>> -{
> >>>> -    char buf[128];
> >>>> -    int i;
> >>>> -
> >>>> -    seq_printf(s, "[%p] %s: %s\n", sync_file,
> >>>> -               sync_file_get_name(sync_file, buf, sizeof(buf)),
> >>>> -               sync_status_str(dma_fence_get_status(sync_file->fence)));
> >>>> -
> >>>> -    if (dma_fence_is_array(sync_file->fence)) {
> >>>> -            struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
> >>>> -
> >>>> -            for (i = 0; i < array->num_fences; ++i)
> >>>> -                    sync_print_fence(s, array->fences[i], true);
> >>>> -    } else {
> >>>> -            sync_print_fence(s, sync_file->fence, true);
> >>>> -    }
> >>>> -}
> >>>> -
> >>>> -static int sync_info_debugfs_show(struct seq_file *s, void *unused)
> >>>> -{
> >>>> -    struct list_head *pos;
> >>>> -
> >>>> -    seq_puts(s, "objs:\n--------------\n");
> >>>> -
> >>>> -    spin_lock_irq(&sync_timeline_list_lock);
> >>>> -    list_for_each(pos, &sync_timeline_list_head) {
> >>>> -            struct sync_timeline *obj =
> >>>> -                    container_of(pos, struct sync_timeline,
> >>>> -                                 sync_timeline_list);
> >>>> -
> >>>> -            sync_print_obj(s, obj);
> >>>> -            seq_putc(s, '\n');
> >>>> -    }
> >>>> -    spin_unlock_irq(&sync_timeline_list_lock);
> >>>> -
> >>>> -    seq_puts(s, "fences:\n--------------\n");
> >>>> -
> >>>> -    spin_lock_irq(&sync_file_list_lock);
> >>>> -    list_for_each(pos, &sync_file_list_head) {
> >>>> -            struct sync_file *sync_file =
> >>>> -                    container_of(pos, struct sync_file, sync_file_list);
> >>>> -
> >>>> -            sync_print_sync_file(s, sync_file);
> >>>> -            seq_putc(s, '\n');
> >>>> -    }
> >>>> -    spin_unlock_irq(&sync_file_list_lock);
> >>>> -    return 0;
> >>>> -}
> >>>> -
> >>>> -DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
> >>>> -
> >>>> -static __init int sync_debugfs_init(void)
> >>>> -{
> >>>> -    dbgfs = debugfs_create_dir("sync", NULL);
> >>>> -
> >>>> -    /*
> >>>> -     * The debugfs files won't ever get removed and thus, there is
> >>>> -     * no need to protect it against removal races. The use of
> >>>> -     * debugfs_create_file_unsafe() is actually safe here.
> >>>> -     */
> >>>> -    debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
> >>>> -                               &sync_info_debugfs_fops);
> >>>> -    debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
> >>>> -                               &sw_sync_debugfs_fops);
> >>>> -
> >>>> -    return 0;
> >>>> -}
> >>>> -late_initcall(sync_debugfs_init);
> >>>> diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
> >>>> deleted file mode 100644
> >>>> index 6176e52ba2d7..000000000000
> >>>> --- a/drivers/dma-buf/sync_debug.h
> >>>> +++ /dev/null
> >>>> @@ -1,72 +0,0 @@
> >>>> -/*
> >>>> - * Sync File validation framework and debug infomation
> >>>> - *
> >>>> - * Copyright (C) 2012 Google, Inc.
> >>>> - *
> >>>> - * This program is distributed in the hope that it will be useful,
> >>>> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> >>>> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> >>>> - * GNU General Public License for more details.
> >>>> - *
> >>>> - */
> >>>> -
> >>>> -#ifndef _LINUX_SYNC_H
> >>>> -#define _LINUX_SYNC_H
> >>>> -
> >>>> -#include <linux/list.h>
> >>>> -#include <linux/rbtree.h>
> >>>> -#include <linux/spinlock.h>
> >>>> -#include <linux/dma-fence.h>
> >>>> -
> >>>> -#include <linux/sync_file.h>
> >>>> -#include <uapi/linux/sync_file.h>
> >>>> -
> >>>> -/**
> >>>> - * struct sync_timeline - sync object
> >>>> - * @kref:           reference count on fence.
> >>>> - * @name:           name of the sync_timeline. Useful for debugging
> >>>> - * @lock:           lock protecting @pt_list and @value
> >>>> - * @pt_tree:                rbtree of active (unsignaled/errored) sync_pts
> >>>> - * @pt_list:                list of active (unsignaled/errored) sync_pts
> >>>> - * @sync_timeline_list:     membership in global sync_timeline_list
> >>>> - */
> >>>> -struct sync_timeline {
> >>>> -    struct kref             kref;
> >>>> -    char                    name[32];
> >>>> -
> >>>> -    /* protected by lock */
> >>>> -    u64                     context;
> >>>> -    int                     value;
> >>>> -
> >>>> -    struct rb_root          pt_tree;
> >>>> -    struct list_head        pt_list;
> >>>> -    spinlock_t              lock;
> >>>> -
> >>>> -    struct list_head        sync_timeline_list;
> >>>> -};
> >>>> -
> >>>> -static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
> >>>> -{
> >>>> -    return container_of(fence->lock, struct sync_timeline, lock);
> >>>> -}
> >>>> -
> >>>> -/**
> >>>> - * struct sync_pt - sync_pt object
> >>>> - * @base: base fence object
> >>>> - * @link: link on the sync timeline's list
> >>>> - * @node: node in the sync timeline's tree
> >>>> - */
> >>>> -struct sync_pt {
> >>>> -    struct dma_fence base;
> >>>> -    struct list_head link;
> >>>> -    struct rb_node node;
> >>>> -};
> >>>> -
> >>>> -extern const struct file_operations sw_sync_debugfs_fops;
> >>>> -
> >>>> -void sync_timeline_debug_add(struct sync_timeline *obj);
> >>>> -void sync_timeline_debug_remove(struct sync_timeline *obj);
> >>>> -void sync_file_debug_add(struct sync_file *fence);
> >>>> -void sync_file_debug_remove(struct sync_file *fence);
> >>>> -
> >>>> -#endif /* _LINUX_SYNC_H */
> >>>> --
> >>>> 2.25.1
> >>>>
> >
>


-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
  2021-07-29 11:59             ` Daniel Vetter
@ 2021-07-29 12:25               ` Christian König
  -1 siblings, 0 replies; 26+ messages in thread
From: Christian König @ 2021-07-29 12:25 UTC (permalink / raw)
  To: Daniel Vetter
  Cc: Sumit Semwal, Gustavo Padovan,
	open list:DMA BUFFER SHARING FRAMEWORK, dri-devel,
	moderated list:DMA BUFFER SHARING FRAMEWORK

Am 29.07.21 um 13:59 schrieb Daniel Vetter:
> On Thu, Jul 29, 2021 at 12:21 PM Christian König
> <ckoenig.leichtzumerken@gmail.com> wrote:
>> Am 29.07.21 um 11:03 schrieb Daniel Vetter:
>>> On Thu, Jul 29, 2021 at 10:38 AM Christian König
>>> <ckoenig.leichtzumerken@gmail.com> wrote:
>>>> Am 29.07.21 um 09:23 schrieb Daniel Vetter:
>>>>> On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
>>>>>> As we now knew controlling dma_fence synchronization from userspace is
>>>>>> extremely dangerous and can not only deadlock drivers but trivially also the
>>>>>> whole kernel memory management.
>>>>>>
>>>>>> Entirely remove this option. We now have in kernel unit tests to exercise the
>>>>>> dma_fence framework and it's containers.
>>>>>>
>>>>>> Signed-off-by: Christian König <christian.koenig@amd.com>
>>>>> There's also igts for this, and Android heavily uses this. So I'm not sure
>>>>> we can just nuke it.
>>>> I feared that you would say that.
>>>>
>>>>> I guess what we could do is maybe just taint the kernel if it's ever used?
>>>> What should we use then? TAINT_SOFTLOCKUP or TAINT_CRAP? :)
>>> I think the cleanest solution would be to install a 10s timer on each
>>> sw fence, and TAINT_SOFTLOOKUP if it fails to signal. That's similar
>>> to what we do with vgem, except vgem then just steps in with a
>>> dma_fence_signal() instead of tainting the kernel.
>> Well vgem is only for testing as far as I know.
> I think CrOS is using it to shovel buffers around between process, as
> a convenient allocator for sw rendering dma-buf. But they don't use
> the dma-fence part afaik.
>
> We can't taint in vgem fences though because our CI reboots on any
> taint (or lockdep splat) and a few others, so that would make it
> useless for testing. Maybe we should have the fence part of vgem
> behind a separate Kconfig?

And then making it depend on CONFIG_BROKEN? :)

But more serious, that it probably makes sense.

Can you take care of that? I'm not that familiar with the vgem code base.

>> When Android is using this for a production environment I think we
>> should be a bit harder and explicitly note that this is not a good idea
>> in sw_sync_debugfs_open().
>>
>>> I think that should keep everyone happy, because if your Android has a
>>> fence stuck for more than 10s it's busted.
>> Well when they are using userptrs they are busted as soon as they have a
>> fence stuck for much less time.
> Hm right I forgot that you can't guarantee that it will complete, ever :-)
>
> Otoh I'm not sure the unconditional taint is very nice to the android
> folks, they'll probably just patch it out and then it looses it
> usefulness. taint on timeout otoh makes sure that normally it's not
> hit, but anytime you die in userptr or shrinker there will be a taint
> before anything else (since 10s is much shorter than the "stuck in
> non-interruptible sleep" warning).

And then you don't hit the timeout during test but only in production, 
e.g. not good enough either.

And yes I know that this is not really nice. But since this depends on 
debugfs and Android usually disables debugfs AFAIK, they probably have 
that heavily patched already anyway.

I just want to make sure that nobody else has the brilliant idea to 
enable this and build an userspace stack on top of a fundamentally 
broken approach.

Alternative is to add a dependency to CONFIG_BROKEN.

Christian.

> -Daniel
>
>> Regards,
>> Christian.
>>
>>> Note that with hwc1 the indefinite fence was actually the model, when
>>> you did a page_flip you got an out-fence back which will only be
>>> signalled when the new buffer _stopped_ being used for scanout. Which
>>> generally depends upon the next page-flip happening (except for manual
>>> mode display, which only scan out once, if you ignore that the
>>> kernel's supposed to restore the display after suspend/resume, which
>>> android did). hwc2 fixed this, so a 10s timeout-to-taint should be
>>> fine with Android people.
>>>
>>> Also since we do have the igt/sw_sync.c test intel-gfx-ci should be
>>> able to check that code even.
>>> -Daniel
>>>
>>>> Regards,
>>>> Christian.
>>>>
>>>>> Also for testing we use vgem now, which enforces a timeout.
>>>>> -Daniel
>>>>>
>>>>>> ---
>>>>>>     drivers/dma-buf/Kconfig      |  13 --
>>>>>>     drivers/dma-buf/Makefile     |   1 -
>>>>>>     drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
>>>>>>     drivers/dma-buf/sync_debug.c | 190 ----------------
>>>>>>     drivers/dma-buf/sync_debug.h |  72 ------
>>>>>>     5 files changed, 688 deletions(-)
>>>>>>     delete mode 100644 drivers/dma-buf/sw_sync.c
>>>>>>     delete mode 100644 drivers/dma-buf/sync_debug.c
>>>>>>     delete mode 100644 drivers/dma-buf/sync_debug.h
>>>>>>
>>>>>> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
>>>>>> index 9561e3d2d428..26c53f45616a 100644
>>>>>> --- a/drivers/dma-buf/Kconfig
>>>>>> +++ b/drivers/dma-buf/Kconfig
>>>>>> @@ -17,19 +17,6 @@ config SYNC_FILE
>>>>>>          Files fds, to the DRM driver for example. More details at
>>>>>>          Documentation/driver-api/sync_file.rst.
>>>>>>
>>>>>> -config SW_SYNC
>>>>>> -    bool "Sync File Validation Framework"
>>>>>> -    default n
>>>>>> -    depends on SYNC_FILE
>>>>>> -    depends on DEBUG_FS
>>>>>> -    help
>>>>>> -      A sync object driver that uses a 32bit counter to coordinate
>>>>>> -      synchronization.  Useful when there is no hardware primitive backing
>>>>>> -      the synchronization.
>>>>>> -
>>>>>> -      WARNING: improper use of this can result in deadlocking kernel
>>>>>> -      drivers from userspace. Intended for test and debug only.
>>>>>> -
>>>>>>     config UDMABUF
>>>>>>        bool "userspace dmabuf misc driver"
>>>>>>        default n
>>>>>> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
>>>>>> index 1ef021273a06..fa3af0953f87 100644
>>>>>> --- a/drivers/dma-buf/Makefile
>>>>>> +++ b/drivers/dma-buf/Makefile
>>>>>> @@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
>>>>>>     obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o
>>>>>>     obj-$(CONFIG_DMABUF_HEAPS) += heaps/
>>>>>>     obj-$(CONFIG_SYNC_FILE)            += sync_file.o
>>>>>> -obj-$(CONFIG_SW_SYNC)               += sw_sync.o sync_debug.o
>>>>>>     obj-$(CONFIG_UDMABUF)              += udmabuf.o
>>>>>>     obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
>>>>>>
>>>>>> diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
>>>>>> deleted file mode 100644
>>>>>> index 348b3a9170fa..000000000000
>>>>>> --- a/drivers/dma-buf/sw_sync.c
>>>>>> +++ /dev/null
>>>>>> @@ -1,412 +0,0 @@
>>>>>> -// SPDX-License-Identifier: GPL-2.0-only
>>>>>> -/*
>>>>>> - * Sync File validation framework
>>>>>> - *
>>>>>> - * Copyright (C) 2012 Google, Inc.
>>>>>> - */
>>>>>> -
>>>>>> -#include <linux/file.h>
>>>>>> -#include <linux/fs.h>
>>>>>> -#include <linux/uaccess.h>
>>>>>> -#include <linux/slab.h>
>>>>>> -#include <linux/sync_file.h>
>>>>>> -
>>>>>> -#include "sync_debug.h"
>>>>>> -
>>>>>> -#define CREATE_TRACE_POINTS
>>>>>> -#include "sync_trace.h"
>>>>>> -
>>>>>> -/*
>>>>>> - * SW SYNC validation framework
>>>>>> - *
>>>>>> - * A sync object driver that uses a 32bit counter to coordinate
>>>>>> - * synchronization.  Useful when there is no hardware primitive backing
>>>>>> - * the synchronization.
>>>>>> - *
>>>>>> - * To start the framework just open:
>>>>>> - *
>>>>>> - * <debugfs>/sync/sw_sync
>>>>>> - *
>>>>>> - * That will create a sync timeline, all fences created under this timeline
>>>>>> - * file descriptor will belong to the this timeline.
>>>>>> - *
>>>>>> - * The 'sw_sync' file can be opened many times as to create different
>>>>>> - * timelines.
>>>>>> - *
>>>>>> - * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
>>>>>> - * sw_sync_create_fence_data as parameter.
>>>>>> - *
>>>>>> - * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
>>>>>> - * with the increment as u32. This will update the last signaled value
>>>>>> - * from the timeline and signal any fence that has a seqno smaller or equal
>>>>>> - * to it.
>>>>>> - *
>>>>>> - * struct sw_sync_create_fence_data
>>>>>> - * @value:  the seqno to initialise the fence with
>>>>>> - * @name:   the name of the new sync point
>>>>>> - * @fence:  return the fd of the new sync_file with the created fence
>>>>>> - */
>>>>>> -struct sw_sync_create_fence_data {
>>>>>> -    __u32   value;
>>>>>> -    char    name[32];
>>>>>> -    __s32   fence; /* fd of new fence */
>>>>>> -};
>>>>>> -
>>>>>> -#define SW_SYNC_IOC_MAGIC   'W'
>>>>>> -
>>>>>> -#define SW_SYNC_IOC_CREATE_FENCE    _IOWR(SW_SYNC_IOC_MAGIC, 0,\
>>>>>> -            struct sw_sync_create_fence_data)
>>>>>> -
>>>>>> -#define SW_SYNC_IOC_INC                     _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
>>>>>> -
>>>>>> -static const struct dma_fence_ops timeline_fence_ops;
>>>>>> -
>>>>>> -static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
>>>>>> -{
>>>>>> -    if (fence->ops != &timeline_fence_ops)
>>>>>> -            return NULL;
>>>>>> -    return container_of(fence, struct sync_pt, base);
>>>>>> -}
>>>>>> -
>>>>>> -/**
>>>>>> - * sync_timeline_create() - creates a sync object
>>>>>> - * @name:   sync_timeline name
>>>>>> - *
>>>>>> - * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
>>>>>> - * case of error.
>>>>>> - */
>>>>>> -static struct sync_timeline *sync_timeline_create(const char *name)
>>>>>> -{
>>>>>> -    struct sync_timeline *obj;
>>>>>> -
>>>>>> -    obj = kzalloc(sizeof(*obj), GFP_KERNEL);
>>>>>> -    if (!obj)
>>>>>> -            return NULL;
>>>>>> -
>>>>>> -    kref_init(&obj->kref);
>>>>>> -    obj->context = dma_fence_context_alloc(1);
>>>>>> -    strlcpy(obj->name, name, sizeof(obj->name));
>>>>>> -
>>>>>> -    obj->pt_tree = RB_ROOT;
>>>>>> -    INIT_LIST_HEAD(&obj->pt_list);
>>>>>> -    spin_lock_init(&obj->lock);
>>>>>> -
>>>>>> -    sync_timeline_debug_add(obj);
>>>>>> -
>>>>>> -    return obj;
>>>>>> -}
>>>>>> -
>>>>>> -static void sync_timeline_free(struct kref *kref)
>>>>>> -{
>>>>>> -    struct sync_timeline *obj =
>>>>>> -            container_of(kref, struct sync_timeline, kref);
>>>>>> -
>>>>>> -    sync_timeline_debug_remove(obj);
>>>>>> -
>>>>>> -    kfree(obj);
>>>>>> -}
>>>>>> -
>>>>>> -static void sync_timeline_get(struct sync_timeline *obj)
>>>>>> -{
>>>>>> -    kref_get(&obj->kref);
>>>>>> -}
>>>>>> -
>>>>>> -static void sync_timeline_put(struct sync_timeline *obj)
>>>>>> -{
>>>>>> -    kref_put(&obj->kref, sync_timeline_free);
>>>>>> -}
>>>>>> -
>>>>>> -static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
>>>>>> -{
>>>>>> -    return "sw_sync";
>>>>>> -}
>>>>>> -
>>>>>> -static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
>>>>>> -{
>>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>>>> -
>>>>>> -    return parent->name;
>>>>>> -}
>>>>>> -
>>>>>> -static void timeline_fence_release(struct dma_fence *fence)
>>>>>> -{
>>>>>> -    struct sync_pt *pt = dma_fence_to_sync_pt(fence);
>>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>>>> -    unsigned long flags;
>>>>>> -
>>>>>> -    spin_lock_irqsave(fence->lock, flags);
>>>>>> -    if (!list_empty(&pt->link)) {
>>>>>> -            list_del(&pt->link);
>>>>>> -            rb_erase(&pt->node, &parent->pt_tree);
>>>>>> -    }
>>>>>> -    spin_unlock_irqrestore(fence->lock, flags);
>>>>>> -
>>>>>> -    sync_timeline_put(parent);
>>>>>> -    dma_fence_free(fence);
>>>>>> -}
>>>>>> -
>>>>>> -static bool timeline_fence_signaled(struct dma_fence *fence)
>>>>>> -{
>>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>>>> -
>>>>>> -    return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
>>>>>> -}
>>>>>> -
>>>>>> -static bool timeline_fence_enable_signaling(struct dma_fence *fence)
>>>>>> -{
>>>>>> -    return true;
>>>>>> -}
>>>>>> -
>>>>>> -static void timeline_fence_value_str(struct dma_fence *fence,
>>>>>> -                                char *str, int size)
>>>>>> -{
>>>>>> -    snprintf(str, size, "%lld", fence->seqno);
>>>>>> -}
>>>>>> -
>>>>>> -static void timeline_fence_timeline_value_str(struct dma_fence *fence,
>>>>>> -                                         char *str, int size)
>>>>>> -{
>>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>>>> -
>>>>>> -    snprintf(str, size, "%d", parent->value);
>>>>>> -}
>>>>>> -
>>>>>> -static const struct dma_fence_ops timeline_fence_ops = {
>>>>>> -    .get_driver_name = timeline_fence_get_driver_name,
>>>>>> -    .get_timeline_name = timeline_fence_get_timeline_name,
>>>>>> -    .enable_signaling = timeline_fence_enable_signaling,
>>>>>> -    .signaled = timeline_fence_signaled,
>>>>>> -    .release = timeline_fence_release,
>>>>>> -    .fence_value_str = timeline_fence_value_str,
>>>>>> -    .timeline_value_str = timeline_fence_timeline_value_str,
>>>>>> -};
>>>>>> -
>>>>>> -/**
>>>>>> - * sync_timeline_signal() - signal a status change on a sync_timeline
>>>>>> - * @obj:    sync_timeline to signal
>>>>>> - * @inc:    num to increment on timeline->value
>>>>>> - *
>>>>>> - * A sync implementation should call this any time one of it's fences
>>>>>> - * has signaled or has an error condition.
>>>>>> - */
>>>>>> -static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
>>>>>> -{
>>>>>> -    struct sync_pt *pt, *next;
>>>>>> -
>>>>>> -    trace_sync_timeline(obj);
>>>>>> -
>>>>>> -    spin_lock_irq(&obj->lock);
>>>>>> -
>>>>>> -    obj->value += inc;
>>>>>> -
>>>>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
>>>>>> -            if (!timeline_fence_signaled(&pt->base))
>>>>>> -                    break;
>>>>>> -
>>>>>> -            list_del_init(&pt->link);
>>>>>> -            rb_erase(&pt->node, &obj->pt_tree);
>>>>>> -
>>>>>> -            /*
>>>>>> -             * A signal callback may release the last reference to this
>>>>>> -             * fence, causing it to be freed. That operation has to be
>>>>>> -             * last to avoid a use after free inside this loop, and must
>>>>>> -             * be after we remove the fence from the timeline in order to
>>>>>> -             * prevent deadlocking on timeline->lock inside
>>>>>> -             * timeline_fence_release().
>>>>>> -             */
>>>>>> -            dma_fence_signal_locked(&pt->base);
>>>>>> -    }
>>>>>> -
>>>>>> -    spin_unlock_irq(&obj->lock);
>>>>>> -}
>>>>>> -
>>>>>> -/**
>>>>>> - * sync_pt_create() - creates a sync pt
>>>>>> - * @obj:    parent sync_timeline
>>>>>> - * @value:  value of the fence
>>>>>> - *
>>>>>> - * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
>>>>>> - * allocated allowing for implementation specific data to be kept after
>>>>>> - * the generic sync_timeline struct. Returns the sync_pt object or
>>>>>> - * NULL in case of error.
>>>>>> - */
>>>>>> -static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
>>>>>> -                                  unsigned int value)
>>>>>> -{
>>>>>> -    struct sync_pt *pt;
>>>>>> -
>>>>>> -    pt = kzalloc(sizeof(*pt), GFP_KERNEL);
>>>>>> -    if (!pt)
>>>>>> -            return NULL;
>>>>>> -
>>>>>> -    sync_timeline_get(obj);
>>>>>> -    dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
>>>>>> -                   obj->context, value);
>>>>>> -    INIT_LIST_HEAD(&pt->link);
>>>>>> -
>>>>>> -    spin_lock_irq(&obj->lock);
>>>>>> -    if (!dma_fence_is_signaled_locked(&pt->base)) {
>>>>>> -            struct rb_node **p = &obj->pt_tree.rb_node;
>>>>>> -            struct rb_node *parent = NULL;
>>>>>> -
>>>>>> -            while (*p) {
>>>>>> -                    struct sync_pt *other;
>>>>>> -                    int cmp;
>>>>>> -
>>>>>> -                    parent = *p;
>>>>>> -                    other = rb_entry(parent, typeof(*pt), node);
>>>>>> -                    cmp = value - other->base.seqno;
>>>>>> -                    if (cmp > 0) {
>>>>>> -                            p = &parent->rb_right;
>>>>>> -                    } else if (cmp < 0) {
>>>>>> -                            p = &parent->rb_left;
>>>>>> -                    } else {
>>>>>> -                            if (dma_fence_get_rcu(&other->base)) {
>>>>>> -                                    sync_timeline_put(obj);
>>>>>> -                                    kfree(pt);
>>>>>> -                                    pt = other;
>>>>>> -                                    goto unlock;
>>>>>> -                            }
>>>>>> -                            p = &parent->rb_left;
>>>>>> -                    }
>>>>>> -            }
>>>>>> -            rb_link_node(&pt->node, parent, p);
>>>>>> -            rb_insert_color(&pt->node, &obj->pt_tree);
>>>>>> -
>>>>>> -            parent = rb_next(&pt->node);
>>>>>> -            list_add_tail(&pt->link,
>>>>>> -                          parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
>>>>>> -    }
>>>>>> -unlock:
>>>>>> -    spin_unlock_irq(&obj->lock);
>>>>>> -
>>>>>> -    return pt;
>>>>>> -}
>>>>>> -
>>>>>> -/*
>>>>>> - * *WARNING*
>>>>>> - *
>>>>>> - * improper use of this can result in deadlocking kernel drivers from userspace.
>>>>>> - */
>>>>>> -
>>>>>> -/* opening sw_sync create a new sync obj */
>>>>>> -static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
>>>>>> -{
>>>>>> -    struct sync_timeline *obj;
>>>>>> -    char task_comm[TASK_COMM_LEN];
>>>>>> -
>>>>>> -    get_task_comm(task_comm, current);
>>>>>> -
>>>>>> -    obj = sync_timeline_create(task_comm);
>>>>>> -    if (!obj)
>>>>>> -            return -ENOMEM;
>>>>>> -
>>>>>> -    file->private_data = obj;
>>>>>> -
>>>>>> -    return 0;
>>>>>> -}
>>>>>> -
>>>>>> -static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
>>>>>> -{
>>>>>> -    struct sync_timeline *obj = file->private_data;
>>>>>> -    struct sync_pt *pt, *next;
>>>>>> -
>>>>>> -    spin_lock_irq(&obj->lock);
>>>>>> -
>>>>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
>>>>>> -            dma_fence_set_error(&pt->base, -ENOENT);
>>>>>> -            dma_fence_signal_locked(&pt->base);
>>>>>> -    }
>>>>>> -
>>>>>> -    spin_unlock_irq(&obj->lock);
>>>>>> -
>>>>>> -    sync_timeline_put(obj);
>>>>>> -    return 0;
>>>>>> -}
>>>>>> -
>>>>>> -static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
>>>>>> -                                   unsigned long arg)
>>>>>> -{
>>>>>> -    int fd = get_unused_fd_flags(O_CLOEXEC);
>>>>>> -    int err;
>>>>>> -    struct sync_pt *pt;
>>>>>> -    struct sync_file *sync_file;
>>>>>> -    struct sw_sync_create_fence_data data;
>>>>>> -
>>>>>> -    if (fd < 0)
>>>>>> -            return fd;
>>>>>> -
>>>>>> -    if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
>>>>>> -            err = -EFAULT;
>>>>>> -            goto err;
>>>>>> -    }
>>>>>> -
>>>>>> -    pt = sync_pt_create(obj, data.value);
>>>>>> -    if (!pt) {
>>>>>> -            err = -ENOMEM;
>>>>>> -            goto err;
>>>>>> -    }
>>>>>> -
>>>>>> -    sync_file = sync_file_create(&pt->base);
>>>>>> -    dma_fence_put(&pt->base);
>>>>>> -    if (!sync_file) {
>>>>>> -            err = -ENOMEM;
>>>>>> -            goto err;
>>>>>> -    }
>>>>>> -
>>>>>> -    data.fence = fd;
>>>>>> -    if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
>>>>>> -            fput(sync_file->file);
>>>>>> -            err = -EFAULT;
>>>>>> -            goto err;
>>>>>> -    }
>>>>>> -
>>>>>> -    fd_install(fd, sync_file->file);
>>>>>> -
>>>>>> -    return 0;
>>>>>> -
>>>>>> -err:
>>>>>> -    put_unused_fd(fd);
>>>>>> -    return err;
>>>>>> -}
>>>>>> -
>>>>>> -static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
>>>>>> -{
>>>>>> -    u32 value;
>>>>>> -
>>>>>> -    if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
>>>>>> -            return -EFAULT;
>>>>>> -
>>>>>> -    while (value > INT_MAX)  {
>>>>>> -            sync_timeline_signal(obj, INT_MAX);
>>>>>> -            value -= INT_MAX;
>>>>>> -    }
>>>>>> -
>>>>>> -    sync_timeline_signal(obj, value);
>>>>>> -
>>>>>> -    return 0;
>>>>>> -}
>>>>>> -
>>>>>> -static long sw_sync_ioctl(struct file *file, unsigned int cmd,
>>>>>> -                      unsigned long arg)
>>>>>> -{
>>>>>> -    struct sync_timeline *obj = file->private_data;
>>>>>> -
>>>>>> -    switch (cmd) {
>>>>>> -    case SW_SYNC_IOC_CREATE_FENCE:
>>>>>> -            return sw_sync_ioctl_create_fence(obj, arg);
>>>>>> -
>>>>>> -    case SW_SYNC_IOC_INC:
>>>>>> -            return sw_sync_ioctl_inc(obj, arg);
>>>>>> -
>>>>>> -    default:
>>>>>> -            return -ENOTTY;
>>>>>> -    }
>>>>>> -}
>>>>>> -
>>>>>> -const struct file_operations sw_sync_debugfs_fops = {
>>>>>> -    .open           = sw_sync_debugfs_open,
>>>>>> -    .release        = sw_sync_debugfs_release,
>>>>>> -    .unlocked_ioctl = sw_sync_ioctl,
>>>>>> -    .compat_ioctl   = compat_ptr_ioctl,
>>>>>> -};
>>>>>> diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
>>>>>> deleted file mode 100644
>>>>>> index 101394f16930..000000000000
>>>>>> --- a/drivers/dma-buf/sync_debug.c
>>>>>> +++ /dev/null
>>>>>> @@ -1,190 +0,0 @@
>>>>>> -// SPDX-License-Identifier: GPL-2.0-only
>>>>>> -/*
>>>>>> - * Sync File validation framework and debug information
>>>>>> - *
>>>>>> - * Copyright (C) 2012 Google, Inc.
>>>>>> - */
>>>>>> -
>>>>>> -#include <linux/debugfs.h>
>>>>>> -#include "sync_debug.h"
>>>>>> -
>>>>>> -static struct dentry *dbgfs;
>>>>>> -
>>>>>> -static LIST_HEAD(sync_timeline_list_head);
>>>>>> -static DEFINE_SPINLOCK(sync_timeline_list_lock);
>>>>>> -static LIST_HEAD(sync_file_list_head);
>>>>>> -static DEFINE_SPINLOCK(sync_file_list_lock);
>>>>>> -
>>>>>> -void sync_timeline_debug_add(struct sync_timeline *obj)
>>>>>> -{
>>>>>> -    unsigned long flags;
>>>>>> -
>>>>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
>>>>>> -    list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
>>>>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
>>>>>> -}
>>>>>> -
>>>>>> -void sync_timeline_debug_remove(struct sync_timeline *obj)
>>>>>> -{
>>>>>> -    unsigned long flags;
>>>>>> -
>>>>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
>>>>>> -    list_del(&obj->sync_timeline_list);
>>>>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
>>>>>> -}
>>>>>> -
>>>>>> -void sync_file_debug_add(struct sync_file *sync_file)
>>>>>> -{
>>>>>> -    unsigned long flags;
>>>>>> -
>>>>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
>>>>>> -    list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
>>>>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
>>>>>> -}
>>>>>> -
>>>>>> -void sync_file_debug_remove(struct sync_file *sync_file)
>>>>>> -{
>>>>>> -    unsigned long flags;
>>>>>> -
>>>>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
>>>>>> -    list_del(&sync_file->sync_file_list);
>>>>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
>>>>>> -}
>>>>>> -
>>>>>> -static const char *sync_status_str(int status)
>>>>>> -{
>>>>>> -    if (status < 0)
>>>>>> -            return "error";
>>>>>> -
>>>>>> -    if (status > 0)
>>>>>> -            return "signaled";
>>>>>> -
>>>>>> -    return "active";
>>>>>> -}
>>>>>> -
>>>>>> -static void sync_print_fence(struct seq_file *s,
>>>>>> -                         struct dma_fence *fence, bool show)
>>>>>> -{
>>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>>>> -    int status;
>>>>>> -
>>>>>> -    status = dma_fence_get_status_locked(fence);
>>>>>> -
>>>>>> -    seq_printf(s, "  %s%sfence %s",
>>>>>> -               show ? parent->name : "",
>>>>>> -               show ? "_" : "",
>>>>>> -               sync_status_str(status));
>>>>>> -
>>>>>> -    if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
>>>>>> -            struct timespec64 ts64 =
>>>>>> -                    ktime_to_timespec64(fence->timestamp);
>>>>>> -
>>>>>> -            seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
>>>>>> -    }
>>>>>> -
>>>>>> -    if (fence->ops->timeline_value_str &&
>>>>>> -            fence->ops->fence_value_str) {
>>>>>> -            char value[64];
>>>>>> -            bool success;
>>>>>> -
>>>>>> -            fence->ops->fence_value_str(fence, value, sizeof(value));
>>>>>> -            success = strlen(value);
>>>>>> -
>>>>>> -            if (success) {
>>>>>> -                    seq_printf(s, ": %s", value);
>>>>>> -
>>>>>> -                    fence->ops->timeline_value_str(fence, value,
>>>>>> -                                                   sizeof(value));
>>>>>> -
>>>>>> -                    if (strlen(value))
>>>>>> -                            seq_printf(s, " / %s", value);
>>>>>> -            }
>>>>>> -    }
>>>>>> -
>>>>>> -    seq_putc(s, '\n');
>>>>>> -}
>>>>>> -
>>>>>> -static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
>>>>>> -{
>>>>>> -    struct list_head *pos;
>>>>>> -
>>>>>> -    seq_printf(s, "%s: %d\n", obj->name, obj->value);
>>>>>> -
>>>>>> -    spin_lock_irq(&obj->lock);
>>>>>> -    list_for_each(pos, &obj->pt_list) {
>>>>>> -            struct sync_pt *pt = container_of(pos, struct sync_pt, link);
>>>>>> -            sync_print_fence(s, &pt->base, false);
>>>>>> -    }
>>>>>> -    spin_unlock_irq(&obj->lock);
>>>>>> -}
>>>>>> -
>>>>>> -static void sync_print_sync_file(struct seq_file *s,
>>>>>> -                              struct sync_file *sync_file)
>>>>>> -{
>>>>>> -    char buf[128];
>>>>>> -    int i;
>>>>>> -
>>>>>> -    seq_printf(s, "[%p] %s: %s\n", sync_file,
>>>>>> -               sync_file_get_name(sync_file, buf, sizeof(buf)),
>>>>>> -               sync_status_str(dma_fence_get_status(sync_file->fence)));
>>>>>> -
>>>>>> -    if (dma_fence_is_array(sync_file->fence)) {
>>>>>> -            struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
>>>>>> -
>>>>>> -            for (i = 0; i < array->num_fences; ++i)
>>>>>> -                    sync_print_fence(s, array->fences[i], true);
>>>>>> -    } else {
>>>>>> -            sync_print_fence(s, sync_file->fence, true);
>>>>>> -    }
>>>>>> -}
>>>>>> -
>>>>>> -static int sync_info_debugfs_show(struct seq_file *s, void *unused)
>>>>>> -{
>>>>>> -    struct list_head *pos;
>>>>>> -
>>>>>> -    seq_puts(s, "objs:\n--------------\n");
>>>>>> -
>>>>>> -    spin_lock_irq(&sync_timeline_list_lock);
>>>>>> -    list_for_each(pos, &sync_timeline_list_head) {
>>>>>> -            struct sync_timeline *obj =
>>>>>> -                    container_of(pos, struct sync_timeline,
>>>>>> -                                 sync_timeline_list);
>>>>>> -
>>>>>> -            sync_print_obj(s, obj);
>>>>>> -            seq_putc(s, '\n');
>>>>>> -    }
>>>>>> -    spin_unlock_irq(&sync_timeline_list_lock);
>>>>>> -
>>>>>> -    seq_puts(s, "fences:\n--------------\n");
>>>>>> -
>>>>>> -    spin_lock_irq(&sync_file_list_lock);
>>>>>> -    list_for_each(pos, &sync_file_list_head) {
>>>>>> -            struct sync_file *sync_file =
>>>>>> -                    container_of(pos, struct sync_file, sync_file_list);
>>>>>> -
>>>>>> -            sync_print_sync_file(s, sync_file);
>>>>>> -            seq_putc(s, '\n');
>>>>>> -    }
>>>>>> -    spin_unlock_irq(&sync_file_list_lock);
>>>>>> -    return 0;
>>>>>> -}
>>>>>> -
>>>>>> -DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
>>>>>> -
>>>>>> -static __init int sync_debugfs_init(void)
>>>>>> -{
>>>>>> -    dbgfs = debugfs_create_dir("sync", NULL);
>>>>>> -
>>>>>> -    /*
>>>>>> -     * The debugfs files won't ever get removed and thus, there is
>>>>>> -     * no need to protect it against removal races. The use of
>>>>>> -     * debugfs_create_file_unsafe() is actually safe here.
>>>>>> -     */
>>>>>> -    debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
>>>>>> -                               &sync_info_debugfs_fops);
>>>>>> -    debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
>>>>>> -                               &sw_sync_debugfs_fops);
>>>>>> -
>>>>>> -    return 0;
>>>>>> -}
>>>>>> -late_initcall(sync_debugfs_init);
>>>>>> diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
>>>>>> deleted file mode 100644
>>>>>> index 6176e52ba2d7..000000000000
>>>>>> --- a/drivers/dma-buf/sync_debug.h
>>>>>> +++ /dev/null
>>>>>> @@ -1,72 +0,0 @@
>>>>>> -/*
>>>>>> - * Sync File validation framework and debug infomation
>>>>>> - *
>>>>>> - * Copyright (C) 2012 Google, Inc.
>>>>>> - *
>>>>>> - * This program is distributed in the hope that it will be useful,
>>>>>> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
>>>>>> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>>>>>> - * GNU General Public License for more details.
>>>>>> - *
>>>>>> - */
>>>>>> -
>>>>>> -#ifndef _LINUX_SYNC_H
>>>>>> -#define _LINUX_SYNC_H
>>>>>> -
>>>>>> -#include <linux/list.h>
>>>>>> -#include <linux/rbtree.h>
>>>>>> -#include <linux/spinlock.h>
>>>>>> -#include <linux/dma-fence.h>
>>>>>> -
>>>>>> -#include <linux/sync_file.h>
>>>>>> -#include <uapi/linux/sync_file.h>
>>>>>> -
>>>>>> -/**
>>>>>> - * struct sync_timeline - sync object
>>>>>> - * @kref:           reference count on fence.
>>>>>> - * @name:           name of the sync_timeline. Useful for debugging
>>>>>> - * @lock:           lock protecting @pt_list and @value
>>>>>> - * @pt_tree:                rbtree of active (unsignaled/errored) sync_pts
>>>>>> - * @pt_list:                list of active (unsignaled/errored) sync_pts
>>>>>> - * @sync_timeline_list:     membership in global sync_timeline_list
>>>>>> - */
>>>>>> -struct sync_timeline {
>>>>>> -    struct kref             kref;
>>>>>> -    char                    name[32];
>>>>>> -
>>>>>> -    /* protected by lock */
>>>>>> -    u64                     context;
>>>>>> -    int                     value;
>>>>>> -
>>>>>> -    struct rb_root          pt_tree;
>>>>>> -    struct list_head        pt_list;
>>>>>> -    spinlock_t              lock;
>>>>>> -
>>>>>> -    struct list_head        sync_timeline_list;
>>>>>> -};
>>>>>> -
>>>>>> -static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
>>>>>> -{
>>>>>> -    return container_of(fence->lock, struct sync_timeline, lock);
>>>>>> -}
>>>>>> -
>>>>>> -/**
>>>>>> - * struct sync_pt - sync_pt object
>>>>>> - * @base: base fence object
>>>>>> - * @link: link on the sync timeline's list
>>>>>> - * @node: node in the sync timeline's tree
>>>>>> - */
>>>>>> -struct sync_pt {
>>>>>> -    struct dma_fence base;
>>>>>> -    struct list_head link;
>>>>>> -    struct rb_node node;
>>>>>> -};
>>>>>> -
>>>>>> -extern const struct file_operations sw_sync_debugfs_fops;
>>>>>> -
>>>>>> -void sync_timeline_debug_add(struct sync_timeline *obj);
>>>>>> -void sync_timeline_debug_remove(struct sync_timeline *obj);
>>>>>> -void sync_file_debug_add(struct sync_file *fence);
>>>>>> -void sync_file_debug_remove(struct sync_file *fence);
>>>>>> -
>>>>>> -#endif /* _LINUX_SYNC_H */
>>>>>> --
>>>>>> 2.25.1
>>>>>>
>


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
@ 2021-07-29 12:25               ` Christian König
  0 siblings, 0 replies; 26+ messages in thread
From: Christian König @ 2021-07-29 12:25 UTC (permalink / raw)
  To: Daniel Vetter
  Cc: moderated list:DMA BUFFER SHARING FRAMEWORK, Gustavo Padovan,
	dri-devel, open list:DMA BUFFER SHARING FRAMEWORK

Am 29.07.21 um 13:59 schrieb Daniel Vetter:
> On Thu, Jul 29, 2021 at 12:21 PM Christian König
> <ckoenig.leichtzumerken@gmail.com> wrote:
>> Am 29.07.21 um 11:03 schrieb Daniel Vetter:
>>> On Thu, Jul 29, 2021 at 10:38 AM Christian König
>>> <ckoenig.leichtzumerken@gmail.com> wrote:
>>>> Am 29.07.21 um 09:23 schrieb Daniel Vetter:
>>>>> On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
>>>>>> As we now knew controlling dma_fence synchronization from userspace is
>>>>>> extremely dangerous and can not only deadlock drivers but trivially also the
>>>>>> whole kernel memory management.
>>>>>>
>>>>>> Entirely remove this option. We now have in kernel unit tests to exercise the
>>>>>> dma_fence framework and it's containers.
>>>>>>
>>>>>> Signed-off-by: Christian König <christian.koenig@amd.com>
>>>>> There's also igts for this, and Android heavily uses this. So I'm not sure
>>>>> we can just nuke it.
>>>> I feared that you would say that.
>>>>
>>>>> I guess what we could do is maybe just taint the kernel if it's ever used?
>>>> What should we use then? TAINT_SOFTLOCKUP or TAINT_CRAP? :)
>>> I think the cleanest solution would be to install a 10s timer on each
>>> sw fence, and TAINT_SOFTLOOKUP if it fails to signal. That's similar
>>> to what we do with vgem, except vgem then just steps in with a
>>> dma_fence_signal() instead of tainting the kernel.
>> Well vgem is only for testing as far as I know.
> I think CrOS is using it to shovel buffers around between process, as
> a convenient allocator for sw rendering dma-buf. But they don't use
> the dma-fence part afaik.
>
> We can't taint in vgem fences though because our CI reboots on any
> taint (or lockdep splat) and a few others, so that would make it
> useless for testing. Maybe we should have the fence part of vgem
> behind a separate Kconfig?

And then making it depend on CONFIG_BROKEN? :)

But more serious, that it probably makes sense.

Can you take care of that? I'm not that familiar with the vgem code base.

>> When Android is using this for a production environment I think we
>> should be a bit harder and explicitly note that this is not a good idea
>> in sw_sync_debugfs_open().
>>
>>> I think that should keep everyone happy, because if your Android has a
>>> fence stuck for more than 10s it's busted.
>> Well when they are using userptrs they are busted as soon as they have a
>> fence stuck for much less time.
> Hm right I forgot that you can't guarantee that it will complete, ever :-)
>
> Otoh I'm not sure the unconditional taint is very nice to the android
> folks, they'll probably just patch it out and then it looses it
> usefulness. taint on timeout otoh makes sure that normally it's not
> hit, but anytime you die in userptr or shrinker there will be a taint
> before anything else (since 10s is much shorter than the "stuck in
> non-interruptible sleep" warning).

And then you don't hit the timeout during test but only in production, 
e.g. not good enough either.

And yes I know that this is not really nice. But since this depends on 
debugfs and Android usually disables debugfs AFAIK, they probably have 
that heavily patched already anyway.

I just want to make sure that nobody else has the brilliant idea to 
enable this and build an userspace stack on top of a fundamentally 
broken approach.

Alternative is to add a dependency to CONFIG_BROKEN.

Christian.

> -Daniel
>
>> Regards,
>> Christian.
>>
>>> Note that with hwc1 the indefinite fence was actually the model, when
>>> you did a page_flip you got an out-fence back which will only be
>>> signalled when the new buffer _stopped_ being used for scanout. Which
>>> generally depends upon the next page-flip happening (except for manual
>>> mode display, which only scan out once, if you ignore that the
>>> kernel's supposed to restore the display after suspend/resume, which
>>> android did). hwc2 fixed this, so a 10s timeout-to-taint should be
>>> fine with Android people.
>>>
>>> Also since we do have the igt/sw_sync.c test intel-gfx-ci should be
>>> able to check that code even.
>>> -Daniel
>>>
>>>> Regards,
>>>> Christian.
>>>>
>>>>> Also for testing we use vgem now, which enforces a timeout.
>>>>> -Daniel
>>>>>
>>>>>> ---
>>>>>>     drivers/dma-buf/Kconfig      |  13 --
>>>>>>     drivers/dma-buf/Makefile     |   1 -
>>>>>>     drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
>>>>>>     drivers/dma-buf/sync_debug.c | 190 ----------------
>>>>>>     drivers/dma-buf/sync_debug.h |  72 ------
>>>>>>     5 files changed, 688 deletions(-)
>>>>>>     delete mode 100644 drivers/dma-buf/sw_sync.c
>>>>>>     delete mode 100644 drivers/dma-buf/sync_debug.c
>>>>>>     delete mode 100644 drivers/dma-buf/sync_debug.h
>>>>>>
>>>>>> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
>>>>>> index 9561e3d2d428..26c53f45616a 100644
>>>>>> --- a/drivers/dma-buf/Kconfig
>>>>>> +++ b/drivers/dma-buf/Kconfig
>>>>>> @@ -17,19 +17,6 @@ config SYNC_FILE
>>>>>>          Files fds, to the DRM driver for example. More details at
>>>>>>          Documentation/driver-api/sync_file.rst.
>>>>>>
>>>>>> -config SW_SYNC
>>>>>> -    bool "Sync File Validation Framework"
>>>>>> -    default n
>>>>>> -    depends on SYNC_FILE
>>>>>> -    depends on DEBUG_FS
>>>>>> -    help
>>>>>> -      A sync object driver that uses a 32bit counter to coordinate
>>>>>> -      synchronization.  Useful when there is no hardware primitive backing
>>>>>> -      the synchronization.
>>>>>> -
>>>>>> -      WARNING: improper use of this can result in deadlocking kernel
>>>>>> -      drivers from userspace. Intended for test and debug only.
>>>>>> -
>>>>>>     config UDMABUF
>>>>>>        bool "userspace dmabuf misc driver"
>>>>>>        default n
>>>>>> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
>>>>>> index 1ef021273a06..fa3af0953f87 100644
>>>>>> --- a/drivers/dma-buf/Makefile
>>>>>> +++ b/drivers/dma-buf/Makefile
>>>>>> @@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
>>>>>>     obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o
>>>>>>     obj-$(CONFIG_DMABUF_HEAPS) += heaps/
>>>>>>     obj-$(CONFIG_SYNC_FILE)            += sync_file.o
>>>>>> -obj-$(CONFIG_SW_SYNC)               += sw_sync.o sync_debug.o
>>>>>>     obj-$(CONFIG_UDMABUF)              += udmabuf.o
>>>>>>     obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
>>>>>>
>>>>>> diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
>>>>>> deleted file mode 100644
>>>>>> index 348b3a9170fa..000000000000
>>>>>> --- a/drivers/dma-buf/sw_sync.c
>>>>>> +++ /dev/null
>>>>>> @@ -1,412 +0,0 @@
>>>>>> -// SPDX-License-Identifier: GPL-2.0-only
>>>>>> -/*
>>>>>> - * Sync File validation framework
>>>>>> - *
>>>>>> - * Copyright (C) 2012 Google, Inc.
>>>>>> - */
>>>>>> -
>>>>>> -#include <linux/file.h>
>>>>>> -#include <linux/fs.h>
>>>>>> -#include <linux/uaccess.h>
>>>>>> -#include <linux/slab.h>
>>>>>> -#include <linux/sync_file.h>
>>>>>> -
>>>>>> -#include "sync_debug.h"
>>>>>> -
>>>>>> -#define CREATE_TRACE_POINTS
>>>>>> -#include "sync_trace.h"
>>>>>> -
>>>>>> -/*
>>>>>> - * SW SYNC validation framework
>>>>>> - *
>>>>>> - * A sync object driver that uses a 32bit counter to coordinate
>>>>>> - * synchronization.  Useful when there is no hardware primitive backing
>>>>>> - * the synchronization.
>>>>>> - *
>>>>>> - * To start the framework just open:
>>>>>> - *
>>>>>> - * <debugfs>/sync/sw_sync
>>>>>> - *
>>>>>> - * That will create a sync timeline, all fences created under this timeline
>>>>>> - * file descriptor will belong to the this timeline.
>>>>>> - *
>>>>>> - * The 'sw_sync' file can be opened many times as to create different
>>>>>> - * timelines.
>>>>>> - *
>>>>>> - * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
>>>>>> - * sw_sync_create_fence_data as parameter.
>>>>>> - *
>>>>>> - * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
>>>>>> - * with the increment as u32. This will update the last signaled value
>>>>>> - * from the timeline and signal any fence that has a seqno smaller or equal
>>>>>> - * to it.
>>>>>> - *
>>>>>> - * struct sw_sync_create_fence_data
>>>>>> - * @value:  the seqno to initialise the fence with
>>>>>> - * @name:   the name of the new sync point
>>>>>> - * @fence:  return the fd of the new sync_file with the created fence
>>>>>> - */
>>>>>> -struct sw_sync_create_fence_data {
>>>>>> -    __u32   value;
>>>>>> -    char    name[32];
>>>>>> -    __s32   fence; /* fd of new fence */
>>>>>> -};
>>>>>> -
>>>>>> -#define SW_SYNC_IOC_MAGIC   'W'
>>>>>> -
>>>>>> -#define SW_SYNC_IOC_CREATE_FENCE    _IOWR(SW_SYNC_IOC_MAGIC, 0,\
>>>>>> -            struct sw_sync_create_fence_data)
>>>>>> -
>>>>>> -#define SW_SYNC_IOC_INC                     _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
>>>>>> -
>>>>>> -static const struct dma_fence_ops timeline_fence_ops;
>>>>>> -
>>>>>> -static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
>>>>>> -{
>>>>>> -    if (fence->ops != &timeline_fence_ops)
>>>>>> -            return NULL;
>>>>>> -    return container_of(fence, struct sync_pt, base);
>>>>>> -}
>>>>>> -
>>>>>> -/**
>>>>>> - * sync_timeline_create() - creates a sync object
>>>>>> - * @name:   sync_timeline name
>>>>>> - *
>>>>>> - * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
>>>>>> - * case of error.
>>>>>> - */
>>>>>> -static struct sync_timeline *sync_timeline_create(const char *name)
>>>>>> -{
>>>>>> -    struct sync_timeline *obj;
>>>>>> -
>>>>>> -    obj = kzalloc(sizeof(*obj), GFP_KERNEL);
>>>>>> -    if (!obj)
>>>>>> -            return NULL;
>>>>>> -
>>>>>> -    kref_init(&obj->kref);
>>>>>> -    obj->context = dma_fence_context_alloc(1);
>>>>>> -    strlcpy(obj->name, name, sizeof(obj->name));
>>>>>> -
>>>>>> -    obj->pt_tree = RB_ROOT;
>>>>>> -    INIT_LIST_HEAD(&obj->pt_list);
>>>>>> -    spin_lock_init(&obj->lock);
>>>>>> -
>>>>>> -    sync_timeline_debug_add(obj);
>>>>>> -
>>>>>> -    return obj;
>>>>>> -}
>>>>>> -
>>>>>> -static void sync_timeline_free(struct kref *kref)
>>>>>> -{
>>>>>> -    struct sync_timeline *obj =
>>>>>> -            container_of(kref, struct sync_timeline, kref);
>>>>>> -
>>>>>> -    sync_timeline_debug_remove(obj);
>>>>>> -
>>>>>> -    kfree(obj);
>>>>>> -}
>>>>>> -
>>>>>> -static void sync_timeline_get(struct sync_timeline *obj)
>>>>>> -{
>>>>>> -    kref_get(&obj->kref);
>>>>>> -}
>>>>>> -
>>>>>> -static void sync_timeline_put(struct sync_timeline *obj)
>>>>>> -{
>>>>>> -    kref_put(&obj->kref, sync_timeline_free);
>>>>>> -}
>>>>>> -
>>>>>> -static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
>>>>>> -{
>>>>>> -    return "sw_sync";
>>>>>> -}
>>>>>> -
>>>>>> -static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
>>>>>> -{
>>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>>>> -
>>>>>> -    return parent->name;
>>>>>> -}
>>>>>> -
>>>>>> -static void timeline_fence_release(struct dma_fence *fence)
>>>>>> -{
>>>>>> -    struct sync_pt *pt = dma_fence_to_sync_pt(fence);
>>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>>>> -    unsigned long flags;
>>>>>> -
>>>>>> -    spin_lock_irqsave(fence->lock, flags);
>>>>>> -    if (!list_empty(&pt->link)) {
>>>>>> -            list_del(&pt->link);
>>>>>> -            rb_erase(&pt->node, &parent->pt_tree);
>>>>>> -    }
>>>>>> -    spin_unlock_irqrestore(fence->lock, flags);
>>>>>> -
>>>>>> -    sync_timeline_put(parent);
>>>>>> -    dma_fence_free(fence);
>>>>>> -}
>>>>>> -
>>>>>> -static bool timeline_fence_signaled(struct dma_fence *fence)
>>>>>> -{
>>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>>>> -
>>>>>> -    return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
>>>>>> -}
>>>>>> -
>>>>>> -static bool timeline_fence_enable_signaling(struct dma_fence *fence)
>>>>>> -{
>>>>>> -    return true;
>>>>>> -}
>>>>>> -
>>>>>> -static void timeline_fence_value_str(struct dma_fence *fence,
>>>>>> -                                char *str, int size)
>>>>>> -{
>>>>>> -    snprintf(str, size, "%lld", fence->seqno);
>>>>>> -}
>>>>>> -
>>>>>> -static void timeline_fence_timeline_value_str(struct dma_fence *fence,
>>>>>> -                                         char *str, int size)
>>>>>> -{
>>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>>>> -
>>>>>> -    snprintf(str, size, "%d", parent->value);
>>>>>> -}
>>>>>> -
>>>>>> -static const struct dma_fence_ops timeline_fence_ops = {
>>>>>> -    .get_driver_name = timeline_fence_get_driver_name,
>>>>>> -    .get_timeline_name = timeline_fence_get_timeline_name,
>>>>>> -    .enable_signaling = timeline_fence_enable_signaling,
>>>>>> -    .signaled = timeline_fence_signaled,
>>>>>> -    .release = timeline_fence_release,
>>>>>> -    .fence_value_str = timeline_fence_value_str,
>>>>>> -    .timeline_value_str = timeline_fence_timeline_value_str,
>>>>>> -};
>>>>>> -
>>>>>> -/**
>>>>>> - * sync_timeline_signal() - signal a status change on a sync_timeline
>>>>>> - * @obj:    sync_timeline to signal
>>>>>> - * @inc:    num to increment on timeline->value
>>>>>> - *
>>>>>> - * A sync implementation should call this any time one of it's fences
>>>>>> - * has signaled or has an error condition.
>>>>>> - */
>>>>>> -static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
>>>>>> -{
>>>>>> -    struct sync_pt *pt, *next;
>>>>>> -
>>>>>> -    trace_sync_timeline(obj);
>>>>>> -
>>>>>> -    spin_lock_irq(&obj->lock);
>>>>>> -
>>>>>> -    obj->value += inc;
>>>>>> -
>>>>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
>>>>>> -            if (!timeline_fence_signaled(&pt->base))
>>>>>> -                    break;
>>>>>> -
>>>>>> -            list_del_init(&pt->link);
>>>>>> -            rb_erase(&pt->node, &obj->pt_tree);
>>>>>> -
>>>>>> -            /*
>>>>>> -             * A signal callback may release the last reference to this
>>>>>> -             * fence, causing it to be freed. That operation has to be
>>>>>> -             * last to avoid a use after free inside this loop, and must
>>>>>> -             * be after we remove the fence from the timeline in order to
>>>>>> -             * prevent deadlocking on timeline->lock inside
>>>>>> -             * timeline_fence_release().
>>>>>> -             */
>>>>>> -            dma_fence_signal_locked(&pt->base);
>>>>>> -    }
>>>>>> -
>>>>>> -    spin_unlock_irq(&obj->lock);
>>>>>> -}
>>>>>> -
>>>>>> -/**
>>>>>> - * sync_pt_create() - creates a sync pt
>>>>>> - * @obj:    parent sync_timeline
>>>>>> - * @value:  value of the fence
>>>>>> - *
>>>>>> - * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
>>>>>> - * allocated allowing for implementation specific data to be kept after
>>>>>> - * the generic sync_timeline struct. Returns the sync_pt object or
>>>>>> - * NULL in case of error.
>>>>>> - */
>>>>>> -static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
>>>>>> -                                  unsigned int value)
>>>>>> -{
>>>>>> -    struct sync_pt *pt;
>>>>>> -
>>>>>> -    pt = kzalloc(sizeof(*pt), GFP_KERNEL);
>>>>>> -    if (!pt)
>>>>>> -            return NULL;
>>>>>> -
>>>>>> -    sync_timeline_get(obj);
>>>>>> -    dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
>>>>>> -                   obj->context, value);
>>>>>> -    INIT_LIST_HEAD(&pt->link);
>>>>>> -
>>>>>> -    spin_lock_irq(&obj->lock);
>>>>>> -    if (!dma_fence_is_signaled_locked(&pt->base)) {
>>>>>> -            struct rb_node **p = &obj->pt_tree.rb_node;
>>>>>> -            struct rb_node *parent = NULL;
>>>>>> -
>>>>>> -            while (*p) {
>>>>>> -                    struct sync_pt *other;
>>>>>> -                    int cmp;
>>>>>> -
>>>>>> -                    parent = *p;
>>>>>> -                    other = rb_entry(parent, typeof(*pt), node);
>>>>>> -                    cmp = value - other->base.seqno;
>>>>>> -                    if (cmp > 0) {
>>>>>> -                            p = &parent->rb_right;
>>>>>> -                    } else if (cmp < 0) {
>>>>>> -                            p = &parent->rb_left;
>>>>>> -                    } else {
>>>>>> -                            if (dma_fence_get_rcu(&other->base)) {
>>>>>> -                                    sync_timeline_put(obj);
>>>>>> -                                    kfree(pt);
>>>>>> -                                    pt = other;
>>>>>> -                                    goto unlock;
>>>>>> -                            }
>>>>>> -                            p = &parent->rb_left;
>>>>>> -                    }
>>>>>> -            }
>>>>>> -            rb_link_node(&pt->node, parent, p);
>>>>>> -            rb_insert_color(&pt->node, &obj->pt_tree);
>>>>>> -
>>>>>> -            parent = rb_next(&pt->node);
>>>>>> -            list_add_tail(&pt->link,
>>>>>> -                          parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
>>>>>> -    }
>>>>>> -unlock:
>>>>>> -    spin_unlock_irq(&obj->lock);
>>>>>> -
>>>>>> -    return pt;
>>>>>> -}
>>>>>> -
>>>>>> -/*
>>>>>> - * *WARNING*
>>>>>> - *
>>>>>> - * improper use of this can result in deadlocking kernel drivers from userspace.
>>>>>> - */
>>>>>> -
>>>>>> -/* opening sw_sync create a new sync obj */
>>>>>> -static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
>>>>>> -{
>>>>>> -    struct sync_timeline *obj;
>>>>>> -    char task_comm[TASK_COMM_LEN];
>>>>>> -
>>>>>> -    get_task_comm(task_comm, current);
>>>>>> -
>>>>>> -    obj = sync_timeline_create(task_comm);
>>>>>> -    if (!obj)
>>>>>> -            return -ENOMEM;
>>>>>> -
>>>>>> -    file->private_data = obj;
>>>>>> -
>>>>>> -    return 0;
>>>>>> -}
>>>>>> -
>>>>>> -static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
>>>>>> -{
>>>>>> -    struct sync_timeline *obj = file->private_data;
>>>>>> -    struct sync_pt *pt, *next;
>>>>>> -
>>>>>> -    spin_lock_irq(&obj->lock);
>>>>>> -
>>>>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
>>>>>> -            dma_fence_set_error(&pt->base, -ENOENT);
>>>>>> -            dma_fence_signal_locked(&pt->base);
>>>>>> -    }
>>>>>> -
>>>>>> -    spin_unlock_irq(&obj->lock);
>>>>>> -
>>>>>> -    sync_timeline_put(obj);
>>>>>> -    return 0;
>>>>>> -}
>>>>>> -
>>>>>> -static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
>>>>>> -                                   unsigned long arg)
>>>>>> -{
>>>>>> -    int fd = get_unused_fd_flags(O_CLOEXEC);
>>>>>> -    int err;
>>>>>> -    struct sync_pt *pt;
>>>>>> -    struct sync_file *sync_file;
>>>>>> -    struct sw_sync_create_fence_data data;
>>>>>> -
>>>>>> -    if (fd < 0)
>>>>>> -            return fd;
>>>>>> -
>>>>>> -    if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
>>>>>> -            err = -EFAULT;
>>>>>> -            goto err;
>>>>>> -    }
>>>>>> -
>>>>>> -    pt = sync_pt_create(obj, data.value);
>>>>>> -    if (!pt) {
>>>>>> -            err = -ENOMEM;
>>>>>> -            goto err;
>>>>>> -    }
>>>>>> -
>>>>>> -    sync_file = sync_file_create(&pt->base);
>>>>>> -    dma_fence_put(&pt->base);
>>>>>> -    if (!sync_file) {
>>>>>> -            err = -ENOMEM;
>>>>>> -            goto err;
>>>>>> -    }
>>>>>> -
>>>>>> -    data.fence = fd;
>>>>>> -    if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
>>>>>> -            fput(sync_file->file);
>>>>>> -            err = -EFAULT;
>>>>>> -            goto err;
>>>>>> -    }
>>>>>> -
>>>>>> -    fd_install(fd, sync_file->file);
>>>>>> -
>>>>>> -    return 0;
>>>>>> -
>>>>>> -err:
>>>>>> -    put_unused_fd(fd);
>>>>>> -    return err;
>>>>>> -}
>>>>>> -
>>>>>> -static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
>>>>>> -{
>>>>>> -    u32 value;
>>>>>> -
>>>>>> -    if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
>>>>>> -            return -EFAULT;
>>>>>> -
>>>>>> -    while (value > INT_MAX)  {
>>>>>> -            sync_timeline_signal(obj, INT_MAX);
>>>>>> -            value -= INT_MAX;
>>>>>> -    }
>>>>>> -
>>>>>> -    sync_timeline_signal(obj, value);
>>>>>> -
>>>>>> -    return 0;
>>>>>> -}
>>>>>> -
>>>>>> -static long sw_sync_ioctl(struct file *file, unsigned int cmd,
>>>>>> -                      unsigned long arg)
>>>>>> -{
>>>>>> -    struct sync_timeline *obj = file->private_data;
>>>>>> -
>>>>>> -    switch (cmd) {
>>>>>> -    case SW_SYNC_IOC_CREATE_FENCE:
>>>>>> -            return sw_sync_ioctl_create_fence(obj, arg);
>>>>>> -
>>>>>> -    case SW_SYNC_IOC_INC:
>>>>>> -            return sw_sync_ioctl_inc(obj, arg);
>>>>>> -
>>>>>> -    default:
>>>>>> -            return -ENOTTY;
>>>>>> -    }
>>>>>> -}
>>>>>> -
>>>>>> -const struct file_operations sw_sync_debugfs_fops = {
>>>>>> -    .open           = sw_sync_debugfs_open,
>>>>>> -    .release        = sw_sync_debugfs_release,
>>>>>> -    .unlocked_ioctl = sw_sync_ioctl,
>>>>>> -    .compat_ioctl   = compat_ptr_ioctl,
>>>>>> -};
>>>>>> diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
>>>>>> deleted file mode 100644
>>>>>> index 101394f16930..000000000000
>>>>>> --- a/drivers/dma-buf/sync_debug.c
>>>>>> +++ /dev/null
>>>>>> @@ -1,190 +0,0 @@
>>>>>> -// SPDX-License-Identifier: GPL-2.0-only
>>>>>> -/*
>>>>>> - * Sync File validation framework and debug information
>>>>>> - *
>>>>>> - * Copyright (C) 2012 Google, Inc.
>>>>>> - */
>>>>>> -
>>>>>> -#include <linux/debugfs.h>
>>>>>> -#include "sync_debug.h"
>>>>>> -
>>>>>> -static struct dentry *dbgfs;
>>>>>> -
>>>>>> -static LIST_HEAD(sync_timeline_list_head);
>>>>>> -static DEFINE_SPINLOCK(sync_timeline_list_lock);
>>>>>> -static LIST_HEAD(sync_file_list_head);
>>>>>> -static DEFINE_SPINLOCK(sync_file_list_lock);
>>>>>> -
>>>>>> -void sync_timeline_debug_add(struct sync_timeline *obj)
>>>>>> -{
>>>>>> -    unsigned long flags;
>>>>>> -
>>>>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
>>>>>> -    list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
>>>>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
>>>>>> -}
>>>>>> -
>>>>>> -void sync_timeline_debug_remove(struct sync_timeline *obj)
>>>>>> -{
>>>>>> -    unsigned long flags;
>>>>>> -
>>>>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
>>>>>> -    list_del(&obj->sync_timeline_list);
>>>>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
>>>>>> -}
>>>>>> -
>>>>>> -void sync_file_debug_add(struct sync_file *sync_file)
>>>>>> -{
>>>>>> -    unsigned long flags;
>>>>>> -
>>>>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
>>>>>> -    list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
>>>>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
>>>>>> -}
>>>>>> -
>>>>>> -void sync_file_debug_remove(struct sync_file *sync_file)
>>>>>> -{
>>>>>> -    unsigned long flags;
>>>>>> -
>>>>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
>>>>>> -    list_del(&sync_file->sync_file_list);
>>>>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
>>>>>> -}
>>>>>> -
>>>>>> -static const char *sync_status_str(int status)
>>>>>> -{
>>>>>> -    if (status < 0)
>>>>>> -            return "error";
>>>>>> -
>>>>>> -    if (status > 0)
>>>>>> -            return "signaled";
>>>>>> -
>>>>>> -    return "active";
>>>>>> -}
>>>>>> -
>>>>>> -static void sync_print_fence(struct seq_file *s,
>>>>>> -                         struct dma_fence *fence, bool show)
>>>>>> -{
>>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
>>>>>> -    int status;
>>>>>> -
>>>>>> -    status = dma_fence_get_status_locked(fence);
>>>>>> -
>>>>>> -    seq_printf(s, "  %s%sfence %s",
>>>>>> -               show ? parent->name : "",
>>>>>> -               show ? "_" : "",
>>>>>> -               sync_status_str(status));
>>>>>> -
>>>>>> -    if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
>>>>>> -            struct timespec64 ts64 =
>>>>>> -                    ktime_to_timespec64(fence->timestamp);
>>>>>> -
>>>>>> -            seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
>>>>>> -    }
>>>>>> -
>>>>>> -    if (fence->ops->timeline_value_str &&
>>>>>> -            fence->ops->fence_value_str) {
>>>>>> -            char value[64];
>>>>>> -            bool success;
>>>>>> -
>>>>>> -            fence->ops->fence_value_str(fence, value, sizeof(value));
>>>>>> -            success = strlen(value);
>>>>>> -
>>>>>> -            if (success) {
>>>>>> -                    seq_printf(s, ": %s", value);
>>>>>> -
>>>>>> -                    fence->ops->timeline_value_str(fence, value,
>>>>>> -                                                   sizeof(value));
>>>>>> -
>>>>>> -                    if (strlen(value))
>>>>>> -                            seq_printf(s, " / %s", value);
>>>>>> -            }
>>>>>> -    }
>>>>>> -
>>>>>> -    seq_putc(s, '\n');
>>>>>> -}
>>>>>> -
>>>>>> -static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
>>>>>> -{
>>>>>> -    struct list_head *pos;
>>>>>> -
>>>>>> -    seq_printf(s, "%s: %d\n", obj->name, obj->value);
>>>>>> -
>>>>>> -    spin_lock_irq(&obj->lock);
>>>>>> -    list_for_each(pos, &obj->pt_list) {
>>>>>> -            struct sync_pt *pt = container_of(pos, struct sync_pt, link);
>>>>>> -            sync_print_fence(s, &pt->base, false);
>>>>>> -    }
>>>>>> -    spin_unlock_irq(&obj->lock);
>>>>>> -}
>>>>>> -
>>>>>> -static void sync_print_sync_file(struct seq_file *s,
>>>>>> -                              struct sync_file *sync_file)
>>>>>> -{
>>>>>> -    char buf[128];
>>>>>> -    int i;
>>>>>> -
>>>>>> -    seq_printf(s, "[%p] %s: %s\n", sync_file,
>>>>>> -               sync_file_get_name(sync_file, buf, sizeof(buf)),
>>>>>> -               sync_status_str(dma_fence_get_status(sync_file->fence)));
>>>>>> -
>>>>>> -    if (dma_fence_is_array(sync_file->fence)) {
>>>>>> -            struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
>>>>>> -
>>>>>> -            for (i = 0; i < array->num_fences; ++i)
>>>>>> -                    sync_print_fence(s, array->fences[i], true);
>>>>>> -    } else {
>>>>>> -            sync_print_fence(s, sync_file->fence, true);
>>>>>> -    }
>>>>>> -}
>>>>>> -
>>>>>> -static int sync_info_debugfs_show(struct seq_file *s, void *unused)
>>>>>> -{
>>>>>> -    struct list_head *pos;
>>>>>> -
>>>>>> -    seq_puts(s, "objs:\n--------------\n");
>>>>>> -
>>>>>> -    spin_lock_irq(&sync_timeline_list_lock);
>>>>>> -    list_for_each(pos, &sync_timeline_list_head) {
>>>>>> -            struct sync_timeline *obj =
>>>>>> -                    container_of(pos, struct sync_timeline,
>>>>>> -                                 sync_timeline_list);
>>>>>> -
>>>>>> -            sync_print_obj(s, obj);
>>>>>> -            seq_putc(s, '\n');
>>>>>> -    }
>>>>>> -    spin_unlock_irq(&sync_timeline_list_lock);
>>>>>> -
>>>>>> -    seq_puts(s, "fences:\n--------------\n");
>>>>>> -
>>>>>> -    spin_lock_irq(&sync_file_list_lock);
>>>>>> -    list_for_each(pos, &sync_file_list_head) {
>>>>>> -            struct sync_file *sync_file =
>>>>>> -                    container_of(pos, struct sync_file, sync_file_list);
>>>>>> -
>>>>>> -            sync_print_sync_file(s, sync_file);
>>>>>> -            seq_putc(s, '\n');
>>>>>> -    }
>>>>>> -    spin_unlock_irq(&sync_file_list_lock);
>>>>>> -    return 0;
>>>>>> -}
>>>>>> -
>>>>>> -DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
>>>>>> -
>>>>>> -static __init int sync_debugfs_init(void)
>>>>>> -{
>>>>>> -    dbgfs = debugfs_create_dir("sync", NULL);
>>>>>> -
>>>>>> -    /*
>>>>>> -     * The debugfs files won't ever get removed and thus, there is
>>>>>> -     * no need to protect it against removal races. The use of
>>>>>> -     * debugfs_create_file_unsafe() is actually safe here.
>>>>>> -     */
>>>>>> -    debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
>>>>>> -                               &sync_info_debugfs_fops);
>>>>>> -    debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
>>>>>> -                               &sw_sync_debugfs_fops);
>>>>>> -
>>>>>> -    return 0;
>>>>>> -}
>>>>>> -late_initcall(sync_debugfs_init);
>>>>>> diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
>>>>>> deleted file mode 100644
>>>>>> index 6176e52ba2d7..000000000000
>>>>>> --- a/drivers/dma-buf/sync_debug.h
>>>>>> +++ /dev/null
>>>>>> @@ -1,72 +0,0 @@
>>>>>> -/*
>>>>>> - * Sync File validation framework and debug infomation
>>>>>> - *
>>>>>> - * Copyright (C) 2012 Google, Inc.
>>>>>> - *
>>>>>> - * This program is distributed in the hope that it will be useful,
>>>>>> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
>>>>>> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>>>>>> - * GNU General Public License for more details.
>>>>>> - *
>>>>>> - */
>>>>>> -
>>>>>> -#ifndef _LINUX_SYNC_H
>>>>>> -#define _LINUX_SYNC_H
>>>>>> -
>>>>>> -#include <linux/list.h>
>>>>>> -#include <linux/rbtree.h>
>>>>>> -#include <linux/spinlock.h>
>>>>>> -#include <linux/dma-fence.h>
>>>>>> -
>>>>>> -#include <linux/sync_file.h>
>>>>>> -#include <uapi/linux/sync_file.h>
>>>>>> -
>>>>>> -/**
>>>>>> - * struct sync_timeline - sync object
>>>>>> - * @kref:           reference count on fence.
>>>>>> - * @name:           name of the sync_timeline. Useful for debugging
>>>>>> - * @lock:           lock protecting @pt_list and @value
>>>>>> - * @pt_tree:                rbtree of active (unsignaled/errored) sync_pts
>>>>>> - * @pt_list:                list of active (unsignaled/errored) sync_pts
>>>>>> - * @sync_timeline_list:     membership in global sync_timeline_list
>>>>>> - */
>>>>>> -struct sync_timeline {
>>>>>> -    struct kref             kref;
>>>>>> -    char                    name[32];
>>>>>> -
>>>>>> -    /* protected by lock */
>>>>>> -    u64                     context;
>>>>>> -    int                     value;
>>>>>> -
>>>>>> -    struct rb_root          pt_tree;
>>>>>> -    struct list_head        pt_list;
>>>>>> -    spinlock_t              lock;
>>>>>> -
>>>>>> -    struct list_head        sync_timeline_list;
>>>>>> -};
>>>>>> -
>>>>>> -static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
>>>>>> -{
>>>>>> -    return container_of(fence->lock, struct sync_timeline, lock);
>>>>>> -}
>>>>>> -
>>>>>> -/**
>>>>>> - * struct sync_pt - sync_pt object
>>>>>> - * @base: base fence object
>>>>>> - * @link: link on the sync timeline's list
>>>>>> - * @node: node in the sync timeline's tree
>>>>>> - */
>>>>>> -struct sync_pt {
>>>>>> -    struct dma_fence base;
>>>>>> -    struct list_head link;
>>>>>> -    struct rb_node node;
>>>>>> -};
>>>>>> -
>>>>>> -extern const struct file_operations sw_sync_debugfs_fops;
>>>>>> -
>>>>>> -void sync_timeline_debug_add(struct sync_timeline *obj);
>>>>>> -void sync_timeline_debug_remove(struct sync_timeline *obj);
>>>>>> -void sync_file_debug_add(struct sync_file *fence);
>>>>>> -void sync_file_debug_remove(struct sync_file *fence);
>>>>>> -
>>>>>> -#endif /* _LINUX_SYNC_H */
>>>>>> --
>>>>>> 2.25.1
>>>>>>
>


^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
  2021-07-29 12:25               ` Christian König
@ 2021-07-29 12:31                 ` Daniel Vetter
  -1 siblings, 0 replies; 26+ messages in thread
From: Daniel Vetter @ 2021-07-29 12:31 UTC (permalink / raw)
  To: Christian König
  Cc: Sumit Semwal, Gustavo Padovan,
	open list:DMA BUFFER SHARING FRAMEWORK, dri-devel,
	moderated list:DMA BUFFER SHARING FRAMEWORK

On Thu, Jul 29, 2021 at 2:25 PM Christian König
<ckoenig.leichtzumerken@gmail.com> wrote:
>
> Am 29.07.21 um 13:59 schrieb Daniel Vetter:
> > On Thu, Jul 29, 2021 at 12:21 PM Christian König
> > <ckoenig.leichtzumerken@gmail.com> wrote:
> >> Am 29.07.21 um 11:03 schrieb Daniel Vetter:
> >>> On Thu, Jul 29, 2021 at 10:38 AM Christian König
> >>> <ckoenig.leichtzumerken@gmail.com> wrote:
> >>>> Am 29.07.21 um 09:23 schrieb Daniel Vetter:
> >>>>> On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
> >>>>>> As we now knew controlling dma_fence synchronization from userspace is
> >>>>>> extremely dangerous and can not only deadlock drivers but trivially also the
> >>>>>> whole kernel memory management.
> >>>>>>
> >>>>>> Entirely remove this option. We now have in kernel unit tests to exercise the
> >>>>>> dma_fence framework and it's containers.
> >>>>>>
> >>>>>> Signed-off-by: Christian König <christian.koenig@amd.com>
> >>>>> There's also igts for this, and Android heavily uses this. So I'm not sure
> >>>>> we can just nuke it.
> >>>> I feared that you would say that.
> >>>>
> >>>>> I guess what we could do is maybe just taint the kernel if it's ever used?
> >>>> What should we use then? TAINT_SOFTLOCKUP or TAINT_CRAP? :)
> >>> I think the cleanest solution would be to install a 10s timer on each
> >>> sw fence, and TAINT_SOFTLOOKUP if it fails to signal. That's similar
> >>> to what we do with vgem, except vgem then just steps in with a
> >>> dma_fence_signal() instead of tainting the kernel.
> >> Well vgem is only for testing as far as I know.
> > I think CrOS is using it to shovel buffers around between process, as
> > a convenient allocator for sw rendering dma-buf. But they don't use
> > the dma-fence part afaik.
> >
> > We can't taint in vgem fences though because our CI reboots on any
> > taint (or lockdep splat) and a few others, so that would make it
> > useless for testing. Maybe we should have the fence part of vgem
> > behind a separate Kconfig?
>
> And then making it depend on CONFIG_BROKEN? :)
>
> But more serious, that it probably makes sense.
>
> Can you take care of that? I'm not that familiar with the vgem code base.

Iirc CONFIG_BROKEN is stuff you can't even enable at all. So not an
option. Just a separate Kconfig for the fence ioctls.

> >> When Android is using this for a production environment I think we
> >> should be a bit harder and explicitly note that this is not a good idea
> >> in sw_sync_debugfs_open().
> >>
> >>> I think that should keep everyone happy, because if your Android has a
> >>> fence stuck for more than 10s it's busted.
> >> Well when they are using userptrs they are busted as soon as they have a
> >> fence stuck for much less time.
> > Hm right I forgot that you can't guarantee that it will complete, ever :-)
> >
> > Otoh I'm not sure the unconditional taint is very nice to the android
> > folks, they'll probably just patch it out and then it looses it
> > usefulness. taint on timeout otoh makes sure that normally it's not
> > hit, but anytime you die in userptr or shrinker there will be a taint
> > before anything else (since 10s is much shorter than the "stuck in
> > non-interruptible sleep" warning).
>
> And then you don't hit the timeout during test but only in production,
> e.g. not good enough either.
>
> And yes I know that this is not really nice. But since this depends on
> debugfs and Android usually disables debugfs AFAIK, they probably have
> that heavily patched already anyway.
>
> I just want to make sure that nobody else has the brilliant idea to
> enable this and build an userspace stack on top of a fundamentally
> broken approach.
>
> Alternative is to add a dependency to CONFIG_BROKEN.

Yeah CONFIG_BROKEN isn't a thing either that is useful. It's for code
that doesn't even compile (or soon will stop compiling) because build
testers never set that. And I think you have to patch the source to
even be able to get at it. So about equivalent to removal, except less
honest :-)

Tbh I'd just leave things as-is. Occasionally we have mildly terrible
stuff in-tree ...
-Daniel

> Christian.
>
> > -Daniel
> >
> >> Regards,
> >> Christian.
> >>
> >>> Note that with hwc1 the indefinite fence was actually the model, when
> >>> you did a page_flip you got an out-fence back which will only be
> >>> signalled when the new buffer _stopped_ being used for scanout. Which
> >>> generally depends upon the next page-flip happening (except for manual
> >>> mode display, which only scan out once, if you ignore that the
> >>> kernel's supposed to restore the display after suspend/resume, which
> >>> android did). hwc2 fixed this, so a 10s timeout-to-taint should be
> >>> fine with Android people.
> >>>
> >>> Also since we do have the igt/sw_sync.c test intel-gfx-ci should be
> >>> able to check that code even.
> >>> -Daniel
> >>>
> >>>> Regards,
> >>>> Christian.
> >>>>
> >>>>> Also for testing we use vgem now, which enforces a timeout.
> >>>>> -Daniel
> >>>>>
> >>>>>> ---
> >>>>>>     drivers/dma-buf/Kconfig      |  13 --
> >>>>>>     drivers/dma-buf/Makefile     |   1 -
> >>>>>>     drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
> >>>>>>     drivers/dma-buf/sync_debug.c | 190 ----------------
> >>>>>>     drivers/dma-buf/sync_debug.h |  72 ------
> >>>>>>     5 files changed, 688 deletions(-)
> >>>>>>     delete mode 100644 drivers/dma-buf/sw_sync.c
> >>>>>>     delete mode 100644 drivers/dma-buf/sync_debug.c
> >>>>>>     delete mode 100644 drivers/dma-buf/sync_debug.h
> >>>>>>
> >>>>>> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
> >>>>>> index 9561e3d2d428..26c53f45616a 100644
> >>>>>> --- a/drivers/dma-buf/Kconfig
> >>>>>> +++ b/drivers/dma-buf/Kconfig
> >>>>>> @@ -17,19 +17,6 @@ config SYNC_FILE
> >>>>>>          Files fds, to the DRM driver for example. More details at
> >>>>>>          Documentation/driver-api/sync_file.rst.
> >>>>>>
> >>>>>> -config SW_SYNC
> >>>>>> -    bool "Sync File Validation Framework"
> >>>>>> -    default n
> >>>>>> -    depends on SYNC_FILE
> >>>>>> -    depends on DEBUG_FS
> >>>>>> -    help
> >>>>>> -      A sync object driver that uses a 32bit counter to coordinate
> >>>>>> -      synchronization.  Useful when there is no hardware primitive backing
> >>>>>> -      the synchronization.
> >>>>>> -
> >>>>>> -      WARNING: improper use of this can result in deadlocking kernel
> >>>>>> -      drivers from userspace. Intended for test and debug only.
> >>>>>> -
> >>>>>>     config UDMABUF
> >>>>>>        bool "userspace dmabuf misc driver"
> >>>>>>        default n
> >>>>>> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
> >>>>>> index 1ef021273a06..fa3af0953f87 100644
> >>>>>> --- a/drivers/dma-buf/Makefile
> >>>>>> +++ b/drivers/dma-buf/Makefile
> >>>>>> @@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
> >>>>>>     obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o
> >>>>>>     obj-$(CONFIG_DMABUF_HEAPS) += heaps/
> >>>>>>     obj-$(CONFIG_SYNC_FILE)            += sync_file.o
> >>>>>> -obj-$(CONFIG_SW_SYNC)               += sw_sync.o sync_debug.o
> >>>>>>     obj-$(CONFIG_UDMABUF)              += udmabuf.o
> >>>>>>     obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
> >>>>>>
> >>>>>> diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
> >>>>>> deleted file mode 100644
> >>>>>> index 348b3a9170fa..000000000000
> >>>>>> --- a/drivers/dma-buf/sw_sync.c
> >>>>>> +++ /dev/null
> >>>>>> @@ -1,412 +0,0 @@
> >>>>>> -// SPDX-License-Identifier: GPL-2.0-only
> >>>>>> -/*
> >>>>>> - * Sync File validation framework
> >>>>>> - *
> >>>>>> - * Copyright (C) 2012 Google, Inc.
> >>>>>> - */
> >>>>>> -
> >>>>>> -#include <linux/file.h>
> >>>>>> -#include <linux/fs.h>
> >>>>>> -#include <linux/uaccess.h>
> >>>>>> -#include <linux/slab.h>
> >>>>>> -#include <linux/sync_file.h>
> >>>>>> -
> >>>>>> -#include "sync_debug.h"
> >>>>>> -
> >>>>>> -#define CREATE_TRACE_POINTS
> >>>>>> -#include "sync_trace.h"
> >>>>>> -
> >>>>>> -/*
> >>>>>> - * SW SYNC validation framework
> >>>>>> - *
> >>>>>> - * A sync object driver that uses a 32bit counter to coordinate
> >>>>>> - * synchronization.  Useful when there is no hardware primitive backing
> >>>>>> - * the synchronization.
> >>>>>> - *
> >>>>>> - * To start the framework just open:
> >>>>>> - *
> >>>>>> - * <debugfs>/sync/sw_sync
> >>>>>> - *
> >>>>>> - * That will create a sync timeline, all fences created under this timeline
> >>>>>> - * file descriptor will belong to the this timeline.
> >>>>>> - *
> >>>>>> - * The 'sw_sync' file can be opened many times as to create different
> >>>>>> - * timelines.
> >>>>>> - *
> >>>>>> - * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
> >>>>>> - * sw_sync_create_fence_data as parameter.
> >>>>>> - *
> >>>>>> - * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
> >>>>>> - * with the increment as u32. This will update the last signaled value
> >>>>>> - * from the timeline and signal any fence that has a seqno smaller or equal
> >>>>>> - * to it.
> >>>>>> - *
> >>>>>> - * struct sw_sync_create_fence_data
> >>>>>> - * @value:  the seqno to initialise the fence with
> >>>>>> - * @name:   the name of the new sync point
> >>>>>> - * @fence:  return the fd of the new sync_file with the created fence
> >>>>>> - */
> >>>>>> -struct sw_sync_create_fence_data {
> >>>>>> -    __u32   value;
> >>>>>> -    char    name[32];
> >>>>>> -    __s32   fence; /* fd of new fence */
> >>>>>> -};
> >>>>>> -
> >>>>>> -#define SW_SYNC_IOC_MAGIC   'W'
> >>>>>> -
> >>>>>> -#define SW_SYNC_IOC_CREATE_FENCE    _IOWR(SW_SYNC_IOC_MAGIC, 0,\
> >>>>>> -            struct sw_sync_create_fence_data)
> >>>>>> -
> >>>>>> -#define SW_SYNC_IOC_INC                     _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
> >>>>>> -
> >>>>>> -static const struct dma_fence_ops timeline_fence_ops;
> >>>>>> -
> >>>>>> -static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
> >>>>>> -{
> >>>>>> -    if (fence->ops != &timeline_fence_ops)
> >>>>>> -            return NULL;
> >>>>>> -    return container_of(fence, struct sync_pt, base);
> >>>>>> -}
> >>>>>> -
> >>>>>> -/**
> >>>>>> - * sync_timeline_create() - creates a sync object
> >>>>>> - * @name:   sync_timeline name
> >>>>>> - *
> >>>>>> - * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
> >>>>>> - * case of error.
> >>>>>> - */
> >>>>>> -static struct sync_timeline *sync_timeline_create(const char *name)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *obj;
> >>>>>> -
> >>>>>> -    obj = kzalloc(sizeof(*obj), GFP_KERNEL);
> >>>>>> -    if (!obj)
> >>>>>> -            return NULL;
> >>>>>> -
> >>>>>> -    kref_init(&obj->kref);
> >>>>>> -    obj->context = dma_fence_context_alloc(1);
> >>>>>> -    strlcpy(obj->name, name, sizeof(obj->name));
> >>>>>> -
> >>>>>> -    obj->pt_tree = RB_ROOT;
> >>>>>> -    INIT_LIST_HEAD(&obj->pt_list);
> >>>>>> -    spin_lock_init(&obj->lock);
> >>>>>> -
> >>>>>> -    sync_timeline_debug_add(obj);
> >>>>>> -
> >>>>>> -    return obj;
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void sync_timeline_free(struct kref *kref)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *obj =
> >>>>>> -            container_of(kref, struct sync_timeline, kref);
> >>>>>> -
> >>>>>> -    sync_timeline_debug_remove(obj);
> >>>>>> -
> >>>>>> -    kfree(obj);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void sync_timeline_get(struct sync_timeline *obj)
> >>>>>> -{
> >>>>>> -    kref_get(&obj->kref);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void sync_timeline_put(struct sync_timeline *obj)
> >>>>>> -{
> >>>>>> -    kref_put(&obj->kref, sync_timeline_free);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
> >>>>>> -{
> >>>>>> -    return "sw_sync";
> >>>>>> -}
> >>>>>> -
> >>>>>> -static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>>>> -
> >>>>>> -    return parent->name;
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void timeline_fence_release(struct dma_fence *fence)
> >>>>>> -{
> >>>>>> -    struct sync_pt *pt = dma_fence_to_sync_pt(fence);
> >>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>>>> -    unsigned long flags;
> >>>>>> -
> >>>>>> -    spin_lock_irqsave(fence->lock, flags);
> >>>>>> -    if (!list_empty(&pt->link)) {
> >>>>>> -            list_del(&pt->link);
> >>>>>> -            rb_erase(&pt->node, &parent->pt_tree);
> >>>>>> -    }
> >>>>>> -    spin_unlock_irqrestore(fence->lock, flags);
> >>>>>> -
> >>>>>> -    sync_timeline_put(parent);
> >>>>>> -    dma_fence_free(fence);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static bool timeline_fence_signaled(struct dma_fence *fence)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>>>> -
> >>>>>> -    return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static bool timeline_fence_enable_signaling(struct dma_fence *fence)
> >>>>>> -{
> >>>>>> -    return true;
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void timeline_fence_value_str(struct dma_fence *fence,
> >>>>>> -                                char *str, int size)
> >>>>>> -{
> >>>>>> -    snprintf(str, size, "%lld", fence->seqno);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void timeline_fence_timeline_value_str(struct dma_fence *fence,
> >>>>>> -                                         char *str, int size)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>>>> -
> >>>>>> -    snprintf(str, size, "%d", parent->value);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static const struct dma_fence_ops timeline_fence_ops = {
> >>>>>> -    .get_driver_name = timeline_fence_get_driver_name,
> >>>>>> -    .get_timeline_name = timeline_fence_get_timeline_name,
> >>>>>> -    .enable_signaling = timeline_fence_enable_signaling,
> >>>>>> -    .signaled = timeline_fence_signaled,
> >>>>>> -    .release = timeline_fence_release,
> >>>>>> -    .fence_value_str = timeline_fence_value_str,
> >>>>>> -    .timeline_value_str = timeline_fence_timeline_value_str,
> >>>>>> -};
> >>>>>> -
> >>>>>> -/**
> >>>>>> - * sync_timeline_signal() - signal a status change on a sync_timeline
> >>>>>> - * @obj:    sync_timeline to signal
> >>>>>> - * @inc:    num to increment on timeline->value
> >>>>>> - *
> >>>>>> - * A sync implementation should call this any time one of it's fences
> >>>>>> - * has signaled or has an error condition.
> >>>>>> - */
> >>>>>> -static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
> >>>>>> -{
> >>>>>> -    struct sync_pt *pt, *next;
> >>>>>> -
> >>>>>> -    trace_sync_timeline(obj);
> >>>>>> -
> >>>>>> -    spin_lock_irq(&obj->lock);
> >>>>>> -
> >>>>>> -    obj->value += inc;
> >>>>>> -
> >>>>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> >>>>>> -            if (!timeline_fence_signaled(&pt->base))
> >>>>>> -                    break;
> >>>>>> -
> >>>>>> -            list_del_init(&pt->link);
> >>>>>> -            rb_erase(&pt->node, &obj->pt_tree);
> >>>>>> -
> >>>>>> -            /*
> >>>>>> -             * A signal callback may release the last reference to this
> >>>>>> -             * fence, causing it to be freed. That operation has to be
> >>>>>> -             * last to avoid a use after free inside this loop, and must
> >>>>>> -             * be after we remove the fence from the timeline in order to
> >>>>>> -             * prevent deadlocking on timeline->lock inside
> >>>>>> -             * timeline_fence_release().
> >>>>>> -             */
> >>>>>> -            dma_fence_signal_locked(&pt->base);
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    spin_unlock_irq(&obj->lock);
> >>>>>> -}
> >>>>>> -
> >>>>>> -/**
> >>>>>> - * sync_pt_create() - creates a sync pt
> >>>>>> - * @obj:    parent sync_timeline
> >>>>>> - * @value:  value of the fence
> >>>>>> - *
> >>>>>> - * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
> >>>>>> - * allocated allowing for implementation specific data to be kept after
> >>>>>> - * the generic sync_timeline struct. Returns the sync_pt object or
> >>>>>> - * NULL in case of error.
> >>>>>> - */
> >>>>>> -static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
> >>>>>> -                                  unsigned int value)
> >>>>>> -{
> >>>>>> -    struct sync_pt *pt;
> >>>>>> -
> >>>>>> -    pt = kzalloc(sizeof(*pt), GFP_KERNEL);
> >>>>>> -    if (!pt)
> >>>>>> -            return NULL;
> >>>>>> -
> >>>>>> -    sync_timeline_get(obj);
> >>>>>> -    dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
> >>>>>> -                   obj->context, value);
> >>>>>> -    INIT_LIST_HEAD(&pt->link);
> >>>>>> -
> >>>>>> -    spin_lock_irq(&obj->lock);
> >>>>>> -    if (!dma_fence_is_signaled_locked(&pt->base)) {
> >>>>>> -            struct rb_node **p = &obj->pt_tree.rb_node;
> >>>>>> -            struct rb_node *parent = NULL;
> >>>>>> -
> >>>>>> -            while (*p) {
> >>>>>> -                    struct sync_pt *other;
> >>>>>> -                    int cmp;
> >>>>>> -
> >>>>>> -                    parent = *p;
> >>>>>> -                    other = rb_entry(parent, typeof(*pt), node);
> >>>>>> -                    cmp = value - other->base.seqno;
> >>>>>> -                    if (cmp > 0) {
> >>>>>> -                            p = &parent->rb_right;
> >>>>>> -                    } else if (cmp < 0) {
> >>>>>> -                            p = &parent->rb_left;
> >>>>>> -                    } else {
> >>>>>> -                            if (dma_fence_get_rcu(&other->base)) {
> >>>>>> -                                    sync_timeline_put(obj);
> >>>>>> -                                    kfree(pt);
> >>>>>> -                                    pt = other;
> >>>>>> -                                    goto unlock;
> >>>>>> -                            }
> >>>>>> -                            p = &parent->rb_left;
> >>>>>> -                    }
> >>>>>> -            }
> >>>>>> -            rb_link_node(&pt->node, parent, p);
> >>>>>> -            rb_insert_color(&pt->node, &obj->pt_tree);
> >>>>>> -
> >>>>>> -            parent = rb_next(&pt->node);
> >>>>>> -            list_add_tail(&pt->link,
> >>>>>> -                          parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
> >>>>>> -    }
> >>>>>> -unlock:
> >>>>>> -    spin_unlock_irq(&obj->lock);
> >>>>>> -
> >>>>>> -    return pt;
> >>>>>> -}
> >>>>>> -
> >>>>>> -/*
> >>>>>> - * *WARNING*
> >>>>>> - *
> >>>>>> - * improper use of this can result in deadlocking kernel drivers from userspace.
> >>>>>> - */
> >>>>>> -
> >>>>>> -/* opening sw_sync create a new sync obj */
> >>>>>> -static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *obj;
> >>>>>> -    char task_comm[TASK_COMM_LEN];
> >>>>>> -
> >>>>>> -    get_task_comm(task_comm, current);
> >>>>>> -
> >>>>>> -    obj = sync_timeline_create(task_comm);
> >>>>>> -    if (!obj)
> >>>>>> -            return -ENOMEM;
> >>>>>> -
> >>>>>> -    file->private_data = obj;
> >>>>>> -
> >>>>>> -    return 0;
> >>>>>> -}
> >>>>>> -
> >>>>>> -static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *obj = file->private_data;
> >>>>>> -    struct sync_pt *pt, *next;
> >>>>>> -
> >>>>>> -    spin_lock_irq(&obj->lock);
> >>>>>> -
> >>>>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> >>>>>> -            dma_fence_set_error(&pt->base, -ENOENT);
> >>>>>> -            dma_fence_signal_locked(&pt->base);
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    spin_unlock_irq(&obj->lock);
> >>>>>> -
> >>>>>> -    sync_timeline_put(obj);
> >>>>>> -    return 0;
> >>>>>> -}
> >>>>>> -
> >>>>>> -static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
> >>>>>> -                                   unsigned long arg)
> >>>>>> -{
> >>>>>> -    int fd = get_unused_fd_flags(O_CLOEXEC);
> >>>>>> -    int err;
> >>>>>> -    struct sync_pt *pt;
> >>>>>> -    struct sync_file *sync_file;
> >>>>>> -    struct sw_sync_create_fence_data data;
> >>>>>> -
> >>>>>> -    if (fd < 0)
> >>>>>> -            return fd;
> >>>>>> -
> >>>>>> -    if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
> >>>>>> -            err = -EFAULT;
> >>>>>> -            goto err;
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    pt = sync_pt_create(obj, data.value);
> >>>>>> -    if (!pt) {
> >>>>>> -            err = -ENOMEM;
> >>>>>> -            goto err;
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    sync_file = sync_file_create(&pt->base);
> >>>>>> -    dma_fence_put(&pt->base);
> >>>>>> -    if (!sync_file) {
> >>>>>> -            err = -ENOMEM;
> >>>>>> -            goto err;
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    data.fence = fd;
> >>>>>> -    if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
> >>>>>> -            fput(sync_file->file);
> >>>>>> -            err = -EFAULT;
> >>>>>> -            goto err;
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    fd_install(fd, sync_file->file);
> >>>>>> -
> >>>>>> -    return 0;
> >>>>>> -
> >>>>>> -err:
> >>>>>> -    put_unused_fd(fd);
> >>>>>> -    return err;
> >>>>>> -}
> >>>>>> -
> >>>>>> -static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
> >>>>>> -{
> >>>>>> -    u32 value;
> >>>>>> -
> >>>>>> -    if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
> >>>>>> -            return -EFAULT;
> >>>>>> -
> >>>>>> -    while (value > INT_MAX)  {
> >>>>>> -            sync_timeline_signal(obj, INT_MAX);
> >>>>>> -            value -= INT_MAX;
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    sync_timeline_signal(obj, value);
> >>>>>> -
> >>>>>> -    return 0;
> >>>>>> -}
> >>>>>> -
> >>>>>> -static long sw_sync_ioctl(struct file *file, unsigned int cmd,
> >>>>>> -                      unsigned long arg)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *obj = file->private_data;
> >>>>>> -
> >>>>>> -    switch (cmd) {
> >>>>>> -    case SW_SYNC_IOC_CREATE_FENCE:
> >>>>>> -            return sw_sync_ioctl_create_fence(obj, arg);
> >>>>>> -
> >>>>>> -    case SW_SYNC_IOC_INC:
> >>>>>> -            return sw_sync_ioctl_inc(obj, arg);
> >>>>>> -
> >>>>>> -    default:
> >>>>>> -            return -ENOTTY;
> >>>>>> -    }
> >>>>>> -}
> >>>>>> -
> >>>>>> -const struct file_operations sw_sync_debugfs_fops = {
> >>>>>> -    .open           = sw_sync_debugfs_open,
> >>>>>> -    .release        = sw_sync_debugfs_release,
> >>>>>> -    .unlocked_ioctl = sw_sync_ioctl,
> >>>>>> -    .compat_ioctl   = compat_ptr_ioctl,
> >>>>>> -};
> >>>>>> diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
> >>>>>> deleted file mode 100644
> >>>>>> index 101394f16930..000000000000
> >>>>>> --- a/drivers/dma-buf/sync_debug.c
> >>>>>> +++ /dev/null
> >>>>>> @@ -1,190 +0,0 @@
> >>>>>> -// SPDX-License-Identifier: GPL-2.0-only
> >>>>>> -/*
> >>>>>> - * Sync File validation framework and debug information
> >>>>>> - *
> >>>>>> - * Copyright (C) 2012 Google, Inc.
> >>>>>> - */
> >>>>>> -
> >>>>>> -#include <linux/debugfs.h>
> >>>>>> -#include "sync_debug.h"
> >>>>>> -
> >>>>>> -static struct dentry *dbgfs;
> >>>>>> -
> >>>>>> -static LIST_HEAD(sync_timeline_list_head);
> >>>>>> -static DEFINE_SPINLOCK(sync_timeline_list_lock);
> >>>>>> -static LIST_HEAD(sync_file_list_head);
> >>>>>> -static DEFINE_SPINLOCK(sync_file_list_lock);
> >>>>>> -
> >>>>>> -void sync_timeline_debug_add(struct sync_timeline *obj)
> >>>>>> -{
> >>>>>> -    unsigned long flags;
> >>>>>> -
> >>>>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
> >>>>>> -    list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
> >>>>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> >>>>>> -}
> >>>>>> -
> >>>>>> -void sync_timeline_debug_remove(struct sync_timeline *obj)
> >>>>>> -{
> >>>>>> -    unsigned long flags;
> >>>>>> -
> >>>>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
> >>>>>> -    list_del(&obj->sync_timeline_list);
> >>>>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> >>>>>> -}
> >>>>>> -
> >>>>>> -void sync_file_debug_add(struct sync_file *sync_file)
> >>>>>> -{
> >>>>>> -    unsigned long flags;
> >>>>>> -
> >>>>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
> >>>>>> -    list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
> >>>>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
> >>>>>> -}
> >>>>>> -
> >>>>>> -void sync_file_debug_remove(struct sync_file *sync_file)
> >>>>>> -{
> >>>>>> -    unsigned long flags;
> >>>>>> -
> >>>>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
> >>>>>> -    list_del(&sync_file->sync_file_list);
> >>>>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static const char *sync_status_str(int status)
> >>>>>> -{
> >>>>>> -    if (status < 0)
> >>>>>> -            return "error";
> >>>>>> -
> >>>>>> -    if (status > 0)
> >>>>>> -            return "signaled";
> >>>>>> -
> >>>>>> -    return "active";
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void sync_print_fence(struct seq_file *s,
> >>>>>> -                         struct dma_fence *fence, bool show)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>>>> -    int status;
> >>>>>> -
> >>>>>> -    status = dma_fence_get_status_locked(fence);
> >>>>>> -
> >>>>>> -    seq_printf(s, "  %s%sfence %s",
> >>>>>> -               show ? parent->name : "",
> >>>>>> -               show ? "_" : "",
> >>>>>> -               sync_status_str(status));
> >>>>>> -
> >>>>>> -    if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
> >>>>>> -            struct timespec64 ts64 =
> >>>>>> -                    ktime_to_timespec64(fence->timestamp);
> >>>>>> -
> >>>>>> -            seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    if (fence->ops->timeline_value_str &&
> >>>>>> -            fence->ops->fence_value_str) {
> >>>>>> -            char value[64];
> >>>>>> -            bool success;
> >>>>>> -
> >>>>>> -            fence->ops->fence_value_str(fence, value, sizeof(value));
> >>>>>> -            success = strlen(value);
> >>>>>> -
> >>>>>> -            if (success) {
> >>>>>> -                    seq_printf(s, ": %s", value);
> >>>>>> -
> >>>>>> -                    fence->ops->timeline_value_str(fence, value,
> >>>>>> -                                                   sizeof(value));
> >>>>>> -
> >>>>>> -                    if (strlen(value))
> >>>>>> -                            seq_printf(s, " / %s", value);
> >>>>>> -            }
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    seq_putc(s, '\n');
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
> >>>>>> -{
> >>>>>> -    struct list_head *pos;
> >>>>>> -
> >>>>>> -    seq_printf(s, "%s: %d\n", obj->name, obj->value);
> >>>>>> -
> >>>>>> -    spin_lock_irq(&obj->lock);
> >>>>>> -    list_for_each(pos, &obj->pt_list) {
> >>>>>> -            struct sync_pt *pt = container_of(pos, struct sync_pt, link);
> >>>>>> -            sync_print_fence(s, &pt->base, false);
> >>>>>> -    }
> >>>>>> -    spin_unlock_irq(&obj->lock);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void sync_print_sync_file(struct seq_file *s,
> >>>>>> -                              struct sync_file *sync_file)
> >>>>>> -{
> >>>>>> -    char buf[128];
> >>>>>> -    int i;
> >>>>>> -
> >>>>>> -    seq_printf(s, "[%p] %s: %s\n", sync_file,
> >>>>>> -               sync_file_get_name(sync_file, buf, sizeof(buf)),
> >>>>>> -               sync_status_str(dma_fence_get_status(sync_file->fence)));
> >>>>>> -
> >>>>>> -    if (dma_fence_is_array(sync_file->fence)) {
> >>>>>> -            struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
> >>>>>> -
> >>>>>> -            for (i = 0; i < array->num_fences; ++i)
> >>>>>> -                    sync_print_fence(s, array->fences[i], true);
> >>>>>> -    } else {
> >>>>>> -            sync_print_fence(s, sync_file->fence, true);
> >>>>>> -    }
> >>>>>> -}
> >>>>>> -
> >>>>>> -static int sync_info_debugfs_show(struct seq_file *s, void *unused)
> >>>>>> -{
> >>>>>> -    struct list_head *pos;
> >>>>>> -
> >>>>>> -    seq_puts(s, "objs:\n--------------\n");
> >>>>>> -
> >>>>>> -    spin_lock_irq(&sync_timeline_list_lock);
> >>>>>> -    list_for_each(pos, &sync_timeline_list_head) {
> >>>>>> -            struct sync_timeline *obj =
> >>>>>> -                    container_of(pos, struct sync_timeline,
> >>>>>> -                                 sync_timeline_list);
> >>>>>> -
> >>>>>> -            sync_print_obj(s, obj);
> >>>>>> -            seq_putc(s, '\n');
> >>>>>> -    }
> >>>>>> -    spin_unlock_irq(&sync_timeline_list_lock);
> >>>>>> -
> >>>>>> -    seq_puts(s, "fences:\n--------------\n");
> >>>>>> -
> >>>>>> -    spin_lock_irq(&sync_file_list_lock);
> >>>>>> -    list_for_each(pos, &sync_file_list_head) {
> >>>>>> -            struct sync_file *sync_file =
> >>>>>> -                    container_of(pos, struct sync_file, sync_file_list);
> >>>>>> -
> >>>>>> -            sync_print_sync_file(s, sync_file);
> >>>>>> -            seq_putc(s, '\n');
> >>>>>> -    }
> >>>>>> -    spin_unlock_irq(&sync_file_list_lock);
> >>>>>> -    return 0;
> >>>>>> -}
> >>>>>> -
> >>>>>> -DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
> >>>>>> -
> >>>>>> -static __init int sync_debugfs_init(void)
> >>>>>> -{
> >>>>>> -    dbgfs = debugfs_create_dir("sync", NULL);
> >>>>>> -
> >>>>>> -    /*
> >>>>>> -     * The debugfs files won't ever get removed and thus, there is
> >>>>>> -     * no need to protect it against removal races. The use of
> >>>>>> -     * debugfs_create_file_unsafe() is actually safe here.
> >>>>>> -     */
> >>>>>> -    debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
> >>>>>> -                               &sync_info_debugfs_fops);
> >>>>>> -    debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
> >>>>>> -                               &sw_sync_debugfs_fops);
> >>>>>> -
> >>>>>> -    return 0;
> >>>>>> -}
> >>>>>> -late_initcall(sync_debugfs_init);
> >>>>>> diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
> >>>>>> deleted file mode 100644
> >>>>>> index 6176e52ba2d7..000000000000
> >>>>>> --- a/drivers/dma-buf/sync_debug.h
> >>>>>> +++ /dev/null
> >>>>>> @@ -1,72 +0,0 @@
> >>>>>> -/*
> >>>>>> - * Sync File validation framework and debug infomation
> >>>>>> - *
> >>>>>> - * Copyright (C) 2012 Google, Inc.
> >>>>>> - *
> >>>>>> - * This program is distributed in the hope that it will be useful,
> >>>>>> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> >>>>>> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> >>>>>> - * GNU General Public License for more details.
> >>>>>> - *
> >>>>>> - */
> >>>>>> -
> >>>>>> -#ifndef _LINUX_SYNC_H
> >>>>>> -#define _LINUX_SYNC_H
> >>>>>> -
> >>>>>> -#include <linux/list.h>
> >>>>>> -#include <linux/rbtree.h>
> >>>>>> -#include <linux/spinlock.h>
> >>>>>> -#include <linux/dma-fence.h>
> >>>>>> -
> >>>>>> -#include <linux/sync_file.h>
> >>>>>> -#include <uapi/linux/sync_file.h>
> >>>>>> -
> >>>>>> -/**
> >>>>>> - * struct sync_timeline - sync object
> >>>>>> - * @kref:           reference count on fence.
> >>>>>> - * @name:           name of the sync_timeline. Useful for debugging
> >>>>>> - * @lock:           lock protecting @pt_list and @value
> >>>>>> - * @pt_tree:                rbtree of active (unsignaled/errored) sync_pts
> >>>>>> - * @pt_list:                list of active (unsignaled/errored) sync_pts
> >>>>>> - * @sync_timeline_list:     membership in global sync_timeline_list
> >>>>>> - */
> >>>>>> -struct sync_timeline {
> >>>>>> -    struct kref             kref;
> >>>>>> -    char                    name[32];
> >>>>>> -
> >>>>>> -    /* protected by lock */
> >>>>>> -    u64                     context;
> >>>>>> -    int                     value;
> >>>>>> -
> >>>>>> -    struct rb_root          pt_tree;
> >>>>>> -    struct list_head        pt_list;
> >>>>>> -    spinlock_t              lock;
> >>>>>> -
> >>>>>> -    struct list_head        sync_timeline_list;
> >>>>>> -};
> >>>>>> -
> >>>>>> -static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
> >>>>>> -{
> >>>>>> -    return container_of(fence->lock, struct sync_timeline, lock);
> >>>>>> -}
> >>>>>> -
> >>>>>> -/**
> >>>>>> - * struct sync_pt - sync_pt object
> >>>>>> - * @base: base fence object
> >>>>>> - * @link: link on the sync timeline's list
> >>>>>> - * @node: node in the sync timeline's tree
> >>>>>> - */
> >>>>>> -struct sync_pt {
> >>>>>> -    struct dma_fence base;
> >>>>>> -    struct list_head link;
> >>>>>> -    struct rb_node node;
> >>>>>> -};
> >>>>>> -
> >>>>>> -extern const struct file_operations sw_sync_debugfs_fops;
> >>>>>> -
> >>>>>> -void sync_timeline_debug_add(struct sync_timeline *obj);
> >>>>>> -void sync_timeline_debug_remove(struct sync_timeline *obj);
> >>>>>> -void sync_file_debug_add(struct sync_file *fence);
> >>>>>> -void sync_file_debug_remove(struct sync_file *fence);
> >>>>>> -
> >>>>>> -#endif /* _LINUX_SYNC_H */
> >>>>>> --
> >>>>>> 2.25.1
> >>>>>>
> >
>


-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
@ 2021-07-29 12:31                 ` Daniel Vetter
  0 siblings, 0 replies; 26+ messages in thread
From: Daniel Vetter @ 2021-07-29 12:31 UTC (permalink / raw)
  To: Christian König
  Cc: moderated list:DMA BUFFER SHARING FRAMEWORK, Gustavo Padovan,
	dri-devel, open list:DMA BUFFER SHARING FRAMEWORK

On Thu, Jul 29, 2021 at 2:25 PM Christian König
<ckoenig.leichtzumerken@gmail.com> wrote:
>
> Am 29.07.21 um 13:59 schrieb Daniel Vetter:
> > On Thu, Jul 29, 2021 at 12:21 PM Christian König
> > <ckoenig.leichtzumerken@gmail.com> wrote:
> >> Am 29.07.21 um 11:03 schrieb Daniel Vetter:
> >>> On Thu, Jul 29, 2021 at 10:38 AM Christian König
> >>> <ckoenig.leichtzumerken@gmail.com> wrote:
> >>>> Am 29.07.21 um 09:23 schrieb Daniel Vetter:
> >>>>> On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
> >>>>>> As we now knew controlling dma_fence synchronization from userspace is
> >>>>>> extremely dangerous and can not only deadlock drivers but trivially also the
> >>>>>> whole kernel memory management.
> >>>>>>
> >>>>>> Entirely remove this option. We now have in kernel unit tests to exercise the
> >>>>>> dma_fence framework and it's containers.
> >>>>>>
> >>>>>> Signed-off-by: Christian König <christian.koenig@amd.com>
> >>>>> There's also igts for this, and Android heavily uses this. So I'm not sure
> >>>>> we can just nuke it.
> >>>> I feared that you would say that.
> >>>>
> >>>>> I guess what we could do is maybe just taint the kernel if it's ever used?
> >>>> What should we use then? TAINT_SOFTLOCKUP or TAINT_CRAP? :)
> >>> I think the cleanest solution would be to install a 10s timer on each
> >>> sw fence, and TAINT_SOFTLOOKUP if it fails to signal. That's similar
> >>> to what we do with vgem, except vgem then just steps in with a
> >>> dma_fence_signal() instead of tainting the kernel.
> >> Well vgem is only for testing as far as I know.
> > I think CrOS is using it to shovel buffers around between process, as
> > a convenient allocator for sw rendering dma-buf. But they don't use
> > the dma-fence part afaik.
> >
> > We can't taint in vgem fences though because our CI reboots on any
> > taint (or lockdep splat) and a few others, so that would make it
> > useless for testing. Maybe we should have the fence part of vgem
> > behind a separate Kconfig?
>
> And then making it depend on CONFIG_BROKEN? :)
>
> But more serious, that it probably makes sense.
>
> Can you take care of that? I'm not that familiar with the vgem code base.

Iirc CONFIG_BROKEN is stuff you can't even enable at all. So not an
option. Just a separate Kconfig for the fence ioctls.

> >> When Android is using this for a production environment I think we
> >> should be a bit harder and explicitly note that this is not a good idea
> >> in sw_sync_debugfs_open().
> >>
> >>> I think that should keep everyone happy, because if your Android has a
> >>> fence stuck for more than 10s it's busted.
> >> Well when they are using userptrs they are busted as soon as they have a
> >> fence stuck for much less time.
> > Hm right I forgot that you can't guarantee that it will complete, ever :-)
> >
> > Otoh I'm not sure the unconditional taint is very nice to the android
> > folks, they'll probably just patch it out and then it looses it
> > usefulness. taint on timeout otoh makes sure that normally it's not
> > hit, but anytime you die in userptr or shrinker there will be a taint
> > before anything else (since 10s is much shorter than the "stuck in
> > non-interruptible sleep" warning).
>
> And then you don't hit the timeout during test but only in production,
> e.g. not good enough either.
>
> And yes I know that this is not really nice. But since this depends on
> debugfs and Android usually disables debugfs AFAIK, they probably have
> that heavily patched already anyway.
>
> I just want to make sure that nobody else has the brilliant idea to
> enable this and build an userspace stack on top of a fundamentally
> broken approach.
>
> Alternative is to add a dependency to CONFIG_BROKEN.

Yeah CONFIG_BROKEN isn't a thing either that is useful. It's for code
that doesn't even compile (or soon will stop compiling) because build
testers never set that. And I think you have to patch the source to
even be able to get at it. So about equivalent to removal, except less
honest :-)

Tbh I'd just leave things as-is. Occasionally we have mildly terrible
stuff in-tree ...
-Daniel

> Christian.
>
> > -Daniel
> >
> >> Regards,
> >> Christian.
> >>
> >>> Note that with hwc1 the indefinite fence was actually the model, when
> >>> you did a page_flip you got an out-fence back which will only be
> >>> signalled when the new buffer _stopped_ being used for scanout. Which
> >>> generally depends upon the next page-flip happening (except for manual
> >>> mode display, which only scan out once, if you ignore that the
> >>> kernel's supposed to restore the display after suspend/resume, which
> >>> android did). hwc2 fixed this, so a 10s timeout-to-taint should be
> >>> fine with Android people.
> >>>
> >>> Also since we do have the igt/sw_sync.c test intel-gfx-ci should be
> >>> able to check that code even.
> >>> -Daniel
> >>>
> >>>> Regards,
> >>>> Christian.
> >>>>
> >>>>> Also for testing we use vgem now, which enforces a timeout.
> >>>>> -Daniel
> >>>>>
> >>>>>> ---
> >>>>>>     drivers/dma-buf/Kconfig      |  13 --
> >>>>>>     drivers/dma-buf/Makefile     |   1 -
> >>>>>>     drivers/dma-buf/sw_sync.c    | 412 -----------------------------------
> >>>>>>     drivers/dma-buf/sync_debug.c | 190 ----------------
> >>>>>>     drivers/dma-buf/sync_debug.h |  72 ------
> >>>>>>     5 files changed, 688 deletions(-)
> >>>>>>     delete mode 100644 drivers/dma-buf/sw_sync.c
> >>>>>>     delete mode 100644 drivers/dma-buf/sync_debug.c
> >>>>>>     delete mode 100644 drivers/dma-buf/sync_debug.h
> >>>>>>
> >>>>>> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
> >>>>>> index 9561e3d2d428..26c53f45616a 100644
> >>>>>> --- a/drivers/dma-buf/Kconfig
> >>>>>> +++ b/drivers/dma-buf/Kconfig
> >>>>>> @@ -17,19 +17,6 @@ config SYNC_FILE
> >>>>>>          Files fds, to the DRM driver for example. More details at
> >>>>>>          Documentation/driver-api/sync_file.rst.
> >>>>>>
> >>>>>> -config SW_SYNC
> >>>>>> -    bool "Sync File Validation Framework"
> >>>>>> -    default n
> >>>>>> -    depends on SYNC_FILE
> >>>>>> -    depends on DEBUG_FS
> >>>>>> -    help
> >>>>>> -      A sync object driver that uses a 32bit counter to coordinate
> >>>>>> -      synchronization.  Useful when there is no hardware primitive backing
> >>>>>> -      the synchronization.
> >>>>>> -
> >>>>>> -      WARNING: improper use of this can result in deadlocking kernel
> >>>>>> -      drivers from userspace. Intended for test and debug only.
> >>>>>> -
> >>>>>>     config UDMABUF
> >>>>>>        bool "userspace dmabuf misc driver"
> >>>>>>        default n
> >>>>>> diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
> >>>>>> index 1ef021273a06..fa3af0953f87 100644
> >>>>>> --- a/drivers/dma-buf/Makefile
> >>>>>> +++ b/drivers/dma-buf/Makefile
> >>>>>> @@ -4,7 +4,6 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
> >>>>>>     obj-$(CONFIG_DMABUF_HEAPS) += dma-heap.o
> >>>>>>     obj-$(CONFIG_DMABUF_HEAPS) += heaps/
> >>>>>>     obj-$(CONFIG_SYNC_FILE)            += sync_file.o
> >>>>>> -obj-$(CONFIG_SW_SYNC)               += sw_sync.o sync_debug.o
> >>>>>>     obj-$(CONFIG_UDMABUF)              += udmabuf.o
> >>>>>>     obj-$(CONFIG_DMABUF_SYSFS_STATS) += dma-buf-sysfs-stats.o
> >>>>>>
> >>>>>> diff --git a/drivers/dma-buf/sw_sync.c b/drivers/dma-buf/sw_sync.c
> >>>>>> deleted file mode 100644
> >>>>>> index 348b3a9170fa..000000000000
> >>>>>> --- a/drivers/dma-buf/sw_sync.c
> >>>>>> +++ /dev/null
> >>>>>> @@ -1,412 +0,0 @@
> >>>>>> -// SPDX-License-Identifier: GPL-2.0-only
> >>>>>> -/*
> >>>>>> - * Sync File validation framework
> >>>>>> - *
> >>>>>> - * Copyright (C) 2012 Google, Inc.
> >>>>>> - */
> >>>>>> -
> >>>>>> -#include <linux/file.h>
> >>>>>> -#include <linux/fs.h>
> >>>>>> -#include <linux/uaccess.h>
> >>>>>> -#include <linux/slab.h>
> >>>>>> -#include <linux/sync_file.h>
> >>>>>> -
> >>>>>> -#include "sync_debug.h"
> >>>>>> -
> >>>>>> -#define CREATE_TRACE_POINTS
> >>>>>> -#include "sync_trace.h"
> >>>>>> -
> >>>>>> -/*
> >>>>>> - * SW SYNC validation framework
> >>>>>> - *
> >>>>>> - * A sync object driver that uses a 32bit counter to coordinate
> >>>>>> - * synchronization.  Useful when there is no hardware primitive backing
> >>>>>> - * the synchronization.
> >>>>>> - *
> >>>>>> - * To start the framework just open:
> >>>>>> - *
> >>>>>> - * <debugfs>/sync/sw_sync
> >>>>>> - *
> >>>>>> - * That will create a sync timeline, all fences created under this timeline
> >>>>>> - * file descriptor will belong to the this timeline.
> >>>>>> - *
> >>>>>> - * The 'sw_sync' file can be opened many times as to create different
> >>>>>> - * timelines.
> >>>>>> - *
> >>>>>> - * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
> >>>>>> - * sw_sync_create_fence_data as parameter.
> >>>>>> - *
> >>>>>> - * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
> >>>>>> - * with the increment as u32. This will update the last signaled value
> >>>>>> - * from the timeline and signal any fence that has a seqno smaller or equal
> >>>>>> - * to it.
> >>>>>> - *
> >>>>>> - * struct sw_sync_create_fence_data
> >>>>>> - * @value:  the seqno to initialise the fence with
> >>>>>> - * @name:   the name of the new sync point
> >>>>>> - * @fence:  return the fd of the new sync_file with the created fence
> >>>>>> - */
> >>>>>> -struct sw_sync_create_fence_data {
> >>>>>> -    __u32   value;
> >>>>>> -    char    name[32];
> >>>>>> -    __s32   fence; /* fd of new fence */
> >>>>>> -};
> >>>>>> -
> >>>>>> -#define SW_SYNC_IOC_MAGIC   'W'
> >>>>>> -
> >>>>>> -#define SW_SYNC_IOC_CREATE_FENCE    _IOWR(SW_SYNC_IOC_MAGIC, 0,\
> >>>>>> -            struct sw_sync_create_fence_data)
> >>>>>> -
> >>>>>> -#define SW_SYNC_IOC_INC                     _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
> >>>>>> -
> >>>>>> -static const struct dma_fence_ops timeline_fence_ops;
> >>>>>> -
> >>>>>> -static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
> >>>>>> -{
> >>>>>> -    if (fence->ops != &timeline_fence_ops)
> >>>>>> -            return NULL;
> >>>>>> -    return container_of(fence, struct sync_pt, base);
> >>>>>> -}
> >>>>>> -
> >>>>>> -/**
> >>>>>> - * sync_timeline_create() - creates a sync object
> >>>>>> - * @name:   sync_timeline name
> >>>>>> - *
> >>>>>> - * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
> >>>>>> - * case of error.
> >>>>>> - */
> >>>>>> -static struct sync_timeline *sync_timeline_create(const char *name)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *obj;
> >>>>>> -
> >>>>>> -    obj = kzalloc(sizeof(*obj), GFP_KERNEL);
> >>>>>> -    if (!obj)
> >>>>>> -            return NULL;
> >>>>>> -
> >>>>>> -    kref_init(&obj->kref);
> >>>>>> -    obj->context = dma_fence_context_alloc(1);
> >>>>>> -    strlcpy(obj->name, name, sizeof(obj->name));
> >>>>>> -
> >>>>>> -    obj->pt_tree = RB_ROOT;
> >>>>>> -    INIT_LIST_HEAD(&obj->pt_list);
> >>>>>> -    spin_lock_init(&obj->lock);
> >>>>>> -
> >>>>>> -    sync_timeline_debug_add(obj);
> >>>>>> -
> >>>>>> -    return obj;
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void sync_timeline_free(struct kref *kref)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *obj =
> >>>>>> -            container_of(kref, struct sync_timeline, kref);
> >>>>>> -
> >>>>>> -    sync_timeline_debug_remove(obj);
> >>>>>> -
> >>>>>> -    kfree(obj);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void sync_timeline_get(struct sync_timeline *obj)
> >>>>>> -{
> >>>>>> -    kref_get(&obj->kref);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void sync_timeline_put(struct sync_timeline *obj)
> >>>>>> -{
> >>>>>> -    kref_put(&obj->kref, sync_timeline_free);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
> >>>>>> -{
> >>>>>> -    return "sw_sync";
> >>>>>> -}
> >>>>>> -
> >>>>>> -static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>>>> -
> >>>>>> -    return parent->name;
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void timeline_fence_release(struct dma_fence *fence)
> >>>>>> -{
> >>>>>> -    struct sync_pt *pt = dma_fence_to_sync_pt(fence);
> >>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>>>> -    unsigned long flags;
> >>>>>> -
> >>>>>> -    spin_lock_irqsave(fence->lock, flags);
> >>>>>> -    if (!list_empty(&pt->link)) {
> >>>>>> -            list_del(&pt->link);
> >>>>>> -            rb_erase(&pt->node, &parent->pt_tree);
> >>>>>> -    }
> >>>>>> -    spin_unlock_irqrestore(fence->lock, flags);
> >>>>>> -
> >>>>>> -    sync_timeline_put(parent);
> >>>>>> -    dma_fence_free(fence);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static bool timeline_fence_signaled(struct dma_fence *fence)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>>>> -
> >>>>>> -    return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static bool timeline_fence_enable_signaling(struct dma_fence *fence)
> >>>>>> -{
> >>>>>> -    return true;
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void timeline_fence_value_str(struct dma_fence *fence,
> >>>>>> -                                char *str, int size)
> >>>>>> -{
> >>>>>> -    snprintf(str, size, "%lld", fence->seqno);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void timeline_fence_timeline_value_str(struct dma_fence *fence,
> >>>>>> -                                         char *str, int size)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>>>> -
> >>>>>> -    snprintf(str, size, "%d", parent->value);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static const struct dma_fence_ops timeline_fence_ops = {
> >>>>>> -    .get_driver_name = timeline_fence_get_driver_name,
> >>>>>> -    .get_timeline_name = timeline_fence_get_timeline_name,
> >>>>>> -    .enable_signaling = timeline_fence_enable_signaling,
> >>>>>> -    .signaled = timeline_fence_signaled,
> >>>>>> -    .release = timeline_fence_release,
> >>>>>> -    .fence_value_str = timeline_fence_value_str,
> >>>>>> -    .timeline_value_str = timeline_fence_timeline_value_str,
> >>>>>> -};
> >>>>>> -
> >>>>>> -/**
> >>>>>> - * sync_timeline_signal() - signal a status change on a sync_timeline
> >>>>>> - * @obj:    sync_timeline to signal
> >>>>>> - * @inc:    num to increment on timeline->value
> >>>>>> - *
> >>>>>> - * A sync implementation should call this any time one of it's fences
> >>>>>> - * has signaled or has an error condition.
> >>>>>> - */
> >>>>>> -static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
> >>>>>> -{
> >>>>>> -    struct sync_pt *pt, *next;
> >>>>>> -
> >>>>>> -    trace_sync_timeline(obj);
> >>>>>> -
> >>>>>> -    spin_lock_irq(&obj->lock);
> >>>>>> -
> >>>>>> -    obj->value += inc;
> >>>>>> -
> >>>>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> >>>>>> -            if (!timeline_fence_signaled(&pt->base))
> >>>>>> -                    break;
> >>>>>> -
> >>>>>> -            list_del_init(&pt->link);
> >>>>>> -            rb_erase(&pt->node, &obj->pt_tree);
> >>>>>> -
> >>>>>> -            /*
> >>>>>> -             * A signal callback may release the last reference to this
> >>>>>> -             * fence, causing it to be freed. That operation has to be
> >>>>>> -             * last to avoid a use after free inside this loop, and must
> >>>>>> -             * be after we remove the fence from the timeline in order to
> >>>>>> -             * prevent deadlocking on timeline->lock inside
> >>>>>> -             * timeline_fence_release().
> >>>>>> -             */
> >>>>>> -            dma_fence_signal_locked(&pt->base);
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    spin_unlock_irq(&obj->lock);
> >>>>>> -}
> >>>>>> -
> >>>>>> -/**
> >>>>>> - * sync_pt_create() - creates a sync pt
> >>>>>> - * @obj:    parent sync_timeline
> >>>>>> - * @value:  value of the fence
> >>>>>> - *
> >>>>>> - * Creates a new sync_pt (fence) as a child of @parent.  @size bytes will be
> >>>>>> - * allocated allowing for implementation specific data to be kept after
> >>>>>> - * the generic sync_timeline struct. Returns the sync_pt object or
> >>>>>> - * NULL in case of error.
> >>>>>> - */
> >>>>>> -static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
> >>>>>> -                                  unsigned int value)
> >>>>>> -{
> >>>>>> -    struct sync_pt *pt;
> >>>>>> -
> >>>>>> -    pt = kzalloc(sizeof(*pt), GFP_KERNEL);
> >>>>>> -    if (!pt)
> >>>>>> -            return NULL;
> >>>>>> -
> >>>>>> -    sync_timeline_get(obj);
> >>>>>> -    dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
> >>>>>> -                   obj->context, value);
> >>>>>> -    INIT_LIST_HEAD(&pt->link);
> >>>>>> -
> >>>>>> -    spin_lock_irq(&obj->lock);
> >>>>>> -    if (!dma_fence_is_signaled_locked(&pt->base)) {
> >>>>>> -            struct rb_node **p = &obj->pt_tree.rb_node;
> >>>>>> -            struct rb_node *parent = NULL;
> >>>>>> -
> >>>>>> -            while (*p) {
> >>>>>> -                    struct sync_pt *other;
> >>>>>> -                    int cmp;
> >>>>>> -
> >>>>>> -                    parent = *p;
> >>>>>> -                    other = rb_entry(parent, typeof(*pt), node);
> >>>>>> -                    cmp = value - other->base.seqno;
> >>>>>> -                    if (cmp > 0) {
> >>>>>> -                            p = &parent->rb_right;
> >>>>>> -                    } else if (cmp < 0) {
> >>>>>> -                            p = &parent->rb_left;
> >>>>>> -                    } else {
> >>>>>> -                            if (dma_fence_get_rcu(&other->base)) {
> >>>>>> -                                    sync_timeline_put(obj);
> >>>>>> -                                    kfree(pt);
> >>>>>> -                                    pt = other;
> >>>>>> -                                    goto unlock;
> >>>>>> -                            }
> >>>>>> -                            p = &parent->rb_left;
> >>>>>> -                    }
> >>>>>> -            }
> >>>>>> -            rb_link_node(&pt->node, parent, p);
> >>>>>> -            rb_insert_color(&pt->node, &obj->pt_tree);
> >>>>>> -
> >>>>>> -            parent = rb_next(&pt->node);
> >>>>>> -            list_add_tail(&pt->link,
> >>>>>> -                          parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
> >>>>>> -    }
> >>>>>> -unlock:
> >>>>>> -    spin_unlock_irq(&obj->lock);
> >>>>>> -
> >>>>>> -    return pt;
> >>>>>> -}
> >>>>>> -
> >>>>>> -/*
> >>>>>> - * *WARNING*
> >>>>>> - *
> >>>>>> - * improper use of this can result in deadlocking kernel drivers from userspace.
> >>>>>> - */
> >>>>>> -
> >>>>>> -/* opening sw_sync create a new sync obj */
> >>>>>> -static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *obj;
> >>>>>> -    char task_comm[TASK_COMM_LEN];
> >>>>>> -
> >>>>>> -    get_task_comm(task_comm, current);
> >>>>>> -
> >>>>>> -    obj = sync_timeline_create(task_comm);
> >>>>>> -    if (!obj)
> >>>>>> -            return -ENOMEM;
> >>>>>> -
> >>>>>> -    file->private_data = obj;
> >>>>>> -
> >>>>>> -    return 0;
> >>>>>> -}
> >>>>>> -
> >>>>>> -static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *obj = file->private_data;
> >>>>>> -    struct sync_pt *pt, *next;
> >>>>>> -
> >>>>>> -    spin_lock_irq(&obj->lock);
> >>>>>> -
> >>>>>> -    list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
> >>>>>> -            dma_fence_set_error(&pt->base, -ENOENT);
> >>>>>> -            dma_fence_signal_locked(&pt->base);
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    spin_unlock_irq(&obj->lock);
> >>>>>> -
> >>>>>> -    sync_timeline_put(obj);
> >>>>>> -    return 0;
> >>>>>> -}
> >>>>>> -
> >>>>>> -static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
> >>>>>> -                                   unsigned long arg)
> >>>>>> -{
> >>>>>> -    int fd = get_unused_fd_flags(O_CLOEXEC);
> >>>>>> -    int err;
> >>>>>> -    struct sync_pt *pt;
> >>>>>> -    struct sync_file *sync_file;
> >>>>>> -    struct sw_sync_create_fence_data data;
> >>>>>> -
> >>>>>> -    if (fd < 0)
> >>>>>> -            return fd;
> >>>>>> -
> >>>>>> -    if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
> >>>>>> -            err = -EFAULT;
> >>>>>> -            goto err;
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    pt = sync_pt_create(obj, data.value);
> >>>>>> -    if (!pt) {
> >>>>>> -            err = -ENOMEM;
> >>>>>> -            goto err;
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    sync_file = sync_file_create(&pt->base);
> >>>>>> -    dma_fence_put(&pt->base);
> >>>>>> -    if (!sync_file) {
> >>>>>> -            err = -ENOMEM;
> >>>>>> -            goto err;
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    data.fence = fd;
> >>>>>> -    if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
> >>>>>> -            fput(sync_file->file);
> >>>>>> -            err = -EFAULT;
> >>>>>> -            goto err;
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    fd_install(fd, sync_file->file);
> >>>>>> -
> >>>>>> -    return 0;
> >>>>>> -
> >>>>>> -err:
> >>>>>> -    put_unused_fd(fd);
> >>>>>> -    return err;
> >>>>>> -}
> >>>>>> -
> >>>>>> -static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
> >>>>>> -{
> >>>>>> -    u32 value;
> >>>>>> -
> >>>>>> -    if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
> >>>>>> -            return -EFAULT;
> >>>>>> -
> >>>>>> -    while (value > INT_MAX)  {
> >>>>>> -            sync_timeline_signal(obj, INT_MAX);
> >>>>>> -            value -= INT_MAX;
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    sync_timeline_signal(obj, value);
> >>>>>> -
> >>>>>> -    return 0;
> >>>>>> -}
> >>>>>> -
> >>>>>> -static long sw_sync_ioctl(struct file *file, unsigned int cmd,
> >>>>>> -                      unsigned long arg)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *obj = file->private_data;
> >>>>>> -
> >>>>>> -    switch (cmd) {
> >>>>>> -    case SW_SYNC_IOC_CREATE_FENCE:
> >>>>>> -            return sw_sync_ioctl_create_fence(obj, arg);
> >>>>>> -
> >>>>>> -    case SW_SYNC_IOC_INC:
> >>>>>> -            return sw_sync_ioctl_inc(obj, arg);
> >>>>>> -
> >>>>>> -    default:
> >>>>>> -            return -ENOTTY;
> >>>>>> -    }
> >>>>>> -}
> >>>>>> -
> >>>>>> -const struct file_operations sw_sync_debugfs_fops = {
> >>>>>> -    .open           = sw_sync_debugfs_open,
> >>>>>> -    .release        = sw_sync_debugfs_release,
> >>>>>> -    .unlocked_ioctl = sw_sync_ioctl,
> >>>>>> -    .compat_ioctl   = compat_ptr_ioctl,
> >>>>>> -};
> >>>>>> diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
> >>>>>> deleted file mode 100644
> >>>>>> index 101394f16930..000000000000
> >>>>>> --- a/drivers/dma-buf/sync_debug.c
> >>>>>> +++ /dev/null
> >>>>>> @@ -1,190 +0,0 @@
> >>>>>> -// SPDX-License-Identifier: GPL-2.0-only
> >>>>>> -/*
> >>>>>> - * Sync File validation framework and debug information
> >>>>>> - *
> >>>>>> - * Copyright (C) 2012 Google, Inc.
> >>>>>> - */
> >>>>>> -
> >>>>>> -#include <linux/debugfs.h>
> >>>>>> -#include "sync_debug.h"
> >>>>>> -
> >>>>>> -static struct dentry *dbgfs;
> >>>>>> -
> >>>>>> -static LIST_HEAD(sync_timeline_list_head);
> >>>>>> -static DEFINE_SPINLOCK(sync_timeline_list_lock);
> >>>>>> -static LIST_HEAD(sync_file_list_head);
> >>>>>> -static DEFINE_SPINLOCK(sync_file_list_lock);
> >>>>>> -
> >>>>>> -void sync_timeline_debug_add(struct sync_timeline *obj)
> >>>>>> -{
> >>>>>> -    unsigned long flags;
> >>>>>> -
> >>>>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
> >>>>>> -    list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
> >>>>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> >>>>>> -}
> >>>>>> -
> >>>>>> -void sync_timeline_debug_remove(struct sync_timeline *obj)
> >>>>>> -{
> >>>>>> -    unsigned long flags;
> >>>>>> -
> >>>>>> -    spin_lock_irqsave(&sync_timeline_list_lock, flags);
> >>>>>> -    list_del(&obj->sync_timeline_list);
> >>>>>> -    spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
> >>>>>> -}
> >>>>>> -
> >>>>>> -void sync_file_debug_add(struct sync_file *sync_file)
> >>>>>> -{
> >>>>>> -    unsigned long flags;
> >>>>>> -
> >>>>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
> >>>>>> -    list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
> >>>>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
> >>>>>> -}
> >>>>>> -
> >>>>>> -void sync_file_debug_remove(struct sync_file *sync_file)
> >>>>>> -{
> >>>>>> -    unsigned long flags;
> >>>>>> -
> >>>>>> -    spin_lock_irqsave(&sync_file_list_lock, flags);
> >>>>>> -    list_del(&sync_file->sync_file_list);
> >>>>>> -    spin_unlock_irqrestore(&sync_file_list_lock, flags);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static const char *sync_status_str(int status)
> >>>>>> -{
> >>>>>> -    if (status < 0)
> >>>>>> -            return "error";
> >>>>>> -
> >>>>>> -    if (status > 0)
> >>>>>> -            return "signaled";
> >>>>>> -
> >>>>>> -    return "active";
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void sync_print_fence(struct seq_file *s,
> >>>>>> -                         struct dma_fence *fence, bool show)
> >>>>>> -{
> >>>>>> -    struct sync_timeline *parent = dma_fence_parent(fence);
> >>>>>> -    int status;
> >>>>>> -
> >>>>>> -    status = dma_fence_get_status_locked(fence);
> >>>>>> -
> >>>>>> -    seq_printf(s, "  %s%sfence %s",
> >>>>>> -               show ? parent->name : "",
> >>>>>> -               show ? "_" : "",
> >>>>>> -               sync_status_str(status));
> >>>>>> -
> >>>>>> -    if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
> >>>>>> -            struct timespec64 ts64 =
> >>>>>> -                    ktime_to_timespec64(fence->timestamp);
> >>>>>> -
> >>>>>> -            seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    if (fence->ops->timeline_value_str &&
> >>>>>> -            fence->ops->fence_value_str) {
> >>>>>> -            char value[64];
> >>>>>> -            bool success;
> >>>>>> -
> >>>>>> -            fence->ops->fence_value_str(fence, value, sizeof(value));
> >>>>>> -            success = strlen(value);
> >>>>>> -
> >>>>>> -            if (success) {
> >>>>>> -                    seq_printf(s, ": %s", value);
> >>>>>> -
> >>>>>> -                    fence->ops->timeline_value_str(fence, value,
> >>>>>> -                                                   sizeof(value));
> >>>>>> -
> >>>>>> -                    if (strlen(value))
> >>>>>> -                            seq_printf(s, " / %s", value);
> >>>>>> -            }
> >>>>>> -    }
> >>>>>> -
> >>>>>> -    seq_putc(s, '\n');
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
> >>>>>> -{
> >>>>>> -    struct list_head *pos;
> >>>>>> -
> >>>>>> -    seq_printf(s, "%s: %d\n", obj->name, obj->value);
> >>>>>> -
> >>>>>> -    spin_lock_irq(&obj->lock);
> >>>>>> -    list_for_each(pos, &obj->pt_list) {
> >>>>>> -            struct sync_pt *pt = container_of(pos, struct sync_pt, link);
> >>>>>> -            sync_print_fence(s, &pt->base, false);
> >>>>>> -    }
> >>>>>> -    spin_unlock_irq(&obj->lock);
> >>>>>> -}
> >>>>>> -
> >>>>>> -static void sync_print_sync_file(struct seq_file *s,
> >>>>>> -                              struct sync_file *sync_file)
> >>>>>> -{
> >>>>>> -    char buf[128];
> >>>>>> -    int i;
> >>>>>> -
> >>>>>> -    seq_printf(s, "[%p] %s: %s\n", sync_file,
> >>>>>> -               sync_file_get_name(sync_file, buf, sizeof(buf)),
> >>>>>> -               sync_status_str(dma_fence_get_status(sync_file->fence)));
> >>>>>> -
> >>>>>> -    if (dma_fence_is_array(sync_file->fence)) {
> >>>>>> -            struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
> >>>>>> -
> >>>>>> -            for (i = 0; i < array->num_fences; ++i)
> >>>>>> -                    sync_print_fence(s, array->fences[i], true);
> >>>>>> -    } else {
> >>>>>> -            sync_print_fence(s, sync_file->fence, true);
> >>>>>> -    }
> >>>>>> -}
> >>>>>> -
> >>>>>> -static int sync_info_debugfs_show(struct seq_file *s, void *unused)
> >>>>>> -{
> >>>>>> -    struct list_head *pos;
> >>>>>> -
> >>>>>> -    seq_puts(s, "objs:\n--------------\n");
> >>>>>> -
> >>>>>> -    spin_lock_irq(&sync_timeline_list_lock);
> >>>>>> -    list_for_each(pos, &sync_timeline_list_head) {
> >>>>>> -            struct sync_timeline *obj =
> >>>>>> -                    container_of(pos, struct sync_timeline,
> >>>>>> -                                 sync_timeline_list);
> >>>>>> -
> >>>>>> -            sync_print_obj(s, obj);
> >>>>>> -            seq_putc(s, '\n');
> >>>>>> -    }
> >>>>>> -    spin_unlock_irq(&sync_timeline_list_lock);
> >>>>>> -
> >>>>>> -    seq_puts(s, "fences:\n--------------\n");
> >>>>>> -
> >>>>>> -    spin_lock_irq(&sync_file_list_lock);
> >>>>>> -    list_for_each(pos, &sync_file_list_head) {
> >>>>>> -            struct sync_file *sync_file =
> >>>>>> -                    container_of(pos, struct sync_file, sync_file_list);
> >>>>>> -
> >>>>>> -            sync_print_sync_file(s, sync_file);
> >>>>>> -            seq_putc(s, '\n');
> >>>>>> -    }
> >>>>>> -    spin_unlock_irq(&sync_file_list_lock);
> >>>>>> -    return 0;
> >>>>>> -}
> >>>>>> -
> >>>>>> -DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
> >>>>>> -
> >>>>>> -static __init int sync_debugfs_init(void)
> >>>>>> -{
> >>>>>> -    dbgfs = debugfs_create_dir("sync", NULL);
> >>>>>> -
> >>>>>> -    /*
> >>>>>> -     * The debugfs files won't ever get removed and thus, there is
> >>>>>> -     * no need to protect it against removal races. The use of
> >>>>>> -     * debugfs_create_file_unsafe() is actually safe here.
> >>>>>> -     */
> >>>>>> -    debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
> >>>>>> -                               &sync_info_debugfs_fops);
> >>>>>> -    debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
> >>>>>> -                               &sw_sync_debugfs_fops);
> >>>>>> -
> >>>>>> -    return 0;
> >>>>>> -}
> >>>>>> -late_initcall(sync_debugfs_init);
> >>>>>> diff --git a/drivers/dma-buf/sync_debug.h b/drivers/dma-buf/sync_debug.h
> >>>>>> deleted file mode 100644
> >>>>>> index 6176e52ba2d7..000000000000
> >>>>>> --- a/drivers/dma-buf/sync_debug.h
> >>>>>> +++ /dev/null
> >>>>>> @@ -1,72 +0,0 @@
> >>>>>> -/*
> >>>>>> - * Sync File validation framework and debug infomation
> >>>>>> - *
> >>>>>> - * Copyright (C) 2012 Google, Inc.
> >>>>>> - *
> >>>>>> - * This program is distributed in the hope that it will be useful,
> >>>>>> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> >>>>>> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> >>>>>> - * GNU General Public License for more details.
> >>>>>> - *
> >>>>>> - */
> >>>>>> -
> >>>>>> -#ifndef _LINUX_SYNC_H
> >>>>>> -#define _LINUX_SYNC_H
> >>>>>> -
> >>>>>> -#include <linux/list.h>
> >>>>>> -#include <linux/rbtree.h>
> >>>>>> -#include <linux/spinlock.h>
> >>>>>> -#include <linux/dma-fence.h>
> >>>>>> -
> >>>>>> -#include <linux/sync_file.h>
> >>>>>> -#include <uapi/linux/sync_file.h>
> >>>>>> -
> >>>>>> -/**
> >>>>>> - * struct sync_timeline - sync object
> >>>>>> - * @kref:           reference count on fence.
> >>>>>> - * @name:           name of the sync_timeline. Useful for debugging
> >>>>>> - * @lock:           lock protecting @pt_list and @value
> >>>>>> - * @pt_tree:                rbtree of active (unsignaled/errored) sync_pts
> >>>>>> - * @pt_list:                list of active (unsignaled/errored) sync_pts
> >>>>>> - * @sync_timeline_list:     membership in global sync_timeline_list
> >>>>>> - */
> >>>>>> -struct sync_timeline {
> >>>>>> -    struct kref             kref;
> >>>>>> -    char                    name[32];
> >>>>>> -
> >>>>>> -    /* protected by lock */
> >>>>>> -    u64                     context;
> >>>>>> -    int                     value;
> >>>>>> -
> >>>>>> -    struct rb_root          pt_tree;
> >>>>>> -    struct list_head        pt_list;
> >>>>>> -    spinlock_t              lock;
> >>>>>> -
> >>>>>> -    struct list_head        sync_timeline_list;
> >>>>>> -};
> >>>>>> -
> >>>>>> -static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
> >>>>>> -{
> >>>>>> -    return container_of(fence->lock, struct sync_timeline, lock);
> >>>>>> -}
> >>>>>> -
> >>>>>> -/**
> >>>>>> - * struct sync_pt - sync_pt object
> >>>>>> - * @base: base fence object
> >>>>>> - * @link: link on the sync timeline's list
> >>>>>> - * @node: node in the sync timeline's tree
> >>>>>> - */
> >>>>>> -struct sync_pt {
> >>>>>> -    struct dma_fence base;
> >>>>>> -    struct list_head link;
> >>>>>> -    struct rb_node node;
> >>>>>> -};
> >>>>>> -
> >>>>>> -extern const struct file_operations sw_sync_debugfs_fops;
> >>>>>> -
> >>>>>> -void sync_timeline_debug_add(struct sync_timeline *obj);
> >>>>>> -void sync_timeline_debug_remove(struct sync_timeline *obj);
> >>>>>> -void sync_file_debug_add(struct sync_file *fence);
> >>>>>> -void sync_file_debug_remove(struct sync_file *fence);
> >>>>>> -
> >>>>>> -#endif /* _LINUX_SYNC_H */
> >>>>>> --
> >>>>>> 2.25.1
> >>>>>>
> >
>


-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
  2021-07-29  7:23     ` Daniel Vetter
@ 2021-07-30  4:52       ` John Stultz
  -1 siblings, 0 replies; 26+ messages in thread
From: John Stultz @ 2021-07-30  4:52 UTC (permalink / raw)
  To: Daniel Vetter
  Cc: Christian König, Gustavo Padovan, dri-devel,
	moderated list:DMA BUFFER SHARING FRAMEWORK, linux-media,
	Alistair Delva, Hridya Valsaraju, Sandeep Patil

On Thu, Jul 29, 2021 at 12:24 AM Daniel Vetter <daniel@ffwll.ch> wrote:
>
> On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
> > As we now knew controlling dma_fence synchronization from userspace is
> > extremely dangerous and can not only deadlock drivers but trivially also the
> > whole kernel memory management.
> >
> > Entirely remove this option. We now have in kernel unit tests to exercise the
> > dma_fence framework and it's containers.
> >
> > Signed-off-by: Christian König <christian.koenig@amd.com>
>
> There's also igts for this, and Android heavily uses this. So I'm not sure
> we can just nuke it.

Eeeeh... I don't think that's actually the case anymore. As of
android12-5.10 CONFIG_SW_SYNC is not turned on.
Further, Android is disabling debugfs in their kernels as it exposes
too much to userland.

That said, there still are some references to it:
  https://cs.android.com/android/platform/superproject/+/master:system/core/libsync/sync.c;l=416

But it looks like the actual users are only kselftest and igt?

Adding Alistair, Hridya and Sandeep in case they have more context.

thanks
-john

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
@ 2021-07-30  4:52       ` John Stultz
  0 siblings, 0 replies; 26+ messages in thread
From: John Stultz @ 2021-07-30  4:52 UTC (permalink / raw)
  To: Daniel Vetter
  Cc: Alistair Delva, Sandeep Patil, Christian König, dri-devel,
	moderated list:DMA BUFFER SHARING FRAMEWORK, Gustavo Padovan,
	Hridya Valsaraju, linux-media

On Thu, Jul 29, 2021 at 12:24 AM Daniel Vetter <daniel@ffwll.ch> wrote:
>
> On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
> > As we now knew controlling dma_fence synchronization from userspace is
> > extremely dangerous and can not only deadlock drivers but trivially also the
> > whole kernel memory management.
> >
> > Entirely remove this option. We now have in kernel unit tests to exercise the
> > dma_fence framework and it's containers.
> >
> > Signed-off-by: Christian König <christian.koenig@amd.com>
>
> There's also igts for this, and Android heavily uses this. So I'm not sure
> we can just nuke it.

Eeeeh... I don't think that's actually the case anymore. As of
android12-5.10 CONFIG_SW_SYNC is not turned on.
Further, Android is disabling debugfs in their kernels as it exposes
too much to userland.

That said, there still are some references to it:
  https://cs.android.com/android/platform/superproject/+/master:system/core/libsync/sync.c;l=416

But it looks like the actual users are only kselftest and igt?

Adding Alistair, Hridya and Sandeep in case they have more context.

thanks
-john

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files
  2021-07-30  4:52       ` John Stultz
  (?)
@ 2021-07-30 20:46       ` Hridya Valsaraju
  -1 siblings, 0 replies; 26+ messages in thread
From: Hridya Valsaraju @ 2021-07-30 20:46 UTC (permalink / raw)
  To: John Stultz
  Cc: Daniel Vetter, Christian König, Gustavo Padovan, dri-devel,
	moderated list:DMA BUFFER SHARING FRAMEWORK, linux-media,
	Alistair Delva, Sandeep Patil

On Thu, Jul 29, 2021 at 9:52 PM John Stultz <john.stultz@linaro.org> wrote:
>
> On Thu, Jul 29, 2021 at 12:24 AM Daniel Vetter <daniel@ffwll.ch> wrote:
> >
> > On Thu, Jul 29, 2021 at 09:03:30AM +0200, Christian König wrote:
> > > As we now knew controlling dma_fence synchronization from userspace is
> > > extremely dangerous and can not only deadlock drivers but trivially also the
> > > whole kernel memory management.
> > >
> > > Entirely remove this option. We now have in kernel unit tests to exercise the
> > > dma_fence framework and it's containers.
> > >
> > > Signed-off-by: Christian König <christian.koenig@amd.com>
> >
> > There's also igts for this, and Android heavily uses this. So I'm not sure
> > we can just nuke it.
>
> Eeeeh... I don't think that's actually the case anymore. As of
> android12-5.10 CONFIG_SW_SYNC is not turned on.
> Further, Android is disabling debugfs in their kernels as it exposes
> too much to userland.
>
> That said, there still are some references to it:
>   https://cs.android.com/android/platform/superproject/+/master:system/core/libsync/sync.c;l=416

Hello,

Like John mentioned, CONFIG_SW_SYNC is not turned on for the *-5.4 and
newer branches in the Android Common Kernel. The references in AOSP
are only in place to support devices with older kernels upgrading to
newer Android versions.

Regards,
Hridya

>
> But it looks like the actual users are only kselftest and igt?
>
> Adding Alistair, Hridya and Sandeep in case they have more context.
>
> thanks
> -john

^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2021-07-30 20:47 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-29  7:03 [PATCH 1/3] dma-buf: nuke seqno-fence Christian König
2021-07-29  7:03 ` [PATCH 2/3] dma-buf: nuke DMA_FENCE_TRACE macros Christian König
2021-07-29  7:22   ` Daniel Vetter
2021-07-29  7:22     ` Daniel Vetter
2021-07-29  7:24     ` Christian König
2021-07-29  7:24       ` Christian König
2021-07-29  7:03 ` [PATCH 3/3] dma-buf: nuke SW_SYNC debugfs files Christian König
2021-07-29  7:23   ` Daniel Vetter
2021-07-29  7:23     ` Daniel Vetter
2021-07-29  8:38     ` Christian König
2021-07-29  8:38       ` Christian König
2021-07-29  9:03       ` Daniel Vetter
2021-07-29  9:03         ` Daniel Vetter
2021-07-29 10:21         ` Christian König
2021-07-29 10:21           ` Christian König
2021-07-29 11:59           ` Daniel Vetter
2021-07-29 11:59             ` Daniel Vetter
2021-07-29 12:25             ` Christian König
2021-07-29 12:25               ` Christian König
2021-07-29 12:31               ` Daniel Vetter
2021-07-29 12:31                 ` Daniel Vetter
2021-07-30  4:52     ` John Stultz
2021-07-30  4:52       ` John Stultz
2021-07-30 20:46       ` Hridya Valsaraju
2021-07-29  7:24 ` [PATCH 1/3] dma-buf: nuke seqno-fence Daniel Vetter
2021-07-29  7:24   ` Daniel Vetter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.