dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Stone <daniels@collabora.com>
To: dri-devel@lists.freedesktop.org
Subject: [RFC PATCH 36/37] drm: mode: Add user object-creation ioctl
Date: Thu, 19 Mar 2015 04:33:35 +0000	[thread overview]
Message-ID: <1426739616-10635-36-git-send-email-daniels@collabora.com> (raw)
In-Reply-To: <1426739616-10635-1-git-send-email-daniels@collabora.com>

Add an ioctl which allows users to create objects from arbitrary data.
Currently this only supports modes, creating a drm_display_mode from the
userspace drm_mode_modeinfo.

Signed-off-by: Daniel Stone <daniels@collabora.com>
---
 drivers/gpu/drm/drm_crtc.c  | 166 ++++++++++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_fops.c  |   7 +-
 drivers/gpu/drm/drm_ioctl.c |   2 +
 include/drm/drmP.h          |   6 ++
 include/drm/drm_crtc.h      |   6 ++
 include/uapi/drm/drm.h      |   2 +
 include/uapi/drm/drm_mode.h |  22 ++++++
 7 files changed, 209 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/drm_crtc.c b/drivers/gpu/drm/drm_crtc.c
index 21e2052..e258e7c 100644
--- a/drivers/gpu/drm/drm_crtc.c
+++ b/drivers/gpu/drm/drm_crtc.c
@@ -4155,6 +4155,19 @@ static void drm_property_destroy_blob(struct drm_device *dev,
 	kfree(blob);
 }
 
