Skip to content

Commit 43add1c

Browse files
pvgtejas-quicmikeNG
authored andcommitted
msm: kgsl: Use dma_buf_get() to get dma_buf structure
Currently we don't ensure if vma->vm_file is associated with dma_buf. This can cause issues later when private_data from a non dma_buf file is used as dma_buf structure. Hence get the fd that is associated with vma->vm_file and use dma_buf_get() to get pointer to dma_buf structure. dma_buf_get() ensures that the file from the input fd is associated with dma_buf. Change-Id: Ib78aef8b16bedca5ca86d3a132278ff9f07dce73 Signed-off-by: Puranam V G Tejaswi <quic_pvgtejas@quicinc.com>
1 parent c9e448a commit 43add1c

1 file changed

Lines changed: 33 additions & 7 deletions

File tree

drivers/gpu/msm/kgsl.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* Copyright (c) 2008-2021, The Linux Foundation. All rights reserved.
2+
* Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
23
*
34
* This program is free software; you can redistribute it and/or modify
45
* it under the terms of the GNU General Public License version 2 and
@@ -2204,6 +2205,15 @@ static int kgsl_setup_anon_useraddr(struct kgsl_pagetable *pagetable,
22042205
}
22052206

22062207
#ifdef CONFIG_DMA_SHARED_BUFFER
2208+
static int match_file(const void *p, struct file *file, unsigned int fd)
2209+
{
2210+
/*
2211+
* We must return fd + 1 because iterate_fd stops searching on
2212+
* non-zero return, but 0 is a valid fd.
2213+
*/
2214+
return (p == file) ? (fd + 1) : 0;
2215+
}
2216+
22072217
static void _setup_cache_mode(struct kgsl_mem_entry *entry,
22082218
struct vm_area_struct *vma)
22092219
{
@@ -2241,6 +2251,8 @@ static int kgsl_setup_dmabuf_useraddr(struct kgsl_device *device,
22412251
vma = find_vma(current->mm, hostptr);
22422252

22432253
if (vma && vma->vm_file) {
2254+
int fd;
2255+
22442256
ret = check_vma_flags(vma, entry->memdesc.flags);
22452257
if (ret) {
22462258
up_read(&current->mm->mmap_sem);
@@ -2256,13 +2268,27 @@ static int kgsl_setup_dmabuf_useraddr(struct kgsl_device *device,
22562268
return -EFAULT;
22572269
}
22582270

2259-
/*
2260-
* Take a refcount because dma_buf_put() decrements the
2261-
* refcount
2262-
*/
2263-
get_file(vma->vm_file);
2264-
2265-
dmabuf = vma->vm_file->private_data;
2271+
/* Look for the fd that matches this vma file */
2272+
fd = iterate_fd(current->files, 0, match_file, vma->vm_file);
2273+
if (fd) {
2274+
dmabuf = dma_buf_get(fd - 1);
2275+
if (IS_ERR(dmabuf)) {
2276+
up_read(&current->mm->mmap_sem);
2277+
return PTR_ERR(dmabuf);
2278+
}
2279+
/*
2280+
* It is possible that the fd obtained from iterate_fd
2281+
* was closed before passing the fd to dma_buf_get().
2282+
* Hence dmabuf returned by dma_buf_get() could be
2283+
* different from vma->vm_file->private_data. Return
2284+
* failure if this happens.
2285+
*/
2286+
if (dmabuf != vma->vm_file->private_data) {
2287+
dma_buf_put(dmabuf);
2288+
up_read(&current->mm->mmap_sem);
2289+
return -EBADF;
2290+
}
2291+
}
22662292
}
22672293

22682294
if (IS_ERR_OR_NULL(dmabuf)) {

0 commit comments

Comments
 (0)