drm: use anon-inode instead of relying on cdevs
authorDavid Herrmann <dh.herrmann@gmail.com>
Fri, 3 Jan 2014 13:24:19 +0000 (14:24 +0100)
committerDavid Herrmann <dh.herrmann@gmail.com>
Sun, 16 Mar 2014 11:23:33 +0000 (12:23 +0100)
DRM drivers share a common address_space across all character-devices of a
single DRM device. This allows simple buffer eviction and mapping-control.
However, DRM core currently waits for the first ->open() on any char-dev
to mark the underlying inode as backing inode of the device. This delayed
initialization causes ugly conditions all over the place:
  if (dev->dev_mapping)
    do_sth();

To avoid delayed initialization and to stop reusing the inode of the
char-dev, we allocate an anonymous inode for each DRM device and reset
filp->f_mapping to it on ->open().

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
15 files changed:
drivers/gpu/drm/ast/ast_ttm.c
drivers/gpu/drm/bochs/bochs_mm.c
drivers/gpu/drm/cirrus/cirrus_ttm.c
drivers/gpu/drm/drm_fops.c
drivers/gpu/drm/drm_stub.c
drivers/gpu/drm/i915/i915_gem.c
drivers/gpu/drm/mgag200/mgag200_ttm.c
drivers/gpu/drm/nouveau/nouveau_gem.c
drivers/gpu/drm/omapdrm/omap_gem.c
drivers/gpu/drm/qxl/qxl_object.c
drivers/gpu/drm/qxl/qxl_ttm.c
drivers/gpu/drm/radeon/radeon_object.c
drivers/gpu/drm/radeon/radeon_ttm.c
drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
include/drm/drmP.h

index 4ea9b17ac17a9c5459617898718f1145eaf232ba..2b491539e60ccdb3c8397388db775b83dfa28e22 100644 (file)
@@ -324,7 +324,7 @@ int ast_bo_create(struct drm_device *dev, int size, int align,
        }
 
        astbo->bo.bdev = &ast->ttm.bdev;
-       astbo->bo.bdev->dev_mapping = dev->dev_mapping;
+       astbo->bo.bdev->dev_mapping = dev->anon_inode->i_mapping;
 
        ast_ttm_placement(astbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
 
index ce6858765b378df01f66192b7fede05070054d3c..2d8546d7b4affb9139defbc035e167d1c50e5190 100644 (file)
@@ -359,7 +359,7 @@ static int bochs_bo_create(struct drm_device *dev, int size, int align,
        }
 
        bochsbo->bo.bdev = &bochs->ttm.bdev;
-       bochsbo->bo.bdev->dev_mapping = dev->dev_mapping;
+       bochsbo->bo.bdev->dev_mapping = dev->anon_inode->i_mapping;
 
        bochs_ttm_placement(bochsbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
 
index 8b37c25ff9bd0074810fc942fcee38fab3cbab4a..efcbd70a8774bd3977caeb70e08998768daacacd 100644 (file)
@@ -329,7 +329,7 @@ int cirrus_bo_create(struct drm_device *dev, int size, int align,
        }
 
        cirrusbo->bo.bdev = &cirrus->ttm.bdev;
-       cirrusbo->bo.bdev->dev_mapping = dev->dev_mapping;
+       cirrusbo->bo.bdev->dev_mapping = dev->anon_inode->i_mapping;
 
        cirrus_ttm_placement(cirrusbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
 
index 7f2af9aca03895b97c75af76968093a61930b501..147a84d9da9b807f8825cefa0513a574395678d7 100644 (file)
@@ -84,8 +84,6 @@ int drm_open(struct inode *inode, struct file *filp)
        struct drm_minor *minor;
        int retcode = 0;
        int need_setup = 0;
-       struct address_space *old_mapping;
-       struct address_space *old_imapping;
 
        minor = idr_find(&drm_minors_idr, minor_id);
        if (!minor)
@@ -99,16 +97,9 @@ int drm_open(struct inode *inode, struct file *filp)
 
        if (!dev->open_count++)
                need_setup = 1;
-       mutex_lock(&dev->struct_mutex);
-       old_imapping = inode->i_mapping;
-       old_mapping = dev->dev_mapping;
-       if (old_mapping == NULL)
-               dev->dev_mapping = &inode->i_data;
-       /* ihold ensures nobody can remove inode with our i_data */
-       ihold(container_of(dev->dev_mapping, struct inode, i_data));
-       inode->i_mapping = dev->dev_mapping;
-       filp->f_mapping = dev->dev_mapping;
-       mutex_unlock(&dev->struct_mutex);
+
+       /* share address_space across all char-devs of a single device */
+       filp->f_mapping = dev->anon_inode->i_mapping;
 
        retcode = drm_open_helper(inode, filp, dev);
        if (retcode)
@@ -121,12 +112,6 @@ int drm_open(struct inode *inode, struct file *filp)
        return 0;
 
 err_undo:
-       mutex_lock(&dev->struct_mutex);
-       filp->f_mapping = old_imapping;
-       inode->i_mapping = old_imapping;
-       iput(container_of(dev->dev_mapping, struct inode, i_data));
-       dev->dev_mapping = old_mapping;
-       mutex_unlock(&dev->struct_mutex);
        dev->open_count--;
        return retcode;
 }
@@ -434,7 +419,6 @@ int drm_lastclose(struct drm_device * dev)
 
        drm_legacy_dma_takedown(dev);
 
-       dev->dev_mapping = NULL;
        mutex_unlock(&dev->struct_mutex);
 
        drm_legacy_dev_reinit(dev);
@@ -549,9 +533,6 @@ int drm_release(struct inode *inode, struct file *filp)
                }
        }
 
