Commit 295992fb authored by Christian König's avatar Christian König

mm: introduce vma_set_file function v5

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: default avatarChristian König <christian.koenig@amd.com>
Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch> (v2)
Reviewed-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Acked-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Link: https://patchwork.freedesktop.org/patch/399360/
parent 1527f926
...@@ -1183,8 +1183,7 @@ int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma, ...@@ -1183,8 +1183,7 @@ int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
return -EINVAL; return -EINVAL;
/* readjust the vma */ /* readjust the vma */
fput(vma->vm_file); vma_set_file(vma, dmabuf->file);
vma->vm_file = get_file(dmabuf->file);
vma->vm_pgoff = pgoff; vma->vm_pgoff = pgoff;
return dmabuf->ops->mmap(dmabuf, vma); return dmabuf->ops->mmap(dmabuf, vma);
......
...@@ -145,10 +145,8 @@ static int etnaviv_gem_mmap_obj(struct etnaviv_gem_object *etnaviv_obj, ...@@ -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, * address_space (so unmap_mapping_range does what we want,
* in particular in the case of mmap'd dmabufs) * 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_pgoff = 0;
vma->vm_file = etnaviv_obj->base.filp; vma_set_file(vma, etnaviv_obj->base.filp);
vma->vm_page_prot = vm_page_prot; vma->vm_page_prot = vm_page_prot;
} }
......
...@@ -114,8 +114,7 @@ static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct * ...@@ -114,8 +114,7 @@ static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *
if (ret) if (ret)
return ret; return ret;
fput(vma->vm_file); vma_set_file(vma, obj->base.filp);
vma->vm_file = get_file(obj->base.filp);
return 0; return 0;
} }
......
...@@ -893,8 +893,9 @@ int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma) ...@@ -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 * requires avoiding extraneous references to their filp, hence why
* we prefer to use an anonymous file for their mmaps. * we prefer to use an anonymous file for their mmaps.
*/ */
fput(vma->vm_file); vma_set_file(vma, anon);
vma->vm_file = anon; /* Drop the initial creation reference, the vma is now holding one. */
fput(anon);
switch (mmo->mmap_type) { switch (mmo->mmap_type) {
case I915_MMAP_TYPE_WC: case I915_MMAP_TYPE_WC:
......
...@@ -212,10 +212,8 @@ int msm_gem_mmap_obj(struct drm_gem_object *obj, ...@@ -212,10 +212,8 @@ int msm_gem_mmap_obj(struct drm_gem_object *obj,
* address_space (so unmap_mapping_range does what we want, * address_space (so unmap_mapping_range does what we want,
* in particular in the case of mmap'd dmabufs) * in particular in the case of mmap'd dmabufs)
*/ */
fput(vma->vm_file);
get_file(obj->filp);
vma->vm_pgoff = 0; 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); vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
} }
......
...@@ -564,9 +564,8 @@ int omap_gem_mmap_obj(struct drm_gem_object *obj, ...@@ -564,9 +564,8 @@ int omap_gem_mmap_obj(struct drm_gem_object *obj,
* address_space (so unmap_mapping_range does what we want, * address_space (so unmap_mapping_range does what we want,
* in particular in the case of mmap'd dmabufs) * in particular in the case of mmap'd dmabufs)
*/ */
fput(vma->vm_file);
vma->vm_pgoff = 0; 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); vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
} }
......
...@@ -403,8 +403,7 @@ static int vgem_prime_mmap(struct drm_gem_object *obj, ...@@ -403,8 +403,7 @@ static int vgem_prime_mmap(struct drm_gem_object *obj,
if (ret) if (ret)
return ret; return ret;
fput(vma->vm_file); vma_set_file(vma, obj->filp);
vma->vm_file = get_file(obj->filp);
vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
......
...@@ -450,9 +450,9 @@ static int ashmem_mmap(struct file *file, struct vm_area_struct *vma) ...@@ -450,9 +450,9 @@ static int ashmem_mmap(struct file *file, struct vm_area_struct *vma)
vma_set_anonymous(vma); vma_set_anonymous(vma);
} }
if (vma->vm_file) vma_set_file(vma, asma->file);
fput(vma->vm_file); /* XXX: merge this with the get_file() above if possible */
vma->vm_file = asma->file; fput(asma->file);
out: out:
mutex_unlock(&ashmem_mutex); mutex_unlock(&ashmem_mutex);
......
...@@ -2719,6 +2719,8 @@ static inline void vma_set_page_prot(struct vm_area_struct *vma) ...@@ -2719,6 +2719,8 @@ static inline void vma_set_page_prot(struct vm_area_struct *vma)
} }
#endif #endif
void vma_set_file(struct vm_area_struct *vma, struct file *file);
#ifdef CONFIG_NUMA_BALANCING #ifdef CONFIG_NUMA_BALANCING
unsigned long change_prot_numa(struct vm_area_struct *vma, unsigned long change_prot_numa(struct vm_area_struct *vma,
unsigned long start, unsigned long end); unsigned long start, unsigned long end);
......
...@@ -311,6 +311,18 @@ int vma_is_stack_for_current(struct vm_area_struct *vma) ...@@ -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)); 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 #ifndef STACK_RND_MASK
#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */ #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment