drm/kms: Catch mode_object lifetime errors
authorDaniel Vetter <daniel.vetter@ffwll.ch>
Fri, 14 Jun 2019 06:17:23 +0000 (08:17 +0200)
committerDaniel Vetter <daniel.vetter@ffwll.ch>
Wed, 10 Jul 2019 15:17:14 +0000 (17:17 +0200)
Only dynamic mode objects, i.e. those which are refcounted and have a free
callback, can be added while the overall drm_device is visible to
userspace. All others must be added before drm_dev_register and
removed after drm_dev_unregister.

Small issue around drivers still using the load/unload callbacks, we
need to make sure we set dev->registered so that load/unload code in
these callbacks doesn't trigger false warnings. Only a small
adjustement in drm_dev_register was needed.

Motivated by some irc discussions about object ids of dynamic objects
like blobs become invalid, and me going on a bit an audit spree.

Reviewed-by: Sean Paul <sean@poorly.run>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190614061723.1173-1-daniel.vetter@ffwll.ch
drivers/gpu/drm/drm_drv.c
drivers/gpu/drm/drm_mode_object.c

index fe0ce86c280f10a1875c926ebbde94d2a267d3c4..c1671ff0f1eb10a0f6d4c654da5a484cf259904b 100644 (file)
@@ -990,14 +990,14 @@ int drm_dev_register(struct drm_device *dev, unsigned long flags)
        if (ret)
                goto err_minors;
 
-       dev->registered = true;
-
        if (dev->driver->load) {
                ret = dev->driver->load(dev, flags);
                if (ret)
                        goto err_minors;
        }
 
+       dev->registered = true;
+
        if (drm_core_check_feature(dev, DRIVER_MODESET))
                drm_modeset_register_all(dev);
 
index 1c6e5113596243780f24d78b40af4263b6f74a69..c355ba8e6d5ddfb2a754bd721a599a49e520346b 100644 (file)
@@ -42,6 +42,8 @@ int __drm_mode_object_add(struct drm_device *dev, struct drm_mode_object *obj,
 {
        int ret;
 
+       WARN_ON(dev->registered && !obj_free_cb);
+
        mutex_lock(&dev->mode_config.idr_mutex);
        ret = idr_alloc(&dev->mode_config.object_idr, register_obj ? obj : NULL,
                        1, 0, GFP_KERNEL);
@@ -102,6 +104,8 @@ void drm_mode_object_register(struct drm_device *dev,
 void drm_mode_object_unregister(struct drm_device *dev,
                                struct drm_mode_object *object)
 {
+       WARN_ON(dev->registered && !object->free_cb);
+
        mutex_lock(&dev->mode_config.idr_mutex);
        if (object->id) {
                idr_remove(&dev->mode_config.object_idr, object->id);