Skip to content

Commit 70e67aa

Browse files
robclarklumag
authored andcommitted
dma-buf/sw_sync: Add fence deadline support
This consists of simply storing the most recent deadline, and adding an ioctl to retrieve the deadline. This can be used in conjunction with the SET_DEADLINE ioctl on a fence fd for testing. Ie. create various sw_sync fences, merge them into a fence-array, set deadline on the fence-array and confirm that it is propagated properly to each fence. v2: Switch UABI to express deadline as u64 v3: More verbose UAPI docs, show how to convert from timespec v4: Better comments, track the soonest deadline, as a normal fence implementation would, return an error if no deadline set. Signed-off-by: Rob Clark <robdclark@chromium.org> Reviewed-by: Christian König <christian.koenig@amd.com> Acked-by: Pekka Paalanen <pekka.paalanen@collabora.com> Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org> Link: https://patchwork.freedesktop.org/patch/msgid/20230823215458.203366-4-robdclark@gmail.com
1 parent 63ee445 commit 70e67aa

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

drivers/dma-buf/sw_sync.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,33 @@ struct sw_sync_create_fence_data {
5252
__s32 fence; /* fd of new fence */
5353
};
5454

55+
/**
56+
* struct sw_sync_get_deadline - get the deadline hint of a sw_sync fence
57+
* @deadline_ns: absolute time of the deadline
58+
* @pad: must be zero
59+
* @fence_fd: the sw_sync fence fd (in)
60+
*
61+
* Return the earliest deadline set on the fence. The timebase for the
62+
* deadline is CLOCK_MONOTONIC (same as vblank). If there is no deadline
63+
* set on the fence, this ioctl will return -ENOENT.
64+
*/
65+
struct sw_sync_get_deadline {
66+
__u64 deadline_ns;
67+
__u32 pad;
68+
__s32 fence_fd;
69+
};
70+
5571
#define SW_SYNC_IOC_MAGIC 'W'
5672

5773
#define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0,\
5874
struct sw_sync_create_fence_data)
5975

6076
#define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
77+
#define SW_SYNC_GET_DEADLINE _IOWR(SW_SYNC_IOC_MAGIC, 2, \
78+
struct sw_sync_get_deadline)
79+
80+
81+
#define SW_SYNC_HAS_DEADLINE_BIT DMA_FENCE_FLAG_USER_BITS
6182

6283
static const struct dma_fence_ops timeline_fence_ops;
6384

@@ -171,6 +192,22 @@ static void timeline_fence_timeline_value_str(struct dma_fence *fence,
171192
snprintf(str, size, "%d", parent->value);
172193
}
173194

195+
static void timeline_fence_set_deadline(struct dma_fence *fence, ktime_t deadline)
196+
{
197+
struct sync_pt *pt = dma_fence_to_sync_pt(fence);
198+
unsigned long flags;
199+
200+
spin_lock_irqsave(fence->lock, flags);
201+
if (test_bit(SW_SYNC_HAS_DEADLINE_BIT, &fence->flags)) {
202+
if (ktime_before(deadline, pt->deadline))
203+
pt->deadline = deadline;
204+
} else {
205+
pt->deadline = deadline;
206+
__set_bit(SW_SYNC_HAS_DEADLINE_BIT, &fence->flags);
207+
}
208+
spin_unlock_irqrestore(fence->lock, flags);
209+
}
210+
174211
static const struct dma_fence_ops timeline_fence_ops = {
175212
.get_driver_name = timeline_fence_get_driver_name,
176213
.get_timeline_name = timeline_fence_get_timeline_name,
@@ -179,6 +216,7 @@ static const struct dma_fence_ops timeline_fence_ops = {
179216
.release = timeline_fence_release,
180217
.fence_value_str = timeline_fence_value_str,
181218
.timeline_value_str = timeline_fence_timeline_value_str,
219+
.set_deadline = timeline_fence_set_deadline,
182220
};
183221

184222
/**
@@ -387,6 +425,47 @@ static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
387425
return 0;
388426
}
389427

428+
static int sw_sync_ioctl_get_deadline(struct sync_timeline *obj, unsigned long arg)
429+
{
430+
struct sw_sync_get_deadline data;
431+
struct dma_fence *fence;
432+
unsigned long flags;
433+
struct sync_pt *pt;
434+
int ret = 0;
435+
436+
if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
437+
return -EFAULT;
438+
439+
if (data.deadline_ns || data.pad)
440+
return -EINVAL;
441+
442+
fence = sync_file_get_fence(data.fence_fd);
443+
if (!fence)
444+
return -EINVAL;
445+
446+
pt = dma_fence_to_sync_pt(fence);
447+
if (!pt)
448+
return -EINVAL;
449+
450+
spin_lock_irqsave(fence->lock, flags);
451+
if (test_bit(SW_SYNC_HAS_DEADLINE_BIT, &fence->flags)) {
452+
data.deadline_ns = ktime_to_ns(pt->deadline);
453+
} else {
454+
ret = -ENOENT;
455+
}
456+
spin_unlock_irqrestore(fence->lock, flags);
457+
458+
dma_fence_put(fence);
459+
460+
if (ret)
461+
return ret;
462+
463+
if (copy_to_user((void __user *)arg, &data, sizeof(data)))
464+
return -EFAULT;
465+
466+
return 0;
467+
}
468+
390469
static long sw_sync_ioctl(struct file *file, unsigned int cmd,
391470
unsigned long arg)
392471
{
@@ -399,6 +478,9 @@ static long sw_sync_ioctl(struct file *file, unsigned int cmd,
399478
case SW_SYNC_IOC_INC:
400479
return sw_sync_ioctl_inc(obj, arg);
401480

481+
case SW_SYNC_GET_DEADLINE:
482+
return sw_sync_ioctl_get_deadline(obj, arg);
483+
402484
default:
403485
return -ENOTTY;
404486
}

drivers/dma-buf/sync_debug.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ static inline struct sync_timeline *dma_fence_parent(struct dma_fence *fence)
5555
* @base: base fence object
5656
* @link: link on the sync timeline's list
5757
* @node: node in the sync timeline's tree
58+
* @deadline: the earliest fence deadline hint
5859
*/
5960
struct sync_pt {
6061
struct dma_fence base;
6162
struct list_head link;
6263
struct rb_node node;
64+
ktime_t deadline;
6365
};
6466

6567
extern const struct file_operations sw_sync_debugfs_fops;

0 commit comments

Comments
 (0)