Skip to contentSkip to conversation

libxc: lzma build fix

PATCH Applied Christoph Egger 5 participants 12 messages Jan 26, 2011 – Feb 11, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxc: lzma build fix

Hi!

Attached patch lets libxc build with lzma support on NetBSD. NetBSD doesn't have sysconf(_SC_PHYS_PAGES).

Signature · 6 lines
---to satisfy European Law for business letters:
Advanced Micro Devices GmbH
Einsteinring 24, 85609 Dornach b. Muenchen
Geschaeftsfuehrer: Alberto Bozzo, Andrew Bowd
Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen
Registergericht Muenchen, HRB Nr. 43632
tools/libxc/xc_dom_bzimageloader.c +4 -0
diff -r fb8a14647f4d -r 48852e88d506 tools/libxc/xc_dom_bzimageloader.c
@@ -144,7 +144,11 @@ static uint64_t physmem(void)
 {
     uint64_t ret = 0;
     const long pagesize = sysconf(_SC_PAGESIZE);
+#ifdef _SC_PHYS_PAGES
     const long pages = sysconf(_SC_PHYS_PAGES);
+#else
+    const long pages = -1;
+#endif
 
     if ( (pagesize != -1) || (pages != -1) )
     {

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw

Christoph Egger writes ("[Xen-devel] [PATCH] libxc: lzma build fix"):

Quoted 2 lines

Attached patch lets libxc build with lzma support on NetBSD. NetBSD doesn't have sysconf(_SC_PHYS_PAGES).

Can you please break this out into separate os-dependent code in an appropriate different file ? These kind of #ifdefs break up the code and make it hard to see what's going on.

Ian.

#2 lore raw
Quoted 7 lines · On Wednesday 26 January 2011 17:16:11 Ian Jackson wrote:

Christoph Egger writes ("[Xen-devel] [PATCH] libxc: lzma build fix"):

Quoted 2 lines

Attached patch lets libxc build with lzma support on NetBSD. NetBSD doesn't have sysconf(_SC_PHYS_PAGES).

Can you please break this out into separate os-dependent code in an appropriate different file ? These kind of #ifdefs break up the code and make it hard to see what's going on.

Yes. Attached patch factors physmem() out into os-dependent files and renamed it to xc_get_physmem() to not pollute the namespace.

Signature · 6 lines
---to satisfy European Law for business letters:
Advanced Micro Devices GmbH
Einsteinring 24, 85609 Dornach b. Muenchen
Geschaeftsfuehrer: Alberto Bozzo, Andrew Bowd
Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen
Registergericht Muenchen, HRB Nr. 43632
tools/libxc/xc_dom_bzimageloader.c +1 -22
diff -r c332e775ba53 tools/libxc/xc_dom_bzimageloader.c
@@ -140,27 +140,6 @@ static int xc_try_bzip2_decode(
 
 #include <lzma.h>
 
-static uint64_t physmem(void)
-{
-    uint64_t ret = 0;
-    const long pagesize = sysconf(_SC_PAGESIZE);
-    const long pages = sysconf(_SC_PHYS_PAGES);
-
-    if ( (pagesize != -1) || (pages != -1) )
-    {
-        /*
-         * According to docs, pagesize * pages can overflow.
-         * Simple case is 32-bit box with 4 GiB or more RAM,
-         * which may report exactly 4 GiB of RAM, and "long"
-         * being 32-bit will overflow. Casting to uint64_t
-         * hopefully avoids overflows in the near future.
-         */
-        ret = (uint64_t)(pagesize) * (uint64_t)(pages);
-    }
-
-    return ret;
-}
-
 static int xc_try_lzma_decode(
     struct xc_dom_image *dom, void **blob, size_t *size)
 {
@@ -173,7 +152,7 @@ static int xc_try_lzma_decode(
     int outsize;
     const char *msg;
 
-    ret = lzma_alone_decoder(&stream, physmem() / 3);
+    ret = lzma_alone_decoder(&stream, xc_get_physmem() / 3);
     if ( ret != LZMA_OK )
     {
         DOMPRINTF("LZMA: Failed to init stream decoder");
tools/libxc/xc_linux.c +21 -0
diff -r c332e775ba53 tools/libxc/xc_linux.c
@@ -55,6 +55,27 @@ void discard_file_cache(xc_interface *xc
     errno = saved_errno;
 }
 
+uint64_t xc_get_physmem(void)
+{
+    uint64_t ret = 0;
+    const long pagesize = sysconf(_SC_PAGESIZE);
+    const long pages = sysconf(_SC_PHYS_PAGES);
+
+    if ( (pagesize != -1) || (pages != -1) )
+    {
+        /*
+         * According to docs, pagesize * pages can overflow.
+         * Simple case is 32-bit box with 4 GiB or more RAM,
+         * which may report exactly 4 GiB of RAM, and "long"
+         * being 32-bit will overflow. Casting to uint64_t
+         * hopefully avoids overflows in the near future.
+         */
+        ret = (uint64_t)(pagesize) * (uint64_t)(pages);
+    }
+
+    return ret;
+}
+
 /*
  * Local variables:
  * mode: C
tools/libxc/xc_netbsd.c +21 -0
diff -r c332e775ba53 tools/libxc/xc_netbsd.c
@@ -23,6 +23,9 @@
 #include <xen/sys/evtchn.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <stdio.h>
+#include <errno.h>
+#include <sys/sysctl.h>
 
 static xc_osdep_handle netbsd_privcmd_open(xc_interface *xch)
 {
@@ -351,6 +354,24 @@ void discard_file_cache(xc_interface *xc
     errno = saved_errno;
 }
 
+uint64_t xc_get_physmem(void)
+{
+    int mib[2], rc;
+    size_t len;
+    uint64_t physmem;
+
+    mib[0] = CTL_HW;
+    mib[1] = HW_PHYSMEM64;
+    rc = sysctl(mib, 2, &physmem, &len, NULL, 0);
+
+    if (rc == -1) {
+        /* PERROR("%s: Failed to get hw.physmem64: %s\n", strerror(errno)); */
+        return 0;
+    }
+
+    return physmem;
+}
+
 static struct xc_osdep_ops *netbsd_osdep_init(xc_interface *xch, enum xc_osdep_type type)
 {
     switch ( type )
tools/libxc/xc_private.h +3 -0
diff -r c332e775ba53 tools/libxc/xc_private.h
@@ -275,6 +275,9 @@ void bitmap_byte_to_64(uint64_t *lp, con
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush);
 
+/* How much physical RAM is available? */
+uint64_t xc_get_physmem(void);
+
 #define MAX_MMU_UPDATES 1024
 struct xc_mmu {
     mmu_update_t updates[MAX_MMU_UPDATES];

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

Acked-by
#3 lore raw

Christoph Egger writes ("Re: [Xen-devel] [PATCH] libxc: lzma build fix"):

Quoted 2 lines

Yes. Attached patch factors physmem() out into os-dependent files and renamed it to xc_get_physmem() to not pollute the namespace.

Great, thanks.

I fixed up your commit message, so it looked like this:

libxc: break xc_get_physmem out into os-dependent files

NetBSD doesn't have sysconf(_SC_PHYS_PAGES).

Factor physmem() out into os-dependent files and rename it to xc_get_physmem() so as not to pollute the namespace.

Thanks,
Ian.

#4 lore raw
In reply to Christoph Egger
Quoted 11 lines · Christoph Egger
Quoted 7 lines · On Wednesday 26 January 2011 17:16:11 Ian Jackson wrote:

Christoph Egger writes ("[Xen-devel] [PATCH] libxc: lzma build fix"):

Quoted 2 lines

Attached patch lets libxc build with lzma support on NetBSD. NetBSD doesn't have sysconf(_SC_PHYS_PAGES).

Can you please break this out into separate os-dependent code in an appropriate different file ? These kind of #ifdefs break up the code and make it hard to see what's going on.

Yes. Attached patch factors physmem() out into os-dependent files and renamed it to xc_get_physmem() to not pollute the namespace.

I see this has now been applied, which is my fault for putting this aside to reply to later and then forgetting...

The physmem value calculated by this function is only used as an argument to lzma_alone_decoder, it is divided by 3 to get the memory limit for the decoder.

It's not clear to me why a userspace lzma decode would want to use that particular value, what bearing it has on anything or why it would assume it could use 1/3 of the total RAM in the system (potentially quite a large amount of RAM) as opposed to any other limit number.

Why not just hardcode 32M or something? A quick scan through the rdepends on Debian shows at least a couple of users doing so.

Ian.

#5 lore raw

Ian Campbell writes ("Re: [Xen-devel] [PATCH] libxc: lzma build fix"):

Quoted 3 lines

The physmem value calculated by this function is only used as an argument to lzma_alone_decoder, it is divided by 3 to get the memory limit for the decoder.

I hadn't spotted that. I should have looked more closely.

Quoted 4 lines

It's not clear to me why a userspace lzma decode would want to use that particular value, what bearing it has on anything or why it would assume it could use 1/3 of the total RAM in the system (potentially quite a large amount of RAM) as opposed to any other limit number.

It's dom0's "physmem", so not the whole system, but yes.

Quoted 2 lines

Why not just hardcode 32M or something? A quick scan through the rdepends on Debian shows at least a couple of users doing so.

Yes, we could revert this patch and hardcode a value. 32M seems plausible.

Ian.

#6 lore raw

Ian Jackson writes ("Re: [Xen-devel] [PATCH] libxc: lzma build fix"):

Quoted 2 lines

Yes, we could revert this patch and hardcode a value. 32M seems plausible.

How about this.

Ian.

libxc: Do not use host physmem as parameter to lzma decoder

It's not clear why a userspace lzma decode would want to use that particular value, what bearing it has on anything or why it would assume it could use 1/3 of the total RAM in the system (potentially quite a large amount of RAM) as opposed to any other limit number.

Instead, hardcode 32Mby.

This reverts 22830:c80960244942, removes the xc_get_physmem/physmem function entirely, and replaces the expression at the call site with a fixed constant.

Cc Ian Campbell <[email protected]>
Cc Christoph Egger <[email protected]>
tools/libxc/xc_dom_bzimageloader.c +1 -1
diff -r 88cf07fed7d2 tools/libxc/xc_dom_bzimageloader.c
@@ -152,7 +152,7 @@ static int xc_try_lzma_decode(
     int outsize;
     const char *msg;
 
-    ret = lzma_alone_decoder(&stream, xc_get_physmem() / 3);
+    ret = lzma_alone_decoder(&stream, 32*1024*1024);
     if ( ret != LZMA_OK )
     {
         DOMPRINTF("LZMA: Failed to init stream decoder");
tools/libxc/xc_linux.c +0 -21
diff -r 88cf07fed7d2 tools/libxc/xc_linux.c
@@ -55,27 +55,6 @@ void discard_file_cache(xc_interface *xc
     errno = saved_errno;
 }
 
-uint64_t xc_get_physmem(void)
-{
-    uint64_t ret = 0;
-    const long pagesize = sysconf(_SC_PAGESIZE);
-    const long pages = sysconf(_SC_PHYS_PAGES);
-
-    if ( (pagesize != -1) || (pages != -1) )
-    {
-        /*
-         * According to docs, pagesize * pages can overflow.
-         * Simple case is 32-bit box with 4 GiB or more RAM,
-         * which may report exactly 4 GiB of RAM, and "long"
-         * being 32-bit will overflow. Casting to uint64_t
-         * hopefully avoids overflows in the near future.
-         */
-        ret = (uint64_t)(pagesize) * (uint64_t)(pages);
-    }
-
-    return ret;
-}
-
 /*
  * Local variables:
  * mode: C
tools/libxc/xc_netbsd.c +0 -21
diff -r 88cf07fed7d2 tools/libxc/xc_netbsd.c
@@ -23,9 +23,6 @@
 #include <xen/sys/evtchn.h>
 #include <unistd.h>
 #include <fcntl.h>
-#include <stdio.h>
-#include <errno.h>
-#include <sys/sysctl.h>
 
 static xc_osdep_handle netbsd_privcmd_open(xc_interface *xch)
 {
@@ -354,24 +351,6 @@ void discard_file_cache(xc_interface *xc
     errno = saved_errno;
 }
 
-uint64_t xc_get_physmem(void)
-{
-    int mib[2], rc;
-    size_t len;
-    uint64_t physmem;
-
-    mib[0] = CTL_HW;
-    mib[1] = HW_PHYSMEM64;
-    rc = sysctl(mib, 2, &physmem, &len, NULL, 0);
-
-    if (rc == -1) {
-        /* PERROR("%s: Failed to get hw.physmem64: %s\n", strerror(errno)); */
-        return 0;
-    }
-
-    return physmem;
-}
-
 static struct xc_osdep_ops *netbsd_osdep_init(xc_interface *xch, enum xc_osdep_type type)
 {
     switch ( type )
tools/libxc/xc_private.h +0 -3
diff -r 88cf07fed7d2 tools/libxc/xc_private.h
@@ -275,9 +275,6 @@ void bitmap_byte_to_64(uint64_t *lp, con
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush);
 
-/* How much physical RAM is available? */
-uint64_t xc_get_physmem(void);
-
 #define MAX_MMU_UPDATES 1024
 struct xc_mmu {
     mmu_update_t updates[MAX_MMU_UPDATES];
Acked-by
#7 lore raw
Quoted 10 lines · Ian Jackson

Ian Jackson writes ("Re: [Xen-devel] [PATCH] libxc: lzma build fix"):

Quoted 2 lines

Yes, we could revert this patch and hardcode a value. 32M seems plausible.

How about this.

Ian.

libxc: Do not use host physmem as parameter to lzma decoder

Strictly speaking it's the dom0 physmem.

Quoted 4 lines

It's not clear why a userspace lzma decode would want to use that particular value, what bearing it has on anything or why it would assume it could use 1/3 of the total RAM in the system (potentially quite a large amount of RAM) as opposed to any other limit number.

Sounds good to me ;-)

Quoted 7 lines

Instead, hardcode 32Mby.

This reverts 22830:c80960244942, removes the xc_get_physmem/physmem function entirely, and replaces the expression at the call site with a fixed constant.

Quoted 96 lines
Cc Christoph Egger <[email protected]>

diff -r 88cf07fed7d2 tools/libxc/xc_dom_bzimageloader.c

tools/libxc/xc_dom_bzimageloader.c +1 -1
@@ -152,7 +152,7 @@ static int xc_try_lzma_decode(
     int outsize;
     const char *msg;
 
-    ret = lzma_alone_decoder(&stream, xc_get_physmem() / 3);
+    ret = lzma_alone_decoder(&stream, 32*1024*1024);
     if ( ret != LZMA_OK )
     {
         DOMPRINTF("LZMA: Failed to init stream decoder");

diff -r 88cf07fed7d2 tools/libxc/xc_linux.c

tools/libxc/xc_linux.c +0 -21
@@ -55,27 +55,6 @@ void discard_file_cache(xc_interface *xc
     errno = saved_errno;
 }
 
-uint64_t xc_get_physmem(void)
-{
-    uint64_t ret = 0;
-    const long pagesize = sysconf(_SC_PAGESIZE);
-    const long pages = sysconf(_SC_PHYS_PAGES);
-
-    if ( (pagesize != -1) || (pages != -1) )
-    {
-        /*
-         * According to docs, pagesize * pages can overflow.
-         * Simple case is 32-bit box with 4 GiB or more RAM,
-         * which may report exactly 4 GiB of RAM, and "long"
-         * being 32-bit will overflow. Casting to uint64_t
-         * hopefully avoids overflows in the near future.
-         */
-        ret = (uint64_t)(pagesize) * (uint64_t)(pages);
-    }
-
-    return ret;
-}
-
 /*
  * Local variables:
  * mode: C

diff -r 88cf07fed7d2 tools/libxc/xc_netbsd.c

tools/libxc/xc_netbsd.c +0 -21
@@ -23,9 +23,6 @@
 #include <xen/sys/evtchn.h>
 #include <unistd.h>
 #include <fcntl.h>
-#include <stdio.h>
-#include <errno.h>
-#include <sys/sysctl.h>
 
 static xc_osdep_handle netbsd_privcmd_open(xc_interface *xch)
 {
@@ -354,24 +351,6 @@ void discard_file_cache(xc_interface *xc
     errno = saved_errno;
 }
 
-uint64_t xc_get_physmem(void)
-{
-    int mib[2], rc;
-    size_t len;
-    uint64_t physmem;
-
-    mib[0] = CTL_HW;
-    mib[1] = HW_PHYSMEM64;
-    rc = sysctl(mib, 2, &physmem, &len, NULL, 0);
-
-    if (rc == -1) {
-        /* PERROR("%s: Failed to get hw.physmem64: %s\n", strerror(errno)); */
-        return 0;
-    }
-
-    return physmem;
-}
-
 static struct xc_osdep_ops *netbsd_osdep_init(xc_interface *xch, enum xc_osdep_type type)
 {
     switch ( type )

diff -r 88cf07fed7d2 tools/libxc/xc_private.h

tools/libxc/xc_private.h +0 -3
@@ -275,9 +275,6 @@ void bitmap_byte_to_64(uint64_t *lp, con
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush);
 
-/* How much physical RAM is available? */
-uint64_t xc_get_physmem(void);
-
 #define MAX_MMU_UPDATES 1024
 struct xc_mmu {
     mmu_update_t updates[MAX_MMU_UPDATES];
Applied
#8 lore raw

Ian Campbell writes ("Re: [Xen-devel] [PATCH] libxc: lzma build fix"):

Quoted 1 line

Strictly speaking it's the dom0 physmem.

Ah, yes, I shouldn't speak so loosely.

Quoted 1 line

Applied, thanks.

Ian.

#9 lore raw
In reply to Ian Jackson
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Ian Jackson

libxc: Do not use host physmem as parameter to lzma decoder

It's not clear why a userspace lzma decode would want to use that particular value, what bearing it has on anything or why it would assume it could use 1/3 of the total RAM in the system (potentially quite a large amount of RAM) as opposed to any other limit number.

Instead, hardcode 32Mby.

Has anyone of you actually tested this? I don't get lzma to work with this set to less than 128Mb, and xz's configure.ac also sort of states this to be a minimal requirement for arbitrary input.

Quoted 7 lines

This reverts 22830:c80960244942, removes the xc_get_physmem/physmem function entirely, and replaces the expression at the call site with a fixed constant.

Cc Ian Campbell <[email protected]>
Cc Christoph Egger <[email protected]>

Jan

#10 lore raw
Quoted 8 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Ian Jackson

libxc: Do not use host physmem as parameter to lzma decoder

It's not clear why a userspace lzma decode would want to use that particular value, what bearing it has on anything or why it would assume it could use 1/3 of the total RAM in the system (potentially quite a large amount of RAM) as opposed to any other limit number.

Quoted 5 lines
Quoted 1 line

Instead, hardcode 32Mby.

Has anyone of you actually tested this? I don't get lzma to work with this set to less than 128Mb, and xz's configure.ac also sort of states this to be a minimal requirement for arbitrary input.

I looked quite hard for any documentation on this and couldn't find any, I could only find examples of people using host physmem like we were or hardcoding ~30MB.

I should have looked in configure.ac, its obvious really ;-)

Putting aside the fact that 128MB is a quite obscene amount of memory as a _minimum_ requirement for something like this I guess we should change the constant.

Ian.

Quoted 11 lines
Quoted 8 lines

This reverts 22830:c80960244942, removes the xc_get_physmem/physmem function entirely, and replaces the expression at the call site with a fixed constant.

Cc Ian Campbell <[email protected]>
Cc Christoph Egger <[email protected]>

Jan

#11 lore raw

Ian Campbell writes ("Re: [Xen-devel] [PATCH] libxc: lzma build fix"):

Quoted 3 lines

Putting aside the fact that 128MB is a quite obscene amount of memory as a _minimum_ requirement for something like this I guess we should change the constant.

I have done so.

Ian.

# HG changeset patch
# User Ian Jackson <[email protected]> # Date 1297446553 0
# Node ID 4376c4f0196f0dc129904a0f8c54c74ae4bcdff2 # Parent 6c22ae0f654084a4d2811e51e22b7f8b20b49cec
libxc: increase lzma max memory constant to 128Mby

According to lzma's configure.ac (!) the minimum memory limit to cope with arbitrary input is 128Mby (!)

This is obviously an unreasonable amount of memory for this kind of task, but we need to increase the constant limit for it not to randomly fail. So do so.

tools/libxc/xc_dom_bzimageloader.c +1 -1
diff -r 6c22ae0f6540 -r 4376c4f0196f tools/libxc/xc_dom_bzimageloader.c
@@ -152,7 +152,7 @@ static int xc_try_lzma_decode(
     int outsize;
     const char *msg;
 
-    ret = lzma_alone_decoder(&stream, 32*1024*1024);
+    ret = lzma_alone_decoder(&stream, 128*1024*1024);
     if ( ret != LZMA_OK )
     {
         DOMPRINTF("LZMA: Failed to init stream decoder");