From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60993) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bwUnJ-000732-FI for qemu-devel@nongnu.org; Tue, 18 Oct 2016 09:51:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bwUnE-000454-89 for qemu-devel@nongnu.org; Tue, 18 Oct 2016 09:51:05 -0400 Received: from mail-qk0-x232.google.com ([2607:f8b0:400d:c09::232]:36463) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1bwUnE-00044r-3o for qemu-devel@nongnu.org; Tue, 18 Oct 2016 09:51:00 -0400 Received: by mail-qk0-x232.google.com with SMTP id o68so339590442qkf.3 for ; Tue, 18 Oct 2016 06:50:59 -0700 (PDT) References: <1467104499-27517-1-git-send-email-pl@kamp.de> <1467104499-27517-6-git-send-email-pl@kamp.de> <20161016050644-mutt-send-email-mst@kernel.org> From: Alex =?utf-8?Q?Benn=C3=A9e?= In-reply-to: <20161016050644-mutt-send-email-mst@kernel.org> Date: Tue, 18 Oct 2016 14:50:57 +0100 Message-ID: <87funtg3zy.fsf@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Subject: Re: [Qemu-devel] [PATCH 05/15] util: add a helper to mmap private anonymous memory List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Michael S. Tsirkin" Cc: Peter Lieven , kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org, dgilbert@redhat.com, kraxel@redhat.com, pbonzini@redhat.com, mreitz@redhat.com Michael S. Tsirkin writes: > On Tue, Jun 28, 2016 at 11:01:29AM +0200, Peter Lieven wrote: >> Signed-off-by: Peter Lieven >> --- >> include/qemu/mmap-alloc.h | 6 ++++++ >> util/mmap-alloc.c | 17 +++++++++++++++++ >> 2 files changed, 23 insertions(+) >> >> diff --git a/include/qemu/mmap-alloc.h b/include/qemu/mmap-alloc.h >> index 0899b2f..a457721 100644 >> --- a/include/qemu/mmap-alloc.h >> +++ b/include/qemu/mmap-alloc.h >> @@ -9,4 +9,10 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared); >> >> void qemu_ram_munmap(void *ptr, size_t size); >> >> +/* qemu_anon_ram_mmap maps private anonymous memory using mmap and >> + * aborts if the allocation fails. its meant to act as an replacement >> + * for g_malloc0 and friends. */ > > This needs better docs. When should one use g_malloc0 and when > qemu_anon_ram_munmap? My concern is does this break memory sanitizers when we could just tweak libc's allocation strategy to use mmap (which it should do for blocks over a certain threshold). > > > >> +void *qemu_anon_ram_mmap(size_t size); >> +void qemu_anon_ram_munmap(void *ptr, size_t size); >> + > > The names are confusing - this isn't guest RAM, this is > just internal QEMU memory, isn't it? > > Just rename it qemu_malloc0 then ... > >> #endif >> diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c >> index 629d97a..c099858 100644 >> --- a/util/mmap-alloc.c >> +++ b/util/mmap-alloc.c >> @@ -107,3 +107,20 @@ void qemu_ram_munmap(void *ptr, size_t size) >> munmap(ptr, size + getpagesize()); >> } >> } >> + >> +void *qemu_anon_ram_mmap(size_t size) >> +{ >> + void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, >> + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); >> + if (ptr == MAP_FAILED) { >> + abort(); >> + } >> + return ptr; >> +} >> + >> +void qemu_anon_ram_munmap(void *ptr, size_t size) >> +{ >> + if (ptr) { >> + munmap(ptr, size); >> + } >> +} >> -- >> 1.9.1 -- Alex Bennée