Cleanup of qemu_oom_check() and qemu_memalign()
Subsystems (3)
cover Cleanup of qemu_oom_check() and qemu_memalign()
This series does some cleanup of the qemu_oom_check() and qemu_memalign() functions; I started looking at the first of these and found myself wanting to tidy some stuff relating to the second in the process. The TLDR is that this series removes qemu_oom_check() (which was mostly being misused), unifies the POSIX and Win32 versions of qemu_memalign() and qemu_try_memalign(), and moves the prototypes out of osdep.h.
qemu_oom_check() is a function which essentially says "if you pass me a NULL pointer then print a message and abort()". On POSIX systems the message includes strerror(errno); on Windows it includes the GetLastError() error value printed as an integer. Other than in the implementation of qemu_memalign(), we use this function only in hw/usb/redirect.c, for three checks:
* on a call to usbredirparser_create()
* on a call to usberedirparser_serialize()
* on a call to malloc()
The usbredir library API functions make no guarantees that they will set errno on errors, let alone that they might set the Windows-specific GetLastError string. So in 2/3 of the cases using qemu_oom_check() is definitely wrong, and in the third it's dubious given that Windows malloc() doesn't set GetLastError (more on that later). So we start by switching these 3 calls to simple in-line error checking code, which means we can make qemu_oom_check() not a public global function.
So now we have a qemu_oom_check() which is used only in qemu_memalign() and whose major difference between the Windows and POSIX versions is that the former prints the GetLastError error number value. This is actually a bug -- in commit dfbd0b873a85021 in 2020 we changed the implementation of qemu_try_memalign() from using VirtualAlloc() without noticing that this also should imply changing what we print in the error case. So we can switch to a single shared qemu_memalign() which does the error check directly itself and always prints errno (and the requested size and alignment, which seem more useful if anybody needs to diagnose the problem).
qemu_try_memalign() also is very similar between POSIX and Windows versions (it used to be less so, but we have gradually ended up with the two versions quite similar). The POSIX version already has an ifdef ladder for different kinds of "allocate aligned memory" host functions, so it seems neater to just have Windows' _aligned_malloc() be another ladder in that ifdef rather than a totally separate function.
Finally, I put the qemu_memalign() and related function prototypes into a new memalign.h header, because that gets them out of osdep.h and into somewhere that only the files that care about these functions need to include.
thanks
--PMM
Peter Maydell (9):
hw/usb/redirect.c: Stop using qemu_oom_check()
util: Make qemu_oom_check() a static function
util: Unify implementations of qemu_memalign() util/oslib-win32: Return NULL on qemu_try_memalign() with zero size meson.build: Don't misdetect posix_memalign() on Windows
util: Share qemu_try_memalign() implementation between POSIX and
Windows
util: Use meson checks for valloc() and memalign() presence
util: Put qemu_vfree() in memalign.c
osdep: Move memalign-related functions to their own header
meson.build | 7 ++- include/qemu-common.h | 2 - include/qemu/memalign.h | 61 +++++++++++++++++++++++ include/qemu/osdep.h | 18 ------- block/blkverify.c | 1 + block/block-copy.c | 1 + block/commit.c | 1 + block/crypto.c | 1 + block/dmg.c | 1 + block/export/fuse.c | 1 + block/file-posix.c | 1 + block/io.c | 1 + block/mirror.c | 1 + block/nvme.c | 1 + block/parallels-ext.c | 1 + block/parallels.c | 1 + block/qcow.c | 1 + block/qcow2-cache.c | 1 + block/qcow2-cluster.c | 1 + block/qcow2-refcount.c | 1 + block/qcow2-snapshot.c | 1 + block/qcow2.c | 1 + block/qed-l2-cache.c | 1 + block/qed-table.c | 1 + block/qed.c | 1 + block/quorum.c | 1 + block/raw-format.c | 1 + block/vdi.c | 1 + block/vhdx-log.c | 1 + block/vhdx.c | 1 + block/vmdk.c | 1 + block/vpc.c | 1 + block/win32-aio.c | 1 + hw/block/dataplane/xen-block.c | 1 + hw/block/fdc.c | 1 + hw/ide/core.c | 1 + hw/ppc/spapr.c | 1 + hw/ppc/spapr_softmmu.c | 1 + hw/scsi/scsi-disk.c | 1 + hw/tpm/tpm_ppi.c | 2 +- hw/usb/redirect.c | 17 +++++-- nbd/server.c | 1 + net/l2tpv3.c | 2 +- plugins/loader.c | 1 + qemu-img.c | 1 + qemu-io-cmds.c | 1 + qom/object.c | 1 + softmmu/physmem.c | 1 + target/i386/hvf/hvf.c | 1 + target/i386/kvm/kvm.c | 1 + tcg/region.c | 1 + tests/bench/atomic_add-bench.c | 1 + tests/bench/qht-bench.c | 1 + util/atomic64.c | 1 + util/memalign.c | 88 ++++++++++++++++++++++++++++++++++ util/oslib-posix.c | 46 ------------------ util/oslib-win32.c | 35 -------------- util/qht.c | 1 + util/meson.build | 1 + 59 files changed, 220 insertions(+), 107 deletions(-) create mode 100644 include/qemu/memalign.h create mode 100644 util/memalign.c
Signature · 1 line
2.25.1
PATCH 1/9 hw/usb/redirect.c: Stop using qemu_oom_check()
qemu_oom_check() is a function which essentially says "if you pass me a NULL pointer then print a message then abort()". On POSIX systems the message includes strerror(errno); on Windows it includes the GetLastError() error value printed as an integer.
Other than in the implementation of qemu_memalign(), we use this function only in hw/usb/redirect.c, for three checks:
* on a call to usbredirparser_create()
* on a call to usberedirparser_serialize()
* on a call to malloc()
The usbredir library API functions make no guarantees that they will set errno on errors, let alone that they might set the Windows-specific GetLastError string. malloc() is documented as setting errno, not GetLastError -- and in any case the only thing it might set errno to is ENOMEM. So qemu_oom_check() isn't the right thing for any of these. Replace them with straightforward error-checking code. This will allow us to get rid of qemu_oom_check().
---
I have left all of these errors as fatal, since that's what they were previously. Possibly somebody with a better understanding of the usbredir code might be able to make them theoretically non-fatal, but we make malloc failures generally fatal anyway.
--- hw/usb/redirect.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
hw/usb/redirect.c +14 -3
@@ -1239,7 +1239,11 @@ static void usbredir_create_parser(USBRedirDevice *dev)DPRINTF ("creating usbredirparser\n" ); - dev->parser =qemu_oom_check (usbredirparser_create ()); + dev->parser =usbredirparser_create (); +if (!dev->parser) { +error_report ("usbredirparser_create() failed" ); +exit (1 ); + } dev->parser->priv = dev; dev->parser->log_func = usbredir_log; dev->parser->read_func = usbredir_read;
@@ -2239,7 +2243,10 @@ static int usbredir_put_parser(QEMUFile *f, void *priv, size_t unused, }usbredirparser_serialize (dev->parser, &data, &len); -qemu_oom_check (data); +if (!data) { +error_report ("usbredirparser_serialize failed" ); +exit (1 ); + }qemu_put_be32 (f, len);qemu_put_buffer (f, data, len);
@@ -2330,7 +2337,11 @@ static int usbredir_get_bufpq(QEMUFile *f, void *priv, size_t unused, bufp->len =qemu_get_be32 (f); bufp->status =qemu_get_be32 (f); bufp->offset =0 ; - bufp->data =qemu_oom_check (malloc (bufp->len));/* regular malloc! */ + bufp->data =malloc (bufp->len);/* regular malloc! */ +if (!bufp->data) { +error_report ("usbredir_get_bufpq: out of memory" ); +exit (1 ); + } bufp->free_on_destroy = bufp->data;qemu_get_buffer (f, bufp->data, bufp->len);QTAILQ_INSERT_TAIL (&endp->bufpq, bufp, next);
Signature · 1 line
2.25.1
Re: [PATCH 1/9] hw/usb/redirect.c: Stop using qemu_oom_check()
On Sat, 26 Feb 2022 at 18:07, Peter Maydell <[email protected]> wrote:
Forgot to cc Gerd on this one as USB maintainer. Sorry..
Quoted 75 lines
qemu_oom_check()is a function which essentially says "if you pass me a NULL pointer then print a message thenabort()". On POSIX systems the message includes strerror(errno); on Windows it includes theGetLastError()error value printed as an integer.Other than in the implementation of
qemu_memalign(), we use this function only inhw/usb/redirect.c, for three checks:* on a call to
usbredirparser_create()
* on a call tousberedirparser_serialize()
* on a call tomalloc()The usbredir library API functions make no guarantees that they will set errno on errors, let alone that they might set the Windows-specific GetLastError string.
malloc()is documented as setting errno, not GetLastError -- and in any case the only thing it might set errno to is ENOMEM. Soqemu_oom_check()isn't the right thing for any of these. Replace them with straightforward error-checking code. This will allow us to get rid ofqemu_oom_check().Signed-off-by Peter Maydell <[email protected]>---
I have left all of these errors as fatal, since that's what they were previously. Possibly somebody with a better understanding of the usbredir code might be able to make them theoretically non-fatal, but we make malloc failures generally fatal anyway.--- hw/usb/redirect.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)hw/usb/redirect.c +14
-4@@ -1239,7 +1239,11 @@ static void usbredir_create_parser(USBRedirDevice *dev)DPRINTF ("creating usbredirparser\n" ); - dev->parser =qemu_oom_check (usbredirparser_create ()); + dev->parser =usbredirparser_create (); +if (!dev->parser) { +error_report ("usbredirparser_create() failed" ); +exit (1 ); + } dev->parser->priv = dev; dev->parser->log_func = usbredir_log; dev->parser->read_func = usbredir_read;@@ -2239,7 +2243,10 @@ static int usbredir_put_parser(QEMUFile *f, void *priv, size_t unused, }usbredirparser_serialize (dev->parser, &data, &len); -qemu_oom_check (data); +if (!data) { +error_report ("usbredirparser_serialize failed" ); +exit (1 ); + }qemu_put_be32 (f, len);qemu_put_buffer (f, data, len);@@ -2330,7 +2337,11 @@ static int usbredir_get_bufpq(QEMUFile *f, void *priv, size_t unused, bufp->len =qemu_get_be32 (f); bufp->status =qemu_get_be32 (f); bufp->offset =0 ; - bufp->data =qemu_oom_check (malloc (bufp->len));/* regular malloc! */ + bufp->data =malloc (bufp->len);/* regular malloc! */ +if (!bufp->data) { +error_report ("usbredir_get_bufpq: out of memory" ); +exit (1 ); + } bufp->free_on_destroy = bufp->data;qemu_get_buffer (f, bufp->data, bufp->len);QTAILQ_INSERT_TAIL (&endp->bufpq, bufp, next); --2.25.1
Re: [PATCH 1/9] hw/usb/redirect.c: Stop using qemu_oom_check()
In reply to Peter MaydellQuoted 22 lines · Peter Maydell
qemu_oom_check()is a function which essentially says "if you pass me a NULL pointer then print a message thenabort()". On POSIX systems the message includes strerror(errno); on Windows it includes theGetLastError()error value printed as an integer.Other than in the implementation of
qemu_memalign(), we use this function only inhw/usb/redirect.c, for three checks:* on a call to
usbredirparser_create()
* on a call tousberedirparser_serialize()
* on a call tomalloc()The usbredir library API functions make no guarantees that they will set errno on errors, let alone that they might set the Windows-specific GetLastError string.
malloc()is documented as setting errno, not GetLastError -- and in any case the only thing it might set errno to is ENOMEM. Soqemu_oom_check()isn't the right thing for any of these. Replace them with straightforward error-checking code. This will allow us to get rid ofqemu_oom_check().Signed-off-by Peter Maydell<[email protected]>
Quoted 8 lines
---
I have left all of these errors as fatal, since that's what they were previously. Possibly somebody with a better understanding of the usbredir code might be able to make them theoretically non-fatal, but we make malloc failures generally fatal anyway.--- hw/usb/redirect.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
r~
Re: [PATCH 1/9] hw/usb/redirect.c: Stop using qemu_oom_check()
In reply to Peter MaydellQuoted 22 lines · Peter Maydell
qemu_oom_check()is a function which essentially says "if you pass me a NULL pointer then print a message thenabort()". On POSIX systems the message includes strerror(errno); on Windows it includes theGetLastError()error value printed as an integer.Other than in the implementation of
qemu_memalign(), we use this function only inhw/usb/redirect.c, for three checks:* on a call to
usbredirparser_create()
* on a call tousberedirparser_serialize()
* on a call tomalloc()The usbredir library API functions make no guarantees that they will set errno on errors, let alone that they might set the Windows-specific GetLastError string.
malloc()is documented as setting errno, not GetLastError -- and in any case the only thing it might set errno to is ENOMEM. Soqemu_oom_check()isn't the right thing for any of these. Replace them with straightforward error-checking code. This will allow us to get rid ofqemu_oom_check().Signed-off-by Peter Maydell <[email protected]>
Quoted 8 lines
---
I have left all of these errors as fatal, since that's what they were previously. Possibly somebody with a better understanding of the usbredir code might be able to make them theoretically non-fatal, but we make malloc failures generally fatal anyway.--- hw/usb/redirect.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
Re: [PATCH 1/9] hw/usb/redirect.c: Stop using qemu_oom_check()
In reply to Peter MaydellQuoted 36 lines · Peter Maydell
qemu_oom_check()is a function which essentially says "if you pass me a NULL pointer then print a message thenabort()". On POSIX systems the message includes strerror(errno); on Windows it includes theGetLastError()error value printed as an integer.Other than in the implementation of
qemu_memalign(), we use this function only inhw/usb/redirect.c, for three checks:* on a call to
usbredirparser_create()
* on a call tousberedirparser_serialize()
* on a call tomalloc()The usbredir library API functions make no guarantees that they will set errno on errors, let alone that they might set the Windows-specific GetLastError string.
malloc()is documented as setting errno, not GetLastError -- and in any case the only thing it might set errno to is ENOMEM. Soqemu_oom_check()isn't the right thing for any of these. Replace them with straightforward error-checking code. This will allow us to get rid ofqemu_oom_check().Signed-off-by Peter Maydell <[email protected]>---
I have left all of these errors as fatal, since that's what they were previously. Possibly somebody with a better understanding of the usbredir code might be able to make them theoretically non-fatal, but we make malloc failures generally fatal anyway.--- hw/usb/redirect.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)hw/usb/redirect.c +0
-0
hw/usb/redirect.c static void usbredir_create_parser(USBRedirDevice *dev) · line 1245
Quoted 8 lines
hw/usb/redirect.c +4
-1@@ -1239,7 +1239,11 @@ static void usbredir_create_parser(USBRedirDevice *dev)DPRINTF ("creating usbredirparser\n" ); - dev->parser =qemu_oom_check (usbredirparser_create ()); + dev->parser =usbredirparser_create (); +if (!dev->parser) { +error_report ("usbredirparser_create() failed" ); +exit (1 );
Is exit(EXIT_FAILURE) worth using in this file? We have an inconsistent history of a magic number vs. a named constant, so either way,
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH 1/9] hw/usb/redirect.c: Stop using qemu_oom_check()
Quoted 3 lines · Eric Blake
Is
exit(EXIT_FAILURE)worth using in this file? We have an inconsistent history of a magic number vs. a named constant, so either way,
I'm not a huge fan of EXIT_FAILURE, I think it tends to obscure more than it helps. We have rather more 'exit(1)' than 'exit(EXIT_FAILURE)' in the codebase.
-- PMM
PATCH 2/9 util: Make qemu_oom_check() a static function
In reply to Peter MaydellThe qemu_oom_check() function, which we define in both oslib-posix.c and oslib-win32.c, is now used only locally in that file; make it static.
--- include/qemu-common.h | 2 -- util/oslib-posix.c | 2 +- util/oslib-win32.c | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-)
include/qemu-common.h +0 -2
@@ -26,8 +26,6 @@int qemu_main (int argc,char **argv,char **envp);#endif -void *qemu_oom_check (void *ptr); - ssize_t qemu_write_full(int fd, const void *buf, size_t count) QEMU_WARN_UNUSED_RESULT;
util/oslib-posix.c +1 -1
@@ -199,7 +199,7 @@ fail_close:return false; } -void *qemu_oom_check (void *ptr) +static void *qemu_oom_check (void *ptr) {if (ptr ==NULL ) {fprintf (stderr,"Failed to allocate memory: %s\n" ,strerror (errno));
util/oslib-win32.c +1 -1
@@ -44,7 +44,7 @@/* this must come after including "trace.h" */ #include <shlobj.h> -void *qemu_oom_check (void *ptr) +static void *qemu_oom_check (void *ptr) {if (ptr ==NULL ) {fprintf (stderr,"Failed to allocate memory: %lu\n" ,GetLastError ());
Signature · 1 line
2.25.1
Re: [PATCH 2/9] util: Make qemu_oom_check() a static function
Quoted 10 lines · Peter Maydell
The
qemu_oom_check()function, which we define in bothoslib-posix.candoslib-win32.c, is now used only locally in that file; make it static.Signed-off-by Peter Maydell<[email protected]>--- include/qemu-common.h | 2 -- util/oslib-posix.c | 2 +- util/oslib-win32.c | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-)
r~
Re: [PATCH 2/9] util: Make qemu_oom_check() a static function
In reply to Peter MaydellQuoted 10 lines · Peter Maydell
The
qemu_oom_check()function, which we define in bothoslib-posix.candoslib-win32.c, is now used only locally in that file; make it static.Signed-off-by Peter Maydell <[email protected]>--- include/qemu-common.h | 2 -- util/oslib-posix.c | 2 +- util/oslib-win32.c | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-)
PATCH 3/9 util: Unify implementations of qemu_memalign()
In reply to Peter MaydellWe implement qemu_memalign() in both oslib-posix.c and oslib-win32.c, but the two versions are essentially the same: they call qemu_try_memalign(), and abort() after printing an error message if it fails. The only difference is that the win32 version prints the GetLastError() value whereas the POSIX version prints strerror(errno). However, this is a bug in the win32 version: in commit dfbd0b873a85021 in 2020 we changed the implementation of qemu_try_memalign() from using VirtualAlloc() (which sets the GetLastError() value) to using _aligned_malloc() (which sets errno), but didn't update the error message to match.
Replace the two separate functions with a single version in a new memalign.c file, which drops the unnecessary extra qemu_oom_check() function and instead prints a more useful message including the requested size and alignment as well as the errno string.
--- util/memalign.c | 39 +++++++++++++++++++++++++++++++++++++++ util/oslib-posix.c | 14 -------------- util/oslib-win32.c | 14 -------------- util/meson.build | 1 + 4 files changed, 40 insertions(+), 28 deletions(-) create mode 100644 util/memalign.c
util/memalign.c new file +39 -0
@@ -0,0 +1,39 @@ +/* +* memalign.c: Allocate an aligned memory region +* +* Copyright (c) 2003-2008 Fabrice Bellard +* Copyright (c) 2010-2016 Red Hat, Inc. +* Copyright (c) 2022 Linaro Ltd +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu/osdep.h" + +void *qemu_memalign (size_t alignment,size_t size) +{ +void *p =qemu_try_memalign (alignment, size); +if (p) { +return p; + } +fprintf (stderr, +"qemu_memalign: failed to allocate %zu bytes at alignment %zu: %s\n" , + size, alignment,strerror (errno)); +abort (); +}
util/oslib-posix.c +0 -14
@@ -199,15 +199,6 @@ fail_close:return false; } -static void *qemu_oom_check (void *ptr) -{ -if (ptr ==NULL ) { -fprintf (stderr,"Failed to allocate memory: %s\n" ,strerror (errno)); -abort (); - } -return ptr; -} -void *qemu_try_memalign (size_t alignment,size_t size) {void *ptr;
@@ -234,11 +225,6 @@ void *qemu_try_memalign(size_t alignment, size_t size)return ptr; } -void *qemu_memalign (size_t alignment,size_t size) -{ -return qemu_oom_check (qemu_try_memalign (alignment, size)); -} -/* alloc shared memory pages */ void *qemu_anon_ram_alloc (size_t size,uint64_t *alignment,bool shared,bool noreserve)
util/oslib-win32.c +0 -14
@@ -44,15 +44,6 @@/* this must come after including "trace.h" */ #include <shlobj.h> -static void *qemu_oom_check (void *ptr) -{ -if (ptr ==NULL ) { -fprintf (stderr,"Failed to allocate memory: %lu\n" ,GetLastError ()); -abort (); - } -return ptr; -} -void *qemu_try_memalign (size_t alignment,size_t size) {void *ptr;
@@ -68,11 +59,6 @@ void *qemu_try_memalign(size_t alignment, size_t size)return ptr; } -void *qemu_memalign (size_t alignment,size_t size) -{ -return qemu_oom_check (qemu_try_memalign (alignment, size)); -} -static int get_allocation_granularity (void ) {SYSTEM_INFO system_info;
util/meson.build +1 -0
@@ -51,6 +51,7 @@ util_ss.add(when: 'CONFIG_POSIX', if_true: files('drm.c')) util_ss.add(files('guest-random.c')) util_ss.add(files('yank.c')) util_ss.add(files('int128.c')) +util_ss.add(files('memalign.c')) if have_user util_ss.add(files('selfmap.c'))
Signature · 1 line
2.25.1
Re: [PATCH 3/9] util: Unify implementations of qemu_memalign()
Quoted 16 lines · Peter Maydell
We implement
qemu_memalign()in bothoslib-posix.candoslib-win32.c, but the two versions are essentially the same: they callqemu_try_memalign(), andabort()after printing an error message if it fails. The only difference is that the win32 version prints theGetLastError()value whereas the POSIX version prints strerror(errno). However, this is a bug in the win32 version: in commitdfbd0b873a85021in 2020 we changed the implementation ofqemu_try_memalign()from usingVirtualAlloc()(which sets theGetLastError()value) to using_aligned_malloc()(which sets errno), but didn't update the error message to match.Replace the two separate functions with a single version in a new
memalign.cfile, which drops the unnecessary extraqemu_oom_check()function and instead prints a more useful message including the requested size and alignment as well as the errno string.
Quoted 8 lines
Signed-off-by Peter Maydell<[email protected]>--- util/memalign.c | 39 +++++++++++++++++++++++++++++++++++++++ util/oslib-posix.c | 14 -------------- util/oslib-win32.c | 14 -------------- util/meson.build | 1 + 4 files changed, 40 insertions(+), 28 deletions(-)create mode 100644
util/memalign.c
r~
PATCH 4/9 util/oslib-win32: Return NULL on qemu_try_memalign() with zero size
In reply to Peter MaydellCurrently if qemu_try_memalign() is asked to allocate 0 bytes, we assert. Instead return NULL; this is in line with the posix_memalign() API, and is valid to pass to _aligned_free() (which will do nothing).
This change is a preparation for sharing the qemu_try_memalign() code between Windows and POSIX -- at the moment only the Windows version has the assert that size != 0.
--- util/oslib-win32.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
util/oslib-win32.c +5 -2
@@ -48,13 +48,16 @@ void *qemu_try_memalign(size_t alignment, size_t size) {void *ptr; -g_assert (size !=0 );if (alignment <sizeof (void *)) { alignment =sizeof (void *); }else {g_assert (is_power_of_2 (alignment)); } - ptr =_aligned_malloc (size, alignment); +if (size) { + ptr =_aligned_malloc (size, alignment); + }else { + ptr =NULL ; + }trace_qemu_memalign (alignment, size, ptr);return ptr; }
Signature · 1 line
2.25.1
Re: [PATCH 4/9] util/oslib-win32: Return NULL on qemu_try_memalign() with zero size
Quoted 4 lines · Peter Maydell
Currently if
qemu_try_memalign()is asked to allocate 0 bytes, we assert. Instead return NULL; this is in line with theposix_memalign()API, and is valid to pass to_aligned_free()(which will do nothing).
Quoted 8 lines
This change is a preparation for sharing the
qemu_try_memalign()code between Windows and POSIX -- at the moment only the Windows version has the assert that size != 0.Signed-off-by Peter Maydell<[email protected]>--- util/oslib-win32.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
That would be my fault, I believe.
r~
Re: [PATCH 4/9] util/oslib-win32: Return NULL on qemu_try_memalign() with zero size
In reply to Peter MaydellQuoted 23 lines · Peter Maydell
Currently if
qemu_try_memalign()is asked to allocate 0 bytes, we assert. Instead return NULL; this is in line with theposix_memalign()API, and is valid to pass to_aligned_free()(which will do nothing).This change is a preparation for sharing the
qemu_try_memalign()code between Windows and POSIX -- at the moment only the Windows version has the assert that size != 0.Signed-off-by Peter Maydell <[email protected]>--- util/oslib-win32.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)util/oslib-win32.c +0
-1@@ -48,13 +48,16 @@ void *qemu_try_memalign(size_t alignment, size_t size) {void *ptr; -g_assert (size !=0 );if (alignment <sizeof (void *)) {
util/oslib-win32.c void *qemu_try_memalign(size_t alignment, size_t size) · line 60
Quoted 10 lines
util/oslib-win32.c +5
-1alignment =sizeof (void *); }else {g_assert (is_power_of_2 (alignment)); } - ptr =_aligned_malloc (size, alignment); +if (size) { + ptr =_aligned_malloc (size, alignment); + }else { + ptr =NULL ; + }
Oh, should we set errno to something here? Otherwise a random value will be used by qemu_memalign.
r~
Quoted 3 lines
util/oslib-win32.c +0
-0trace_qemu_memalign (alignment, size, ptr);return ptr; }
Re: [PATCH 4/9] util/oslib-win32: Return NULL on qemu_try_memalign() with zero size
Quoted 23 lines · Richard Henderson
Quoted 21 lines · Peter Maydell
Currently if
qemu_try_memalign()is asked to allocate 0 bytes, we assert. Instead return NULL; this is in line with theposix_memalign()API, and is valid to pass to_aligned_free()(which will do nothing).This change is a preparation for sharing the
qemu_try_memalign()code between Windows and POSIX -- at the moment only the Windows version has the assert that size != 0.Signed-off-by Peter Maydell <[email protected]>--- util/oslib-win32.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)util/oslib-win32.c +0
-0@@ -48,13 +48,16 @@ void *qemu_try_memalign(size_t alignment, size_t size) {void *ptr;
Quoted 15 lines
util/oslib-win32.c void *qemu_try_memalign(size_t alignment, size_t size) · line 60
Quoted 12 lines
util/oslib-win32.c +5
-2-g_assert (size !=0 );if (alignment <sizeof (void *)) { alignment =sizeof (void *); }else {g_assert (is_power_of_2 (alignment)); } - ptr =_aligned_malloc (size, alignment); +if (size) { + ptr =_aligned_malloc (size, alignment); + }else { + ptr =NULL ; + }Oh, should we set errno to something here? Otherwise a random value will be used by
qemu_memalign.
Yeah, I guess so, though the errno to use isn't obvious. Maybe EINVAL?
The alternative would be to try to audit all the callsites to confirm they don't ever try to allocate 0 bytes and then have the assert for both Windows and POSIX versions...
-- PMM
Re: [PATCH 4/9] util/oslib-win32: Return NULL on qemu_try_memalign() with zero size
Quoted 6 lines · Peter Maydell
Quoted 6 lines
Quoted 5 lines
util/oslib-win32.c +5
-0+if (size) { + ptr =_aligned_malloc (size, alignment); + }else { + ptr =NULL ; + }
Quoted 8 lines
Quoted 2 lines
Oh, should we set errno to something here? Otherwise a random value will be used by
qemu_memalign.Yeah, I guess so, though the errno to use isn't obvious. Maybe EINVAL?
The alternative would be to try to audit all the callsites to confirm they don't ever try to allocate 0 bytes and then have the assert for both Windows and POSIX versions...
Alternately, force size == 1, so that we always get a non-NULL value that can be freed. That's a change on the POSIX side as well, of course.
r~
Re: [PATCH 4/9] util/oslib-win32: Return NULL on qemu_try_memalign() with zero size
Quoted 11 lines · Richard Henderson
Quoted 9 lines · Peter Maydell
Quoted 8 lines
util/oslib-win32.c void *qemu_try_memalign(size_t alignment, size_t size) · line 60
Quoted 5 lines
util/oslib-win32.c +5
-0+if (size) { + ptr =_aligned_malloc (size, alignment); + }else { + ptr =NULL ; + }Oh, should we set errno to something here? Otherwise a random value will be used by
qemu_memalign.
Quoted 8 lines
Quoted 5 lines
Yeah, I guess so, though the errno to use isn't obvious. Maybe EINVAL?
The alternative would be to try to audit all the callsites to confirm they don't ever try to allocate 0 bytes and then have the assert for both Windows and POSIX versions...
Alternately, force size == 1, so that we always get a non-NULL value that can be freed. That's a change on the POSIX side as well, of course.
Yes, I had a look at what actual malloc() implementations tend to do, and the answer seems to be that forcing size to 1 gives less weird behaviour for the application. So here that would be
if (size == 0) {
size++;
}
ptr = _aligned_malloc(size, alignment);We don't need to do anything on the POSIX side (unless we want to enforce consistency of handling the size==0 case).
I'd quite like to get this series in before softfreeze (though mostly just for my personal convenience so it's not hanging around as a loose end I have to come back to after we reopen for 7.1). Does anybody object if I squash in that change and put this in a pullrequest, or would you prefer to see a v2 series first?
thanks
-- PMM
Re: [PATCH 4/9] util/oslib-win32: Return NULL on qemu_try_memalign() with zero size
Quoted 7 lines · Peter Maydell
Quoted 2 lines
Alternately, force size == 1, so that we always get a non-NULL value that can be freed. That's a change on the POSIX side as well, of course.
Yes, I had a look at what actual
malloc()implementations tend to do, and the answer seems to be that forcing size to 1 gives less weird behaviour for the application. So here that would be
Quoted 7 lines
if (size == 0) { size++; } ptr = _aligned_malloc(size, alignment);We don't need to do anything on the POSIX side (unless we want to enforce consistency of handling the size==0 case).
I would do this unconditionally. The POSIX manpage says that either NULL or a unique pointer is a valid return value into *memptr here for size == 0. What we want in our caller is NULL if and only if error.
Quoted 5 lines
I'd quite like to get this series in before softfreeze (though mostly just for my personal convenience so it's not hanging around as a loose end I have to come back to after we reopen for 7.1). Does anybody object if I squash in that change and put this in a pullrequest, or would you prefer to see a v2 series first?
I'm happy with a squash and PR.
r~
Re: [PATCH 4/9] util/oslib-win32: Return NULL on qemu_try_memalign() with zero size
Quoted 14 lines · Richard Henderson
Quoted 12 lines · Peter Maydell
Quoted 2 lines
Alternately, force size == 1, so that we always get a non-NULL value that can be freed. That's a change on the POSIX side as well, of course.
Yes, I had a look at what actual
malloc()implementations tend to do, and the answer seems to be that forcing size to 1 gives less weird behaviour for the application. So here that would beif (size == 0) { size++; } ptr = _aligned_malloc(size, alignment);
Quoted 6 lines
Quoted 2 lines
We don't need to do anything on the POSIX side (unless we want to enforce consistency of handling the size==0 case).
I would do this unconditionally. The POSIX manpage says that either NULL or a unique pointer is a valid return value into *memptr here for size == 0. What we want in our caller is NULL if and only if error.
Mm, I guess. I was trying to avoid changing the POSIX-side behaviour, but this seems safe enough.
-- PMM
PATCH 5/9 meson.build: Don't misdetect posix_memalign() on Windows
In reply to Peter MaydellCurrently we incorrectly think that posix_memalign() exists on Windows. This is because of a combination of:
* the msys2/mingw toolchain/libc claim to have a __builtin_posix_memalign when there isn't a builtin of that name
* meson will assume that if you have a __builtin_foo that counts for has_function('foo')
Specifying a specific include file via prefix: causes meson to not treat builtins as sufficient and actually look for the function itself; see this meson pull request which added that as the official way to get the right answer:
https://github.com/mesonbuild/meson/pull/1150
Currently this misdectection doesn't cause problems because we only use CONFIG_POSIX_MEMALIGN in oslib-posix.c; however that will change in a following commit.
--- meson.build | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
meson.build +3 -1
@@ -1606,7 +1606,9 @@ config_host_data.set('CONFIG_CLOCK_ADJTIME', cc.has_function('clock_adjtime')) config_host_data.set('CONFIG_DUP3', cc.has_function('dup3')) config_host_data.set('CONFIG_FALLOCATE', cc.has_function('fallocate')) config_host_data.set('CONFIG_POSIX_FALLOCATE', cc.has_function('posix_fallocate')) -config_host_data.set('CONFIG_POSIX_MEMALIGN', cc.has_function('posix_memalign')) +# Note that we need to specify prefix: here to avoid incorrectly +# thinking that Windows has posix_memalign() +config_host_data.set('CONFIG_POSIX_MEMALIGN', cc.has_function('posix_memalign', prefix: '#include <stdlib.h>')) config_host_data.set('CONFIG_PPOLL', cc.has_function('ppoll')) config_host_data.set('CONFIG_PREADV', cc.has_function('preadv', prefix: '#include <sys/uio.h>')) config_host_data.set('CONFIG_SEM_TIMEDWAIT', cc.has_function('sem_timedwait', dependencies: threads))
Signature · 1 line
2.25.1
Re: [PATCH 5/9] meson.build: Don't misdetect posix_memalign() on Windows
Quoted 14 lines · Peter Maydell
Currently we incorrectly think that
posix_memalign()exists on Windows. This is because of a combination of:* the msys2/mingw toolchain/libc claim to have a
__builtin_posix_memalignwhen there isn't a builtin of that name
* meson will assume that if you have a__builtin_foothat counts forhas_function('foo')Specifying a specific include file via prefix: causes meson to not treat builtins as sufficient and actually look for the function itself; see this meson pull request which added that as the official way to get the right answer:
https://github.com/mesonbuild/meson/pull/1150
Quoted 8 lines
Currently this misdectection doesn't cause problems because we only use
CONFIG_POSIX_MEMALIGNinoslib-posix.c; however that will change in a following commit.Signed-off-by Peter Maydell<[email protected]>--- meson.build | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
r~
PATCH 6/9 util: Share qemu_try_memalign() implementation between POSIX and Windows
In reply to Peter MaydellThe qemu_try_memalign() functions for POSIX and Windows used to be significantly different, but these days they are identical except for the actual allocation function called, and the POSIX version already has to have ifdeffery for different allocation functions.
Move to a single implementation in memalign.c, which uses the Windows _aligned_malloc if we detect that function in meson.
--- meson.build | 1 + util/memalign.c | 33 +++++++++++++++++++++++++++++++++ util/oslib-posix.c | 26 -------------------------- util/oslib-win32.c | 18 ------------------ 4 files changed, 34 insertions(+), 44 deletions(-)
meson.build +1 -0
@@ -1609,6 +1609,7 @@ config_host_data.set('CONFIG_POSIX_FALLOCATE', cc.has_function('posix_fallocate' # Note that we need to specify prefix: here to avoid incorrectly # thinking that Windows has posix_memalign() config_host_data.set('CONFIG_POSIX_MEMALIGN', cc.has_function('posix_memalign', prefix: '#include <stdlib.h>')) +config_host_data.set('CONFIG_ALIGNED_MALLOC', cc.has_function('_aligned_malloc')) config_host_data.set('CONFIG_PPOLL', cc.has_function('ppoll')) config_host_data.set('CONFIG_PREADV', cc.has_function('preadv', prefix: '#include <sys/uio.h>')) config_host_data.set('CONFIG_SEM_TIMEDWAIT', cc.has_function('sem_timedwait', dependencies: threads))
util/memalign.c +33 -0
@@ -26,6 +26,39 @@#include "qemu/osdep.h" +void *qemu_try_memalign (size_t alignment,size_t size) +{ +void *ptr; + +if (alignment <sizeof (void *)) { + alignment =sizeof (void *); + }else { +g_assert (is_power_of_2 (alignment)); + } + +#if defined(CONFIG_POSIX_MEMALIGN ) +int ret; + ret =posix_memalign (&ptr, alignment, size); +if (ret !=0 ) { + errno = ret; + ptr =NULL ; + } +#elif defined(CONFIG_ALIGNED_MALLOC ) +/* _aligned_malloc() fails on 0 size */ +if (size) { + ptr =_aligned_malloc (size, alignment); + }else { + ptr =NULL ; + } +#elif defined(CONFIG_BSD ) + ptr =valloc (size); +#else + ptr =memalign (alignment, size); +#endif +trace_qemu_memalign (alignment, size, ptr); +return ptr; +} +void *qemu_memalign (size_t alignment,size_t size) {void *p =qemu_try_memalign (alignment, size);
util/oslib-posix.c +0 -26
@@ -199,32 +199,6 @@ fail_close:return false; } -void *qemu_try_memalign (size_t alignment,size_t size) -{ -void *ptr; - -if (alignment <sizeof (void *)) { - alignment =sizeof (void *); - }else { -g_assert (is_power_of_2 (alignment)); - } - -#if defined(CONFIG_POSIX_MEMALIGN ) -int ret; - ret =posix_memalign (&ptr, alignment, size); -if (ret !=0 ) { - errno = ret; - ptr =NULL ; - } -#elif defined(CONFIG_BSD ) - ptr =valloc (size); -#else - ptr =memalign (alignment, size); -#endif -trace_qemu_memalign (alignment, size, ptr); -return ptr; -} -/* alloc shared memory pages */ void *qemu_anon_ram_alloc (size_t size,uint64_t *alignment,bool shared,bool noreserve)
util/oslib-win32.c +0 -18
@@ -44,24 +44,6 @@/* this must come after including "trace.h" */ #include <shlobj.h> -void *qemu_try_memalign (size_t alignment,size_t size) -{ -void *ptr; - -if (alignment <sizeof (void *)) { - alignment =sizeof (void *); - }else { -g_assert (is_power_of_2 (alignment)); - } -if (size) { - ptr =_aligned_malloc (size, alignment); - }else { - ptr =NULL ; - } -trace_qemu_memalign (alignment, size, ptr); -return ptr; -} -static int get_allocation_granularity (void ) {SYSTEM_INFO system_info;
Signature · 1 line
2.25.1
Re: [PATCH 6/9] util: Share qemu_try_memalign() implementation between POSIX and Windows
Quoted 8 lines · Peter Maydell
The
qemu_try_memalign()functions for POSIX and Windows used to be significantly different, but these days they are identical except for the actual allocation function called, and the POSIX version already has to have ifdeffery for different allocation functions.Move to a single implementation in
memalign.c, which uses the Windows_aligned_mallocif we detect that function in meson.
Quoted 7 lines
Signed-off-by Peter Maydell<[email protected]>--- meson.build | 1 + util/memalign.c | 33 +++++++++++++++++++++++++++++++++ util/oslib-posix.c | 26 -------------------------- util/oslib-win32.c | 18 ------------------ 4 files changed, 34 insertions(+), 44 deletions(-)
r~
PATCH 7/9 util: Use meson checks for valloc() and memalign() presence
In reply to Peter MaydellInstead of assuming that all CONFIG_BSD have valloc() and anything else is memalign(), explicitly check for those functions in meson.build and use the "is the function present" define. Tests for specific functionality are better than which-OS checks; this also lets us give a helpful error message if somehow there's no usable function present.
--- meson.build | 2 ++ util/memalign.c | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-)
meson.build +2 -0
@@ -1610,6 +1610,8 @@ config_host_data.set('CONFIG_POSIX_FALLOCATE', cc.has_function('posix_fallocate' # thinking that Windows has posix_memalign() config_host_data.set('CONFIG_POSIX_MEMALIGN', cc.has_function('posix_memalign', prefix: '#include <stdlib.h>')) config_host_data.set('CONFIG_ALIGNED_MALLOC', cc.has_function('_aligned_malloc')) +config_host_data.set('CONFIG_VALLOC', cc.has_function('valloc')) +config_host_data.set('CONFIG_MEMALIGN', cc.has_function('memalign')) config_host_data.set('CONFIG_PPOLL', cc.has_function('ppoll')) config_host_data.set('CONFIG_PREADV', cc.has_function('preadv', prefix: '#include <sys/uio.h>')) config_host_data.set('CONFIG_SEM_TIMEDWAIT', cc.has_function('sem_timedwait', dependencies: threads))
util/memalign.c +6 -2
@@ -25,6 +25,8 @@*/ #include "qemu/osdep.h" +#include "qemu/host-utils.h" +#include "trace.h" void *qemu_try_memalign (size_t alignment,size_t size) {
@@ -50,10 +52,12 @@ void *qemu_try_memalign(size_t alignment, size_t size) } else { ptr =NULL ; } -#elif defined(CONFIG_BSD) +#elif defined(CONFIG_VALLOC) ptr =valloc (size); -#else +#elif defined(CONFIG_MEMALIGN) ptr =memalign (alignment, size); +#else +#error No function to allocate aligned memory available#endif trace_qemu_memalign (alignment, size, ptr);return ptr;
Signature · 1 line
2.25.1
Re: [PATCH 7/9] util: Use meson checks for valloc() and memalign() presence
Quoted 7 lines · Peter Maydell
Instead of assuming that all
CONFIG_BSDhavevalloc()and anything else ismemalign(), explicitly check for those functions in meson.build and use the "is the function present" define. Tests for specific functionality are better than which-OS checks; this also lets us give a helpful error message if somehow there's no usable function present.
Quoted 5 lines
Signed-off-by Peter Maydell<[email protected]>--- meson.build | 2 ++ util/memalign.c | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-)
r~
Re: [PATCH 7/9] util: Use meson checks for valloc() and memalign() presence
In reply to Peter MaydellQuoted 7 lines · Peter Maydell
Instead of assuming that all
CONFIG_BSDhavevalloc()and anything else ismemalign(), explicitly check for those functions in meson.build and use the "is the function present" define. Tests for specific functionality are better than which-OS checks; this also lets us give a helpful error message if somehow there's no usable function present.
Quoted 5 lines
Signed-off-by Peter Maydell <[email protected]>--- meson.build | 2 ++ util/memalign.c | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-)
PATCH 8/9 util: Put qemu_vfree() in memalign.c
In reply to Peter Maydellqemu_vfree() is the companion free function to qemu_memalign(); put it in memalign.c so the allocation and free functions are together.
--- util/memalign.c | 11 +++++++++++ util/oslib-posix.c | 6 ------ util/oslib-win32.c | 6 ------ 3 files changed, 11 insertions(+), 12 deletions(-)
util/memalign.c +11 -0
@@ -74,3 +74,14 @@ void *qemu_memalign(size_t alignment, size_t size) size, alignment, strerror(errno));abort (); } + +void qemu_vfree (void *ptr) +{ +trace_qemu_vfree (ptr); +#if !defined(CONFIG_POSIX_MEMALIGN ) && defined(CONFIG_ALIGNED_MALLOC ) +/* Only Windows _aligned_malloc needs a special free function */ +_aligned_free (ptr); +#else +free (ptr); +#endif +}
util/oslib-posix.c +0 -6
@@ -220,12 +220,6 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared,return ptr; } -void qemu_vfree (void *ptr) -{ -trace_qemu_vfree (ptr); -free (ptr); -} -void qemu_anon_ram_free (void *ptr,size_t size) {trace_qemu_anon_ram_free (ptr, size);
util/oslib-win32.c +0 -6
@@ -75,12 +75,6 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared,return ptr; } -void qemu_vfree (void *ptr) -{ -trace_qemu_vfree (ptr); -_aligned_free (ptr); -} -void qemu_anon_ram_free (void *ptr,size_t size) {trace_qemu_anon_ram_free (ptr, size);
Signature · 1 line
2.25.1
Re: [PATCH 8/9] util: Put qemu_vfree() in memalign.c
Quoted 9 lines · Peter Maydell
qemu_vfree()is the companion free function toqemu_memalign(); put it inmemalign.cso the allocation and free functions are together.Signed-off-by Peter Maydell<[email protected]>--- util/memalign.c | 11 +++++++++++ util/oslib-posix.c | 6 ------ util/oslib-win32.c | 6 ------ 3 files changed, 11 insertions(+), 12 deletions(-)
r~
PATCH 9/9 osdep: Move memalign-related functions to their own header
In reply to Peter MaydellMove the various memalign-related functions out of osdep.h and into their own header, which we include only where they are used. While we're doing this, add some brief documentation comments.
--- include/qemu/memalign.h | 61 ++++++++++++++++++++++++++++++++++ include/qemu/osdep.h | 18 ---------- block/blkverify.c | 1 + block/block-copy.c | 1 + block/commit.c | 1 + block/crypto.c | 1 + block/dmg.c | 1 + block/export/fuse.c | 1 + block/file-posix.c | 1 + block/io.c | 1 + block/mirror.c | 1 + block/nvme.c | 1 + block/parallels-ext.c | 1 + block/parallels.c | 1 + block/qcow.c | 1 + block/qcow2-cache.c | 1 + block/qcow2-cluster.c | 1 + block/qcow2-refcount.c | 1 + block/qcow2-snapshot.c | 1 + block/qcow2.c | 1 + block/qed-l2-cache.c | 1 + block/qed-table.c | 1 + block/qed.c | 1 + block/quorum.c | 1 + block/raw-format.c | 1 + block/vdi.c | 1 + block/vhdx-log.c | 1 + block/vhdx.c | 1 + block/vmdk.c | 1 + block/vpc.c | 1 + block/win32-aio.c | 1 + hw/block/dataplane/xen-block.c | 1 + hw/block/fdc.c | 1 + hw/ide/core.c | 1 + hw/ppc/spapr.c | 1 + hw/ppc/spapr_softmmu.c | 1 + hw/scsi/scsi-disk.c | 1 + hw/tpm/tpm_ppi.c | 2 +- nbd/server.c | 1 + net/l2tpv3.c | 2 +- plugins/loader.c | 1 + qemu-img.c | 1 + qemu-io-cmds.c | 1 + qom/object.c | 1 + softmmu/physmem.c | 1 + target/i386/hvf/hvf.c | 1 + target/i386/kvm/kvm.c | 1 + tcg/region.c | 1 + tests/bench/atomic_add-bench.c | 1 + tests/bench/qht-bench.c | 1 + util/atomic64.c | 1 + util/memalign.c | 1 + util/qht.c | 1 + 53 files changed, 112 insertions(+), 20 deletions(-) create mode 100644 include/qemu/memalign.h
include/qemu/memalign.h new file +61 -0
@@ -0,0 +1,61 @@ +/* +* Allocation and free functions for aligned memory +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ + +#ifndef QEMU_MEMALIGN_H +#define QEMU_MEMALIGN_H + +/** +* qemu_try_memalign: Allocate aligned memory +* @alignment: required alignment, in bytes +* @size: size of allocation, in bytes +* +* Allocate memory on an aligned boundary (i.e. the returned +* address will be an exact multiple of @alignment). +* @alignment must be a power of 2, or the function will assert(). +* On success, returns allocated memory; on failure, returns NULL. +* +* The memory allocated through this function must be freed via +* qemu_vfree() (and not via free()). +*/ +void *qemu_try_memalign (size_t alignment,size_t size); +/** +* qemu_memalign: Allocate aligned memory, without failing +* @alignment: required alignment, in bytes +* @size: size of allocation, in bytes +* +* Allocate memory in the same way as qemu_try_memalign(), but +* abort() with an error message if the memory allocation fails. +* +* The memory allocated through this function must be freed via +* qemu_vfree() (and not via free()). +*/ +void *qemu_memalign (size_t alignment,size_t size); +/** +* qemu_vfree: Free memory allocated through qemu_memalign +* @ptr: memory to free +* +* This function must be used to free memory allocated via qemu_memalign() +* or qemu_try_memalign(). (Using the wrong free function will cause +* subtle bugs on Windows hosts.) +*/ +void qemu_vfree (void *ptr); +/* +* It's an analog of GLIB's g_autoptr_cleanup_generic_gfree(), used to define +* g_autofree macro. +*/ +static inline void qemu_cleanup_generic_vfree (void *p) +{ +void **pp = (void **)p; +qemu_vfree (*pp); +} + +/* +* Analog of g_autofree, but qemu_vfree is called on cleanup instead of g_free. +*/ +#define QEMU_AUTO_VFREE __attribute__((cleanup(qemu_cleanup_generic_vfree))) + +#endif
include/qemu/osdep.h +0 -18
@@ -379,28 +379,10 @@ extern "C" {#endif int qemu_daemon (int nochdir,int noclose); -void *qemu_try_memalign (size_t alignment,size_t size); -void *qemu_memalign (size_t alignment,size_t size);void *qemu_anon_ram_alloc (size_t size,uint64_t *align,bool shared,bool noreserve); -void qemu_vfree (void *ptr);void qemu_anon_ram_free (void *ptr,size_t size); -/* -* It's an analog of GLIB's g_autoptr_cleanup_generic_gfree(), used to define -* g_autofree macro. -*/ -static inline void qemu_cleanup_generic_vfree (void *p) -{ -void **pp = (void **)p; -qemu_vfree (*pp); -} - -/* -* Analog of g_autofree, but qemu_vfree is called on cleanup instead of g_free. -*/ -#define QEMU_AUTO_VFREE __attribute__((cleanup(qemu_cleanup_generic_vfree))) -#ifdef _WIN32#define HAVE_CHARDEV_SERIAL 1#elif defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
block/blkverify.c +1 -0
@@ -16,6 +16,7 @@#include "qemu/cutils.h" #include "qemu/module.h" #include "qemu/option.h" +#include "qemu/memalign.h" typedef struct { BdrvChild *test_file;
block/block-copy.c +1 -0
@@ -22,6 +22,7 @@#include "qemu/coroutine.h" #include "block/aio_task.h" #include "qemu/error-report.h" +#include "qemu/memalign.h" #define BLOCK_COPY_MAX_COPY_RANGE (16 * MiB)#define BLOCK_COPY_MAX_BUFFER (1 * MiB)
block/commit.c +1 -0
@@ -20,6 +20,7 @@#include "qapi/error.h" #include "qapi/qmp/qerror.h" #include "qemu/ratelimit.h" +#include "qemu/memalign.h" #include "sysemu/block-backend.h" enum {
block/crypto.c +1 -0
@@ -31,6 +31,7 @@#include "qemu/module.h" #include "qemu/option.h" #include "qemu/cutils.h" +#include "qemu/memalign.h" #include "crypto.h" typedef struct BlockCrypto BlockCrypto ;
block/dmg.c +1 -0
@@ -27,6 +27,7 @@#include "qemu/bswap.h" #include "qemu/error-report.h" #include "qemu/module.h" +#include "qemu/memalign.h" #include "dmg.h" int (*dmg_uncompress_bz2)(char *next_in, unsigned int avail_in,
block/export/fuse.c +1 -0
@@ -19,6 +19,7 @@#define FUSE_USE_VERSION 31#include "qemu/osdep.h" +#include "qemu/memalign.h" #include "block/aio.h" #include "block/block.h" #include "block/export.h"
block/file-posix.c +1 -0
@@ -31,6 +31,7 @@#include "qemu/module.h" #include "qemu/option.h" #include "qemu/units.h" +#include "qemu/memalign.h" #include "trace.h" #include "block/thread-pool.h" #include "qemu/iov.h"
block/io.c +1 -0
@@ -32,6 +32,7 @@#include "block/coroutines.h" #include "block/write-threshold.h" #include "qemu/cutils.h" +#include "qemu/memalign.h" #include "qapi/error.h" #include "qemu/error-report.h" #include "qemu/main-loop.h"
block/mirror.c +1 -0
@@ -23,6 +23,7 @@#include "qapi/qmp/qerror.h" #include "qemu/ratelimit.h" #include "qemu/bitmap.h" +#include "qemu/memalign.h" #define MAX_IN_FLIGHT 16#define MAX_IO_BYTES (1 << 20)/* 1 Mb */
block/nvme.c +1 -0
@@ -21,6 +21,7 @@#include "qemu/module.h" #include "qemu/cutils.h" #include "qemu/option.h" +#include "qemu/memalign.h" #include "qemu/vfio-helpers.h" #include "block/block_int.h" #include "sysemu/replay.h"
block/parallels-ext.c +1 -0
@@ -29,6 +29,7 @@#include "parallels.h" #include "crypto/hash.h" #include "qemu/uuid.h" +#include "qemu/memalign.h" #define PARALLELS_FORMAT_EXTENSION_MAGIC 0xAB234CEF23DCEA87ULL
block/parallels.c +1 -0
@@ -41,6 +41,7 @@#include "qapi/qapi-visit-block-core.h" #include "qemu/bswap.h" #include "qemu/bitmap.h" +#include "qemu/memalign.h" #include "migration/blocker.h" #include "parallels.h"
block/qcow.c +1 -0
@@ -32,6 +32,7 @@#include "qemu/option.h" #include "qemu/bswap.h" #include "qemu/cutils.h" +#include "qemu/memalign.h" #include <zlib.h> #include "qapi/qmp/qdict.h" #include "qapi/qmp/qstring.h"
block/qcow2-cache.c +1 -0
@@ -23,6 +23,7 @@*/ #include "qemu/osdep.h" +#include "qemu/memalign.h" #include "qcow2.h" #include "trace.h"
block/qcow2-cluster.c +1 -0
@@ -28,6 +28,7 @@#include "qapi/error.h" #include "qcow2.h" #include "qemu/bswap.h" +#include "qemu/memalign.h" #include "trace.h" int qcow2_shrink_l1_table (BlockDriverState *bs,uint64_t exact_size)
block/qcow2-refcount.c +1 -0
@@ -28,6 +28,7 @@#include "qemu/range.h" #include "qemu/bswap.h" #include "qemu/cutils.h" +#include "qemu/memalign.h" #include "trace.h" static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size,
block/qcow2-snapshot.c +1 -0
@@ -29,6 +29,7 @@#include "qemu/bswap.h" #include "qemu/error-report.h" #include "qemu/cutils.h" +#include "qemu/memalign.h" static void qcow2_free_single_snapshot (BlockDriverState *bs,int i) {
block/qcow2.c +1 -0
@@ -38,6 +38,7 @@#include "qemu/option_int.h" #include "qemu/cutils.h" #include "qemu/bswap.h" +#include "qemu/memalign.h" #include "qapi/qobject-input-visitor.h" #include "qapi/qapi-visit-block-core.h" #include "crypto.h"
block/qed-l2-cache.c +1 -0
@@ -51,6 +51,7 @@*/ #include "qemu/osdep.h" +#include "qemu/memalign.h" #include "trace.h" #include "qed.h"
block/qed-table.c +1 -0
@@ -17,6 +17,7 @@#include "qemu/sockets.h" /* for EINPROGRESS on Windows */ #include "qed.h" #include "qemu/bswap.h" +#include "qemu/memalign.h" /* Called with table_lock held. */ static int coroutine_fn qed_read_table(BDRVQEDState *s, uint64_t offset,
block/qed.c +1 -0
@@ -20,6 +20,7 @@#include "qemu/main-loop.h" #include "qemu/module.h" #include "qemu/option.h" +#include "qemu/memalign.h" #include "trace.h" #include "qed.h" #include "sysemu/block-backend.h"
block/quorum.c +1 -0
@@ -17,6 +17,7 @@#include "qemu/cutils.h" #include "qemu/module.h" #include "qemu/option.h" +#include "qemu/memalign.h" #include "block/block_int.h" #include "block/coroutines.h" #include "block/qdict.h"
block/raw-format.c +1 -0
@@ -31,6 +31,7 @@#include "qapi/error.h" #include "qemu/module.h" #include "qemu/option.h" +#include "qemu/memalign.h" typedef struct BDRVRawState { uint64_t offset;
block/vdi.c +1 -0
@@ -64,6 +64,7 @@#include "qemu/coroutine.h" #include "qemu/cutils.h" #include "qemu/uuid.h" +#include "qemu/memalign.h" /* Code configuration options. */
block/vhdx-log.c +1 -0
@@ -23,6 +23,7 @@#include "block/block_int.h" #include "qemu/error-report.h" #include "qemu/bswap.h" +#include "qemu/memalign.h" #include "vhdx.h"
block/vhdx.c +1 -0
@@ -25,6 +25,7 @@#include "qemu/crc32c.h" #include "qemu/bswap.h" #include "qemu/error-report.h" +#include "qemu/memalign.h" #include "vhdx.h" #include "migration/blocker.h" #include "qemu/uuid.h"
block/vmdk.c +1 -0
@@ -33,6 +33,7 @@#include "qemu/module.h" #include "qemu/option.h" #include "qemu/bswap.h" +#include "qemu/memalign.h" #include "migration/blocker.h" #include "qemu/cutils.h" #include <zlib.h>
block/vpc.c +1 -0
@@ -33,6 +33,7 @@#include "migration/blocker.h" #include "qemu/bswap.h" #include "qemu/uuid.h" +#include "qemu/memalign.h" #include "qapi/qmp/qdict.h" #include "qapi/qobject-input-visitor.h" #include "qapi/qapi-visit-block-core.h"
block/win32-aio.c +1 -0
@@ -29,6 +29,7 @@#include "block/raw-aio.h" #include "qemu/event_notifier.h" #include "qemu/iov.h" +#include "qemu/memalign.h" #include <windows.h> #include <winioctl.h>
hw/block/dataplane/xen-block.c +1 -0
@@ -21,6 +21,7 @@#include "qemu/osdep.h" #include "qemu/error-report.h" #include "qemu/main-loop.h" +#include "qemu/memalign.h" #include "qapi/error.h" #include "hw/xen/xen_common.h" #include "hw/block/xen_blkif.h"
hw/block/fdc.c +1 -0
@@ -32,6 +32,7 @@#include "qapi/error.h" #include "qemu/error-report.h" #include "qemu/timer.h" +#include "qemu/memalign.h" #include "hw/irq.h" #include "hw/isa/isa.h" #include "hw/qdev-properties.h"
hw/ide/core.c +1 -0
@@ -30,6 +30,7 @@#include "qemu/main-loop.h" #include "qemu/timer.h" #include "qemu/hw-version.h" +#include "qemu/memalign.h" #include "sysemu/sysemu.h" #include "sysemu/blockdev.h" #include "sysemu/dma.h"
hw/ppc/spapr.c +1 -0
@@ -27,6 +27,7 @@#include "qemu/osdep.h" #include "qemu-common.h" #include "qemu/datadir.h" +#include "qemu/memalign.h" #include "qapi/error.h" #include "qapi/qapi-events-machine.h" #include "qapi/qapi-events-qdev.h"
hw/ppc/spapr_softmmu.c +1 -0
@@ -1,5 +1,6 @@#include "qemu/osdep.h" #include "qemu/cutils.h" +#include "qemu/memalign.h" #include "cpu.h" #include "helper_regs.h" #include "hw/ppc/spapr.h"
hw/scsi/scsi-disk.c +1 -0
@@ -26,6 +26,7 @@#include "qemu/main-loop.h" #include "qemu/module.h" #include "qemu/hw-version.h" +#include "qemu/memalign.h" #include "hw/scsi/scsi.h" #include "migration/qemu-file-types.h" #include "migration/vmstate.h"
hw/tpm/tpm_ppi.c +1 -1
@@ -12,7 +12,7 @@*/ #include "qemu/osdep.h" - +#include "qemu/memalign.h" #include "qapi/error.h" #include "sysemu/memory_mapping.h" #include "migration/vmstate.h"
nbd/server.c +1 -0
@@ -25,6 +25,7 @@#include "trace.h" #include "nbd-internal.h" #include "qemu/units.h" +#include "qemu/memalign.h" #define NBD_META_ID_BASE_ALLOCATION 0#define NBD_META_ID_ALLOCATION_DEPTH 1
net/l2tpv3.c +1 -1
@@ -34,7 +34,7 @@#include "qemu/sockets.h" #include "qemu/iov.h" #include "qemu/main-loop.h" - +#include "qemu/memalign.h" /* The buffer size needs to be investigated for optimum numbers and * optimum means of paging in on different systems. This size is
plugins/loader.c +1 -0
@@ -27,6 +27,7 @@#include "qemu/cacheinfo.h" #include "qemu/xxhash.h" #include "qemu/plugin.h" +#include "qemu/memalign.h" #include "hw/core/cpu.h" #include "exec/exec-all.h" #ifndef CONFIG_USER_ONLY
qemu-img.c +1 -0
@@ -42,6 +42,7 @@#include "qemu/module.h" #include "qemu/sockets.h" #include "qemu/units.h" +#include "qemu/memalign.h" #include "qom/object_interfaces.h" #include "sysemu/block-backend.h" #include "block/block_int.h"
qemu-io-cmds.c +1 -0
@@ -21,6 +21,7 @@#include "qemu/option.h" #include "qemu/timer.h" #include "qemu/cutils.h" +#include "qemu/memalign.h" #define CMD_NOFILE_OK 0x01
qom/object.c +1 -0
@@ -16,6 +16,7 @@#include "qom/object.h" #include "qom/object_interfaces.h" #include "qemu/cutils.h" +#include "qemu/memalign.h" #include "qapi/visitor.h" #include "qapi/string-input-visitor.h" #include "qapi/string-output-visitor.h"
softmmu/physmem.c +1 -0
@@ -42,6 +42,7 @@#include "qemu/config-file.h" #include "qemu/error-report.h" #include "qemu/qemu-print.h" +#include "qemu/memalign.h" #include "exec/memory.h" #include "exec/ioport.h" #include "sysemu/dma.h"
target/i386/hvf/hvf.c +1 -0
@@ -49,6 +49,7 @@#include "qemu/osdep.h" #include "qemu-common.h" #include "qemu/error-report.h" +#include "qemu/memalign.h" #include "sysemu/hvf.h" #include "sysemu/hvf_int.h"
target/i386/kvm/kvm.c +1 -0
@@ -37,6 +37,7 @@#include "qemu/main-loop.h" #include "qemu/config-file.h" #include "qemu/error-report.h" +#include "qemu/memalign.h" #include "hw/i386/x86.h" #include "hw/i386/apic.h" #include "hw/i386/apic_internal.h"
tcg/region.c +1 -0
@@ -26,6 +26,7 @@#include "qemu/units.h" #include "qemu/madvise.h" #include "qemu/mprotect.h" +#include "qemu/memalign.h" #include "qemu/cacheinfo.h" #include "qapi/error.h" #include "exec/exec-all.h"
tests/bench/atomic_add-bench.c +1 -0
@@ -2,6 +2,7 @@#include "qemu/thread.h" #include "qemu/host-utils.h" #include "qemu/processor.h" +#include "qemu/memalign.h" struct thread_info {uint64_t r;
tests/bench/qht-bench.c +1 -0
@@ -10,6 +10,7 @@#include "qemu/qht.h" #include "qemu/rcu.h" #include "qemu/xxhash.h" +#include "qemu/memalign.h" struct thread_stats {size_t rd;
util/atomic64.c +1 -0
@@ -8,6 +8,7 @@#include "qemu/atomic.h" #include "qemu/thread.h" #include "qemu/cacheinfo.h" +#include "qemu/memalign.h" #ifdef CONFIG_ATOMIC64 #error This file must only be compiled if !CONFIG_ATOMIC64
util/memalign.c +1 -0
@@ -26,6 +26,7 @@#include "qemu/osdep.h" #include "qemu/host-utils.h" +#include "qemu/memalign.h" #include "trace.h" void *qemu_try_memalign (size_t alignment,size_t size)
util/qht.c +1 -0
@@ -69,6 +69,7 @@#include "qemu/qht.h" #include "qemu/atomic.h" #include "qemu/rcu.h" +#include "qemu/memalign.h" //#define QHT_DEBUG
Signature · 1 line
2.25.1
Re: [PATCH 9/9] osdep: Move memalign-related functions to their own header
Quoted 6 lines · Peter Maydell
Move the various memalign-related functions out of
osdep.hand into their own header, which we include only where they are used. While we're doing this, add some brief documentation comments.Signed-off-by Peter Maydell<[email protected]>---
r~
Re: [PATCH 9/9] osdep: Move memalign-related functions to their own header
In reply to Peter MaydellQuoted 60 lines · Peter Maydell
Move the various memalign-related functions out of
osdep.hand into their own header, which we include only where they are used. While we're doing this, add some brief documentation comments.Signed-off-by Peter Maydell <[email protected]>--- include/qemu/memalign.h | 61 ++++++++++++++++++++++++++++++++++ include/qemu/osdep.h | 18 ---------- block/blkverify.c | 1 + block/block-copy.c | 1 + block/commit.c | 1 + block/crypto.c | 1 + block/dmg.c | 1 + block/export/fuse.c | 1 + block/file-posix.c | 1 + block/io.c | 1 + block/mirror.c | 1 + block/nvme.c | 1 + block/parallels-ext.c | 1 + block/parallels.c | 1 + block/qcow.c | 1 + block/qcow2-cache.c | 1 + block/qcow2-cluster.c | 1 + block/qcow2-refcount.c | 1 + block/qcow2-snapshot.c | 1 + block/qcow2.c | 1 + block/qed-l2-cache.c | 1 + block/qed-table.c | 1 + block/qed.c | 1 + block/quorum.c | 1 + block/raw-format.c | 1 + block/vdi.c | 1 + block/vhdx-log.c | 1 + block/vhdx.c | 1 + block/vmdk.c | 1 + block/vpc.c | 1 + block/win32-aio.c | 1 + hw/block/dataplane/xen-block.c | 1 + hw/block/fdc.c | 1 + hw/ide/core.c | 1 + hw/ppc/spapr.c | 1 + hw/ppc/spapr_softmmu.c | 1 + hw/scsi/scsi-disk.c | 1 + hw/tpm/tpm_ppi.c | 2 +- nbd/server.c | 1 + net/l2tpv3.c | 2 +- plugins/loader.c | 1 + qemu-img.c | 1 + qemu-io-cmds.c | 1 + qom/object.c | 1 + softmmu/physmem.c | 1 + target/i386/hvf/hvf.c | 1 + target/i386/kvm/kvm.c | 1 + tcg/region.c | 1 + tests/bench/atomic_add-bench.c | 1 + tests/bench/qht-bench.c | 1 + util/atomic64.c | 1 + util/memalign.c | 1 + util/qht.c | 1 + 53 files changed, 112 insertions(+), 20 deletions(-)
Quoted 1 line
create mode 100644
include/qemu/memalign.h