qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Jan Kiszka <jan.kiszka@web.de>
To: David Gibson <david@gibson.dropbear.id.au>
Cc: mtosatti@redhat.com, avi@redhat.com, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] ioctl() numbers are unsigned (the man page lies)
Date: Thu, 09 Feb 2012 09:30:09 +0100	[thread overview]
Message-ID: <4F338411.20408@web.de> (raw)
In-Reply-To: <1328761361-23119-1-git-send-email-david@gibson.dropbear.id.au>

[-- Attachment #1: Type: text/plain, Size: 2279 bytes --]

Looks like the subject would warmly welcome a "kvm:" tag.

On 2012-02-09 05:22, David Gibson wrote:
> 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.
> 
> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>  kvm-all.c |    5 ++++-
>  1 files changed, 4 insertions(+), 1 deletions(-)
> 
> diff --git a/kvm-all.c b/kvm-all.c
> index 0b87658..681ad15 100644
> --- a/kvm-all.c
> +++ b/kvm-all.c
> @@ -78,7 +78,10 @@ struct KVMState
>      int 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. */

> +    unsigned irqchip_inject_ioctl;
>  #ifdef KVM_CAP_IRQ_ROUTING
>      struct kvm_irq_routing *irq_routes;
>      int nr_allocated_irq_routes;

Jan


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 262 bytes --]

  reply	other threads:[~2012-02-09  8:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-09  4:22 [Qemu-devel] [PATCH] ioctl() numbers are unsigned (the man page lies) David Gibson
2012-02-09  8:30 ` Jan Kiszka [this message]
2012-02-09  9:58   ` David Gibson
2012-02-09 10:06     ` Jan Kiszka
2012-02-09 12:07       ` Paul Brook
2012-02-09 14:11         ` Jan Kiszka
2012-02-10  6:45           ` David Gibson
2012-02-10  6:43       ` David Gibson
2012-02-10  7:27         ` Jan Kiszka

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=4F338411.20408@web.de \
    --to=jan.kiszka@web.de \
    --cc=avi@redhat.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=mtosatti@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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;
as well as URLs for NNTP newsgroup(s).