-       BUG_ON(dev->dev_mapping == NULL);
-       iput(container_of(dev->dev_mapping, struct inode, i_data));
-
        /* drop the reference held my the file priv */
        if (file_priv->master)
                drm_master_put(&file_priv->master);
index f2903d7e3d8e50dc4f2f87544f1879e23f0f8a95..04c25cedd4c19df74ffe80ac3945cbe361cde8b9 100644 (file)
@@ -526,8 +526,15 @@ struct drm_device *drm_dev_alloc(struct drm_driver *driver,
        mutex_init(&dev->struct_mutex);
        mutex_init(&dev->ctxlist_mutex);
 
-       if (drm_ht_create(&dev->map_hash, 12))
+       dev->anon_inode = drm_fs_inode_new();
+       if (IS_ERR(dev->anon_inode)) {
+               ret = PTR_ERR(dev->anon_inode);
+               DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
                goto err_free;
+       }
+
+       if (drm_ht_create(&dev->map_hash, 12))
+               goto err_inode;
 
        ret = drm_ctxbitmap_init(dev);
        if (ret) {
@@ -549,6 +556,8 @@ err_ctxbitmap:
        drm_ctxbitmap_cleanup(dev);
 err_ht:
        drm_ht_remove(&dev->map_hash);
+err_inode:
+       drm_fs_inode_free(dev->anon_inode);
 err_free:
        kfree(dev);
        return NULL;
@@ -576,6 +585,7 @@ void drm_dev_free(struct drm_device *dev)
 
        drm_ctxbitmap_cleanup(dev);
        drm_ht_remove(&dev->map_hash);
+       drm_fs_inode_free(dev->anon_inode);
 
        kfree(dev->devname);
        kfree(dev);
index 3618bb0cda0a84c89c5856da4593c53a8b01b77c..928e15bb2cf756271935b7fde8dec8bf4677ebcc 100644 (file)
@@ -1508,7 +1508,8 @@ i915_gem_release_mmap(struct drm_i915_gem_object *obj)
        if (!obj->fault_mappable)
                return;
 
-       drm_vma_node_unmap(&obj->base.vma_node, obj->base.dev->dev_mapping);
+       drm_vma_node_unmap(&obj->base.vma_node,
+                          obj->base.dev->anon_inode->i_mapping);
        obj->fault_mappable = false;
 }
 
index adb5166a5dfdbe3a77e4e7154411e5162df1abd3..c1c2cb608ab32b55313ab72a8f170db952d174ee 100644 (file)
@@ -324,7 +324,7 @@ int mgag200_bo_create(struct drm_device *dev, int size, int align,
        }
 
        mgabo->bo.bdev = &mdev->ttm.bdev;
-       mgabo->bo.bdev->dev_mapping = dev->dev_mapping;
+       mgabo->bo.bdev->dev_mapping = dev->anon_inode->i_mapping;
 
        mgag200_ttm_placement(mgabo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
 
index 27c3fd89e8ceb657595d188a8a3ff16cadde0edc..5b193b898f5a1e50e7a7a8f257ab25aab48b37f8 100644 (file)
@@ -228,7 +228,7 @@ nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
        struct nouveau_bo *nvbo = NULL;
        int ret = 0;
 
-       drm->ttm.bdev.dev_mapping = drm->dev->dev_mapping;
+       drm->ttm.bdev.dev_mapping = drm->dev->anon_inode->i_mapping;
 
        if (!pfb->memtype_valid(pfb, req->info.tile_flags)) {
                NV_ERROR(cli, "bad page flags: 0x%08x\n", req->info.tile_flags);
index 5aec3e81fe241e8ef1308d1b53feca75b3dfa34f..c8d972763889d9f99344b70f3166624d7b756665 100644 (file)
@@ -153,24 +153,24 @@ static struct {
 static void evict_entry(struct drm_gem_object *obj,
                enum tiler_fmt fmt, struct usergart_entry *entry)
 {
-       if (obj->dev->dev_mapping) {
-               struct omap_gem_object *omap_obj = to_omap_bo(obj);
-               int n = usergart[fmt].height;
-               size_t size = PAGE_SIZE * n;
-               loff_t off = mmap_offset(obj) +
-                               (entry->obj_pgoff << PAGE_SHIFT);
-               const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
-               if (m > 1) {
-                       int i;
-                       /* if stride > than PAGE_SIZE then sparse mapping: */
-                       for (i = n; i > 0; i--) {
-                               unmap_mapping_range(obj->dev->dev_mapping,
-                                               off, PAGE_SIZE, 1);
-                               off += PAGE_SIZE * m;
-                       }
-               } else {
-                       unmap_mapping_range(obj->dev->dev_mapping, off, size, 1);
+       struct omap_gem_object *omap_obj = to_omap_bo(obj);
+       int n = usergart[fmt].height;
+       size_t size = PAGE_SIZE * n;
+       loff_t off = mmap_offset(obj) +
+                       (entry->obj_pgoff << PAGE_SHIFT);
+       const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
+
+       if (m > 1) {
+               int i;
+               /* if stride > than PAGE_SIZE then sparse mapping: */
+               for (i = n; i > 0; i--) {
+                       unmap_mapping_range(obj->dev->anon_inode->i_mapping,
+                                           off, PAGE_SIZE, 1);
+                       off += PAGE_SIZE * m;
                }
+       } else {
+               unmap_mapping_range(obj->dev->anon_inode->i_mapping,
+                                   off, size, 1);
        }
 
        entry->obj = NULL;
index 8691c76c5ef0a440b9391765b7c0ca099e2d6e2c..7e20c21b08791ee42d9de41c10fd6189b906e69a 100644 (file)
@@ -82,8 +82,7 @@ int qxl_bo_create(struct qxl_device *qdev,
        enum ttm_bo_type type;
        int r;
 
-       if (unlikely(qdev->mman.bdev.dev_mapping == NULL))
-               qdev->mman.bdev.dev_mapping = qdev->ddev->dev_mapping;
+       qdev->mman.bdev.dev_mapping = qdev->ddev->anon_inode->i_mapping;
        if (kernel)
                type = ttm_bo_type_kernel;
        else
index c7e7e6590c2baeb29485e6a9f31b44f0631fc606..78cbc40a8efb987d03ba20efc672c83bc5949b3f 100644 (file)
@@ -518,8 +518,7 @@ int qxl_ttm_init(struct qxl_device *qdev)
                 ((unsigned)num_io_pages * PAGE_SIZE) / (1024 * 1024));
        DRM_INFO("qxl: %uM of Surface memory size\n",
                 (unsigned)qdev->surfaceram_size / (1024 * 1024));
-       if (unlikely(qdev->mman.bdev.dev_mapping == NULL))
-               qdev->mman.bdev.dev_mapping = qdev->ddev->dev_mapping;
+       qdev->mman.bdev.dev_mapping = qdev->ddev->anon_inode->i_mapping;
        r = qxl_ttm_debugfs_init(qdev);
        if (r) {
                DRM_ERROR("Failed to init debugfs\n");
index ca79431b2c1cb0689cfdef39225a5e61f5a6f355..6a7f3c6ffbbe8abd38df0afea7b68e5716e97aa4 100644 (file)
@@ -145,7 +145,7 @@ int radeon_bo_create(struct radeon_device *rdev,
 
        size = ALIGN(size, PAGE_SIZE);
 
-       rdev->mman.bdev.dev_mapping = rdev->ddev->dev_mapping;
+       rdev->mman.bdev.dev_mapping = rdev->ddev->anon_inode->i_mapping;
        if (kernel) {
                type = ttm_bo_type_kernel;
        } else if (sg) {
index 60dfce889ecfb3eac52f79b784b42dfcb68d8f84..4663fbcfda28d190465e9f25e267341e68758d67 100644 (file)
@@ -745,7 +745,7 @@ int radeon_ttm_init(struct radeon_device *rdev)
        }
        DRM_INFO("radeon: %uM of GTT memory ready.\n",
                 (unsigned)(rdev->mc.gtt_size / (1024 * 1024)));
-       rdev->mman.bdev.dev_mapping = rdev->ddev->dev_mapping;
+       rdev->mman.bdev.dev_mapping = rdev->ddev->anon_inode->i_mapping;
 
        r = radeon_ttm_debugfs_init(rdev);
        if (r) {
index 0083cbf99edfd33bdfac9ad819ff4b12b1e7b5d8..df4b03e035bc68bc0b70788391ea90b98792c642 100644 (file)
@@ -969,7 +969,7 @@ static int vmw_driver_open(struct drm_device *dev, struct drm_file *file_priv)
                goto out_no_shman;
 
        file_priv->driver_priv = vmw_fp;
-       dev_priv->bdev.dev_mapping = dev->dev_mapping;
+       dev_priv->bdev.dev_mapping = dev->anon_inode->i_mapping;
 
        return 0;
 
index 04a7f31301f8fda61ab05cf42a3bea5c28431178..3227b716ffdf0cd3324805d4d515c83019b76c7f 100644 (file)
@@ -1183,7 +1183,7 @@ struct drm_device {
        struct drm_sg_mem *sg;  /**< Scatter gather memory */
        unsigned int num_crtcs;                  /**< Number of CRTCs on this device */
        void *dev_private;              /**< device private data */
-       struct address_space *dev_mapping;
+       struct inode *anon_inode;
        struct drm_sigdata sigdata;        /**< For block_all_signals */
        sigset_t sigmask;