Skip to contentSkip to conversation

drm/xe: Add bounds check for num_binds to prevent memory exhaustion

PATCH Superseded Ramesh Adhikari 4 participants 6 messages May 6 – Aug 4

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/xe: Add bounds check for num_binds to prevent memory exhaustion

The xe_vm_bind_ioctl function accepts user-controlled num_binds without

bounds checking, allowing arbitrarily large memory allocations. This

follows the same vulnerability pattern that was fixed for num_syncs in

commit 8e461304009d ("drm/xe: Limit num_syncs to prevent huge allocations").

Add DRM_XE_MAX_BINDS (1024) limit and validate num_binds before allocation,

matching the num_syncs fix pattern.

Similar unbounded allocations exist for num_mem_ranges and OA n_regs,

which should be addressed in follow-up patches.

---
 drivers/gpu/drm/xe/xe_vm.c | 5 +++++
 include/uapi/drm/xe_drm.h  | 1 +
 2 files changed, 6 insertions(+)
drivers/gpu/drm/xe/xe_vm.c +5 -0
index a717a2b8dea..1ff66874f43 100644
@@ -3841,6 +3841,11 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		return -EINVAL;
 
 	err = vm_bind_ioctl_check_args(xe, vm, args, &bind_ops);
+
+	if (XE_IOCTL_DBG(xe, args->num_binds > DRM_XE_MAX_BINDS)) {
+		err = -EINVAL;
+		goto put_vm;
+	}
 	if (err)
 		goto put_vm;
 
include/uapi/drm/xe_drm.h +1 -0
index ae2fda23ce7..804ccb23b11 100644
@@ -1606,6 +1606,7 @@ struct drm_xe_exec {
 	__u32 exec_queue_id;
 
 #define DRM_XE_MAX_SYNCS 1024
+#define DRM_XE_MAX_BINDS 1024
 	/** @num_syncs: Amount of struct drm_xe_sync in array. */
 	__u32 num_syncs;
 
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 8 lines · Ramesh Adhikari

The xe_vm_bind_ioctl function accepts user-controlled num_binds without

bounds checking, allowing arbitrarily large memory allocations. This

follows the same vulnerability pattern that was fixed for num_syncs in

commit 8e461304009d ("drm/xe: Limit num_syncs to prevent huge allocations").

The difference here is we issues kvmalloc (2G) vs kmalloc (4M) in the sync case. So still possible a user triggers kvmalloc over 2G...

Quoted 21 lines

Add DRM_XE_MAX_BINDS (1024) limit and validate num_binds before allocation,

matching the num_syncs fix pattern.

Similar unbounded allocations exist for num_mem_ranges and OA n_regs,

which should be addressed in follow-up patches.

---
 drivers/gpu/drm/xe/xe_vm.c | 5 +++++
 include/uapi/drm/xe_drm.h  | 1 +
 2 files changed, 6 insertions(+)
drivers/gpu/drm/xe/xe_vm.c +0 -0
index a717a2b8dea..1ff66874f43 100644

drivers/gpu/drm/xe/xe_vm.c int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file) · line 3848

Quoted 8 lines
drivers/gpu/drm/xe/xe_vm.c +5 -0
@@ -3841,6 +3841,11 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 		return -EINVAL;
 
 	err = vm_bind_ioctl_check_args(xe, vm, args, &bind_ops);
+
+	if (XE_IOCTL_DBG(xe, args->num_binds > DRM_XE_MAX_BINDS)) {
+		err = -EINVAL;kvmalloc
+		goto put_vm;
+	}

We had something like this early Xe, IIRC, the max was 512 but we found for Vk / Mesa they will a huge number in an array of binds. So 1k likely isn't enough and this patch would be considered uAPI regression, so this as is a no go. Maybe we can figure out some reasonable upper bound (64k, 128k), idk.

Matt

Quoted 18 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 	if (err)
 		goto put_vm;
 
