drm/amdgpu: validate the size in GEM_USERPTR ioctl
Subsystems (2)
PATCH 1/1 drm/amdgpu: validate the size in GEM_USERPTR ioctl
Nothing limits args->size in amdgpu_gem_userptr_ioctl(), only the alignment and access_ok() are checked. Acquiring the DMA addresses for a huge range and cleaning everything up again whenever it changes is expensive enough that a sufficiently large userptr can keep the machine busy more or less indefinitely.
On top of that the interface is increasingly used as a replacement for real driver or udmabuf backed allocations, which it was never meant for.
Reject a zero size and anything above 1GiB. That is far more than a temporary upload buffer needs and stops the ioctl from being abused as a general purpose memory allocator.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4 -0
@@ -566,6 +566,10 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,if (!access_ok ((void __user *)(uintptr_t )args->addr, args->size))return -EFAULT ; +/* Userptrs are for temporary uploads, not a general memory allocator */ +if (args->size ==0 || args->size >SZ_1G ) +return -EINVAL ; +/* reject unknown flag values */ if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY | AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
Signature · 1 line
2.34.1
Hi,
Quoted 6 lines · Perry Yuan
Nothing limits
args->sizeinamdgpu_gem_userptr_ioctl(), only the alignment andaccess_ok()are checked. Acquiring the DMA addresses for a huge range and cleaning everything up again whenever it changes is expensive enough that a sufficiently large userptr can keep the machine busy more or less indefinitely.
Quoted 6 lines
On top of that the interface is increasingly used as a replacement for real driver or udmabuf backed allocations, which it was never meant for.
Reject a zero size and anything above 1GiB. That is far more than a temporary upload buffer needs and stops the ioctl from being abused as a general purpose memory allocator.
FWIW, Proton games may attempt to use userptr backing for some game allocations, and I don't think we can assume that games won't try allocating userptr ranges >1GiB, nor that games will gracefully handle userptr allocation failing, so hard-capping the size might break some userspace programs.
Not sure if bad ioctl usage should really be worked around by outright rejecting certain inputs.
Best,
Natalie
Quoted 22 lines
Suggested-by Christian König <[email protected]>Signed-off-by Perry Yuan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4
-0@@ -566,6 +566,10 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,if (!access_ok ((void __user *)(uintptr_t )args->addr, args->size))return -EFAULT ; +/* Userptrs are for temporary uploads, not a general memory allocator */ +if (args->size ==0 || args->size >SZ_1G ) +return -EINVAL ; +/* reject unknown flag values */ if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY | AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
Christian König
maintainerHi,
Quoted 12 lines · Natalie Vock
Hi,
Quoted 9 lines · Perry Yuan
Nothing limits
args->sizeinamdgpu_gem_userptr_ioctl(), only the alignment andaccess_ok()are checked. Acquiring the DMA addresses for a huge range and cleaning everything up again whenever it changes is expensive enough that a sufficiently large userptr can keep the machine busy more or less indefinitely.On top of that the interface is increasingly used as a replacement for real driver or udmabuf backed allocations, which it was never meant for.
Quoted 8 lines
Quoted 3 lines
Reject a zero size and anything above 1GiB. That is far more than a temporary upload buffer needs and stops the ioctl from being abused as a general purpose memory allocator.
FWIW, Proton games may attempt to use userptr backing for some game allocations, and I don't think we can assume that games won't try allocating userptr ranges >1GiB, nor that games will gracefully handle userptr allocation failing, so hard-capping the size might break some userspace programs.
Oh that's an interesting information. I wasn't expecting games to do something like that.
Quoted 2 lines
Not sure if bad ioctl usage should really be worked around by outright rejecting certain inputs.
The problem is that we can work around the limitations we have but userptrs will always be slower and more fragile than native allocations or proper DMA-buf imports (udmabuf).
Having no limitation on the usage created the impression on the customer side that they should be equal and that is clearly not the case.
I mean we can release pages as batch (which BTW is only used on really old kernels in the DKMS module), but that really doesn't solve the underlying issue.
Regards,
Christian.
Quoted 27 lines
Best,
NatalieQuoted 22 lines
Suggested-by Christian König <[email protected]>Signed-off-by Perry Yuan <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4
-0@@ -566,6 +566,10 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,if (!access_ok ((void __user *)(uintptr_t )args->addr, args->size))return -EFAULT ; +/* Userptrs are for temporary uploads, not a general memory allocator */ +if (args->size ==0 || args->size >SZ_1G ) +return -EINVAL ; +/* reject unknown flag values */ if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY | AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
Quoted 9 lines · Perry Yuan
Nothing limits
args->sizeinamdgpu_gem_userptr_ioctl(), only the alignment andaccess_ok()are checked. Acquiring the DMA addresses for a huge range and cleaning everything up again whenever it changes is expensive enough that a sufficiently large userptr can keep the machine busy more or less indefinitely.On top of that the interface is increasingly used as a replacement for real driver or udmabuf backed allocations, which it was never meant for.
Quoted 6 lines
Reject a zero size and anything above 1GiB. That is far more than a temporary upload buffer needs and stops the ioctl from being abused as a general purpose memory allocator.
Suggested-by Christian König <[email protected]>Signed-off-by Perry Yuan <[email protected]>
We might need to adjust the limit based on customer feedback and/or disable it with some debug/workaround flag.
Regards,
Christian.
Quoted 19 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4
-0@@ -566,6 +566,10 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,if (!access_ok ((void __user *)(uintptr_t )args->addr, args->size))return -EFAULT ; +/* Userptrs are for temporary uploads, not a general memory allocator */ +if (args->size ==0 || args->size >SZ_1G ) +return -EINVAL ; +/* reject unknown flag values */ if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY | AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
AMD General
Hi Christian
Quoted 26 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, November 2, 2026 9:40 PM
To: Yuan, Perry <[email protected]>; Deucher, Alexander <[email protected]>; [email protected]; Natalie Vock <[email protected]>
Cc: Zhang, Yifan <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: validate the size inGEM_USERPTRioctlQuoted 9 lines · Perry Yuan
Nothing limits
args->sizeinamdgpu_gem_userptr_ioctl(), only the alignment andaccess_ok()are checked. Acquiring the DMA addresses for a huge range and cleaning everything up again whenever it changes is expensive enough that a sufficiently large userptr can keep the machine busy more or less indefinitely.On top of that the interface is increasingly used as a replacement for real driver or udmabuf backed allocations, which it was never meant for.
Quoted 6 lines
Reject a zero size and anything above 1GiB. That is far more than a temporary upload buffer needs and stops the ioctl from being abused as a general purpose memory allocator.
Suggested-by Christian König <[email protected]>Signed-off-by Perry Yuan <[email protected]>
Quoted 7 lines
Reviewed-by Christian König <[email protected]> for now.We might need to adjust the limit based on customer feedback and/or disable it with some debug/workaround flag.
Regards,
Christian.
Sure, Will make a flag to allow users to disable the limit.
Thanks for the review & feedback.
Best Regards.
Perry.
Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_userptr_ioctl(struct drm_device *dev,
Quoted 11 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0
-0@@ -566,6 +566,10 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev,void *data,
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +4
-0if (!access_ok ((void __user *)(uintptr_t )args->addr, args->size))return -EFAULT ; +/* Userptrs are for temporary uploads, not a general memory allocator */ +if (args->size ==0 || args->size >SZ_1G ) +return -EINVAL ; +/* reject unknown flag values */ if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY | AMDGPU_GEM_USERPTR_ANONONLY |
AMDGPU_GEM_USERPTR_VALIDATE|