Skip to contentSkip to conversation

drm: close race in connector registration

RFC PATCH In review Dave Airlie 3 participants 5 messages Jul 24, 2014

Subsystems (2)
lore
Share & tools
lore

Dave Airlie

author maintainer
#0 lore raw

RFC PATCH 1/1 drm: close race in connector registration

From: Dave Airlie <[email protected]>

Daniel pointed out with hotplug that userspace could be trying to oops us as root for lols, and that to be correct we shouldn't register the object with the idr before we have fully set the connector object up.

His proposed solution was a lot more life changing, this seemed like a simpler proposition to me, get the connector object id from the idr, but don't register the object until the drm_connector_register callback.

The open question is whether the drm_mode_object_register needs a bigger lock than just the idr one, but I can't see why it would, but I can be locking challenged.

---
 drivers/gpu/drm/drm_crtc.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_crtc.c +21 -4
index 1ccf5cb..9f8cc1a 100644
@@ -376,13 +376,14 @@ EXPORT_SYMBOL(drm_get_format_name);
  * New unique (relative to other objects in @dev) integer identifier for the
  * object.
  */
-int drm_mode_object_get(struct drm_device *dev,
-			struct drm_mode_object *obj, uint32_t obj_type)
+static int drm_mode_object_get_noreg(struct drm_device *dev,
+				     struct drm_mode_object *obj, uint32_t obj_type,
+				     bool noreg)
 {
 	int ret;
 
 	mutex_lock(&dev->mode_config.idr_mutex);
-	ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
+	ret = idr_alloc(&dev->mode_config.crtc_idr, noreg ? NULL : obj, 1, 0, GFP_KERNEL);
 	if (ret >= 0) {
 		/*
 		 * Set up the object linking under the protection of the idr
@@ -396,6 +397,20 @@ int drm_mode_object_get(struct drm_device *dev,
 	return ret < 0 ? ret : 0;
 }
 
+int drm_mode_object_get(struct drm_device *dev,
+			struct drm_mode_object *obj, uint32_t obj_type)
+{
+	return drm_mode_object_get_noreg(dev, obj, obj_type, false);
+}
+
+static void drm_mode_object_register(struct drm_device *dev,
+				     struct drm_mode_object *obj)
+{
+	mutex_lock(&dev->mode_config.idr_mutex);
+	idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
+	mutex_unlock(&dev->mode_config.idr_mutex);
+}
+
 /**
  * drm_mode_object_put - free a modeset identifer
  * @dev: DRM device
@@ -849,7 +864,7 @@ int drm_connector_init(struct drm_device *dev,
 
 	drm_modeset_lock_all(dev);
 
-	ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
+	ret = drm_mode_object_get_noreg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, true);
 	if (ret)
 		goto out_unlock;
 
@@ -942,6 +957,8 @@ int drm_connector_register(struct drm_connector *connector)
 {
 	int ret;
 
+	drm_mode_object_register(connector->dev, &connector->base);
+
 	ret = drm_sysfs_connector_add(connector);
 	if (ret)
 		return ret;
Signature · 1 line
1.9.3
#1 lore raw
Quoted 10 lines · Dave Airlie

From: Dave Airlie <[email protected]>

Daniel pointed out with hotplug that userspace could be trying to oops us as root for lols, and that to be correct we shouldn't register the object with the idr before we have fully set the connector object up.

His proposed solution was a lot more life changing, this seemed like a simpler proposition to me, get the connector object id from the idr, but don't register the object until the drm_connector_register callback.

Quoted 5 lines

The open question is whether the drm_mode_object_register needs a bigger lock than just the idr one, but I can't see why it would, but I can be locking challenged.

The noreg=false double negation is a bit a bender, I'd invert the meaning of that. But otherwise I couldn't poke holes into this (but found another one around the idr_mutex). Fix for that is on its way.

With noreg negated and the kerneldoc/EXPORT_SYMBOL moved to the new place this is Reviewed-by: Daniel Vetter <[email protected]>

Cheers, Daniel

Quoted 72 lines
---
 drivers/gpu/drm/drm_crtc.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_crtc.c +21 -4
index 1ccf5cb..9f8cc1a 100644
@@ -376,13 +376,14 @@ EXPORT_SYMBOL(drm_get_format_name);
  * New unique (relative to other objects in @dev) integer identifier for the
  * object.
  */
-int drm_mode_object_get(struct drm_device *dev,
-			struct drm_mode_object *obj, uint32_t obj_type)
+static int drm_mode_object_get_noreg(struct drm_device *dev,
+				     struct drm_mode_object *obj, uint32_t obj_type,
+				     bool noreg)
 {
 	int ret;
 
 	mutex_lock(&dev->mode_config.idr_mutex);
-	ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
+	ret = idr_alloc(&dev->mode_config.crtc_idr, noreg ? NULL : obj, 1, 0, GFP_KERNEL);
 	if (ret >= 0) {
 		/*
 		 * Set up the object linking under the protection of the idr
@@ -396,6 +397,20 @@ int drm_mode_object_get(struct drm_device *dev,
 	return ret < 0 ? ret : 0;
 }
 
+int drm_mode_object_get(struct drm_device *dev,
+			struct drm_mode_object *obj, uint32_t obj_type)
+{
+	return drm_mode_object_get_noreg(dev, obj, obj_type, false);
+}
+
+static void drm_mode_object_register(struct drm_device *dev,
+				     struct drm_mode_object *obj)
+{
+	mutex_lock(&dev->mode_config.idr_mutex);
+	idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
+	mutex_unlock(&dev->mode_config.idr_mutex);
+}
+
 /**
  * drm_mode_object_put - free a modeset identifer
  * @dev: DRM device
@@ -849,7 +864,7 @@ int drm_connector_init(struct drm_device *dev,
 
 	drm_modeset_lock_all(dev);
 
-	ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
+	ret = drm_mode_object_get_noreg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, true);
 	if (ret)
 		goto out_unlock;
 
@@ -942,6 +957,8 @@ int drm_connector_register(struct drm_connector *connector)
 {
 	int ret;
 
+	drm_mode_object_register(connector->dev, &connector->base);
+
 	ret = drm_sysfs_connector_add(connector);
 	if (ret)
 		return ret;
Signature · 6 lines
1.9.3

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
Reviewed-by on v1 1/1
#2 lore raw
In reply to Dave Airlie

Hi

Quoted 72 lines · Dave Airlie

From: Dave Airlie <[email protected]>

Daniel pointed out with hotplug that userspace could be trying to oops us as root for lols, and that to be correct we shouldn't register the object with the idr before we have fully set the connector object up.

His proposed solution was a lot more life changing, this seemed like a simpler proposition to me, get the connector object id from the idr, but don't register the object until the drm_connector_register callback.

The open question is whether the drm_mode_object_register needs a bigger lock than just the idr one, but I can't see why it would, but I can be locking challenged.

---
 drivers/gpu/drm/drm_crtc.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_crtc.c +19 -4
index 1ccf5cb..9f8cc1a 100644
@@ -376,13 +376,14 @@ EXPORT_SYMBOL(drm_get_format_name);
  * New unique (relative to other objects in @dev) integer identifier for the
  * object.
  */
-int drm_mode_object_get(struct drm_device *dev,
-                       struct drm_mode_object *obj, uint32_t obj_type)
+static int drm_mode_object_get_noreg(struct drm_device *dev,
+                                    struct drm_mode_object *obj, uint32_t obj_type,
+                                    bool noreg)
 {
        int ret;
 
        mutex_lock(&dev->mode_config.idr_mutex);
-       ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
+       ret = idr_alloc(&dev->mode_config.crtc_idr, noreg ? NULL : obj, 1, 0, GFP_KERNEL);
        if (ret >= 0) {
                /*
                 * Set up the object linking under the protection of the idr
@@ -396,6 +397,20 @@ int drm_mode_object_get(struct drm_device *dev,
        return ret < 0 ? ret : 0;
 }
 
+int drm_mode_object_get(struct drm_device *dev,
+                       struct drm_mode_object *obj, uint32_t obj_type)
+{
+       return drm_mode_object_get_noreg(dev, obj, obj_type, false);
+}
+
+static void drm_mode_object_register(struct drm_device *dev,
+                                    struct drm_mode_object *obj)
+{
+       mutex_lock(&dev->mode_config.idr_mutex);
+       idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
+       mutex_unlock(&dev->mode_config.idr_mutex);
+}
+
 /**
  * drm_mode_object_put - free a modeset identifer
  * @dev: DRM device
@@ -849,7 +864,7 @@ int drm_connector_init(struct drm_device *dev,
 
        drm_modeset_lock_all(dev);
 
-       ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
+       ret = drm_mode_object_get_noreg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, true);
        if (ret)
                goto out_unlock;
 

drivers/gpu/drm/drm_crtc.c int drm_connector_register(struct drm_connector *connector) · line 961

Quoted 5 lines
drivers/gpu/drm/drm_crtc.c +2 -0
@@ -942,6 +957,8 @@ int drm_connector_register(struct drm_connector *connector)
 {
        int ret;
 
+       drm_mode_object_register(connector->dev, &connector->base);
+

This is the same we do for minor-objects, so fine with me. I'd prefer if the registration is done last in drm_connector_register(), not first, but I'm not sure the debugfs hooks work without the connector available in the lookup tables. Maybe worth a try.

Maybe you also want to do the reverse in drm_connector_unregister()? Making sure no user-space call can look them up anymore.

This patch is:

Thanks
David

Quoted 10 lines
drivers/gpu/drm/drm_crtc.c +0 -1
        ret = drm_sysfs_connector_add(connector);
        if (ret)
                return ret;
--

1.9.3

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

author maintainer
#3 lore raw
Quoted 5 lines

This is the same we do for minor-objects, so fine with me. I'd prefer if the registration is done last in drm_connector_register(), not first, but I'm not sure the debugfs hooks work without the connector available in the lookup tables. Maybe worth a try.

I was worried sysfs registration would trigger something in userspace to life, which wuold then fail because the connector wasn't there.

Dave.

#4 lore raw

Hi

Quoted 8 lines · Dave Airlie
Quoted 5 lines

This is the same we do for minor-objects, so fine with me. I'd prefer if the registration is done last in drm_connector_register(), not first, but I'm not sure the debugfs hooks work without the connector available in the lookup tables. Maybe worth a try.

I was worried sysfs registration would trigger something in userspace to life, which wuold then fail because the connector wasn't there.

Good point. So all fine with me the way it is.

Thanks
David