From: Roman Kagan <rkagan@amazon.de>
To: Ilias Stamatis <ilstam@amazon.com>
Cc: <kvm@vger.kernel.org>, <pbonzini@redhat.com>,
<pdurrant@amazon.co.uk>, <dwmw@amazon.co.uk>,
<Laurent.Vivier@bull.net>, <ghaskins@novell.com>,
<avi@redhat.com>, <mst@redhat.com>, <levinsasha928@gmail.com>,
<peng.hao2@zte.com.cn>, <nh-open-source@amazon.com>
Subject: Re: [PATCH 1/6] KVM: Fix coalesced_mmio_has_room()
Date: Fri, 12 Jul 2024 17:55:47 +0200 [thread overview]
Message-ID: <ZpFSA4CA1FaS4iWV@u40bc5e070a0153.ant.amazon.com> (raw)
In-Reply-To: <20240710085259.2125131-2-ilstam@amazon.com>
On Wed, Jul 10, 2024 at 09:52:54AM +0100, Ilias Stamatis wrote:
> The following calculation used in coalesced_mmio_has_room() to check
> whether the ring buffer is full is wrong and only allows half the buffer
> to be used.
>
> avail = (ring->first - last - 1) % KVM_COALESCED_MMIO_MAX;
> if (avail == 0)
> /* full */
>
> The % operator in C is not the modulo operator but the remainder
> operator. Modulo and remainder operators differ with respect to negative
> values. But all values are unsigned in this case anyway.
>
> The above might have worked as expected in python for example:
> >>> (-86) % 170
> 84
>
> However it doesn't work the same way in C.
>
> printf("avail: %d\n", (-86) % 170);
> printf("avail: %u\n", (-86) % 170);
> printf("avail: %u\n", (-86u) % 170u);
>
> Using gcc-11 these print:
>
> avail: -86
> avail: 4294967210
> avail: 0
Where exactly do you see a problem? As you correctly point out, all
values are unsigned, so unsigned arithmetics with wraparound applies,
and then % operator is applied to the resulting unsigned value. Out
your three examples, only the last one is relevant, and it's perfectly
the intended behavior.
Thanks,
Roman.
Amazon Web Services Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
next prev parent reply other threads:[~2024-07-12 15:56 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-10 8:52 [PATCH 0/6] KVM: Improve MMIO Coalescing API Ilias Stamatis
2024-07-10 8:52 ` [PATCH 1/6] KVM: Fix coalesced_mmio_has_room() Ilias Stamatis
2024-07-12 13:13 ` Paul Durrant
2024-07-12 15:55 ` Roman Kagan [this message]
2024-07-12 19:03 ` Stamatis, Ilias
2024-07-15 9:30 ` Kagan, Roman
2024-07-10 8:52 ` [PATCH 2/6] KVM: Add KVM_CREATE_COALESCED_MMIO_BUFFER ioctl Ilias Stamatis
2024-07-12 13:22 ` Paul Durrant
2024-07-10 8:52 ` [PATCH 3/6] KVM: Support poll() on coalesced mmio buffer fds Ilias Stamatis
2024-07-12 13:26 ` Paul Durrant
2024-07-10 8:52 ` [PATCH 4/6] KVM: Add KVM_(UN)REGISTER_COALESCED_MMIO2 ioctls Ilias Stamatis
2024-07-13 6:19 ` Paul Durrant
2024-07-13 14:15 ` kernel test robot
2024-07-13 20:48 ` kernel test robot
2024-07-10 8:52 ` [PATCH 5/6] KVM: Documentation: Document v2 of coalesced MMIO API Ilias Stamatis
2024-07-13 6:19 ` Paul Durrant
2024-07-10 8:52 ` [PATCH 6/6] KVM: selftests: Add coalesced_mmio_test Ilias Stamatis
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=ZpFSA4CA1FaS4iWV@u40bc5e070a0153.ant.amazon.com \
--to=rkagan@amazon.de \
--cc=Laurent.Vivier@bull.net \
--cc=avi@redhat.com \
--cc=dwmw@amazon.co.uk \
--cc=ghaskins@novell.com \
--cc=ilstam@amazon.com \
--cc=kvm@vger.kernel.org \
--cc=levinsasha928@gmail.com \
--cc=mst@redhat.com \
--cc=nh-open-source@amazon.com \
--cc=pbonzini@redhat.com \
--cc=pdurrant@amazon.co.uk \
--cc=peng.hao2@zte.com.cn \
/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