]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/commitdiff
mm: introduce vma_set_file function v5
authorChristian König <christian.koenig@amd.com>
Mon, 14 Sep 2020 13:09:33 +0000 (15:09 +0200)
committerChristian König <christian.koenig@amd.com>
Thu, 19 Nov 2020 09:36:36 +0000 (10:36 +0100)
Add the new vma_set_file() function to allow changing
vma->vm_file with the necessary refcount dance.

v2: add more users of this.
v3: add missing EXPORT_SYMBOL, rebase on mmap cleanup,
    add comments why we drop the reference on two occasions.
v4: make it clear that changing an anonymous vma is illegal.
v5: move vma_set_file to mm/util.c

Signed-off-by: Christian König <christian.koenig@amd.com>
Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch> (v2)
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Acked-by: Andrew Morton <akpm@linux-foundation.org>
Link: https://patchwork.freedesktop.org/patch/399360/
drivers/dma-buf/dma-buf.c
drivers/gpu/drm/etnaviv/etnaviv_gem.c
drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c
drivers/gpu/drm/i915/gem/i915_gem_mman.c
drivers/gpu/drm/msm/msm_gem.c
drivers/gpu/drm/omapdrm/omap_gem.c
drivers/gpu/drm/vgem/vgem_drv.c
drivers/staging/android/ashmem.c
include/linux/mm.h
mm/util.c

index 282bd8b8417088c850d18a4f942181945f6157ef..e63684d4cd904149db83cff229226f4b97333fe1 100644 (file)
@@ -1183,8 +1183,7 @@ int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
                return -EINVAL;
 
        /* readjust the vma */
-       fput(vma->vm_file);
-       vma->vm_file = get_file(dmabuf->file);
+       vma_set_file(vma, dmabuf->file);
        vma->vm_pgoff = pgoff;
 
        return dmabuf->ops->mmap(dmabuf, vma);
index bbd235473645650cdc21fe6da194a3fe20dc9d39..6d38c5c17f23eb8522e4d8558af56b19be66da80 100644 (file)
@@ -145,10 +145,8 @@ static int etnaviv_gem_mmap_obj(struct etnaviv_gem_object *etnaviv_obj,
                 * address_space (so unmap_mapping_range does what we want,
                 * in particular in the case of mmap'd dmabufs)
                 */
-               fput(vma->vm_file);
-               get_file(etnaviv_obj->base.filp);
                vma->vm_pgoff = 0;
-               vma->vm_file  = etnaviv_obj->base.filp;
+               vma_set_file(vma, etnaviv_obj->base.filp);
 
                vma->vm_page_prot = vm_page_prot;
        }
index 0dd477e56573629801ffdc50be2c710b6c3e08dd..04e9c04545ad4db5aaf00ae956be6da2cb10831c 100644 (file)
@@ -114,8 +114,7 @@ static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *
        if (ret)
                return ret;
 
-       fput(vma->vm_file);
-       vma->vm_file = get_file(obj->base.filp);
+       vma_set_file(vma, obj->base.filp);
 
        return 0;
 }
index 3d69e51f3e4df0a3002a14d5ebf1995b35fed9a0..ec28a6cde49bddf206ade1c71e20249257f729cd 100644 (file)
@@ -893,8 +893,9 @@ int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma)
         * requires avoiding extraneous references to their filp, hence why
         * we prefer to use an anonymous file for their mmaps.
         */
-       fput(vma->vm_file);
-       vma->vm_file = anon;
+       vma_set_file(vma, anon);
+       /* Drop the initial creation reference, the vma is now holding one. */
+       fput(anon);
 
        switch (mmo->mmap_type) {
        case I915_MMAP_TYPE_WC:
index 2e1bce7c0b1975718d5e325038850f2f325d7cdd..311721ceee5021d3e32047f442a597edb60347ea 100644 (file)
@@ -212,10 +212,8 @@ int msm_gem_mmap_obj(struct drm_gem_object *obj,
                 * address_space (so unmap_mapping_range does what we want,
                 * in particular in the case of mmap'd dmabufs)
                 */
-               fput(vma->vm_file);
-               get_file(obj->filp);
                vma->vm_pgoff = 0;
-               vma->vm_file  = obj->filp;
+               vma_set_file(vma, obj->filp);
 
                vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
        }
index 68c271f4250becf488e9e1af51a684a9e74c3632..30d299ca8795d6a945287b2c335d88b77f1e671a 100644 (file)
@@ -564,9 +564,8 @@ int omap_gem_mmap_obj(struct drm_gem_object *obj,
                 * address_space (so unmap_mapping_range does what we want,
                 * in particular in the case of mmap'd dmabufs)
                 */
-               fput(vma->vm_file);
                vma->vm_pgoff = 0;
-               vma->vm_file  = get_file(obj->filp);
+               vma_set_file(vma, obj->filp);
 
                vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
        }
index 9a413091abb6cb7c47e46ff2fb1f6b7c7500c65b..f8635ccaf9a1703338aa633ff1507b5611e0d473 100644 (file)
@@ -403,8 +403,7 @@ static int vgem_prime_mmap(struct drm_gem_object *obj,
        if (ret)
                return ret;
 
-       fput(vma->vm_file);
-       vma->vm_file = get_file(obj->filp);
+       vma_set_file(vma, obj->filp);
        vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
        vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
 
index 10b4be1f3e78e47e8809c4504fd51d8776700364..4789d36ddfd3078c52a5e23b7825e9db4a5b0859 100644 (file)
@@ -450,9 +450,9 @@ static int ashmem_mmap(struct file *file, struct vm_area_struct *vma)
                vma_set_anonymous(vma);
        }
 
-       if (vma->vm_file)
-               fput(vma->vm_file);
-       vma->vm_file = asma->file;
+       vma_set_file(vma, asma->file);
+       /* XXX: merge this with the get_file() above if possible */
+       fput(asma->file);
 
 out:
        mutex_unlock(&ashmem_mutex);
index db6ae4d3fb4edc82ad0c7ccf80eac47726862b20..47bff16c182d9bcb14cd04075205d7a3e94c616e 100644 (file)
@@ -2719,6 +2719,8 @@ static inline void vma_set_page_prot(struct vm_area_struct *vma)
 }
 #endif
 
+void vma_set_file(struct vm_area_struct *vma, struct file *file);
+
 #ifdef CONFIG_NUMA_BALANCING
 unsigned long change_prot_numa(struct vm_area_struct *vma,
                        unsigned long start, unsigned long end);
index 4ddb6e186dd5cff8768ccbc186c85b31f05fe2c0..8c9b7d1e7c499828c429b93853af28e7299edcc1 100644 (file)
--- a/mm/util.c
+++ b/mm/util.c
@@ -311,6 +311,18 @@ int vma_is_stack_for_current(struct vm_area_struct *vma)
        return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t));
 }
 
+/*
+ * Change backing file, only valid to use during initial VMA setup.
+ */
+void vma_set_file(struct vm_area_struct *vma, struct file *file)
+{
+       /* Changing an anonymous vma with this is illegal */
+       get_file(file);
+       swap(vma->vm_file, file);
+       fput(file);
+}
+EXPORT_SYMBOL(vma_set_file);
+
 #ifndef STACK_RND_MASK
 #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12))     /* 8MB of VA */
 #endif