From: Cao jin <caoj.fnst@cn.fujitsu.com>
To: Thomas Huth <thuth@redhat.com>, <qemu-devel@nongnu.org>,
<qemu-trivial@nongnu.org>
Cc: <peter.maydell@linaro.org>, <armbru@redhat.com>, <mst@redhat.com>
Subject: Re: [Qemu-trivial] [Qemu-devel] [PATCH] util/mmap-alloc: check parameter before using
Date: Wed, 26 Oct 2016 18:14:06 +0800 [thread overview]
Message-ID: <581081EE.5070106@cn.fujitsu.com> (raw)
In-Reply-To: <b324af65-ea5e-e39b-75c2-0c54b941bce7@redhat.com>
On 10/26/2016 04:10 PM, Thomas Huth wrote:
> On 26.10.2016 09:18, Cao jin wrote:
>> Also refactor some code hunk for readability
>>
>> Signed-off-by: Cao jin <caoj.fnst@cn.fujitsu.com>
>> ---
>> util/mmap-alloc.c | 20 +++++++++-----------
>> 1 file changed, 9 insertions(+), 11 deletions(-)
>>
>> diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c
>> index 5a85aa3..92c123a 100644
>> --- a/util/mmap-alloc.c
>> +++ b/util/mmap-alloc.c
>> @@ -41,6 +41,11 @@ size_t qemu_fd_getpagesize(int fd)
>>
>> void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
>> {
>> + /* Make sure align is a power of 2 */
>> + assert(!(align & (align - 1)));
>> + /* Always align to host page size */
>> + assert(align >= getpagesize());
>
> Now you've moved code before the declaration of the local variables.
> That's also some kind of ugly (and might not work with older versions of
> C compilers?)...
>
Yes, I think it is ugly too, my personal taste is declaration should
exist at the head of the function body, then parameters' check, then
assignment to the variables in separate lines, but for this patch, I
think that won't be liked by maintainers:)
>> /*
>> * Note: this always allocates at least one extra page of virtual address
>> * space, even if size is already aligned.
>> @@ -68,11 +73,6 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
>> return MAP_FAILED;
>> }
>>
>> - /* Make sure align is a power of 2 */
>> - assert(!(align & (align - 1)));
>> - /* Always align to host page size */
>> - assert(align >= getpagesize());
>
> ... so maybe simply move the setting of "offset" (which requires
> "align") here right in front of the mmap() instead?
A better choice to me:) Thanks
Cao jin
>
>> ptr1 = mmap(ptr + offset, size, PROT_READ | PROT_WRITE,
>> MAP_FIXED |
>> (fd == -1 ? MAP_ANONYMOUS : 0) |
>> @@ -83,22 +83,20 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
>> return MAP_FAILED;
>> }
>>
>> - ptr += offset;
>> - total -= offset;
>> -
>> if (offset > 0) {
>> - munmap(ptr - offset, offset);
>> + munmap(ptr, offset);
>> }
>>
>> /*
>> * Leave a single PROT_NONE page allocated after the RAM block, to serve as
>> * a guard page guarding against potential buffer overflows.
>> */
>> + total -= offset;
>> if (total > size + getpagesize()) {
>> - munmap(ptr + size + getpagesize(), total - size - getpagesize());
>> + munmap(ptr1 + size + getpagesize(), total - size - getpagesize());
>> }
>>
>> - return ptr;
>> + return ptr1;
>> }
>
> Thomas
>
>
>
>
> .
>
WARNING: multiple messages have this Message-ID (diff)
From: Cao jin <caoj.fnst@cn.fujitsu.com>
To: Thomas Huth <thuth@redhat.com>,
qemu-devel@nongnu.org, qemu-trivial@nongnu.org
Cc: peter.maydell@linaro.org, armbru@redhat.com, mst@redhat.com
Subject: Re: [Qemu-devel] [PATCH] util/mmap-alloc: check parameter before using
Date: Wed, 26 Oct 2016 18:14:06 +0800 [thread overview]
Message-ID: <581081EE.5070106@cn.fujitsu.com> (raw)
In-Reply-To: <b324af65-ea5e-e39b-75c2-0c54b941bce7@redhat.com>
On 10/26/2016 04:10 PM, Thomas Huth wrote:
> On 26.10.2016 09:18, Cao jin wrote:
>> Also refactor some code hunk for readability
>>
>> Signed-off-by: Cao jin <caoj.fnst@cn.fujitsu.com>
>> ---
>> util/mmap-alloc.c | 20 +++++++++-----------
>> 1 file changed, 9 insertions(+), 11 deletions(-)
>>
>> diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c
>> index 5a85aa3..92c123a 100644
>> --- a/util/mmap-alloc.c
>> +++ b/util/mmap-alloc.c
>> @@ -41,6 +41,11 @@ size_t qemu_fd_getpagesize(int fd)
>>
>> void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
>> {
>> + /* Make sure align is a power of 2 */
>> + assert(!(align & (align - 1)));
>> + /* Always align to host page size */
>> + assert(align >= getpagesize());
>
> Now you've moved code before the declaration of the local variables.
> That's also some kind of ugly (and might not work with older versions of
> C compilers?)...
>
Yes, I think it is ugly too, my personal taste is declaration should
exist at the head of the function body, then parameters' check, then
assignment to the variables in separate lines, but for this patch, I
think that won't be liked by maintainers:)
>> /*
>> * Note: this always allocates at least one extra page of virtual address
>> * space, even if size is already aligned.
>> @@ -68,11 +73,6 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
>> return MAP_FAILED;
>> }
>>
>> - /* Make sure align is a power of 2 */
>> - assert(!(align & (align - 1)));
>> - /* Always align to host page size */
>> - assert(align >= getpagesize());
>
> ... so maybe simply move the setting of "offset" (which requires
> "align") here right in front of the mmap() instead?
A better choice to me:) Thanks
Cao jin
>
>> ptr1 = mmap(ptr + offset, size, PROT_READ | PROT_WRITE,
>> MAP_FIXED |
>> (fd == -1 ? MAP_ANONYMOUS : 0) |
>> @@ -83,22 +83,20 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
>> return MAP_FAILED;
>> }
>>
>> - ptr += offset;
>> - total -= offset;
>> -
>> if (offset > 0) {
>> - munmap(ptr - offset, offset);
>> + munmap(ptr, offset);
>> }
>>
>> /*
>> * Leave a single PROT_NONE page allocated after the RAM block, to serve as
>> * a guard page guarding against potential buffer overflows.
>> */
>> + total -= offset;
>> if (total > size + getpagesize()) {
>> - munmap(ptr + size + getpagesize(), total - size - getpagesize());
>> + munmap(ptr1 + size + getpagesize(), total - size - getpagesize());
>> }
>>
>> - return ptr;
>> + return ptr1;
>> }
>
> Thomas
>
>
>
>
> .
>
next prev parent reply other threads:[~2016-10-26 10:12 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-10-26 7:18 [Qemu-trivial] [PATCH] util/mmap-alloc: check parameter before using Cao jin
2016-10-26 7:18 ` [Qemu-devel] " Cao jin
2016-10-26 8:10 ` [Qemu-trivial] " Thomas Huth
2016-10-26 8:10 ` Thomas Huth
2016-10-26 10:14 ` Cao jin [this message]
2016-10-26 10:14 ` Cao jin
2016-10-26 13:33 ` [Qemu-trivial] " Eric Blake
2016-10-26 13:33 ` Eric Blake
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=581081EE.5070106@cn.fujitsu.com \
--to=caoj.fnst@cn.fujitsu.com \
--cc=armbru@redhat.com \
--cc=mst@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-trivial@nongnu.org \
--cc=thuth@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.