anon_inodes: allow external inode allocations
Subsystems (11)
PATCH v2 1/2 anon_inodes: allow external inode allocations
DRM core shares a single address_space across all inodes that belong to the same DRM device. This allows efficient unmapping of user-space mappings during buffer eviction. However, there is no easy way to get a shared address_space for DRM devices during initialization. Therefore, we currently delay this step until the first ->open() and save the given inode for future use.
This causes ugly delayed initialization throughout the DRM core. TTM devices end up without a dev_mapping pointer and we have to carefully respect any underlying filesystem implementation so we don't corrupt the inode->i_mapping and inode->i_data fields.
We can avoid this if we were allowed to allocate an anonymous inode for each DRM device. We only have to set file->f_mapping during ->open() and no longer need to adjust inode mappings. As fs/anon_inodes.c already provides a minimal internal FS mount, we extend it to also provide anonymous inodes for use in drivers like DRM.
---
Hi Linus & Al
Any chance I could get an ACK on this? It's fairly trivial and allows us to simplify the DRM logic quite a bit (see patch 2/2 and I have 2 more DRM cleanup patches pending). This has been pending on fsdevel for 2 months now and all we need is an ACK from an VFS maintainer.
I ditched the idea to allocate "struct address_space" separately in DRM as akpm told me that this might get messy without an underlying inode. So I think the anon-inode approach is the way to go.
Thanks
David
fs/anon_inodes.c | 36 +++++++++++++++++++++++++++++------- include/linux/anon_inodes.h | 1 + 2 files changed, 30 insertions(+), 7 deletions(-)
fs/anon_inodes.c +29 -7
@@ -25,6 +25,7 @@static struct vfsmount *anon_inode_mnt__read_mostly ;static struct inode *anon_inode_inode;static const struct file_operations anon_inode_fops; +static struct dentry *anon_inode_root;/* * anon_inodefs_dname() is called from d_path().
@@ -87,19 +88,18 @@ static struct inode *anon_inode_mkinode(struct super_block *s)static struct dentry *anon_inodefs_mount (struct file_system_type *fs_type,int flags,const char *dev_name,void *data) { -struct dentry *root; - root =mount_pseudo (fs_type,"anon_inode:" ,NULL , + anon_inode_root =mount_pseudo (fs_type,"anon_inode:" ,NULL , &anon_inodefs_dentry_operations,ANON_INODE_FS_MAGIC ); -if (!IS_ERR (root)) { -struct super_block *s = root->d_sb; +if (!IS_ERR (anon_inode_root)) { +struct super_block *s = anon_inode_root->d_sb; anon_inode_inode =anon_inode_mkinode (s);if (IS_ERR (anon_inode_inode)) { -dput (root); +dput (anon_inode_root);deactivate_locked_super (s); - root =ERR_CAST (anon_inode_inode); + anon_inode_root =ERR_CAST (anon_inode_inode); } } -return root; +return anon_inode_root; }static struct file_system_type anon_inode_fs_type = {
@@ -285,6 +285,28 @@ err_put_unused_fd: }EXPORT_SYMBOL_GPL (anon_inode_getfd); +/** +* anon_inode_new - create private anonymous inode +* +* Creates a new inode on the anonymous inode FS for driver's use. The inode has +* it's own address_space compared to the shared anon_inode_inode. It can be +* used in situations where user-space mappings have to be shared across +* different files but no backing inode is available. +* +* Call iput(inode) to release the inode. +* +* RETURNS: +* New inode on success, error pointer on failure. +*/ +struct inode *anon_inode_new (void ) +{ +if (IS_ERR (anon_inode_root)) +return ERR_CAST (anon_inode_root); + +return anon_inode_mkinode (anon_inode_root->d_sb); +} +EXPORT_SYMBOL_GPL (anon_inode_new); +static int __init anon_inode_init (void ) {int error;
include/linux/anon_inodes.h +1 -0
@@ -18,6 +18,7 @@ struct file *anon_inode_getfile_private(const char *name, void *priv, int flags);int anon_inode_getfd (const char *name,const struct file_operations *fops,void *priv,int flags); +struct inode *anon_inode_new (void );#endif /* _LINUX_ANON_INODES_H */
Signature · 1 line
1.8.4
PATCH 2/2 DRM: use anon_inode instead of delayed inode init
Instead of delaying inode initialization until first ->open(), we can use an anonymous inode. This avoids modifying FS internal inode fields and provides us a private address_space right during initialization.
Delayed TTM dev_mapping initialization is currently left untouched to keep this simple. But we could now safely provide the address_space during ttm_bo_device_init() instead of delaying until first buffer ->mmap().
Note that this also fixes several bugs:
- We currently call iput(container_of(..dev_mapping..)) before drm_lastclose(), but we reset dev_mapping to zero at the end of drm_lastclose(). This fails if dev_mapping points to an address_space other than the current inode and the char-dev got already removed.
- We also drop dev_mapping during any drm_lastclose() call. So if user-space still has VMAs to our buffers, we will be unable to unmap them if the next ->firstopen() is on another inode. dev_mapping will then point to a new address_space and we leak mappings that we no longer control.
- We ignore inode->i_mapping completely. It is unlikely that a FS uses it to overwrite inode->i_data for char-devs, but it definitely doesn't look very nice to ignore it silently.
Tested with nouveau on x86_64.
--- drivers/gpu/drm/ast/ast_ttm.c | 2 +- drivers/gpu/drm/cirrus/cirrus_ttm.c | 2 +- drivers/gpu/drm/drm_drv.c | 1 - drivers/gpu/drm/drm_fops.c | 24 +++--------------------- drivers/gpu/drm/drm_stub.c | 12 +++++++++++- drivers/gpu/drm/i915/i915_gem.c | 3 ++- drivers/gpu/drm/mgag200/mgag200_ttm.c | 2 +- drivers/gpu/drm/nouveau/nouveau_gem.c | 2 +- drivers/gpu/drm/omapdrm/omap_gem.c | 7 ++++--- drivers/gpu/drm/qxl/qxl_object.c | 2 +- drivers/gpu/drm/qxl/qxl_ttm.c | 2 +- drivers/gpu/drm/radeon/radeon_object.c | 2 +- drivers/gpu/drm/radeon/radeon_ttm.c | 2 +- drivers/gpu/drm/vmwgfx/vmwgfx_drv.c | 2 +- include/drm/drmP.h | 2 +- 15 files changed, 30 insertions(+), 37 deletions(-)
drivers/gpu/drm/ast/ast_ttm.c +1 -1
@@ -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 );
drivers/gpu/drm/cirrus/cirrus_ttm.c +1 -1
@@ -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 );
drivers/gpu/drm/drm_drv.c +0 -1
@@ -231,7 +231,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);
drivers/gpu/drm/drm_fops.c +3 -21
@@ -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); + +/* set address_space for shared mappings */ + filp->f_mapping = dev->anon_inode->i_mapping; retcode =drm_open_helper (inode, filp, dev);if (retcode)
@@ -122,12 +113,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; }
@@ -492,9 +477,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);
drivers/gpu/drm/drm_stub.c +11 -1
@@ -31,6 +31,7 @@* DEALINGS IN THE SOFTWARE. */ +#include <linux/anon_inodes.h> #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/slab.h>
@@ -271,8 +272,14 @@ int drm_fill_in_dev(struct drm_device *dev,mutex_init (&dev->struct_mutex);mutex_init (&dev->ctxlist_mutex); +/* create private address_space on anon inode */ + dev->anon_inode =anon_inode_new (); +if (IS_ERR (dev->anon_inode)) +return PTR_ERR (dev->anon_inode); +if (drm_ht_create (&dev->map_hash,12 )) { -return -ENOMEM ; + retcode = -ENOMEM ; + goto err_inode; }/* the DRM has 6 basic counters */
@@ -313,6 +320,8 @@ int drm_fill_in_dev(struct drm_device *dev, error_out_unreg:drm_lastclose (dev); +err_inode: +iput (dev->anon_inode);return retcode; }EXPORT_SYMBOL (drm_fill_in_dev);
@@ -465,6 +474,7 @@ void drm_put_dev(struct drm_device *dev)drm_put_minor (&dev->primary); +iput (dev->anon_inode);list_del (&dev->driver_item);kfree (dev->devname);kfree (dev);
drivers/gpu/drm/i915/i915_gem.c +2 -1
@@ -1435,7 +1435,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; }
drivers/gpu/drm/mgag200/mgag200_ttm.c +1 -1
@@ -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 );
drivers/gpu/drm/nouveau/nouveau_gem.c +1 -1
@@ -237,7 +237,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);
drivers/gpu/drm/omapdrm/omap_gem.c +4 -3
@@ -153,7 +153,7 @@ static struct {static void evict_entry (struct drm_gem_object *obj,enum tiler_fmt fmt,struct usergart_entry *entry) { -if (obj->dev->dev_mapping) { +if (obj->dev->anon_inode->i_mapping) {struct omap_gem_object *omap_obj =to_omap_bo (obj);int n = usergart[fmt].height;size_t size =PAGE_SIZE * n;
@@ -164,12 +164,13 @@ static void evict_entry(struct drm_gem_object *obj,int i;/* if stride > than PAGE_SIZE then sparse mapping: */ for (i = n; i >0 ; i--) { -unmap_mapping_range (obj->dev->dev_mapping, +unmap_mapping_range (obj->dev->anon_inode->i_mapping, off,PAGE_SIZE ,1 ); off +=PAGE_SIZE * m; } }else { -unmap_mapping_range (obj->dev->dev_mapping, off, size,1 ); +unmap_mapping_range (obj->dev->anon_inode->i_mapping, + off, size,1 ); } }
drivers/gpu/drm/qxl/qxl_object.c +1 -1
@@ -83,7 +83,7 @@ int qxl_bo_create(struct qxl_device *qdev,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
drivers/gpu/drm/qxl/qxl_ttm.c +1 -1
@@ -517,7 +517,7 @@ int qxl_ttm_init(struct qxl_device *qdev)DRM_INFO ("qxl: %luM of IO pages memory ready (VRAM domain)\n" , ((unsigned )num_io_pages *PAGE_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" );
drivers/gpu/drm/radeon/radeon_object.c +1 -1
@@ -120,7 +120,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) {
drivers/gpu/drm/radeon/radeon_ttm.c +1 -1
@@ -737,7 +737,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) {
drivers/gpu/drm/vmwgfx/vmwgfx_drv.c +1 -1
@@ -762,7 +762,7 @@ static int vmw_driver_open(struct drm_device *dev, struct drm_file *file_priv) goto out_no_tfile; 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 ;
include/drm/drmP.h +1 -1
@@ -1197,7 +1197,7 @@ struct drm_device {unsigned int num_crtcs;/**< Number of CRTCs on this device */ void *dev_private;/**< device private data */ void *mm_private; -struct address_space *dev_mapping; +struct inode *anon_inode;struct drm_sigdata sigdata;/**< For block_all_signals */ sigset_t sigmask;
Signature · 1 line
1.8.4
Al Viro
maintainerQuoted 12 lines · David Herrmann
DRM core shares a single
address_spaceacross all inodes that belong to the same DRM device. This allows efficient unmapping of user-space mappings during buffer eviction. However, there is no easy way to get a sharedaddress_spacefor DRM devices during initialization. Therefore, we currently delay this step until the first ->open() and save the given inode for future use.This causes ugly delayed initialization throughout the DRM core. TTM devices end up without a
dev_mappingpointer and we have to carefully respect any underlying filesystem implementation so we don't corrupt theinode->i_mappingandinode->i_datafields.
Quoted 8 lines
We can avoid this if we were allowed to allocate an anonymous inode for each DRM device. We only have to set
file->f_mappingduring ->open() and no longer need to adjust inode mappings. Asfs/anon_inodes.calready provides a minimal internal FS mount, we extend it to also provide anonymous inodes for use in drivers like DRM.Signed-off-by David Herrmann <[email protected]>Wanted-by Daniel Vetter <[email protected]>
Note that anon_inode_getfile_private() is not going to survive - for exact same reasons. It's already removed in vfs.git#experimental, shortly in #for-next as well. Do NOT extend fs/anon_inodes.c; if you need a minimal internal fs mount, just do what fs/aio.c does in the same branch.