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 28/37] drm: mode: Add kref to modes
Date: Thu, 19 Mar 2015 04:33:27 +0000	[thread overview]
Message-ID: <1426739616-10635-28-git-send-email-daniels@collabora.com> (raw)
In-Reply-To: <1426739616-10635-1-git-send-email-daniels@collabora.com>

In order to expose modes to users, we need to be able to reason about
their lifetimes. As we currently treat modes as just a bucket of bits to
be shovelled around, we can't do that: reference counting them enables
us to sensibly deal with their lifetimes, to provide useful guarantees
to userspace.

Signed-off-by: Daniel Stone <daniels@collabora.com>
---
 drivers/gpu/drm/drm_crtc.c        |  2 +-
 drivers/gpu/drm/drm_crtc_helper.c |  3 ++-
 drivers/gpu/drm/drm_modes.c       | 40 ++++++++++++++++++++++++++++++++-------
 drivers/gpu/drm/tegra/dc.c        |  2 +-
 include/drm/drm_modes.h           |  6 ++++++
 5 files changed, 43 insertions(+), 10 deletions(-)

diff --git a/drivers/gpu/drm/drm_crtc.c b/drivers/gpu/drm/drm_crtc.c
index e50eda2..90bf355 100644
--- a/drivers/gpu/drm/drm_crtc.c
+++ b/drivers/gpu/drm/drm_crtc.c
@@ -701,7 +701,7 @@ void drm_crtc_cleanup(struct drm_crtc *crtc)
 {
 	struct drm_device *dev = crtc->dev;
 
-	kfree(crtc->mode);
+	drm_mode_destroy(crtc->dev, crtc->mode);
 	crtc->mode = NULL;
 
 	kfree(crtc->gamma_store);
diff --git a/drivers/gpu/drm/drm_crtc_helper.c b/drivers/gpu/drm/drm_crtc_helper.c
index 46497dd..c23f31f 100644
--- a/drivers/gpu/drm/drm_crtc_helper.c
+++ b/drivers/gpu/drm/drm_crtc_helper.c
@@ -990,7 +990,8 @@ int drm_helper_crtc_mode_set(struct drm_crtc *crtc,
 	if (crtc_funcs->atomic_check) {
 		ret = crtc_funcs->atomic_check(crtc, crtc_state);
 		if (ret) {
-			kfree(crtc_state->mode);
+			/* XXX: crtc_state_destroy?! */
+			drm_mode_destroy(crtc->dev, crtc_state->mode);
 			kfree(crtc_state);
 
 			return ret;
diff --git a/drivers/gpu/drm/drm_modes.c b/drivers/gpu/drm/drm_modes.c
index 0883f64..e87f547 100644
--- a/drivers/gpu/drm/drm_modes.c
+++ b/drivers/gpu/drm/drm_modes.c
@@ -82,25 +82,44 @@ struct drm_display_mode *drm_mode_create(struct drm_device *dev)
 		return NULL;
 	}
 
+	kref_init(&nmode->refcount);
+	nmode->dev = dev;
+
 	return nmode;
 }
 EXPORT_SYMBOL(drm_mode_create);
 
 /**
- * drm_mode_destroy - remove a mode
+ * drm_mode_free - free a mode
+ * @kref: Reference count inside mode
+ *
+ * Release the unique ID of the mode referred to by @kref, then free the
+ * structure itself using kfree.
+ */
+static void drm_mode_free(struct kref *kref)
+{
+	struct drm_display_mode *mode =
+		container_of(kref, struct drm_display_mode, refcount);
+
+	drm_mode_object_put(mode->dev, &mode->base);
+
+	kfree(mode);
+}
+
+/**
+ * drm_mode_destroy - drop reference count on mode
  * @dev: DRM device
  * @mode: mode to remove
  *
- * Release @mode's unique ID, then free it @mode structure itself using kfree.
+ * Drop a reference on a drm_mode. Does not actually free the mode, unless
+ * the reference count drops to zero.
  */
 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
 {
 	if (!mode)
 		return;
 
-	drm_mode_object_put(dev, &mode->base);
-
-	kfree(mode);
+	kref_put(&mode->refcount, drm_mode_free);
 }
 EXPORT_SYMBOL(drm_mode_destroy);
 
@@ -851,15 +870,17 @@ EXPORT_SYMBOL(drm_mode_set_crtcinfo);
  * @dst: mode to overwrite
  * @src: mode to copy
  *
- * Copy an existing mode into another mode, preserving the object id and
- * list head of the destination mode.
+ * Copy an existing mode into another mode, preserving the object id,
+ * refcount, and list head of the destination mode.
  */
 void drm_mode_copy(struct drm_display_mode *dst, const struct drm_display_mode *src)
 {
+	struct kref refcount = dst->refcount;
 	int id = dst->base.id;
 	struct list_head head = dst->head;
 
 	*dst = *src;
+	dst->refcount = refcount;
 	dst->base.id = id;
 	dst->head = head;
 }
@@ -887,6 +908,11 @@ struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
 
 	drm_mode_copy(nmode, mode);
 
+	/* Often modes are duplicated from static lists created upfront without
+	 * the device member set. Ensure that we have a real usable device to
+	 * work from. */
+	nmode->dev = dev;
+
 	return nmode;
 }
 EXPORT_SYMBOL(drm_mode_duplicate);
diff --git a/drivers/gpu/drm/tegra/dc.c b/drivers/gpu/drm/tegra/dc.c
index 6717f07..4dd51fe 100644
--- a/drivers/gpu/drm/tegra/dc.c
+++ b/drivers/gpu/drm/tegra/dc.c
@@ -1039,7 +1039,7 @@ err:
 static void tegra_crtc_atomic_destroy_state(struct drm_crtc *crtc,
 					    struct drm_crtc_state *state)
 {
-	kfree(state->mode);
+	drm_mode_destroy(crtc->dev, state->mode);
 	kfree(state);
 }
 
diff --git a/include/drm/drm_modes.h b/include/drm/drm_modes.h
index 46ed207..70a6c71 100644
--- a/include/drm/drm_modes.h
+++ b/include/drm/drm_modes.h
@@ -99,9 +99,15 @@ enum drm_mode_status {
 struct drm_display_mode {
 	/* Header */
 	struct list_head head;
+	struct drm_device *dev;
 	struct drm_mode_object base;
 	struct drm_mode_modeinfo umode;
 
+	/* As mode objects are now exposed to userspace, rather than just
+	 * having their contents shovelled around, we maintain a refcount
+	 * on them in order to reason about their lifetimes. */
+	struct kref refcount;
+
 	char name[DRM_DISPLAY_MODE_LEN];
 
 	enum drm_mode_status status;
-- 
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   ` Daniel Stone [this message]
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   ` [RFC PATCH 36/37] drm: mode: Add user object-creation ioctl Daniel Stone
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-28-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