From: Andrea Arcangeli <aarcange@redhat.com>
To: "Daniel P. Berrange" <berrange@redhat.com>
Cc: qemu-devel@nongnu.org, Jitendra Kolhe <jitendra.kolhe@hpe.com>,
Michal Privoznik <mprivozn@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [Qemu-devel] [PATCH] os: don't corrupt pre-existing memory-backend data with prealloc
Date: Mon, 27 Feb 2017 16:53:24 +0100 [thread overview]
Message-ID: <20170227155324.GC5816@redhat.com> (raw)
In-Reply-To: <20170224172714.26026-1-berrange@redhat.com>
Hello,
On Fri, Feb 24, 2017 at 05:27:14PM +0000, Daniel P. Berrange wrote:
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index 35012b9..2a5bb93 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -355,7 +355,20 @@ void os_mem_prealloc(int fd, char *area, size_t memory, Error **errp)
>
> /* MAP_POPULATE silently ignores failures */
> for (i = 0; i < numpages; i++) {
> - memset(area + (hpagesize * i), 0, 1);
> + /*
> + * Read & write back the same value, so we don't
> + * corrupt existinng user/app data that might be
> + * stored.
> + *
> + * 'volatile' to stop compiler optimizing this away
> + * to a no-op
> + *
> + * TODO: get a better solution from kernel so we
> + * don't need to write at all so we don't cause
> + * wear on the storage backing the region...
> + */
It would be better that if MAP_POPULATE failed like mlock does, at
least for those get_user_pages errors like -ENOMEM that are already a
retval for mmap it sounds weird that it's not being forwarded. It'd be
enough to call do_munmap to rollback all mmap work before returning
-ENOMEM from mmap instead of succees.
-EFAULT or other get_user_pages errors are more troublesome, as mmap
wouldn't otherwise return -EFAULT, but those would generally be qemu
bugs or hardware errors and MAP_POPULATE I doubt is meant to be a
debug/robustness aid. All we care about here is memory resource
management and not to risk -ENOMEM at runtime and immediate peak
performance. So perhaps a kernel patch that forwards -ENOMEM from
__mm_populate to mmap retval would be enough to make it usable?
> + volatile char val = *(area + (hpagesize * i));
> + *(area + (hpagesize * i)) = val;
It's not "var" that should be volatile, nor the pointer, but only the
memory area you write to, because the write to the memory are is the
only thing we care about.
If "val" is volatile gcc could read the memory area and cache the
value of the memory area, write it into the volatile var, then read
the volatile var again, compare it with the cached value and skip the
write to the memory if it's equal. In practice it's likely not doing
that, and making var volatile has the same effect, so it's probably
only a theoretical issue of course.
I would suggest this to correct it (untested):
char *tmparea = area + (hpagesize * i);
*(volatile char *)tmparea = *tmparea;
Thanks,
Andrea
next prev parent reply other threads:[~2017-02-27 15:53 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-02-24 17:27 [Qemu-devel] [PATCH] os: don't corrupt pre-existing memory-backend data with prealloc Daniel P. Berrange
2017-02-24 17:33 ` no-reply
2017-02-27 9:25 ` Daniel P. Berrange
2017-02-24 19:04 ` Eric Blake
2017-02-27 13:28 ` Stefan Hajnoczi
2017-02-27 15:53 ` Andrea Arcangeli [this message]
-- strict thread matches above, loose matches on Subject: below --
2017-02-23 10:59 Daniel P. Berrange
2017-02-23 12:05 ` Michal Privoznik
2017-02-23 12:07 ` Daniel P. Berrange
2017-02-24 9:05 ` Michal Privoznik
2017-02-24 9:24 ` Daniel P. Berrange
2017-02-24 12:12 ` Dr. David Alan Gilbert
2017-02-24 12:18 ` Paolo Bonzini
2017-02-27 11:10 ` Stefan Hajnoczi
2017-02-27 13:46 ` Rik van Riel
2017-02-27 13:58 ` Daniel P. Berrange
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=20170227155324.GC5816@redhat.com \
--to=aarcange@redhat.com \
--cc=berrange@redhat.com \
--cc=jitendra.kolhe@hpe.com \
--cc=mprivozn@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@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.