Skip to contentSkip to conversation

candidate fixes for 5.1-rc1 (shippable, semihosting, OOM tcg)

PATCH Superseded Alex Bennée 6 participants 19 messages Jul 17, 2020 – Jul 21, 2020

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover candidate fixes for 5.1-rc1 (shippable, semihosting, OOM tcg)

Hi,

These are some candidate patches for rc1.

The first is a quick fix that finally gets shippable up and running again. We may want to consider a grand renaming of our docker scheme but I don't think thats something worth dropping in while we are stabilising - especially if we change the project name on gitlab.

The semihosting fixes had just slipped off my radar but are pretty minor bug fixes.

Finally I whipped up the last two because of a report of QEMU OOM'ing on some CI systems with low memory. It doesn't restore the link to guest ram size but instead introduces an advisory helper function to probe guest physical memory.

Please review.

Alex Bennée (3):
shippable: add one more qemu to registry url
util: add qemu_get_host_physmem utility function accel/tcg: better handle memory constrained systems

KONRAD Frederic (2):
semihosting: defer connect_chardevs a little more to use serialx
semihosting: don't send the trailing '\0'

 include/qemu/osdep.h      | 10 ++++++++++
 accel/tcg/translate-all.c |  7 ++++++-
 hw/semihosting/console.c  |  4 +++-
 softmmu/vl.c              |  5 +++--
 util/oslib-posix.c        | 11 +++++++++++
 util/oslib-win32.c        |  6 ++++++
 .shippable.yml            |  2 +-
 7 files changed, 40 insertions(+), 5 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/5 shippable: add one more qemu to registry url

The registry url is <project>/<repo>/qemu/<image>

Perhaps we should rationalise that some day but for now.

---
 .shippable.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
.shippable.yml +1 -1
index f6b742432e5..89d8be4291b 100644
@@ -27,7 +27,7 @@ env:
       TARGET_LIST=ppc64-softmmu,ppc64-linux-user,ppc64abi32-linux-user
 build:
   pre_ci_boot:
-    image_name: registry.gitlab.com/qemu-project/qemu/${IMAGE}
+    image_name: registry.gitlab.com/qemu-project/qemu/qemu/${IMAGE}
     image_tag: latest
     pull: true
     options: "-e HOME=/root"
Signature · 1 line
2.20.1
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH v1 1/5] shippable: add one more qemu to registry url

Quoted 15 lines · Alex Bennée

The registry url is <project>/<repo>/qemu/<image>

Perhaps we should rationalise that some day but for now.

---
 .shippable.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
.shippable.yml +0 -0
index f6b742432e5..89d8be4291b 100644

.shippable.yml env: · line 33

Quoted 8 lines
.shippable.yml +1 -1
@@ -27,7 +27,7 @@ env:
       TARGET_LIST=ppc64-softmmu,ppc64-linux-user,ppc64abi32-linux-user
 build:
   pre_ci_boot:
-    image_name: registry.gitlab.com/qemu-project/qemu/${IMAGE}
+    image_name: registry.gitlab.com/qemu-project/qemu/qemu/${IMAGE}
     image_tag: latest
     pull: true
     options: "-e HOME=/root"
#3 lore raw

PATCH 2/5 semihosting: defer connect_chardevs a little more to use serialx

In reply to Alex Bennée

From: KONRAD Frederic <[email protected]>

With that we can just use -semihosting-config chardev=serial0.

Message-Id: <[email protected]>

[AJB: tweak commit message]
---
 softmmu/vl.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
softmmu/vl.c +3 -2
index f476ef89edb..4fedbe60c39 100644
@@ -4131,8 +4131,6 @@ void qemu_init(int argc, char **argv, char **envp)
 
     qemu_opts_foreach(qemu_find_opts("chardev"),
                       chardev_init_func, NULL, &error_fatal);