include/uapi/drm/xe_drm.h +1 -0
index ae2fda23ce7..804ccb23b11 100644
@@ -1606,6 +1606,7 @@ struct drm_xe_exec {
 	__u32 exec_queue_id;
 
 #define DRM_XE_MAX_SYNCS 1024
+#define DRM_XE_MAX_BINDS 1024
 	/** @num_syncs: Amount of struct drm_xe_sync in array. */
 	__u32 num_syncs;
 
Signature · 1 line
2.43.0
#2 lore raw
Quoted 53 lines · Matthew Brost
Quoted 12 lines · Ramesh Adhikari

The xe_vm_bind_ioctl function accepts user-controlled num_binds without

bounds checking, allowing arbitrarily large memory allocations. This

follows the same vulnerability pattern that was fixed for num_syncs in

commit 8e461304009d ("drm/xe: Limit num_syncs to prevent huge allocations").

The difference here is we issues kvmalloc (2G) vs kmalloc (4M) in the sync case. So still possible a user triggers kvmalloc over 2G...

Quoted 31 lines

Add DRM_XE_MAX_BINDS (1024) limit and validate num_binds before allocation,

matching the num_syncs fix pattern.

Similar unbounded allocations exist for num_mem_ranges and OA n_regs,

which should be addressed in follow-up patches.

---
 drivers/gpu/drm/xe/xe_vm.c | 5 +++++
 include/uapi/drm/xe_drm.h  | 1 +

 2 files changed, 6 insertions(+)
drivers/gpu/drm/xe/xe_vm.c +0 -0
index a717a2b8dea..1ff66874f43 100644
@@ -3841,6 +3841,11 @@ int xe_vm_bind_ioctl(struct drm_device *dev,
 void *data, struct drm_file *file)
  		return -EINVAL;


  err = vm_bind_ioctl_check_args(xe, vm, args, &bind_ops);

drivers/gpu/drm/xe/xe_vm.c +2 -0
+
+	if (XE_IOCTL_DBG(xe, args->num_binds > DRM_XE_MAX_BINDS))

{

drivers/gpu/drm/xe/xe_vm.c int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file) · line 3848

Quoted 3 lines
drivers/gpu/drm/xe/xe_vm.c +3 -0
+		err = -EINVAL;kvmalloc
+		goto put_vm;
+	}

We had something like this early Xe, IIRC, the max was 512 but we

Quoted 8 lines

found
for Vk / Mesa they will a huge number in an array of binds. So 1k likely
isn't enough and this patch would be considered uAPI regression, so this
as is a no go. Maybe we can figure out some reasonable upper bound (64k,
128k), idk.

IIRC we debated this back and forth. The challenging argument was that if we consume all memory we'd get an error back, which is sort of true but then we should've really made sure that all memory allocated was also accounted against the cgroup, with __GFP_ACCOUNT. We only did that for one large allocation.

But I think we made sure to avoid future regressions (functional, not performance) by requiring UMD to handle -ENOBUFS, meaning "split the array bind and retry". So whatever limit we come up with we should not return -EINVAL but -ENOBUFS.

Thanks,
Thomas

Quoted 21 lines

Matt

Quoted 18 lines

  if (err)
  goto put_vm;

include/uapi/drm/xe_drm.h +0 -0
index ae2fda23ce7..804ccb23b11 100644
@@ -1606,6 +1606,7 @@ struct drm_xe_exec {
  	__u32 exec_queue_id;


 #define DRM_XE_MAX_SYNCS 1024
+#define DRM_XE_MAX_BINDS 1024
  /** @num_syncs: Amount of struct drm_xe_sync in array. */ __u32 num_syncs;

Signature · 1 line
2.43.0
maintainer
#3 lore raw
Quoted 69 lines · Thomas Hellström
Quoted 53 lines · Matthew Brost
Quoted 12 lines · Ramesh Adhikari

The xe_vm_bind_ioctl function accepts user-controlled num_binds without

bounds checking, allowing arbitrarily large memory allocations. This

follows the same vulnerability pattern that was fixed for num_syncs in

commit 8e461304009d ("drm/xe: Limit num_syncs to prevent huge allocations").

The difference here is we issues kvmalloc (2G) vs kmalloc (4M) in the sync case. So still possible a user triggers kvmalloc over 2G...

Quoted 31 lines

Add DRM_XE_MAX_BINDS (1024) limit and validate num_binds before allocation,

matching the num_syncs fix pattern.

Similar unbounded allocations exist for num_mem_ranges and OA n_regs,

which should be addressed in follow-up patches.

---
 drivers/gpu/drm/xe/xe_vm.c | 5 +++++
 include/uapi/drm/xe_drm.h  | 1 +

 2 files changed, 6 insertions(+)
drivers/gpu/drm/xe/xe_vm.c +0 -0
index a717a2b8dea..1ff66874f43 100644
@@ -3841,6 +3841,11 @@ int xe_vm_bind_ioctl(struct drm_device *dev,
 void *data, struct drm_file *file)
  		return -EINVAL;


  err = vm_bind_ioctl_check_args(xe, vm, args, &bind_ops);

drivers/gpu/drm/xe/xe_vm.c +2 -0
+
+	if (XE_IOCTL_DBG(xe, args->num_binds > DRM_XE_MAX_BINDS))

{

drivers/gpu/drm/xe/xe_vm.c int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file) · line 3848

Quoted 3 lines
drivers/gpu/drm/xe/xe_vm.c +3 -0
+		err = -EINVAL;kvmalloc
+		goto put_vm;
+	}

We had something like this early Xe, IIRC, the max was 512 but we

Quoted 8 lines

found
for Vk / Mesa they will a huge number in an array of binds. So 1k likely
isn't enough and this patch would be considered uAPI regression, so this
as is a no go. Maybe we can figure out some reasonable upper bound (64k,
128k), idk.

IIRC we debated this back and forth. The challenging argument was that if we consume all memory we'd get an error back, which is sort of true but then we should've really made sure that all memory allocated was also accounted against the cgroup, with __GFP_ACCOUNT. We only did that for one large allocation.

Quoted 4 lines

