ioctl() numbers are unsigned (the man page lies)
PATCH 1/1 ioctl() numbers are unsigned (the man page lies)
In kvm-all.c we store an ioctl cmd number in the irqchip_inject_ioctl field of KVMState, which has type 'int'. This seems to make sense since the ioctl() man page says that the cmd parameter has type int.
However, the kernel treats ioctl numbers as unsigned - sys_ioctl() takes an unsigned int, and the macros which generate ioctl numbers expand to unsigned expressions. Furthermore, some ioctls (IOC_READ ioctls on x86 and IOC_WRITE ioctls on powerpc) have bit 31 set, and so would be negative if interpreted as an int. This has the surprising and compile-breaking consequence that in kvm_irqchip_set_irq() where we do:
return (s->irqchip_inject_ioctl == KVM_IRQ_LINE) ? 1 : event.status;
We will get a "comparison is always false due to limited range of data type" warning from gcc if KVM_IRQ_LINE is one of the bit-31-set ioctls, which it is on powerpc.
So, despite the fact that the man page and posix say ioctl numbers are signed, they're actually unsigned. The kernel uses unsigned, the glibc header uses unsigned long, and FreeBSD, NetBSD and OSX also use unsigned long ioctl numbers in the code.
Therefore, this patch changes the variable to be unsigned, fixing the compile.
--- kvm-all.c | 5 ++++- 1 files changed, 4 insertions(+), 1 deletions(-)
kvm-all.c +4 -1
@@ -78,7 +78,10 @@ struct KVMStateint pit_in_kernel;int xsave, xcrs;int many_ioeventfds; -int irqchip_inject_ioctl; +/* The man page (and posix) say ioctl numbers are signed int, but +* they're not. Linux, glibc and *BSD all treat ioctl numbers as +* unsigned, and treating them as signed here can break things */ +unsigned irqchip_inject_ioctl;#ifdef KVM_CAP_IRQ_ROUTING struct kvm_irq_routing *irq_routes;int nr_allocated_irq_routes;
Signature · 1 line
1.7.8.3
Looks like the subject would warmly welcome a "kvm:" tag.
Quoted 33 lines · David Gibson
In
kvm-all.cwe store an ioctl cmd number in theirqchip_inject_ioctlfield of KVMState, which has type 'int'. This seems to make sense since theioctl()man page says that the cmd parameter has type int.However, the kernel treats ioctl numbers as unsigned -
sys_ioctl()takes an unsigned int, and the macros which generate ioctl numbers expand to unsigned expressions. Furthermore, some ioctls (IOC_READioctls on x86 andIOC_WRITEioctls on powerpc) have bit 31 set, and so would be negative if interpreted as an int. This has the surprising and compile-breaking consequence that inkvm_irqchip_set_irq()where we do:return (s->irqchip_inject_ioctl == KVM_IRQ_LINE) ? 1 : event.status;We will get a "comparison is always false due to limited range of data type" warning from gcc if
KVM_IRQ_LINEis one of the bit-31-set ioctls, which it is on powerpc.So, despite the fact that the man page and posix say ioctl numbers are signed, they're actually unsigned. The kernel uses unsigned, the glibc header uses unsigned long, and FreeBSD, NetBSD and OSX also use unsigned long ioctl numbers in the code.
Therefore, this patch changes the variable to be unsigned, fixing the compile.
Signed-off-by David Gibson <[email protected]>--- kvm-all.c | 5 ++++- 1 files changed, 4 insertions(+), 1 deletions(-)kvm-all.c +0
-0
kvm-all.c struct KVMState · line 83
Quoted 7 lines
kvm-all.c +3
-1@@ -78,7 +78,10 @@ struct KVMStateint pit_in_kernel;int xsave, xcrs;int many_ioeventfds; -int irqchip_inject_ioctl; +/* The man page (and posix) say ioctl numbers are signed int, but +* they're not. Linux, glibc and *BSD all treat ioctl numbers as +* unsigned, and treating them as signed here can break things */
What about naming the problem instead:
/* Comparison with IOCTL macros on 32-bit hosts requires unsigned. */
kvm-all.c struct KVMState · line 87
Quoted 4 lines
kvm-all.c +1
-0+unsigned irqchip_inject_ioctl;#ifdef KVM_CAP_IRQ_ROUTING struct kvm_irq_routing *irq_routes;int nr_allocated_irq_routes;
Jan
Quoted 3 lines · Jan Kiszka
Looks like the subject would warmly welcome a "kvm:" tag.
On 2012-02-09 05:22, David Gibson wrote:
[snip]
Quoted 4 lines
What about naming the problem instead:
/* Comparison with IOCTL macros on 32-bit hosts requires unsigned. */
Just once, it would be nice to post something to this list and get a substantive comment _before_ the bitching about minutiae.
Oh, and it's not just 32-bit hosts, it's anywhere sizeof(int)==4, which includes ppc64 amongst other 64-bit hosts.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 8 lines · David Gibson
Quoted 3 lines · Jan Kiszka
Looks like the subject would warmly welcome a "kvm:" tag.
On 2012-02-09 05:22, David Gibson wrote:
[snip]
Quoted 3 lines
What about naming the problem instead:
Quoted 7 lines
Quoted 1 line
/* Comparison with IOCTL macros on 32-bit hosts requires unsigned. */
Just once, it would be nice to post something to this list and get a substantive comment _before_ the bitching about minutiae.
Oh, and it's not just 32-bit hosts, it's anywhere sizeof(int)==4, which includes ppc64 amongst other 64-bit hosts.
Then state "if int is 32 bits". The comment is unfortunately not helpful without the commit log.
Jan
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
What about naming the problem instead:
/* Comparison with IOCTL macros on 32-bit hosts requires unsigned. */
Quoted 8 lines
Quoted 5 lines
Just once, it would be nice to post something to this list and get a substantive comment _before_ the bitching about minutiae.
Oh, and it's not just 32-bit hosts, it's anywhere sizeof(int)==4, which includes ppc64 amongst other 64-bit hosts.
Then state "if int is 32 bits". The comment is unfortunately not helpful without the commit log.
Int is 32 bits on every host we're ever likey to care about, so clearly there's something else going on.
Paul
Quoted 7 lines · Paul Brook
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
What about naming the problem instead:
/* Comparison with IOCTL macros on 32-bit hosts requires unsigned. */
Just once, it would be nice to post something to this list and get a substantive comment _before_ the bitching about minutiae.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Oh, and it's not just 32-bit hosts, it's anywhere sizeof(int)==4, which includes ppc64 amongst other 64-bit hosts.
Then state "if int is 32 bits". The comment is unfortunately not helpful without the commit log.
Int is 32 bits on every host we're ever likey to care about, so clearly there's something else going on.
Yep. So a better explanation could be "Required for comparison with unsigned IOCTL macro values". This wasn't noticed so far as the original code was never build on anything but IA32/64.
Jan
Quoted 14 lines · Jan Kiszka
Quoted 13 lines · Paul Brook
Quoted 12 lines
Quoted 9 lines
Quoted 3 lines
What about naming the problem instead:
/* Comparison with IOCTL macros on 32-bit hosts requires unsigned. */
Just once, it would be nice to post something to this list and get a substantive comment _before_ the bitching about minutiae.
Oh, and it's not just 32-bit hosts, it's anywhere sizeof(int)==4, which includes ppc64 amongst other 64-bit hosts.
Then state "if int is 32 bits". The comment is unfortunately not helpful without the commit log.
Quoted 6 lines
Quoted 2 lines
Int is 32 bits on every host we're ever likey to care about, so clearly there's something else going on.
Yep. So a better explanation could be "Required for comparison with unsigned IOCTL macro values". This wasn't noticed so far as the original code was never build on anything but IA32/64.
Right. The problem still exists on x86 and x86_64, but it occurs for a different set of ioctls (IOC_READ instead of IOC_WRITE). I guess we never did this sort of signed comparison on an IOC_WRITE ioctl number (comparing ioctl numbers for equality isn't a particularly obvious thing to do from userspace, after all).
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 11 lines · Jan Kiszka
Quoted 10 lines · David Gibson
Quoted 3 lines · Jan Kiszka
Looks like the subject would warmly welcome a "kvm:" tag.
On 2012-02-09 05:22, David Gibson wrote:
[snip]
Quoted 4 lines
What about naming the problem instead:
/* Comparison with IOCTL macros on 32-bit hosts requires unsigned. */
Quoted 8 lines
Quoted 5 lines
Just once, it would be nice to post something to this list and get a substantive comment _before_ the bitching about minutiae.
Oh, and it's not just 32-bit hosts, it's anywhere sizeof(int)==4, which includes ppc64 amongst other 64-bit hosts.
Then state "if int is 32 bits". The comment is unfortunately not helpful without the commit log.
It'd also be nice if people didn't drop me from the CC list on my own patches.
I have resent the patch with a revised summary.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 15 lines · David Gibson
Quoted 14 lines · Jan Kiszka
Quoted 13 lines · David Gibson
Quoted 3 lines · Jan Kiszka
Looks like the subject would warmly welcome a "kvm:" tag.
On 2012-02-09 05:22, David Gibson wrote:
[snip]
Quoted 4 lines
What about naming the problem instead:
/* Comparison with IOCTL macros on 32-bit hosts requires unsigned. */
Just once, it would be nice to post something to this list and get a substantive comment _before_ the bitching about minutiae.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Oh, and it's not just 32-bit hosts, it's anywhere sizeof(int)==4, which includes ppc64 amongst other 64-bit hosts.
Then state "if int is 32 bits". The comment is unfortunately not helpful without the commit log.
It'd also be nice if people didn't drop me from the CC list on my own patches.
Sorry, that was not intentional. My mail client possibly followed a tag like in this mail:
Mail-Followup-To: Jan Kiszka <[email protected]>, [email protected],
[email protected], [email protected], [email protected]
I had to add you by hand to the list again. Maybe you wanna check what is causing this on your side.
Jan