Skip to content

Commit be7c5dc

Browse files
Tvrtko Ursulingregkh
authored andcommitted
drm/amdgpu: Use vmemdup_array_user in amdgpu_bo_create_list_entry_array
[ Upstream commit c4ac100 ] Replace kvmalloc_array() + copy_from_user() with vmemdup_array_user() on the fast path. This shrinks the source code and improves separation between the kernel and userspace slabs. Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@igalia.com> Signed-off-by: Alex Deucher <alexander.deucher@amd.com> Signed-off-by: Fang Wang <32840572@qq.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent c7f4dad commit be7c5dc

1 file changed

Lines changed: 17 additions & 24 deletions

File tree

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -195,43 +195,36 @@ void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
195195
int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
196196
struct drm_amdgpu_bo_list_entry **info_param)
197197
{
198-
const void __user *uptr = u64_to_user_ptr(in->bo_info_ptr);
199198
const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
199+
const void __user *uptr = u64_to_user_ptr(in->bo_info_ptr);
200+
const uint32_t bo_info_size = in->bo_info_size;
201+
const uint32_t bo_number = in->bo_number;
200202
struct drm_amdgpu_bo_list_entry *info;
201-
int r;
202-
203-
info = kvmalloc_array(in->bo_number, info_size, GFP_KERNEL);
204-
if (!info)
205-
return -ENOMEM;
206203

207204
/* copy the handle array from userspace to a kernel buffer */
208-
r = -EFAULT;
209-
if (likely(info_size == in->bo_info_size)) {
210-
unsigned long bytes = in->bo_number *
211-
in->bo_info_size;
212-
213-
if (copy_from_user(info, uptr, bytes))
214-
goto error_free;
215-
205+
if (likely(info_size == bo_info_size)) {
206+
info = vmemdup_array_user(uptr, bo_number, info_size);
207+
if (IS_ERR(info))
208+
return PTR_ERR(info);
216209
} else {
217-
unsigned long bytes = min(in->bo_info_size, info_size);
210+
const uint32_t bytes = min(bo_info_size, info_size);
218211
unsigned i;
219212

220-
memset(info, 0, in->bo_number * info_size);
221-
for (i = 0; i < in->bo_number; ++i) {
222-
if (copy_from_user(&info[i], uptr, bytes))
223-
goto error_free;
213+
info = kvmalloc_array(bo_number, info_size, GFP_KERNEL);
214+
if (!info)
215+
return -ENOMEM;
224216

225-
uptr += in->bo_info_size;
217+
memset(info, 0, bo_number * info_size);
218+
for (i = 0; i < bo_number; ++i, uptr += bo_info_size) {
219+
if (copy_from_user(&info[i], uptr, bytes)) {
220+
kvfree(info);
221+
return -EFAULT;
222+
}
226223
}
227224
}
228225

229226
*info_param = info;
230227
return 0;
231-
232-
error_free:
233-
kvfree(info);
234-
return r;
235228
}
236229

237230
int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,

0 commit comments

Comments
 (0)