From: Sasha Levin <sashal@kernel.org>
To: stable@vger.kernel.org
Cc: "Fei Yu" <fei.yu@intel.com>,
syzbot+95416f957d84e858b377@syzkaller.appspotmail.com,
"Christian König" <christian.koenig@amd.com>,
"Sasha Levin" <sashal@kernel.org>
Subject: [PATCH 6.18.y] drm/syncobj: Limit the number of handles and use kvmalloc for user-sized arrays
Date: Thu, 3 Dec 2026 19:35:33 -0500 [thread overview]
Message-ID: <20261204003458.1490232-1-sashal@kernel.org> (raw)
In-Reply-To: <2026120228-hypocrite-unpaved-d907@gregkh>
From: Fei Yu <fei.yu@intel.com>
[ Upstream commit 6ca87397aeebcfae3057653c3e7c008ccb0bb926 ]
The syncobj ioctls allocate several temporary arrays whose size is taken
directly from the u32 count_handles provided by userspace:
- handles and syncobjs in drm_syncobj_array_find()
- points and entries in drm_syncobj_array_wait_timeout()
- points and chains in drm_syncobj_timeline_signal_ioctl()
These are kmalloc() allocations, so a count above KMALLOC_MAX_SIZE /
element size trips the WARN_ON_ONCE_GFP() in ___kmalloc_large_node():
WARNING: mm/slub.c:5459 at ___kmalloc_large_node+0x10c/0x170 mm/slub.c:5459
Call Trace:
__kmalloc_large_node_noprof+0x18/0x90 mm/slub.c:5502
__do_kmalloc_node mm/slub.c:5519 [inline]
__kmalloc_noprof+0x3f6/0x710 mm/slub.c:5556
_kmalloc_noprof include/linux/slab.h:995 [inline]
_kmalloc_array_noprof include/linux/slab.h:1211 [inline]
drm_syncobj_array_find+0x3a/0x440 drivers/gpu/drm/drm_syncobj.c:1241
drm_syncobj_timeline_wait_ioctl+0x18a/0x640 drivers/gpu/drm/drm_syncobj.c:1356
drm_ioctl_kernel+0x2df/0x3b0 drivers/gpu/drm/drm_ioctl.c:817
drm_ioctl+0x70e/0xba0 drivers/gpu/drm/drm_ioctl.c:914
The wait entries are 64 bytes each on 64-bit, so with a 4 MiB
KMALLOC_MAX_SIZE a wait on more than about 64K handles already fails
today, with a warning.
Fix this in two parts:
- Reject more than DRM_SYNCOBJ_MAX_HANDLES (1M) handles with -EINVAL in
drm_syncobj_array_find(), which every ioctl taking a handle array goes
through. The limit is a fixed number, so it is the same on all
architectures. It is above what these ioctls can handle today: with a
4 MiB KMALLOC_MAX_SIZE the 8-byte arrays already fail above 512K
handles on 64-bit, so no request that succeeds now is rejected. A
static_assert() makes sure that the largest per-handle allocation, the
wait entries, stays below the INT_MAX size limit of kvmalloc().
- Switch the six allocations to kvmalloc(), so that handle counts up to
the limit (64 MiB of wait entries for 1M handles) no longer depend on
physically contiguous memory.
Using kvmalloc() alone is not enough: the reproducer asks for more than
INT_MAX bytes, which hits the WARN_ON_ONCE() in __kvmalloc_node_noprof()
instead.
Fixes: 3e6fb72d6cef ("drm/syncobj: Add a syncobj_array_find helper")
Reported-by: syzbot+95416f957d84e858b377@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=95416f957d84e858b377
Suggested-by: Christian König <christian.koenig@amd.com>
Cc: stable@vger.kernel.org
Signed-off-by: Fei Yu <fei.yu@intel.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Christian König <christian.koenig@amd.com>
Link: https://lore.kernel.org/r/20261109014852.2127723-1-fei.yu@intel.com
[ sashal: Replaced the kzalloc_objs()/kmalloc_objs() helpers, which this tree lacks, with kvcalloc() and kvmalloc_array() in three allocation calls. ]
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
drivers/gpu/drm/drm_syncobj.c | 45 +++++++++++++++++++++++------------
1 file changed, 30 insertions(+), 15 deletions(-)
diff --git a/drivers/gpu/drm/drm_syncobj.c b/drivers/gpu/drm/drm_syncobj.c
index c0cb7d79672d8..17b438816c349 100644
--- a/drivers/gpu/drm/drm_syncobj.c
+++ b/drivers/gpu/drm/drm_syncobj.c
@@ -199,6 +199,7 @@
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/sched/signal.h>
+#include <linux/sizes.h>
#include <linux/sync_file.h>
#include <linux/uaccess.h>
@@ -220,6 +221,16 @@ struct syncobj_wait_entry {
u64 point;
};
+/*
+ * Upper limit for the number of handles passed to a single ioctl. This is a
+ * fixed number so that the limit is the same on all architectures. The
+ * largest per-handle allocation is the wait entry array, which must stay
+ * below the INT_MAX size limit of kvmalloc().
+ */
+#define DRM_SYNCOBJ_MAX_HANDLES SZ_1M
+static_assert(DRM_SYNCOBJ_MAX_HANDLES <=
+ INT_MAX / sizeof(struct syncobj_wait_entry));
+
static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
struct syncobj_wait_entry *wait);
@@ -1076,7 +1087,7 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
lockdep_assert_none_held_once();
}
- points = kmalloc_array(count, sizeof(*points), GFP_KERNEL);
+ points = kvmalloc_array(count, sizeof(*points), GFP_KERNEL);
if (points == NULL)
return -ENOMEM;
@@ -1089,7 +1100,7 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
goto err_free_points;
}
- entries = kcalloc(count, sizeof(*entries), GFP_KERNEL);
+ entries = kvcalloc(count, sizeof(*entries), GFP_KERNEL);
if (!entries) {
timeout = -ENOMEM;
goto err_free_points;
@@ -1210,10 +1221,10 @@ static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
&entries[i].fence_cb);
dma_fence_put(entries[i].fence);
}
- kfree(entries);
+ kvfree(entries);
err_free_points:
- kfree(points);
+ kvfree(points);
return timeout;
}
@@ -1296,7 +1307,10 @@ static int drm_syncobj_array_find(struct drm_file *file_private,
struct drm_syncobj **syncobjs;
int ret;
- handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL);
+ if (count_handles > DRM_SYNCOBJ_MAX_HANDLES)
+ return -EINVAL;
+
+ handles = kvmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL);
if (handles == NULL)
return -ENOMEM;
@@ -1306,7 +1320,7 @@ static int drm_syncobj_array_find(struct drm_file *file_private,
goto err_free_handles;
}
- syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL);
+ syncobjs = kvmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL);
if (syncobjs == NULL) {
ret = -ENOMEM;
goto err_free_handles;
@@ -1320,16 +1334,16 @@ static int drm_syncobj_array_find(struct drm_file *file_private,
}
}
- kfree(handles);
+ kvfree(handles);
*syncobjs_out = syncobjs;
return 0;
err_put_syncobjs:
while (i-- > 0)
drm_syncobj_put(syncobjs[i]);
- kfree(syncobjs);
+ kvfree(syncobjs);
err_free_handles:
- kfree(handles);
+ kvfree(handles);
return ret;
}
@@ -1341,7 +1355,7 @@ static void drm_syncobj_array_free(struct drm_syncobj **syncobjs,
for (i = 0; i < count; i++)
drm_syncobj_put(syncobjs[i]);
- kfree(syncobjs);
+ kvfree(syncobjs);
}
int
@@ -1630,8 +1644,8 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,
if (ret < 0)
return ret;
- points = kmalloc_array(args->count_handles, sizeof(*points),
- GFP_KERNEL);
+ points = kvmalloc_array(args->count_handles, sizeof(*points),
+ GFP_KERNEL);
if (!points) {
ret = -ENOMEM;
goto out;
@@ -1644,7 +1658,8 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,
goto err_points;
}
- chains = kmalloc_array(args->count_handles, sizeof(void *), GFP_KERNEL);
+ chains = kvmalloc_array(args->count_handles, sizeof(void *),
+ GFP_KERNEL);
if (!chains) {
ret = -ENOMEM;
goto err_points;
@@ -1667,9 +1682,9 @@ drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,
dma_fence_put(fence);
}
err_chains:
- kfree(chains);
+ kvfree(chains);
err_points:
- kfree(points);
+ kvfree(points);
out:
drm_syncobj_array_free(syncobjs, args->count_handles);
--
2.53.0
prev parent reply other threads:[~2026-12-04 0:35 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 10:53 FAILED: patch "[PATCH] drm/syncobj: Limit the number of handles and use kvmalloc for" failed to apply to 6.18-stable tree gregkh
2026-12-04 0:35 ` Sasha Levin [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204003458.1490232-1-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=christian.koenig@amd.com \
--cc=fei.yu@intel.com \
--cc=stable@vger.kernel.org \
--cc=syzbot+95416f957d84e858b377@syzkaller.appspotmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox