Skip to contentSkip to conversation

kvm: Don't assume accel_ioctl_end() preserves @errno

PATCH Applied Markus Armbruster 3 participants 6 messages Nov 25, 2025 – Nov 29, 2025 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm: Don't assume accel_ioctl_end() preserves @errno

Retrieve the @errno set by ioctl() before we call accel_ioctl_end() instead of afterwards, so it works whether accel_ioctl_end() preserves @errno or not.

---
 accel/kvm/kvm-all.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
accel/kvm/kvm-all.c +2 -2
index f9254ae654..28006d73c5 100644
@@ -3373,10 +3373,10 @@ int kvm_vm_ioctl(KVMState *s, unsigned long type, ...)
     trace_kvm_vm_ioctl(type, arg);
     accel_ioctl_begin();
     ret = ioctl(s->vmfd, type, arg);
-    accel_ioctl_end();
     if (ret == -1) {
         ret = -errno;
     }
+    accel_ioctl_end();
     return ret;
 }
 
@@ -3413,10 +3413,10 @@ int kvm_device_ioctl(int fd, unsigned long type, ...)
     trace_kvm_device_ioctl(fd, type, arg);
     accel_ioctl_begin();
     ret = ioctl(fd, type, arg);
-    accel_ioctl_end();
     if (ret == -1) {
         ret = -errno;
     }
+    accel_ioctl_end();
     return ret;
 }
 
Signature · 1 line
2.49.0
#1 lore raw
Quoted 5 lines · Markus Armbruster

Retrieve the @errno set by ioctl() before we call accel_ioctl_end() instead of afterwards, so it works whether accel_ioctl_end() preserves @errno or not.

I did not check whether the assumption holds or not. If it doesn't, then this needs

Fixes a27dd2de68f3 (KVM: keep track of running ioctls)
#2 lore raw
Quoted 9 lines · Markus Armbruster
Quoted 5 lines · Markus Armbruster

Retrieve the @errno set by ioctl() before we call accel_ioctl_end() instead of afterwards, so it works whether accel_ioctl_end() preserves @errno or not.

I did not check whether the assumption holds or not.

Indeed, on Linux the futex syscall is called via qemu_event_set.

Quoted 4 lines

If it doesn't,
then this needs

Fixes a27dd2de68f3 (KVM: keep track of running ioctls)

LGTM.

#3 lore raw
Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Markus Armbruster
Quoted 4 lines · Markus Armbruster

Retrieve the @errno set by ioctl() before we call accel_ioctl_end() instead of afterwards, so it works whether accel_ioctl_end() preserves @errno or not.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

I did not check whether the assumption holds or not.

Indeed, on Linux the futex syscall is called via qemu_event_set.

So ...

Quoted 4 lines
Quoted 4 lines

If it doesn't,
then this needs

Fixes a27dd2de68f3 (KVM: keep track of running ioctls)

... we definitely want this tag, and the commit message should be clarified as well. Here's my try:

kvm: Fix kvm_vm_ioctl() and kvm_device_ioctl() return value

These functions wrap ioctl(). When ioctl() fails, it sets @errno. The wrappers then return that @errno negated.

Except they call accel_ioctl_end() between calling ioctl() and reading @errno. accel_ioctl_end() can clobber @errno, e.g. when a futex() system call fails. Seems unlikely, but it's a bug all the same.

Fix by retrieving @errno before calling accel_ioctl_end().

Fixes a27dd2de68f3 (KVM: keep track of running ioctls)
Quoted 3 lines

LGTM.

Thanks!