Re: [PATCH v6 6/6] drm/fence: add out-fences support

From: Brian Starkey
Date: Fri Oct 28 2016 - 05:24:01 EST


Hi Gustavo,

On Thu, Oct 27, 2016 at 05:37:11PM -0200, Gustavo Padovan wrote:
From: Gustavo Padovan <gustavo.padovan@xxxxxxxxxxxxxxx>

Support DRM out-fences by creating a sync_file with a fence for each CRTC
that sets the OUT_FENCE_PTR property.

We use the out_fence pointer received in the OUT_FENCE_PTR prop to send
the sync_file fd back to userspace.

The sync_file and fd are allocated/created before commit, but the
fd_install operation only happens after we know that commit succeed.

v2: Comment by Rob Clark:
- Squash commit that adds DRM_MODE_ATOMIC_OUT_FENCE flag here.

Comment by Daniel Vetter:
- Add clean up code for out_fences

v3: Comments by Daniel Vetter:
- create DRM_MODE_ATOMIC_EVENT_MASK
- userspace should fill out_fences_ptr with the crtc_ids for which
it wants fences back.

v4: Create OUT_FENCE_PTR properties and remove old approach.

v5: Comments by Brian Starkey:
- Remove extra fence_get() in atomic_ioctl()
- Check ret before iterating on the crtc_state
- check ret before fd_install
- set fence_state to NULL at the beginning
- check fence_state->out_fence_ptr before put_user()
- change order of fput() and put_unused_fd() on failure

- Add access_ok() check to the out_fence_ptr received
- Rebase after fence -> dma_fence rename
- Store out_fence_ptr in the drm_atomic_state
- Split crtc_setup_out_fence()
- return -1 as out_fence with TEST_ONLY flag

Signed-off-by: Gustavo Padovan <gustavo.padovan@xxxxxxxxxxxxxxx>
---
drivers/gpu/drm/drm_atomic.c | 199 ++++++++++++++++++++++++++++++++++++-------
drivers/gpu/drm/drm_crtc.c | 8 ++
include/drm/drm_atomic.h | 1 +
include/drm/drm_crtc.h | 17 ++++
4 files changed, 196 insertions(+), 29 deletions(-)

