[PATCH] virhostmem: Get total memory on macOS properly

Michal Privoznik posted 1 patch 5 months, 3 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/974bf5c00427ba1b734065c673ac47dc81d39183.1699262917.git.mprivozn@redhat.com
There is a newer version of this series
src/util/virhostmem.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
[PATCH] virhostmem: Get total memory on macOS properly
Posted by Michal Privoznik 5 months, 3 weeks ago
Problem with HW_PHYSMEM sysctl on 64-bit macOS is that it
returns a 32-bit signed value. Thus it overflows. Switching to
HW_MEMSIZE is recommended as it's of an uint_64 type [1].

1: https://github.com/apple-oss-distributions/xnu/blob/xnu-10002.1.13/bsd/sys/sysctl.h

Reported-by: Jaroslav Suchanek <jsuchane@redhat.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/util/virhostmem.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/util/virhostmem.c b/src/util/virhostmem.c
index 1da2759ac3..a7027af835 100644
--- a/src/util/virhostmem.c
+++ b/src/util/virhostmem.c
@@ -617,7 +617,10 @@ virHostMemGetTotal(void)
     unsigned long long physmem = 0;
     size_t len = sizeof(physmem);
 
-    if (sysctlbyname("hw.physmem", &physmem, &len, NULL, 0) < 0) {
+    /* On macOS hw.physmem is int32_t which doesn't fly with >4GiB of memory.
+     * But hw.memsize is uint64_t. */
+    if (sysctlbyname("hw.memsize", &physmem, &len, NULL, 0) < 0 &&
+        sysctlbyname("hw.physmem", &physmem, &len, NULL, 0) < 0) {
         virReportSystemError(errno, "%s",
                              _("Unable to query memory total"));
         return 0;
-- 
2.41.0
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org
Re: [PATCH] virhostmem: Get total memory on macOS properly
Posted by Daniel P. Berrangé 5 months, 3 weeks ago
On Mon, Nov 06, 2023 at 10:28:37AM +0100, Michal Privoznik wrote:
> Problem with HW_PHYSMEM sysctl on 64-bit macOS is that it
> returns a 32-bit signed value. Thus it overflows. Switching to
> HW_MEMSIZE is recommended as it's of an uint_64 type [1].
> 
> 1: https://github.com/apple-oss-distributions/xnu/blob/xnu-10002.1.13/bsd/sys/sysctl.h
> 
> Reported-by: Jaroslav Suchanek <jsuchane@redhat.com>
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/util/virhostmem.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org