+void drm_property_destroy_user_blobs(struct drm_device *dev,
+				     struct drm_file *file_priv)
+{
+	struct drm_property_blob *blob, *bt;
+
+	mutex_lock(&file_priv->properties_lock);
+	list_for_each_entry_safe(blob, bt, &file_priv->properties, head) {
+		drm_property_destroy_blob(dev, blob);
+	}
+	mutex_unlock(&file_priv->properties_lock);
+	mutex_destroy(&file_priv->properties_lock);
+}
+
 /**
  * drm_mode_getblob_ioctl - get the contents of a blob property value
  * @dev: DRM device
@@ -4216,6 +4229,159 @@ done:
 }
 
 /**
+ * drm_mode_createblob_ioctl - create a new blob property
+ * @dev: DRM device
+ * @data: ioctl data
+ * @file_priv: DRM file info
+ *
+ * This function creates a new blob property with user-defined values. In order
+ * to give us sensible validation and checking when creating, rather than at
+ * every potential use, we also require a type to be provided upfront.
+ *
+ * Called by the user via ioctl.
+ *
+ * Returns:
+ * Zero on success, negative errno on failure.
+ */
+int drm_mode_createblob_ioctl(struct drm_device *dev,
+			      void *data, struct drm_file *file_priv)
+{
+	struct drm_mode_create_blob *out_resp = data;
+	uint32_t length = 0;
+	void *blob_data;
+	int ret = 0;
+	void __user *blob_ptr;
+
+	if (!drm_core_check_feature(dev, DRIVER_MODESET))
+		return -EINVAL;
+
+	switch (out_resp->type) {
+	case DRM_MODE_BLOB_TYPE_MODE: {
+		length = sizeof(struct drm_mode_modeinfo);
+		if (out_resp->length != length) {
+			ret = -E2BIG;
+			goto out_unlocked;
+		}
+		break;
+	}
+	default:
+		ret = -EINVAL;
+		goto out_unlocked;
+	}
+
+	blob_data = kzalloc(length, GFP_USER);
+	if (!blob_data) {
+		ret = -ENOMEM;
+		goto out_unlocked;
+	}
+
+	blob_ptr = (void __user *)(unsigned long)out_resp->data;
+	if (copy_from_user(blob_data, blob_ptr, out_resp->length)) {
+		ret = -EFAULT;
+		goto out_data;
+	}
+
+	drm_modeset_lock_all(dev);
+
+	switch (out_resp->type) {
+	case DRM_MODE_BLOB_TYPE_MODE: {
+		struct drm_display_mode *mode;
+
+		mode = drm_mode_new_from_umode(dev, blob_data);
+		if (!mode) {
+			ret = -EINVAL;
+			goto out_locked;
+		}
+
+		out_resp->blob_id = mode->base.id;
+
+		/* XXX: When we were using a property as the base here, we
+		 *      added the mode to the properties list to ensure it
+		 *      got cleaned up when the drm_file disappeared. But
+		 *      by using drm_display_mode as the base object, we
+		 *      can't add it to the list anymore. Adding a specific
+		 *      mode list feels pretty unclean though ...
+		mutex_lock(&file_priv->properties_lock);
+		list_add_tail(&blob->head, &file_priv->properties);
+		mutex_unlock(&file_priv->properties_lock); */
+
+		break;
+	}
+	}
+
+out_locked:
+	drm_modeset_unlock_all(dev);
+out_data:
+	kfree(blob_data);
+out_unlocked:
+	return ret;
+}
+
+/**
+ * drm_mode_destroyblob_ioctl - destroy a user blob property
+ * @dev: DRM device
+ * @data: ioctl data
+ * @file_priv: DRM file info
+ *
+ * Destroy an existing user-defined blob property.
+ *
+ * Called by the user via ioctl.
+ *
+ * Returns:
+ * Zero on success, negative errno on failure.
+ */
+int drm_mode_destroyblob_ioctl(struct drm_device *dev,
+			       void *data, struct drm_file *file_priv)
+{
+	struct drm_mode_destroy_blob *out_resp = data;
+	struct drm_property_blob *blob = NULL, *bt;
+	struct drm_display_mode *mode = NULL;
+	bool found = false;
+	int ret = 0;
+
+	if (!drm_core_check_feature(dev, DRIVER_MODESET))
+		return -EINVAL;
+
+	drm_modeset_lock_all(dev);
+	blob = drm_property_blob_find(dev, out_resp->blob_id);
+	if (!blob)
+		mode = drm_mode_find(dev, out_resp->blob_id);
+
+	if (!blob && !mode) {
+		ret = -ENOENT;
+		goto out_locked;
+	}
+
+	/* Ensure the property was actually created by this user. */
+	if (blob) {
+		mutex_lock(&file_priv->properties_lock);
+
+		list_for_each_entry(bt, &file_priv->properties, head) {
+			if (bt == blob) {
+				found = true;
+				break;
+			}
+		}
+
+		if (found) {
+			drm_property_destroy_blob(dev, blob);
+			ret = 0;
+		} else {
+			ret = -EPERM;
+		}
+
+		mutex_unlock(&file_priv->properties_lock);
+	} else if (mode) {
+		/* XXX: This is missing all the nice checking from above ... */
+		drm_mode_destroy(dev, mode);
+	}
+
+out_locked:
+	drm_modeset_unlock_all(dev);
+	return ret;
+}
+
+/**
  * drm_mode_connector_set_path_property - set tile property on connector
  * @connector: connector to set property on.
  * @path: path to use for property.
diff --git a/drivers/gpu/drm/drm_fops.c b/drivers/gpu/drm/drm_fops.c
index 076dd60..6e389ea 100644
--- a/drivers/gpu/drm/drm_fops.c
+++ b/drivers/gpu/drm/drm_fops.c
@@ -167,6 +167,8 @@ static int drm_open_helper(struct file *filp, struct drm_minor *minor)
 	INIT_LIST_HEAD(&priv->lhead);
 	INIT_LIST_HEAD(&priv->fbs);
 	mutex_init(&priv->fbs_lock);
+	INIT_LIST_HEAD(&priv->properties);
+	mutex_init(&priv->properties_lock);
 	INIT_LIST_HEAD(&priv->event_list);
 	init_waitqueue_head(&priv->event_wait);
 	priv->event_space = 4096; /* set aside 4k for event buffer */
@@ -408,8 +410,10 @@ int drm_release(struct inode *inode, struct file *filp)
 
 	drm_events_release(file_priv);
 
-	if (drm_core_check_feature(dev, DRIVER_MODESET))
+	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
 		drm_fb_release(file_priv);
+		drm_property_destroy_user_blobs(dev, file_priv);
+	}
 
 	if (drm_core_check_feature(dev, DRIVER_GEM))
 		drm_gem_release(dev, file_priv);
@@ -452,7 +456,6 @@ int drm_release(struct inode *inode, struct file *filp)
 	if (dev->driver->postclose)
 		dev->driver->postclose(dev, file_priv);
 
-
 	if (drm_core_check_feature(dev, DRIVER_PRIME))
 		drm_prime_destroy_file_private(&file_priv->prime);
 