-    /* now chardevs have been created we may have semihosting to connect */
-    qemu_semihosting_connect_chardevs();
 
 #ifdef CONFIG_VIRTFS
     qemu_opts_foreach(qemu_find_opts("fsdev"),
@@ -4281,6 +4279,9 @@ void qemu_init(int argc, char **argv, char **envp)
     if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
         exit(1);
 
+    /* now chardevs have been created we may have semihosting to connect */
+    qemu_semihosting_connect_chardevs();
+
     /* If no default VGA is requested, the default is "none".  */
     if (default_vga) {
         vga_model = get_default_vga_model(machine_class);
Signature · 1 line
2.20.1
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH v1 2/5] semihosting: defer connect_chardevs a little more to use serialx

Quoted 11 lines · Alex Bennée

From: KONRAD Frederic <[email protected]>

With that we can just use -semihosting-config chardev=serial0.

Message-Id: <[email protected]>

[AJB: tweak commit message]
---
 softmmu/vl.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

r~

#5 lore raw

PATCH 3/5 semihosting: don't send the trailing '\0'

In reply to Alex Bennée

From: KONRAD Frederic <[email protected]>

Don't send the trailing 0 from the string.

Message-Id: <[email protected]>

---
 hw/semihosting/console.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/semihosting/console.c +3 -1
index 22e7827824a..9b4fee92602 100644
@@ -52,7 +52,9 @@ static GString *copy_user_string(CPUArchState *env, target_ulong addr)
 
     do {
         if (cpu_memory_rw_debug(cpu, addr++, &c, 1, 0) == 0) {
-            s = g_string_append_c(s, c);
+            if (c) {
+                s = g_string_append_c(s, c);
+            }
         } else {
             qemu_log_mask(LOG_GUEST_ERROR,
                           "%s: passed inaccessible address " TARGET_FMT_lx,
Signature · 1 line
2.20.1
Reviewed-by on v1 3/5
#6 lore raw

Re: [PATCH v1 3/5] semihosting: don't send the trailing '\0'

Quoted 11 lines · Alex Bennée

From: KONRAD Frederic <[email protected]>

Don't send the trailing 0 from the string.

Message-Id: <[email protected]>

---
 hw/semihosting/console.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 6 lines
hw/semihosting/console.c +0 -0
index 22e7827824a..9b4fee92602 100644

hw/semihosting/console.c static GString *copy_user_string(CPUArchState *env, target_ulong addr) · line 60

Quoted 10 lines
hw/semihosting/console.c +3 -1
@@ -52,7 +52,9 @@ static GString *copy_user_string(CPUArchState *env, target_ulong addr)
 
     do {
         if (cpu_memory_rw_debug(cpu, addr++, &c, 1, 0) == 0) {
-            s = g_string_append_c(s, c);
+            if (c) {
+                s = g_string_append_c(s, c);
+            }
         } else {
             qemu_log_mask(LOG_GUEST_ERROR,
                           "%s: passed inaccessible address " TARGET_FMT_lx,

Next cycle, we could clean up this loop a bit, rather than testing c != 0 twice. E.g.

    while (1) {
        if (cpu_memory_rw_debug(cpu, addr++, &c, 1, 0)) {
            error;
            break;
        }
        if (c == 0) {
            break;
        }
        s = g_string_append_c(s, c);
    }

r~

#7 lore raw

PATCH 4/5 util: add qemu_get_host_physmem utility function

In reply to Alex Bennée

This will be used in a future patch. For POSIX systems _SC_PHYS_PAGES isn't standardised but at least appears in the man pages for Open/FreeBSD. The result is advisory so any users of it shouldn't just fail if we can't work it out.

The win32 stub currently returns 0 until someone with a Windows system can develop and test a patch.

Cc BALATON Zoltan <[email protected]>
Cc Christian Ehrhardt <[email protected]>
---
 include/qemu/osdep.h | 10 ++++++++++
 util/oslib-posix.c   | 11 +++++++++++
 util/oslib-win32.c   |  6 ++++++
 3 files changed, 27 insertions(+)
include/qemu/osdep.h +10 -0
index 4841b5c6b5f..7ff209983e2 100644
@@ -665,4 +665,14 @@ static inline void qemu_reset_optind(void)
  */
 char *qemu_get_host_name(Error **errp);
 
+/**
+ * qemu_get_host_physmem:
+ *
+ * Operating system agnostiv way of querying host memory.
+ *
+ * Returns amount of physical memory on the system. This is purely
+ * advisery and may return 0 if we can't work it out.
+ */
+size_t qemu_get_host_physmem(void);
+
 #endif
util/oslib-posix.c +11 -0
index 36bf8593f8c..d9da782b896 100644
@@ -839,3 +839,14 @@ char *qemu_get_host_name(Error **errp)
 
     return g_steal_pointer(&hostname);
 }
+
+size_t qemu_get_host_physmem(void)
+{
+#ifdef _SC_PHYS_PAGES
+    long pages = sysconf(_SC_PHYS_PAGES);
+    if (pages > 0) {
+        return pages * qemu_real_host_page_size;
+    }
+#endif
+    return 0;
+}
util/oslib-win32.c +6 -0
index 7eedbe5859a..31030463cc9 100644
@@ -828,3 +828,9 @@ char *qemu_get_host_name(Error **errp)
 
     return g_utf16_to_utf8(tmp, size, NULL, NULL, NULL);
 }
+
+size_t qemu_get_host_physmem(void)
+{
+    /* currently unimplemented */
+    return 0;
+}
Signature · 1 line
2.20.1
#8 lore raw

Re: [PATCH v1 4/5] util: add qemu_get_host_physmem utility function

Quoted 22 lines · Alex Bennée

This will be used in a future patch. For POSIX systems _SC_PHYS_PAGES isn't standardised but at least appears in the man pages for Open/FreeBSD. The result is advisory so any users of it shouldn't just fail if we can't work it out.

The win32 stub currently returns 0 until someone with a Windows system can develop and test a patch.

Cc BALATON Zoltan <[email protected]>
Cc Christian Ehrhardt <[email protected]>

---

include/qemu/osdep.h | 10 ++++++++++
util/oslib-posix.c   | 11 +++++++++++
util/oslib-win32.c   |  6 ++++++
3 files changed, 27 insertions(+)
include/qemu/osdep.h +0 -0
index 4841b5c6b5f..7ff209983e2 100644

include/qemu/osdep.h static inline void qemu_reset_optind(void) · line 671

Quoted 7 lines
include/qemu/osdep.h +4 -0
@@ -665,4 +665,14 @@ static inline void qemu_reset_optind(void)
 */
 char *qemu_get_host_name(Error **errp);
 
+/**
+ * qemu_get_host_physmem:
+ *
+ * Operating system agnostiv way of querying host memory.

Typo: agnostiv -> agnostic

Quoted 12 lines
include/qemu/osdep.h +6 -0
+ *
+ * Returns amount of physical memory on the system. This is purely
+ * advisery and may return 0 if we can't work it out.
+ */
+size_t qemu_get_host_physmem(void);
+
 #endif
util/oslib-posix.c +0 -0
index 36bf8593f8c..d9da782b896 100644

util/oslib-posix.c char *qemu_get_host_name(Error **errp) · line 848

Quoted 10 lines
util/oslib-posix.c +7 -0
@@ -839,3 +839,14 @@ char *qemu_get_host_name(Error **errp)
 
    return g_steal_pointer(&hostname);
 }
+
+size_t qemu_get_host_physmem(void)
+{
+#ifdef _SC_PHYS_PAGES
+    long pages = sysconf(_SC_PHYS_PAGES);
+    if (pages > 0) {
+        return pages * qemu_real_host_page_size;

The Linux man page warns that this product may overflow so maybe you could return pages here.

Quoted 9 lines
util/oslib-posix.c +4 -0
+    }
+#endif
+    return 0;
+}
util/oslib-win32.c +0 -0
index 7eedbe5859a..31030463cc9 100644

util/oslib-win32.c char *qemu_get_host_name(Error **errp) · line 836

Quoted 9 lines
util/oslib-win32.c +6 -0
@@ -828,3 +828,9 @@ char *qemu_get_host_name(Error **errp)
 
    return g_utf16_to_utf8(tmp, size, NULL, NULL, NULL);
 }
+
+size_t qemu_get_host_physmem(void)
+{
+    /* currently unimplemented */
+    return 0;
+}

For Windows this may help:

https://stackoverflow.com/questions/5553665/get-ram-system-size

not sure about other OSes.

Regards,
BALATON Zoltan

#9 lore raw

Re: [PATCH v1 4/5] util: add qemu_get_host_physmem utility function

Quoted 46 lines · BALATON Zoltan
Quoted 22 lines · Alex Bennée

This will be used in a future patch. For POSIX systems _SC_PHYS_PAGES isn't standardised but at least appears in the man pages for Open/FreeBSD. The result is advisory so any users of it shouldn't just fail if we can't work it out.

The win32 stub currently returns 0 until someone with a Windows system can develop and test a patch.

Cc BALATON Zoltan <[email protected]>
Cc Christian Ehrhardt <[email protected]>

---

include/qemu/osdep.h | 10 ++++++++++
util/oslib-posix.c   | 11 +++++++++++
util/oslib-win32.c   |  6 ++++++
3 files changed, 27 insertions(+)
include/qemu/osdep.h +0 -0
index 4841b5c6b5f..7ff209983e2 100644

include/qemu/osdep.h static inline void qemu_reset_optind(void) · line 671

Quoted 7 lines
include/qemu/osdep.h +4 -0
@@ -665,4 +665,14 @@ static inline void qemu_reset_optind(void)
 */
 char *qemu_get_host_name(Error **errp);
 
+/**
+ * qemu_get_host_physmem:
+ *
+ * Operating system agnostiv way of querying host memory.

Typo: agnostiv -> agnostic

Quoted 13 lines
include/qemu/osdep.h +6 -0
+ *
+ * Returns amount of physical memory on the system. This is purely
+ * advisery and may return 0 if we can't work it out.
+ */
+size_t qemu_get_host_physmem(void);
+
 #endif
util/oslib-posix.c +0 -0
index 36bf8593f8c..d9da782b896 100644
@@ -839,3 +839,14 @@ char *qemu_get_host_name(Error **errp)
 
Quoted 13 lines

util/oslib-posix.c char *qemu_get_host_name(Error **errp) · line 848

Quoted 9 lines
util/oslib-posix.c +7 -0
    return g_steal_pointer(&hostname);
 }
+
+size_t qemu_get_host_physmem(void)
+{
+#ifdef _SC_PHYS_PAGES
+    long pages = sysconf(_SC_PHYS_PAGES);
+    if (pages > 0) {
+        return pages * qemu_real_host_page_size;

The Linux man page warns that this product may overflow so maybe you could return pages here.

The caller might be even less aware of that than this function - so maybe better handle it here.
How about handling overflows and cutting it to MiB before returning?

Quoted 27 lines
Quoted 9 lines
util/oslib-posix.c +4 -0
+    }
+#endif
+    return 0;
+}
util/oslib-win32.c +0 -0
index 7eedbe5859a..31030463cc9 100644

util/oslib-win32.c char *qemu_get_host_name(Error **errp) · line 836

Quoted 9 lines
util/oslib-win32.c +6 -0
@@ -828,3 +828,9 @@ char *qemu_get_host_name(Error **errp)
 
    return g_utf16_to_utf8(tmp, size, NULL, NULL, NULL);
 }
+
+size_t qemu_get_host_physmem(void)
+{
+    /* currently unimplemented */
+    return 0;
+}

For Windows this may help:

https://stackoverflow.com/questions/5553665/get-ram-system-size

not sure about other OSes.

Regards,
BALATON Zoltan

Signature · 3 lines
Christian Ehrhardt
Staff Engineer, Ubuntu Server
Canonical Ltd
#10 lore raw

Re: [PATCH v1 4/5] util: add qemu_get_host_physmem utility function

Quoted 11 lines · Christian Ehrhardt

util/oslib-posix.c char *qemu_get_host_name(Error **errp) · line 848

Quoted 6 lines
util/oslib-posix.c +6 -0
+size_t qemu_get_host_physmem(void)
+{
+#ifdef _SC_PHYS_PAGES
+    long pages = sysconf(_SC_PHYS_PAGES);
+    if (pages > 0) {
+        return pages * qemu_real_host_page_size;

The Linux man page warns that this product may overflow so maybe you could return pages here.

Quoted 3 lines

The caller might be even less aware of that than this function - so maybe better handle it here.
How about handling overflows and cutting it to MiB before returning?

Indeed, the caller may be less aware, so we should handle it here. But I don't think truncating to MiB helps at all, because again, the caller has to handle overflow.

Better, I think, to saturate the result to ~(size_t)0 and leave it at that.

r~

#11 lore raw

Re: [PATCH v1 4/5] util: add qemu_get_host_physmem utility function

Quoted 16 lines · Richard Henderson
Quoted 11 lines · Christian Ehrhardt

util/oslib-posix.c char *qemu_get_host_name(Error **errp) · line 848

Quoted 6 lines
util/oslib-posix.c +6 -0
+size_t qemu_get_host_physmem(void)
+{
+#ifdef _SC_PHYS_PAGES
+    long pages = sysconf(_SC_PHYS_PAGES);
+    if (pages > 0) {
+        return pages * qemu_real_host_page_size;

The Linux man page warns that this product may overflow so maybe you could return pages here.

Quoted 3 lines

The caller might be even less aware of that than this function - so maybe better handle it here.
How about handling overflows and cutting it to MiB before returning?

Quoted 6 lines

Indeed, the caller may be less aware, so we should handle it here. But I don't think truncating to MiB helps at all, because again, the caller has to handle overflow.

Better, I think, to saturate the result to ~(size_t)0 and leave it at that.

So I went for:

  size_t qemu_get_host_physmem(void)
  {
  #ifdef _SC_PHYS_PAGES
      long pages = sysconf(_SC_PHYS_PAGES);
      if (pages > 0) {
          if (pages > SIZE_MAX / qemu_real_host_page_size) {
              return SIZE_MAX;
          } else {
              return pages * qemu_real_host_page_size;
          }
      }
  #endif
      return 0;
  }

apparently the first case of saturating integer arithmetic outside of the instruction emulation in QEMU :-/

Signature · 1 line
Alex Bennée
#12 lore raw

Re: [PATCH v1 4/5] util: add qemu_get_host_physmem utility function

In reply to Alex Bennée

util/oslib-posix.c char *qemu_get_host_name(Error **errp) · line 852

Quoted 10 lines · Alex Bennée
util/oslib-posix.c +10 -0
+size_t qemu_get_host_physmem(void)
+{
+#ifdef _SC_PHYS_PAGES
+    long pages = sysconf(_SC_PHYS_PAGES);
+    if (pages > 0) {
+        return pages * qemu_real_host_page_size;
+    }
+#endif
+    return 0;
+}

Is it worth examining our own RLIMIT_{AS,DATA} as well?

I suppose that's not usually what is tweaked in the example of a ram-limited container...

r~

#13 lore raw

Re: [PATCH v1 4/5] util: add qemu_get_host_physmem utility function

Quoted 13 lines · Richard Henderson

util/oslib-posix.c char *qemu_get_host_name(Error **errp) · line 852

Quoted 10 lines · Alex Bennée
util/oslib-posix.c +10 -0
+size_t qemu_get_host_physmem(void)
+{
+#ifdef _SC_PHYS_PAGES
+    long pages = sysconf(_SC_PHYS_PAGES);
+    if (pages > 0) {
+        return pages * qemu_real_host_page_size;
+    }
+#endif
+    return 0;
+}

Is it worth examining our own RLIMIT_{AS,DATA} as well?

We don't anywhere else in the code so for now I'd say not.

Quoted 6 lines

I suppose that's not usually what is tweaked in the example of a ram-limited container...

r~

Signature · 1 line
Alex Bennée
#14 lore raw

PATCH 5/5 accel/tcg: better handle memory constrained systems

In reply to Alex Bennée

It turns out there are some 64 bit systems that have relatively low amounts of physical memory available to them (typically CI system). Even with swapping available a 1GB translation buffer that fills up can put the machine under increased memory pressure. Detect these low memory situations and reduce tb_size appropriately.

Fixes 600e17b261
Cc BALATON Zoltan <[email protected]>
Cc Christian Ehrhardt <[email protected]>
---
 accel/tcg/translate-all.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
accel/tcg/translate-all.c +6 -1
index 2afa46bd2b1..2ff0ba6d19b 100644
@@ -976,7 +976,12 @@ static inline size_t size_code_gen_buffer(size_t tb_size)
 {
     /* Size the buffer.  */
     if (tb_size == 0) {
-        tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
+        size_t phys_mem = qemu_get_host_physmem();
+        if (phys_mem > 0 && phys_mem < (2 * DEFAULT_CODE_GEN_BUFFER_SIZE)) {
+            tb_size = phys_mem / 4;
+        } else {
+            tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
+        }
     }
     if (tb_size < MIN_CODE_GEN_BUFFER_SIZE) {
         tb_size = MIN_CODE_GEN_BUFFER_SIZE;
Signature · 1 line
2.20.1
#15 lore raw

Re: [PATCH v1 5/5] accel/tcg: better handle memory constrained systems

Quoted 19 lines · Alex Bennée

It turns out there are some 64 bit systems that have relatively low amounts of physical memory available to them (typically CI system). Even with swapping available a 1GB translation buffer that fills up can put the machine under increased memory pressure. Detect these low memory situations and reduce tb_size appropriately.

Fixes 600e17b261
Cc BALATON Zoltan <[email protected]>
Cc Christian Ehrhardt <[email protected]>
---
 accel/tcg/translate-all.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
accel/tcg/translate-all.c +0 -0
index 2afa46bd2b1..2ff0ba6d19b 100644

accel/tcg/translate-all.c static inline size_t size_code_gen_buffer(size_t · line 984

Quoted 10 lines
accel/tcg/translate-all.c +3 -1
@@ -976,7 +976,12 @@ static inline size_t size_code_gen_buffer(size_t
 tb_size)
 {
     /* Size the buffer.  */
     if (tb_size == 0) {
-        tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
+        size_t phys_mem = qemu_get_host_physmem();
+        if (phys_mem > 0 && phys_mem < (2 *
 DEFAULT_CODE_GEN_BUFFER_SIZE)) {
+            tb_size = phys_mem / 4;
 

In my experiments I've found that /8 more closely matches the former behavior
on small hosts while at the same time not affecting common large hosts.

Quoted 10 lines
accel/tcg/translate-all.c +3 -0
+        } else {
+            tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
+        }
     }
     if (tb_size < MIN_CODE_GEN_BUFFER_SIZE) {
         tb_size = MIN_CODE_GEN_BUFFER_SIZE;

--
2.20.1

Signature · 3 lines
Christian Ehrhardt
Staff Engineer, Ubuntu Server
Canonical Ltd
#16 lore raw

Re: [PATCH v1 5/5] accel/tcg: better handle memory constrained systems

In reply to Alex Bennée
Quoted 19 lines · Alex Bennée

It turns out there are some 64 bit systems that have relatively low amounts of physical memory available to them (typically CI system). Even with swapping available a 1GB translation buffer that fills up can put the machine under increased memory pressure. Detect these low memory situations and reduce tb_size appropriately.

Fixes 600e17b261
Cc BALATON Zoltan <[email protected]>
Cc Christian Ehrhardt <[email protected]>
---
 accel/tcg/translate-all.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
accel/tcg/translate-all.c +0 -0
index 2afa46bd2b1..2ff0ba6d19b 100644

accel/tcg/translate-all.c static inline size_t size_code_gen_buffer(size_t tb_size) · line 984

Quoted 10 lines
accel/tcg/translate-all.c +6 -1
@@ -976,7 +976,12 @@ static inline size_t size_code_gen_buffer(size_t tb_size)
 {
     /* Size the buffer.  */
     if (tb_size == 0) {
-        tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
+        size_t phys_mem = qemu_get_host_physmem();
+        if (phys_mem > 0 && phys_mem < (2 * DEFAULT_CODE_GEN_BUFFER_SIZE)) {
+            tb_size = phys_mem / 4;
+        } else {
+            tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
+        }

I'm not convinced this is going to work when running QEMU inside a container environment with RAM cap, because the physmem level is completely unrelated to the RAM the container is permitted to actually use in practice. ie host has 32 GB of RAM, but the container QEMU is in only has 1 GB permitted.

I don't have much of a better suggestion, as I don't think we want to get into reading the cgroups memory limits. It does feel like the assumption we can blindly use a 1GB cache though is invalid even with this patch applied.

Regards,
Daniel

Signature · 3 lines
|: 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 :|
#17 lore raw

Re: [PATCH v1 5/5] accel/tcg: better handle memory constrained systems

Quoted 28 lines · Daniel P. Berrangé
Quoted 27 lines · Alex Bennée

It turns out there are some 64 bit systems that have relatively low amounts of physical memory available to them (typically CI system). Even with swapping available a 1GB translation buffer that fills up can put the machine under increased memory pressure. Detect these low memory situations and reduce tb_size appropriately.

Fixes 600e17b261
Cc BALATON Zoltan <[email protected]>
Cc Christian Ehrhardt <[email protected]>
---
 accel/tcg/translate-all.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
accel/tcg/translate-all.c +4 -1
index 2afa46bd2b1..2ff0ba6d19b 100644
@@ -976,7 +976,12 @@ static inline size_t size_code_gen_buffer(size_t tb_size)
 {
     /* Size the buffer.  */
     if (tb_size == 0) {
-        tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
+        size_t phys_mem = qemu_get_host_physmem();
+        if (phys_mem > 0 && phys_mem < (2 * DEFAULT_CODE_GEN_BUFFER_SIZE)) {
+            tb_size = phys_mem / 4;
+        } else {
Quoted 8 lines

accel/tcg/translate-all.c static inline size_t size_code_gen_buffer(size_t tb_size) · line 984

Quoted 2 lines
accel/tcg/translate-all.c +2 -0
+            tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
+        }

I'm not convinced this is going to work when running QEMU inside a container environment with RAM cap, because the physmem level is completely unrelated to the RAM the container is permitted to actually use in practice. ie host has 32 GB of RAM, but the container QEMU is in only has 1 GB permitted.

What will happen when the mmap happens? Will a capped container limit the attempted mmap? I would hope the container case at least gave different feedback than a "silent" OOM.

Quoted 7 lines

I don't have much of a better suggestion, as I don't think we want to get into reading the cgroups memory limits. It does feel like the assumption we can blindly use a 1GB cache though is invalid even with this patch applied.

Regards,
Daniel

Signature · 1 line
Alex Bennée
#18 lore raw

Re: [PATCH v1 5/5] accel/tcg: better handle memory constrained systems

Quoted 40 lines · Alex Bennée
Quoted 28 lines · Daniel P. Berrangé
Quoted 27 lines · Alex Bennée

It turns out there are some 64 bit systems that have relatively low amounts of physical memory available to them (typically CI system). Even with swapping available a 1GB translation buffer that fills up can put the machine under increased memory pressure. Detect these low memory situations and reduce tb_size appropriately.

Fixes 600e17b261
Cc BALATON Zoltan <[email protected]>
Cc Christian Ehrhardt <[email protected]>
---
 accel/tcg/translate-all.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
accel/tcg/translate-all.c +4 -1
index 2afa46bd2b1..2ff0ba6d19b 100644
@@ -976,7 +976,12 @@ static inline size_t size_code_gen_buffer(size_t tb_size)
 {
     /* Size the buffer.  */
     if (tb_size == 0) {
-        tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
+        size_t phys_mem = qemu_get_host_physmem();
+        if (phys_mem > 0 && phys_mem < (2 * DEFAULT_CODE_GEN_BUFFER_SIZE)) {
+            tb_size = phys_mem / 4;
+        } else {
Quoted 8 lines

accel/tcg/translate-all.c static inline size_t size_code_gen_buffer(size_t tb_size) · line 984

Quoted 2 lines
accel/tcg/translate-all.c +2 -0
+            tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
+        }

I'm not convinced this is going to work when running QEMU inside a container environment with RAM cap, because the physmem level is completely unrelated to the RAM the container is permitted to actually use in practice. ie host has 32 GB of RAM, but the container QEMU is in only has 1 GB permitted.

Quoted 3 lines

What will happen when the mmap happens? Will a capped container limit the attempted mmap? I would hope the container case at least gave different feedback than a "silent" OOM.

IIRC it should trigger the OOM killer on process(s) within the container.

Regards,
Daniel

Signature · 3 lines
|: 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 :|