But I think we made sure to avoid future regressions (functional, not performance) by requiring UMD to handle -ENOBUFS, meaning "split the array bind and retry". So whatever limit we come up with we should not return -EINVAL but -ENOBUFS.

Yes, we can currently hit -ENOBUFS on large array of binds when we run out space for instructions in batch buffers programming the bind and Mesa gracefully handles this breaking down an array into individual binds.

Matt

Quoted 27 lines

Thanks,
Thomas

Quoted 21 lines

Matt

Quoted 18 lines

  if (err)
  goto put_vm;

include/uapi/drm/xe_drm.h +0 -0
index ae2fda23ce7..804ccb23b11 100644
@@ -1606,6 +1606,7 @@ struct drm_xe_exec {
  	__u32 exec_queue_id;


 #define DRM_XE_MAX_SYNCS 1024
+#define DRM_XE_MAX_BINDS 1024
  /** @num_syncs: Amount of struct drm_xe_sync in array. */ __u32 num_syncs;

Signature · 1 line
2.43.0
#4 lore raw

✗ LGCI.VerificationFailed: failure for drm/xe: Add bounds check for num_binds to prevent memory exhaustion (rev2)

In reply to Ramesh Adhikari
== Series Details ==

== Series Details ==

Series: drm/xe: Add bounds check for num_binds to prevent memory exhaustion (rev2)
URL : https://patchwork.freedesktop.org/series/166134/
State : failure

== Summary ==

Address '[email protected]' is not on the allowlist, which prevents CI from being triggered for this patch. If you want Intel GFX CI to accept this address, please contact the script maintainers at [email protected]. Exception occurred during validation, bailing out!

#5 lore raw

PATCH v5 1/1 drm/xe: Add bounds check for num_binds to prevent memory exhaustion

In reply to Ramesh Adhikari

The xe_vm_bind_ioctl function accepts user-controlled num_binds without any bounds checking. This follows the same pattern that was fixed for num_syncs in commit 8e461304009d ("drm/xe: Limit num_syncs to prevent huge allocations").

While the main allocations (bind_ops, bos, ops arrays) use __GFP_ACCOUNT, vm_bind_ioctl_ops_create makes additional allocations in a loop that don't:
- drm_gpuva_ops (16 bytes) at drm_gpuvm.c:2949
- xe_vma_op (144 bytes) at xe_vm.c:1318

Both use kzalloc_obj() which defaults to GFP_KERNEL without __GFP_ACCOUNT.

For 268M binds, the loop runs 268M times, allocating 160 bytes per iteration. That's about 43 GB allocated without cgroup accounting before the code even hits the main allocation (which will fail due to the 4MB kmalloc limit).

Add DRM_XE_MAX_BINDS (65536) limit, checked before any allocations happen. At 65536 binds, we're allocating ~10MB in the loop, which is reasonable and won't force unnecessary fallbacks in userspace.

Return -ENOBUFS instead of -EINVAL so Mesa can retry with smaller batches if needed.

Note on v3/v4: I sent v3 twice (2026-05-07, 2026-05-08) and v4 (2026-05-09) addressing the feedback below, but none of them appear to have reached the list or Patchwork - likely a subscription/moderation issue on my end that should now be resolved. Resending the same content as v5 in case v3/v4 never arrived:

v4: [email protected] (2026-05-09)
v3: [email protected] (2026-05-08)
v3: (resend, 2026-05-07, same content)

v4: Increased limit to 65536 (64k) per maintainer feedback, changed error
to -ENOBUFS for graceful retry
v3: Changed to -ENOBUFS, moved check earlier, added allocation analysis
v2: Increased limit from 1024 to 2048 after Mesa source analysis

---
 drivers/gpu/drm/xe/xe_vm.c | 7 +++++++
 include/uapi/drm/xe_drm.h  | 1 +
 2 files changed, 8 insertions(+)
drivers/gpu/drm/xe/xe_vm.c +7 -0
index a717a2b8dea..1ab020cbdc1 100644
@@ -3840,7 +3840,14 @@ int xe_vm_bind_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
     if (XE_IOCTL_DBG(xe, !vm))
         return -EINVAL;
 
+    /* Prevent unbounded allocations in vm_bind_ioctl_ops_create loop */
+    if (XE_IOCTL_DBG(xe, args->num_binds > DRM_XE_MAX_BINDS)) {
+        err = -ENOBUFS;
+        goto put_vm;
+    }
+
     err = vm_bind_ioctl_check_args(xe, vm, args, &bind_ops);
+
     if (err)
         goto put_vm;
 
include/uapi/drm/xe_drm.h +1 -0
index ae2fda23ce7..33f23f6638e 100644
@@ -1606,6 +1606,7 @@ struct drm_xe_exec {
     __u32 exec_queue_id;
 
 #define DRM_XE_MAX_SYNCS 1024
+#define DRM_XE_MAX_BINDS 65536
     /** @num_syncs: Amount of struct drm_xe_sync in array. */
     __u32 num_syncs;
 

--
2.43.0