diff --git a/drivers/gpu/drm/drm_ioctl.c b/drivers/gpu/drm/drm_ioctl.c
index a6d773a..d180377 100644
--- a/drivers/gpu/drm/drm_ioctl.c
+++ b/drivers/gpu/drm/drm_ioctl.c
@@ -636,6 +636,8 @@ static const struct drm_ioctl_desc drm_ioctls[] = {
 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_OBJ_SETPROPERTY, drm_mode_obj_set_property_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CURSOR2, drm_mode_cursor2_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
 	DRM_IOCTL_DEF(DRM_IOCTL_MODE_ATOMIC, drm_mode_atomic_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
+	DRM_IOCTL_DEF(DRM_IOCTL_MODE_CREATEPROPBLOB, drm_mode_createblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
+	DRM_IOCTL_DEF(DRM_IOCTL_MODE_DESTROYPROPBLOB, drm_mode_destroyblob_ioctl, DRM_CONTROL_ALLOW|DRM_UNLOCKED),
 };
 
 #define DRM_CORE_IOCTL_COUNT	ARRAY_SIZE( drm_ioctls )
diff --git a/include/drm/drmP.h b/include/drm/drmP.h
index 63c0b01..62b4d8d 100644
--- a/include/drm/drmP.h
+++ b/include/drm/drmP.h
@@ -322,6 +322,12 @@ struct drm_file {
 	struct list_head fbs;
 	struct mutex fbs_lock;
 
+	/** User-created blob properties; this retains a reference on the
+	 *  property. */
+	struct list_head properties;
+	/** Lock for synchronisation of access to properties. */
+	struct mutex properties_lock;
+
 	wait_queue_head_t event_wait;
 	struct list_head event_list;
 	int event_space;
diff --git a/include/drm/drm_crtc.h b/include/drm/drm_crtc.h
index 5c8cb25..d4d914c 100644
--- a/include/drm/drm_crtc.h
+++ b/include/drm/drm_crtc.h
@@ -1284,6 +1284,8 @@ extern const char *drm_get_dvi_i_select_name(int val);
 extern const char *drm_get_tv_subconnector_name(int val);
 extern const char *drm_get_tv_select_name(int val);
 extern void drm_fb_release(struct drm_file *file_priv);
+extern void drm_property_destroy_user_blobs(struct drm_device *dev,
+                                            struct drm_file *file_priv);
 extern int drm_mode_group_init_legacy_group(struct drm_device *dev, struct drm_mode_group *group);
 extern void drm_mode_group_destroy(struct drm_mode_group *group);
 extern void drm_reinit_primary_mode_group(struct drm_device *dev);
@@ -1422,6 +1424,10 @@ extern int drm_mode_getproperty_ioctl(struct drm_device *dev,
 				      void *data, struct drm_file *file_priv);
 extern int drm_mode_getblob_ioctl(struct drm_device *dev,
 				  void *data, struct drm_file *file_priv);
+extern int drm_mode_createblob_ioctl(struct drm_device *dev,
+				     void *data, struct drm_file *file_priv);
+extern int drm_mode_destroyblob_ioctl(struct drm_device *dev,
+				      void *data, struct drm_file *file_priv);
 extern int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
 					      void *data, struct drm_file *file_priv);
 extern int drm_mode_getencoder(struct drm_device *dev,
diff --git a/include/uapi/drm/drm.h b/include/uapi/drm/drm.h
index ff6ef62..3801584 100644
--- a/include/uapi/drm/drm.h
+++ b/include/uapi/drm/drm.h
@@ -786,6 +786,8 @@ struct drm_prime_handle {
 #define DRM_IOCTL_MODE_OBJ_SETPROPERTY	DRM_IOWR(0xBA, struct drm_mode_obj_set_property)
 #define DRM_IOCTL_MODE_CURSOR2		DRM_IOWR(0xBB, struct drm_mode_cursor2)
 #define DRM_IOCTL_MODE_ATOMIC		DRM_IOWR(0xBC, struct drm_mode_atomic)
+#define DRM_IOCTL_MODE_CREATEPROPBLOB	DRM_IOWR(0xBD, struct drm_mode_create_blob)
+#define DRM_IOCTL_MODE_DESTROYPROPBLOB	DRM_IOWR(0xBE, struct drm_mode_destroy_blob)
 
 /**
  * Device specific ioctls should only be in their respective headers
diff --git a/include/uapi/drm/drm_mode.h b/include/uapi/drm/drm_mode.h
index dbeba94..73ac925 100644
--- a/include/uapi/drm/drm_mode.h
+++ b/include/uapi/drm/drm_mode.h
@@ -558,4 +558,26 @@ struct drm_mode_atomic {
 	__u64 user_data;
 };
 
+#define DRM_MODE_BLOB_TYPE_MODE		1 /**< drm_mode_modeinfo */
+
+/**
+ * Create a new 'blob' data property, copying length bytes from data pointer,
+ * and returning new blob ID.
+ */
+struct drm_mode_create_blob {
+	__u32 type;
+	__u32 length;
+	/** Pointer to data to create blob. */
+	__u64 data;
+	/** Return: new property ID. */
+	__u32 blob_id;
+};
+
+/**
+ * Destroy a user-created blob property.
+ */
+struct drm_mode_destroy_blob {
+	__u32 blob_id;
+};
+
 #endif
-- 
2.3.2

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

  parent reply	other threads:[~2015-03-19  4:34 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-19  4:32 [RFC PATCH 00/37] Modesetting for atomic modesetting Daniel Stone
2015-03-19  4:33 ` [RFC PATCH 01/37] drm: mode: Fix typo in kerneldoc Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 02/37] drm: fb_helper: Simplify exit condition Daniel Stone
2015-03-20 15:57     ` Daniel Vetter
2015-03-19  4:33   ` [RFC PATCH 03/37] drm: mode: Allow NULL modes for equality check Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 04/37] drm: crtc_helper: Update hwmode before mode_set call Daniel Stone
2015-03-20 16:05     ` Daniel Vetter
2015-03-19  4:33   ` [RFC PATCH 05/37] drm: Exynos: Remove mode validation inside mode_fixup Daniel Stone
2015-03-20 15:59     ` Daniel Vetter
2015-03-19  4:33   ` [RFC PATCH 06/37] drm: Exynos: Use hwmode for adjusted_mode Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 07/37] drm: sti: Use crtc->hwmode for adjusted mode Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 08/37] drm: ast: Split register set from get_vbios_mode_info Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 09/37] drm: ast: Split mode adjustment " Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 10/37] drm: armada: Use crtc->hwmode for adjusted mode Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 11/37] drm: bridge: Constify mode parameters Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 12/37] drm: encoder-slave: " Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 13/37] drm: connector-helper: Constify mode_valid parameter Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 14/37] drm: connector-helper: Constify mode_set parameters Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 15/37] drm: crtc-helper: " Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 16/37] drm: fb_helper: Constify modeset mode member Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 17/37] DRM: Atomic: Use pointer for mode in CRTC state Daniel Stone
2015-03-20 16:16     ` Daniel Vetter
2015-03-19  4:33   ` [RFC PATCH 18/37] DRM: CRTC: Use pointer for display mode Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 19/37] DRM: Constify crtc->mode pointer Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 20/37] DRM: mode: Rename and combine drm_crtc_convert_umode Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 21/37] DRM: mode: Un-staticise drm_mode_new_from_umode Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 22/37] DRM: mode: Un-staticise drm_crtc_convert_to_umode Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 23/37] drm: mode: Cache userspace mode representation Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 24/37] drm: mode: Use cached usermode representation Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 25/37] drm: mode: Allow userspace to fetch mode as blob Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 26/37] drm: atomic: Expose CRTC active property Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 27/37] drm: atomic: Allow setting " Daniel Stone
2015-03-20 16:21     ` Daniel Vetter
2015-03-19  4:33   ` [RFC PATCH 28/37] drm: mode: Add kref to modes Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 29/37] drm: mode: Add drm_mode_reference Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 30/37] drm: fb_helper: Reference, not duplicate, modes Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 31/37] drm: crtc_helper: " Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 32/37] drm: atomic_helper: " Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 33/37] drm: i915/tegra: atomic: " Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 34/37] drm: atomic: Add MODE_ID property Daniel Stone
2015-03-19  4:33   ` [RFC PATCH 35/37] drm: property: Allow non-global blob properties Daniel Stone
2015-03-19  4:33   ` Daniel Stone [this message]
2015-03-19  4:33   ` [RFC PATCH 37/37] Tegra: SOR: Don't always assume a valid mode Daniel Stone
2015-03-23  8:20 ` [RFC PATCH 00/37] Modesetting for atomic modesetting Daniel Vetter
2015-03-23 16:58   ` Daniel Stone
2015-03-24  8:55     ` Daniel Vetter
2015-03-24 22:49       ` Daniel Stone
2015-03-25  8:37         ` Daniel Vetter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1426739616-10635-36-git-send-email-daniels@collabora.com \
    --to=daniels@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox