drm: introduce drm_dev_free() to fix error paths
authorDavid Herrmann <dh.herrmann@gmail.com>
Wed, 2 Oct 2013 09:23:37 +0000 (11:23 +0200)
committerDave Airlie <airlied@redhat.com>
Wed, 9 Oct 2013 05:55:09 +0000 (15:55 +1000)
The error paths in DRM bus drivers currently leak memory as they don't
correctly revert drm_dev_alloc(). Introduce drm_dev_free() to free DRM
devices which haven't been registered, yet.

We must be careful not to introduce any side-effects with cleanups done in
drm_dev_free(). drm_ht_remove(), drm_ctxbitmap_cleanup() and
drm_gem_destroy() are all fine in that regard.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
drivers/gpu/drm/drm_pci.c
drivers/gpu/drm/drm_platform.c
drivers/gpu/drm/drm_stub.c
drivers/gpu/drm/drm_usb.c
include/drm/drmP.h

index 743589dc47ce686e540fcede547e40a4b8050740..cabe2bd702ae968495c3b5d9805a3c7b7bccfd64 100644 (file)
@@ -354,7 +354,7 @@ int drm_get_pci_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
 err_pci:
        pci_disable_device(pdev);
 err_free:
-       kfree(dev);
+       drm_dev_free(dev);
        return ret;
 }
 EXPORT_SYMBOL(drm_get_pci_dev);
index a0f91f85651f5e0753cb02d7e04cf017c540ab05..fc24fee8ec833b6bdb2c26ada2b45e947622b941 100644 (file)
@@ -64,7 +64,7 @@ static int drm_get_platform_dev(struct platform_device *platdev,
        return 0;
 
 err_free:
-       kfree(dev);
+       drm_dev_free(dev);
        return ret;
 }
 
index 432994aafc3b9b525e989d402de84d9a344e52f5..3b5b07482de8e2206a5cff7e2381aadaddc2e398 100644 (file)
@@ -363,7 +363,6 @@ static void drm_unplug_minor(struct drm_minor *minor)
  */
 void drm_put_dev(struct drm_device *dev)
 {
-       struct drm_driver *driver;
        struct drm_map_list *r_list, *list_temp;
 
        DRM_DEBUG("\n");
@@ -372,7 +371,6 @@ void drm_put_dev(struct drm_device *dev)
                DRM_ERROR("cleanup called no dev\n");
                return;
        }
-       driver = dev->driver;
 
        drm_lastclose(dev);
 
@@ -386,9 +384,6 @@ void drm_put_dev(struct drm_device *dev)
 
        list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
                drm_rmmap(dev, r_list->map);
-       drm_ht_remove(&dev->map_hash);
-
-       drm_ctxbitmap_cleanup(dev);
 
        if (drm_core_check_feature(dev, DRIVER_MODESET))
                drm_put_minor(&dev->control);
@@ -396,14 +391,11 @@ void drm_put_dev(struct drm_device *dev)
        if (dev->render)
                drm_put_minor(&dev->render);
 
-       if (driver->driver_features & DRIVER_GEM)
-               drm_gem_destroy(dev);
-
        drm_put_minor(&dev->primary);
 
        list_del(&dev->driver_item);
-       kfree(dev->devname);
-       kfree(dev);
+
+       drm_dev_free(dev);
 }
 EXPORT_SYMBOL(drm_put_dev);
 
@@ -501,6 +493,29 @@ err_free:
 }
 EXPORT_SYMBOL(drm_dev_alloc);
 
+/**
+ * drm_dev_free - Free DRM device
+ * @dev: DRM device to free
+ *
+ * Free a DRM device that has previously been allocated via drm_dev_alloc().
+ * You must not use kfree() instead or you will leak memory.
+ *
+ * This must not be called once the device got registered. Use drm_put_dev()
+ * instead, which then calls drm_dev_free().
+ */
+void drm_dev_free(struct drm_device *dev)
+{
+       if (dev->driver->driver_features & DRIVER_GEM)
+               drm_gem_destroy(dev);
+
+       drm_ctxbitmap_cleanup(dev);
+       drm_ht_remove(&dev->map_hash);
+
+       kfree(dev->devname);
+       kfree(dev);
+}
+EXPORT_SYMBOL(drm_dev_free);
+
 /**
  * drm_dev_register - Register DRM device
  * @dev: Device to register
index 5ef353f77b5adf6e542f0e269b732f1387e8e097..b179b70e7853b4bb4eb69c9a97a57295ae26a084 100644 (file)
@@ -29,7 +29,7 @@ int drm_get_usb_dev(struct usb_interface *interface,
        return 0;
 
 err_free:
-       kfree(dev);
+       drm_dev_free(dev);
        return ret;
 
 }
index 1973f7966511233d81054c54d63ea37ab8e67142..537833c9ab839f8a276500b2c2df02166cad05af 100644 (file)
@@ -1642,6 +1642,7 @@ static __inline__ void drm_core_dropmap(struct drm_local_map *map)
 
 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
                                 struct device *parent);
+void drm_dev_free(struct drm_device *dev);
 int drm_dev_register(struct drm_device *dev, unsigned long flags);
 int drm_get_minor(struct drm_device *dev, struct drm_minor **minor, int type);
 /*@}*/