Skip to content

Commit 0f899fd

Browse files
fxkamdalexdeucher
authored andcommitted
drm/amdkfd: Improve kfd_process lookup in kfd_ioctl
Use filep->private_data to store a pointer to the kfd_process data structure. Take an extra reference for that, which gets released in the kfd_release callback. Check that the process calling kfd_ioctl is the same that opened the file descriptor. Return -EBADF if it's not, so that this error can be distinguished in user mode. Signed-off-by: Felix Kuehling <[email protected]> Reviewed-by: Philip Yang <[email protected]> Signed-off-by: Alex Deucher <[email protected]>
1 parent ea11731 commit 0f899fd

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
static long kfd_ioctl(struct file *, unsigned int, unsigned long);
4444
static int kfd_open(struct inode *, struct file *);
45+
static int kfd_release(struct inode *, struct file *);
4546
static int kfd_mmap(struct file *, struct vm_area_struct *);
4647

4748
static const char kfd_dev_name[] = "kfd";
@@ -51,6 +52,7 @@ static const struct file_operations kfd_fops = {
5152
.unlocked_ioctl = kfd_ioctl,
5253
.compat_ioctl = compat_ptr_ioctl,
5354
.open = kfd_open,
55+
.release = kfd_release,
5456
.mmap = kfd_mmap,
5557
};
5658

@@ -124,15 +126,30 @@ static int kfd_open(struct inode *inode, struct file *filep)
124126
if (IS_ERR(process))
125127
return PTR_ERR(process);
126128

127-
if (kfd_is_locked())
129+
if (kfd_is_locked()) {
130+
kfd_unref_process(process);
128131
return -EAGAIN;
132+
}
133+
134+
/* filep now owns the reference returned by kfd_create_process */
135+
filep->private_data = process;
129136

130137
dev_dbg(kfd_device, "process %d opened, compat mode (32 bit) - %d\n",
131138
process->pasid, process->is_32bit_user_mode);
132139

133140
return 0;
134141
}
135142

143+
static int kfd_release(struct inode *inode, struct file *filep)
144+
{
145+
struct kfd_process *process = filep->private_data;
146+
147+
if (process)
148+
kfd_unref_process(process);
149+
150+
return 0;
151+
}
152+
136153
static int kfd_ioctl_get_version(struct file *filep, struct kfd_process *p,
137154
void *data)
138155
{
@@ -1801,9 +1818,14 @@ static long kfd_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
18011818

18021819
dev_dbg(kfd_device, "ioctl cmd 0x%x (#0x%x), arg 0x%lx\n", cmd, nr, arg);
18031820

1804-
process = kfd_get_process(current);
1805-
if (IS_ERR(process)) {
1806-
dev_dbg(kfd_device, "no process\n");
1821+
/* Get the process struct from the filep. Only the process
1822+
* that opened /dev/kfd can use the file descriptor. Child
1823+
* processes need to create their own KFD device context.
1824+
*/
1825+
process = filep->private_data;
1826+
if (process->lead_thread != current->group_leader) {
1827+
dev_dbg(kfd_device, "Using KFD FD in wrong process\n");
1828+
retcode = -EBADF;
18071829
goto err_i1;
18081830
}
18091831

drivers/gpu/drm/amd/amdkfd/kfd_process.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ struct kfd_process *kfd_create_process(struct file *filep)
324324
(int)process->lead_thread->pid);
325325
}
326326
out:
327+
if (!IS_ERR(process))
328+
kref_get(&process->ref);
327329
mutex_unlock(&kfd_processes_mutex);
328330

329331
return process;

0 commit comments

Comments
 (0)