diff --git a/drivers/gpu/drm/drm_atomic.c b/drivers/gpu/drm/drm_atomic.c
index 28d9366..9b70a27 100644
--- a/drivers/gpu/drm/drm_atomic.c
+++ b/drivers/gpu/drm/drm_atomic.c
@@ -290,6 +290,46 @@ drm_atomic_get_crtc_state(struct drm_atomic_state *state,
EXPORT_SYMBOL(drm_atomic_get_crtc_state);

/**
+ * drm_atomic_set_out_fence_for_crtc - set CRTC out fence pointer
+ * @state: global atomic state object
+ * @crtc: crtc to set the out fence pointer
+ * @fence_ptr: the userspace pointer to user
+ *
+ * This function stores the out fence pointer in the atomic state.
+ */
+static void
+drm_atomic_set_out_fence_for_crtc(struct drm_atomic_state *state,
+ struct drm_crtc *crtc, u64 __user *fence_ptr)

bikeshed: I'd rather these were called set/get_out_fence_ptr_for_crtc,
as they don't involve struct drm_fence at all.

+{
+ state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
+}
+
+/**
+ * drm_atomic_get_out_fence_for_crtc - get CRTC out fence pointer
+ * @state: global atomic state object
+ * @crtc: crtc to set the out fence pointer
+ *
+ * Get the user pointer that should be used for to store the out fence fd.
+ * This function should be called only once per atomic state as it clears
+ * the out_fence_ptr store there to prevent drivers to access them.
+ *
+ * Returns:
+ *
+ * The out fence user pointer.
+ */
+static u64 __user *
+drm_atomic_get_out_fence_for_crtc(struct drm_atomic_state *state,
+ struct drm_crtc *crtc)
+{
+ u64 __user *fence_ptr;
+
+ fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
+ state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
+
+ return fence_ptr;
+}
+
+/**
* drm_atomic_set_mode_for_crtc - set mode for CRTC
* @state: the CRTC whose incoming state to update
* @mode: kernel-internal mode to use for the CRTC, or NULL to disable
@@ -490,6 +530,12 @@ int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
&replaced);
state->color_mgmt_changed |= replaced;
return ret;
+ } else if (property == config->prop_out_fence_ptr) {
+ uint64_t __user *fence_ptr = u64_to_user_ptr(val);
+ if (!access_ok(VERIFY_WRITE, fence_ptr, sizeof(*fence_ptr)))
+ return -EFAULT;
+
+ drm_atomic_set_out_fence_for_crtc(state->state, crtc, fence_ptr);
} else if (crtc->funcs->atomic_set_property)
return crtc->funcs->atomic_set_property(crtc, state, property, val);
else
@@ -532,6 +578,8 @@ drm_atomic_crtc_get_property(struct drm_crtc *crtc,
*val = (state->ctm) ? state->ctm->base.id : 0;
else if (property == config->gamma_lut_property)
*val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
+ else if (property == config->prop_out_fence_ptr)
+ *val = 0;
else if (crtc->funcs->atomic_get_property)
return crtc->funcs->atomic_get_property(crtc, state, property, val);
else
@@ -1506,11 +1554,9 @@ EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
*/

static struct drm_pending_vblank_event *create_vblank_event(
- struct drm_device *dev, struct drm_file *file_priv,
- struct dma_fence *fence, uint64_t user_data)
+ struct drm_device *dev, uint64_t user_data)
{
struct drm_pending_vblank_event *e = NULL;
- int ret;

e = kzalloc(sizeof *e, GFP_KERNEL);
if (!e)
@@ -1520,17 +1566,6 @@ static struct drm_pending_vblank_event *create_vblank_event(
e->event.base.length = sizeof(e->event);
e->event.user_data = user_data;

- if (file_priv) {
- ret = drm_event_reserve_init(dev, file_priv, &e->base,
- &e->event.base);
- if (ret) {
- kfree(e);
- return NULL;
- }
- }
-
- e->base.fence = fence;
-
return e;
}

@@ -1635,6 +1670,38 @@ void drm_atomic_clean_old_fb(struct drm_device *dev,
}
EXPORT_SYMBOL(drm_atomic_clean_old_fb);

+struct dma_fence *get_crtc_fence(struct drm_crtc *crtc,
+ struct drm_crtc_state *crtc_state)
+{
+ struct dma_fence *fence;
+
+ fence = kzalloc(sizeof(*fence), GFP_KERNEL);
+ if (!fence)
+ return NULL;
+
+ dma_fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock,
+ crtc->fence_context, ++crtc->fence_seqno);
+
+ return fence;
+}
+
+static int setup_out_fence(struct drm_out_fence_state *fence_state,
+ struct dma_fence *fence)
+{
+ fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
+ if (fence_state->fd < 0)
+ return fence_state->fd;
+
+ if (put_user(fence_state->fd, fence_state->out_fence_ptr))
+ return -EFAULT;
+
+ fence_state->sync_file = sync_file_create(fence);
+ if(!fence_state->sync_file)
+ return -ENOMEM;
+
+ return 0;
+}
+
int drm_mode_atomic_ioctl(struct drm_device *dev,
void *data, struct drm_file *file_priv)
{
@@ -1649,9 +1716,10 @@ int drm_mode_atomic_ioctl(struct drm_device *dev,
struct drm_plane *plane;
struct drm_crtc *crtc;
struct drm_crtc_state *crtc_state;
+ struct drm_out_fence_state *fence_state = NULL;
unsigned plane_mask;
int ret = 0;
- unsigned int i, j;
+ unsigned int i, j, fence_idx = 0;

/* disallow for drivers not supporting atomic: */
if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
@@ -1766,13 +1834,33 @@ int drm_mode_atomic_ioctl(struct drm_device *dev,
drm_mode_object_unreference(obj);
}

+ fence_state = kcalloc(dev->mode_config.num_crtc, sizeof(*fence_state),
+ GFP_KERNEL);
+ if (!fence_state) {
+ ret = -ENOMEM;
+ goto out;
+ }

- if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
- for_each_crtc_in_state(state, crtc, crtc_state, i) {
+ for_each_crtc_in_state(state, crtc, crtc_state, i) {
+
+ fence_state->out_fence_ptr =
+ drm_atomic_get_out_fence_for_crtc(crtc_state->state, crtc);

I think you're missing some subscripting on fence_state in this loop.
Here you set fence_state->out_fence_ptr directly, whereas I guess it
should be fence_state[fence_idx].out_fence_ptr;

+
+ if (fence_state->out_fence_ptr &&
+ (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
+ if (put_user(-1, fence_state->out_fence_ptr)) {
+ ret = -EFAULT;
+ goto out;
+ }
+
+ continue;
+ }
+
+ if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT ||
+ fence_state->out_fence_ptr) {
struct drm_pending_vblank_event *e;

- e = create_vblank_event(dev, file_priv, NULL,
- arg->user_data);
+ e = create_vblank_event(dev, arg->user_data);
if (!e) {
ret = -ENOMEM;
goto out;
@@ -1780,6 +1868,39 @@ int drm_mode_atomic_ioctl(struct drm_device *dev,

crtc_state->event = e;
}
+
+ if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
+ struct drm_pending_vblank_event *e = crtc_state->event;
+
+ if (!file_priv)
+ continue;
+
+ ret = drm_event_reserve_init(dev, file_priv, &e->base,
+ &e->event.base);
+ if (ret) {
+ kfree(e);
+ crtc_state->event = NULL;
+ goto out;
+ }
+ }
+
+ if (fence_state->out_fence_ptr) {
+ struct dma_fence *fence;
+
+ fence = get_crtc_fence(crtc, crtc_state);
+ if (!fence) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ ret = setup_out_fence(&fence_state[fence_idx++], fence);

Related to above, I think if you're going to manipulate the
fence_state array outside of setup_out_fence you should drop the
inline increment here, as it will be easy for a refactor to miss it
and add bugs.

+ if (ret) {
+ dma_fence_put(fence);
+ goto out;
+ }
+
+ crtc_state->event->base.fence = fence;
+ }
}

if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
@@ -1794,24 +1915,44 @@ int drm_mode_atomic_ioctl(struct drm_device *dev,
ret = drm_atomic_commit(state);
}

+ if (ret)
+ goto out;
+
+ for (i = 0; i < fence_idx; i++)
+ fd_install(fence_state[i].fd, fence_state[i].sync_file->file);
+
out:
drm_atomic_clean_old_fb(dev, plane_mask, ret);

- if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
- /*
- * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
- * if they weren't, this code should be called on success
- * for TEST_ONLY too.
- */
-
+ if (ret) {
for_each_crtc_in_state(state, crtc, crtc_state, i) {
- if (!crtc_state->event)
- continue;
+ /*
+ * TEST_ONLY and PAGE_FLIP_EVENT are mutually
+ * exclusive, if they weren't, this code should be
+ * called on success for TEST_ONLY too.
+ */
+ if (crtc_state->event)
+ drm_event_cancel_free(dev,
+ &crtc_state->event->base);
+ }

- drm_event_cancel_free(dev, &crtc_state->event->base);
+ if (fence_state) {
+ for (i = 0; i < fence_idx; i++) {
+ if (fence_state[i].sync_file)
+ fput(fence_state[i].sync_file->file);
+ if (fence_state[i].fd >= 0)
+ put_unused_fd(fence_state[i].fd);
+
+ /* If this fails log error to the user */
+ if (fence_state[i].out_fence_ptr &&
+ put_user(-1, fence_state[i].out_fence_ptr))
+ DRM_INFO("Couldn't clear out_fence_ptr\n");
+ }
}
}

+ kfree(fence_state);
+
if (ret == -EDEADLK) {
drm_atomic_state_clear(state);
drm_modeset_backoff(&ctx);
diff --git a/drivers/gpu/drm/drm_crtc.c b/drivers/gpu/drm/drm_crtc.c
index e2a06c8..a18d81b 100644
--- a/drivers/gpu/drm/drm_crtc.c
+++ b/drivers/gpu/drm/drm_crtc.c
@@ -274,6 +274,8 @@ int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
drm_object_attach_property(&crtc->base, config->prop_active, 0);
drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
+ drm_object_attach_property(&crtc->base,
+ config->prop_out_fence_ptr, 0);
}

return 0;
@@ -434,6 +436,12 @@ static int drm_mode_create_standard_properties(struct drm_device *dev)
return -ENOMEM;
dev->mode_config.prop_in_fence_fd = prop;

+ prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
+ "OUT_FENCE_PTR", 0, U64_MAX);
+ if (!prop)
+ return -ENOMEM;
+ dev->mode_config.prop_out_fence_ptr = prop;
+
prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
"CRTC_ID", DRM_MODE_OBJECT_CRTC);
if (!prop)
diff --git a/include/drm/drm_atomic.h b/include/drm/drm_atomic.h
index 2d1e9c9..ebde60e 100644
--- a/include/drm/drm_atomic.h
+++ b/include/drm/drm_atomic.h
@@ -144,6 +144,7 @@ struct __drm_crtcs_state {
struct drm_crtc *ptr;
struct drm_crtc_state *state;
struct drm_crtc_commit *commit;
+ u64 __user *out_fence_ptr;
};

struct __drm_connnectors_state {
diff --git a/include/drm/drm_crtc.h b/include/drm/drm_crtc.h
index 278dbdd..2539394 100644
--- a/include/drm/drm_crtc.h
+++ b/include/drm/drm_crtc.h
@@ -768,6 +768,18 @@ static inline struct drm_crtc *fence_to_crtc(struct dma_fence *fence)
}

/**
+ * struct drm_out_fence_state - store out_fence data for install and clean up
+ * @out_fence_ptr: user pointer to store the fence fd in.
+ * @sync_file: sync_file related to the out_fence
+ * @fd: file descriptor to installed on the sync_file.
+ */
+struct drm_out_fence_state {
+ u64 __user *out_fence_ptr;
+ struct sync_file *sync_file;
+ int fd;
+};
+
+/*
* struct drm_mode_set - new values for a CRTC config change
* @fb: framebuffer to use for new config
* @crtc: CRTC whose configuration we're about to change
@@ -1245,6 +1257,11 @@ struct drm_mode_config {
*/
struct drm_property *prop_in_fence_fd;
/**
+ * @prop_out_fence_ptr: Sync File fd pointer representing the
+ * outgoing fences for a CRTC.
+ */

Is it worth noting that userspace should provide a pointer to a u64
and not a plain int? Either here or in some more detailed kernel-doc
for the OUT_FENCE_PTR property.

Cheers,
Brian

+ struct drm_property *prop_out_fence_ptr;
+ /**
* @prop_crtc_id: Default atomic plane property to specify the
* &drm_crtc.
*/
--
2.5.5