Skip to contentSkip to conversation

RFC: fix for random Qemu crashes

Qemu-devel J. Mayer 6 participants 17 messages Nov 15, 2007 – Nov 17, 2007 Kbuild

lore
Share & tools
lore

J. Mayer

author
#0 lore raw

Some may have experienced of having some Qemu builds crashing, apparently at random places, but in a reproducable way.
I found one reason for this crashes: it appears that with the growth of the op.c file, there may be cases where we could reach the inlining limits of gcc. In such a case, gcc would not inline some declared "inline" function but would emit a call and provide a separate function. Unfortunately, this is not acceptable in op.o context as it will slowdown the emulation and because the call is likely to break the specific compilation rules (ie reserved registers) used while compiling op.o
I found some workaround to avoid this behavior and I'd like to get opinions about it.
The first idea is to change all occurences of 'inline' with 'always_inline' in all headers included in op.c. It then appeared to me that always_inline is not globally declared and that the definition is duplicated in vl.h and exec-all.h. But it's not declared in darwin-user/qemu.h and linux-user/qemu.h, which is not consistent with the declaration in vl.h. Further investigations showed me that the osdep.h header is the one that is actually included everywhere. Even if those are more compiler than OS dependent, I decided to move the definitions for glue, tostring, likely, unlikely, always_inline and REGPARM to this header so they can be globally used. I also changed the include orders in darwin-user/qemu.h to be sure this header will be included first. This patch is attached in common_defs.diff. Giving this patch, I've been able to replace all occurence of 'inline' with 'always_inline' in all headers included from op.c (given the generated .d file). Some would say I'd better add a #define inline always_inline somewhere. I personnally dislike this solution as this kind of macro as it tends to expand recursivally (always_inline definition contains the inline word) and this may lead to compilation warnings or errors in some context; one could do tests using the linux kernel headers to get convinced that it can happen.
Then, I choosed to replace 'inline' by 'always_inline', which is more invasive but have less risks of side effects. The diff is attached in always_inline.diff.
The last thing that helps solve the problem is to change the inlining limits of gcc, at least to compile the op.o file.Unfortunatelly, there is no way to disable those limits (I checked in the source code), then I put them to an arbitrary high level. I also added the -funit-at-a-time switch, as this kind of optimisation would not be relevant in op.o context. The diff is attached in gcc_inline_limits.diff.

Please comment.

Signature · 2 lines
J. Mayer <[email protected]>
Never organized

Index: exec-all.h
===================================================================
RCS file: /sources/qemu/qemu/exec-all.h,v
retrieving revision 1.70

exec-all.h +0 -30
diff -u -d -d -p -r1.70 exec-all.h
@@ -21,36 +21,6 @@
 /* allow to see translation results - the slowdown should be negligible, so we leave it */
 #define DEBUG_DISAS
 
-#ifndef glue
-#define xglue(x, y) x ## y
-#define glue(x, y) xglue(x, y)
-#define stringify(s)	tostring(s)
-#define tostring(s)	#s
-#endif
-
-#ifndef likely
-#if __GNUC__ < 3
-#define __builtin_expect(x, n) (x)
-#endif
-
-#define likely(x)   __builtin_expect(!!(x), 1)
-#define unlikely(x)   __builtin_expect(!!(x), 0)
-#endif
-
-#ifndef always_inline
-#if (__GNUC__ < 3) || defined(__APPLE__)
-#define always_inline inline
-#else
-#define always_inline __attribute__ (( always_inline )) inline
-#endif
-#endif
-
-#ifdef __i386__
-#define REGPARM(n) __attribute((regparm(n)))
-#else
-#define REGPARM(n)
-#endif
-
 /* is_jmp field values */
 #define DISAS_NEXT    0 /* next instruction can be analyzed */
 #define DISAS_JUMP    1 /* only pc was modified dynamically */

Index: osdep.h
===================================================================
RCS file: /sources/qemu/qemu/osdep.h,v
retrieving revision 1.10

osdep.h +37 -0
diff -u -d -d -p -r1.10 osdep.h
@@ -3,6 +3,43 @@
 
 #include <stdarg.h>
 
+#ifndef glue
+#define xglue(x, y) x ## y
+#define glue(x, y) xglue(x, y)
+#define stringify(s)	tostring(s)
+#define tostring(s)	#s
+#endif
+
+#ifndef likely
+#if __GNUC__ < 3
+#define __builtin_expect(x, n) (x)
+#endif
+
+#define likely(x)   __builtin_expect(!!(x), 1)
+#define unlikely(x)   __builtin_expect(!!(x), 0)
+#endif
+
+#ifndef MIN
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif
+#ifndef MAX
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+#endif
+
+#ifndef always_inline
+#if (__GNUC__ < 3) || defined(__APPLE__)
+#define always_inline inline
+#else
+#define always_inline __attribute__ (( always_inline )) inline
+#endif
+#endif
+
+#ifdef __i386__
+#define REGPARM(n) __attribute((regparm(n)))
+#else
+#define REGPARM(n)
+#endif
+
 #define qemu_printf printf
 
 void *qemu_malloc(size_t size);

Index: vl.h
===================================================================
RCS file: /sources/qemu/qemu/vl.h,v
retrieving revision 1.295

vl.h +0 -31
diff -u -d -d -p -r1.295 vl.h
@@ -29,37 +29,6 @@
 /* FIXME: Remove this.  */
 #include "block.h"
 
-#ifndef glue
-#define xglue(x, y) x ## y
-#define glue(x, y) xglue(x, y)
-#define stringify(s)	tostring(s)
-#define tostring(s)	#s
-#endif
-
-#ifndef likely
-#if __GNUC__ < 3
-#define __builtin_expect(x, n) (x)
-#endif
-
-#define likely(x)   __builtin_expect(!!(x), 1)
-#define unlikely(x)   __builtin_expect(!!(x), 0)
-#endif
-
-#ifndef MIN
-#define MIN(a, b) (((a) < (b)) ? (a) : (b))
-#endif
-#ifndef MAX
-#define MAX(a, b) (((a) > (b)) ? (a) : (b))
-#endif
-
-#ifndef always_inline
-#if (__GNUC__ < 3) || defined(__APPLE__)
-#define always_inline inline
-#else
-#define always_inline __attribute__ (( always_inline )) inline
-#endif
-#endif
-
 #include "audio/audio.h"
 
 /* vl.c */

Index: darwin-user/qemu.h
===================================================================
RCS file: /sources/qemu/qemu/darwin-user/qemu.h,v
retrieving revision 1.1

qemu.h +2 -2
diff -u -d -d -p -r1.1 qemu.h
@@ -1,13 +1,13 @@
 #ifndef GEMU_H
 #define GEMU_H
 
-#include "thunk.h"
-
 #include <signal.h>
 #include <string.h>
 
 #include "cpu.h"
 
+#include "thunk.h"
+
 #include "gdbstub.h"
 
 typedef siginfo_t target_siginfo_t;

Index: bswap.h
===================================================================
RCS file: /sources/qemu/qemu/bswap.h,v
retrieving revision 1.6

bswap.h +18 -18
diff -u -d -d -p -r1.6 bswap.h
@@ -43,32 +43,32 @@
 
 #endif /* !HAVE_BYTESWAP_H */
 
-static inline uint16_t bswap16(uint16_t x)
+static always_inline uint16_t bswap16(uint16_t x)
 {
     return bswap_16(x);
 }
 
-static inline uint32_t bswap32(uint32_t x)
+static always_inline uint32_t bswap32(uint32_t x)
 {
     return bswap_32(x);
 }
 
-static inline uint64_t bswap64(uint64_t x)
+static always_inline uint64_t bswap64(uint64_t x)
 {
     return bswap_64(x);
 }
 
-static inline void bswap16s(uint16_t *s)
+static always_inline void bswap16s(uint16_t *s)
 {
     *s = bswap16(*s);
 }
 
-static inline void bswap32s(uint32_t *s)
+static always_inline void bswap32s(uint32_t *s)
 {
     *s = bswap32(*s);
 }
 
-static inline void bswap64s(uint64_t *s)
+static always_inline void bswap64s(uint64_t *s)
 {
     *s = bswap64(*s);
 }
@@ -86,32 +86,32 @@ static inline void bswap64s(uint64_t *s)
 #endif
 
 #define CPU_CONVERT(endian, size, type)\
-static inline type endian ## size ## _to_cpu(type v)\
+static always_inline type endian ## size ## _to_cpu(type v)\
 {\
     return endian ## _bswap(v, size);\
 }\
 \
-static inline type cpu_to_ ## endian ## size(type v)\
+static always_inline type cpu_to_ ## endian ## size(type v)\
 {\
     return endian ## _bswap(v, size);\
 }\
 \
-static inline void endian ## size ## _to_cpus(type *p)\
+static always_inline void endian ## size ## _to_cpus(type *p)\
 {\
     endian ## _bswaps(p, size)\
 }\
 \
-static inline void cpu_to_ ## endian ## size ## s(type *p)\
+static always_inline void cpu_to_ ## endian ## size ## s(type *p)\
 {\
     endian ## _bswaps(p, size)\
 }\
 \
-static inline type endian ## size ## _to_cpup(const type *p)\
+static always_inline type endian ## size ## _to_cpup(const type *p)\
 {\
     return endian ## size ## _to_cpu(*p);\
 }\
 \
-static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
+static always_inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
 {\
      *p = cpu_to_ ## endian ## size(v);\
 }
@@ -138,7 +138,7 @@ CPU_CONVERT(le, 64, uint64_t)
 
 #else
 
-static inline void cpu_to_le16wu(uint16_t *p, uint16_t v)
+static always_inline void cpu_to_le16wu(uint16_t *p, uint16_t v)
 {
     uint8_t *p1 = (uint8_t *)p;
 
@@ -146,7 +146,7 @@ static inline void cpu_to_le16wu(uint16_
     p1[1] = v >> 8;
 }
 
-static inline void cpu_to_le32wu(uint32_t *p, uint32_t v)
+static always_inline void cpu_to_le32wu(uint32_t *p, uint32_t v)
 {
     uint8_t *p1 = (uint8_t *)p;
 
@@ -156,19 +156,19 @@ static inline void cpu_to_le32wu(uint32_
     p1[3] = v >> 24;
 }
 
-static inline uint16_t le16_to_cpupu(const uint16_t *p)
+static always_inline uint16_t le16_to_cpupu(const uint16_t *p)
 {
     const uint8_t *p1 = (const uint8_t *)p;
     return p1[0] | (p1[1] << 8);
 }
 
-static inline uint32_t le32_to_cpupu(const uint32_t *p)
+static always_inline uint32_t le32_to_cpupu(const uint32_t *p)
 {
     const uint8_t *p1 = (const uint8_t *)p;
     return p1[0] | (p1[1] << 8) | (p1[2] << 16) | (p1[3] << 24);
 }
 
-static inline void cpu_to_be16wu(uint16_t *p, uint16_t v)
+static always_inline void cpu_to_be16wu(uint16_t *p, uint16_t v)
 {
     uint8_t *p1 = (uint8_t *)p;
 
@@ -176,7 +176,7 @@ static inline void cpu_to_be16wu(uint16_
     p1[1] = v;
 }
 
-static inline void cpu_to_be32wu(uint32_t *p, uint32_t v)
+static always_inline void cpu_to_be32wu(uint32_t *p, uint32_t v)
 {
     uint8_t *p1 = (uint8_t *)p;
 

Index: cpu-all.h
===================================================================
RCS file: /sources/qemu/qemu/cpu-all.h,v
retrieving revision 1.78

cpu-all.h +78 -78

537 lines not on this page · raw message

Index: dyngen.c
===================================================================
RCS file: /sources/qemu/qemu/dyngen.c,v
retrieving revision 1.57

dyngen.c +1 -0
diff -u -d -d -p -r1.57 dyngen.c
@@ -219,6 +219,7 @@ struct nlist_extended
 
 #endif /* CONFIG_FORMAT_MACH */
 
+#include "osdep.h"
 #include "bswap.h"
 
 enum {

Index: exec-all.h
===================================================================
RCS file: /sources/qemu/qemu/exec-all.h,v
retrieving revision 1.70

exec-all.h +37 -33

224 lines not on this page · raw message

Index: qemu-common.h
===================================================================
RCS file: /sources/qemu/qemu/qemu-common.h,v
retrieving revision 1.1

qemu-common.h +1 -1
diff -u -d -d -p -r1.1 qemu-common.h
@@ -36,7 +36,7 @@ extern int qemu_ftruncate64(int, int64_t
 #define ftruncate qemu_ftruncate64
 
 
-static inline char *realpath(const char *path, char *resolved_path)
+static always_inline char *realpath(const char *path, char *resolved_path)
 {
     _fullpath(resolved_path, path, _MAX_PATH);
     return resolved_path;

Index: softmmu-semi.h
===================================================================
RCS file: /sources/qemu/qemu/softmmu-semi.h,v
retrieving revision 1.3

softmmu-semi.h +4 -3
diff -u -d -d -p -r1.3 softmmu-semi.h
@@ -7,14 +7,14 @@
  * This code is licenced under the GPL
  */
 
-static inline uint32_t softmmu_tget32(CPUState *env, uint32_t addr)
+static always_inline uint32_t softmmu_tget32(CPUState *env, uint32_t addr)
 {
     uint32_t val;
 
     cpu_memory_rw_debug(env, addr, (uint8_t *)&val, 4, 0);
     return tswap32(val);
 }
-static inline uint32_t softmmu_tget8(CPUState *env, uint32_t addr)
+static always_inline uint32_t softmmu_tget8(CPUState *env, uint32_t addr)
 {
     uint8_t val;
 
@@ -24,7 +24,8 @@ static inline uint32_t softmmu_tget8(CPU
 #define tget32(p) softmmu_tget32(env, p)
 #define tget8(p) softmmu_tget8(env, p)
 
-static inline void softmmu_tput32(CPUState *env, uint32_t addr, uint32_t val)
+static always_inline void softmmu_tput32(CPUState *env,
+                                         uint32_t addr, uint32_t val)
 {
     val = tswap32(val);
     cpu_memory_rw_debug(env, addr, (uint8_t *)&val, 4, 1);

Index: softmmu_header.h
===================================================================
RCS file: /sources/qemu/qemu/softmmu_header.h,v
retrieving revision 1.18

softmmu_header.h +12 -10
diff -u -d -d -p -r1.18 softmmu_header.h
@@ -79,7 +79,7 @@ void REGPARM(2) glue(glue(__st, SUFFIX),
 
 #define CPU_TLB_ENTRY_BITS 4
 
-static inline RES_TYPE glue(glue(ld, USUFFIX), MEMSUFFIX)(target_ulong ptr)
+static always_inline RES_TYPE glue(glue(ld, USUFFIX), MEMSUFFIX)(target_ulong ptr)
 {
     int res;
 
@@ -122,7 +122,7 @@ static inline RES_TYPE glue(glue(ld, USU
 }
 
 #if DATA_SIZE <= 2
-static inline int glue(glue(lds, SUFFIX), MEMSUFFIX)(target_ulong ptr)
+static always_inline int glue(glue(lds, SUFFIX), MEMSUFFIX)(target_ulong ptr)
 {
     int res;
 
@@ -169,7 +169,8 @@ static inline int glue(glue(lds, SUFFIX)
 }
 #endif
 
-static inline void glue(glue(st, SUFFIX), MEMSUFFIX)(target_ulong ptr, RES_TYPE v)
+static always_inline void glue(glue(st, SUFFIX), MEMSUFFIX)(target_ulong ptr,
+                                                            RES_TYPE v)
 {
     asm volatile ("movl %0, %%edx\n"
                   "movl %0, %%eax\n"
@@ -223,7 +224,7 @@ static inline void glue(glue(st, SUFFIX)
 
 /* generic load/store macros */
 
-static inline RES_TYPE glue(glue(ld, USUFFIX), MEMSUFFIX)(target_ulong ptr)
+static always_inline RES_TYPE glue(glue(ld, USUFFIX), MEMSUFFIX)(target_ulong ptr)
 {
     int index;
     RES_TYPE res;
@@ -245,7 +246,7 @@ static inline RES_TYPE glue(glue(ld, USU
 }
 
 #if DATA_SIZE <= 2
-static inline int glue(glue(lds, SUFFIX), MEMSUFFIX)(target_ulong ptr)
+static always_inline int glue(glue(lds, SUFFIX), MEMSUFFIX)(target_ulong ptr)
 {
     int res, index;
     target_ulong addr;
@@ -270,7 +271,8 @@ static inline int glue(glue(lds, SUFFIX)
 
 /* generic store macro */
 
-static inline void glue(glue(st, SUFFIX), MEMSUFFIX)(target_ulong ptr, RES_TYPE v)
+static always_inline void glue(glue(st, SUFFIX), MEMSUFFIX)(target_ulong ptr,
+                                                            RES_TYPE v)
 {
     int index;
     target_ulong addr;
@@ -296,7 +298,7 @@ static inline void glue(glue(st, SUFFIX)
 #if ACCESS_TYPE != (NB_MMU_MODES + 1)
 
 #if DATA_SIZE == 8
-static inline float64 glue(ldfq, MEMSUFFIX)(target_ulong ptr)
+static always_inline float64 glue(ldfq, MEMSUFFIX)(target_ulong ptr)
 {
     union {
         float64 d;
@@ -306,7 +308,7 @@ static inline float64 glue(ldfq, MEMSUFF
     return u.d;
 }
 
-static inline void glue(stfq, MEMSUFFIX)(target_ulong ptr, float64 v)
+static always_inline void glue(stfq, MEMSUFFIX)(target_ulong ptr, float64 v)
 {
     union {
         float64 d;
@@ -318,7 +320,7 @@ static inline void glue(stfq, MEMSUFFIX)
 #endif /* DATA_SIZE == 8 */
 
 #if DATA_SIZE == 4
-static inline float32 glue(ldfl, MEMSUFFIX)(target_ulong ptr)
+static always_inline float32 glue(ldfl, MEMSUFFIX)(target_ulong ptr)
 {
     union {
         float32 f;
@@ -328,7 +330,7 @@ static inline float32 glue(ldfl, MEMSUFF
     return u.f;
 }
 
-static inline void glue(stfl, MEMSUFFIX)(target_ulong ptr, float32 v)
+static always_inline void glue(stfl, MEMSUFFIX)(target_ulong ptr, float32 v)
 {
     union {
         float32 f;

Index: softmmu_template.h
===================================================================
RCS file: /sources/qemu/qemu/softmmu_template.h,v
retrieving revision 1.19

softmmu_template.h +6 -6
diff -u -d -d -p -r1.19 softmmu_template.h
@@ -50,8 +50,8 @@
 static DATA_TYPE glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(target_ulong addr,
                                                         int mmu_idx,
                                                         void *retaddr);
-static inline DATA_TYPE glue(io_read, SUFFIX)(target_phys_addr_t physaddr,
-                                              target_ulong tlb_addr)
+static always_inline DATA_TYPE glue(io_read, SUFFIX)(target_phys_addr_t physaddr,
+                                                     target_ulong tlb_addr)
 {
     DATA_TYPE res;
     int index;
@@ -183,10 +183,10 @@ static void glue(glue(slow_st, SUFFIX), 
                                                    int mmu_idx,
                                                    void *retaddr);
 
-static inline void glue(io_write, SUFFIX)(target_phys_addr_t physaddr,
-                                          DATA_TYPE val,
-                                          target_ulong tlb_addr,
-                                          void *retaddr)
+static always_inline void glue(io_write, SUFFIX)(target_phys_addr_t physaddr,
+                                                 DATA_TYPE val,
+                                                 target_ulong tlb_addr,
+                                                 void *retaddr)
 {
     int index;
 

Index: vl.h
===================================================================
RCS file: /sources/qemu/qemu/vl.h,v
retrieving revision 1.295

vl.h +18 -17
diff -u -d -d -p -r1.295 vl.h
@@ -319,12 +288,13 @@ struct DisplayState {
                           uint8_t *image, uint8_t *mask);
 };
 
-static inline void dpy_update(DisplayState *s, int x, int y, int w, int h)
+static always_inline void dpy_update(DisplayState *s, int x,
+                                     int y, int w, int h)
 {
     s->dpy_update(s, x, y, w, h);
 }
 
-static inline void dpy_resize(DisplayState *s, int w, int h)
+static always_inline void dpy_resize(DisplayState *s, int w, int h)
 {
     s->dpy_resize(s, w, h);
 }
@@ -464,42 +434,42 @@ unsigned int qemu_get_be16(QEMUFile *f);
 unsigned int qemu_get_be32(QEMUFile *f);
 uint64_t qemu_get_be64(QEMUFile *f);
 
-static inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv)
+static always_inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv)
 {
     qemu_put_be64(f, *pv);
 }
 
-static inline void qemu_put_be32s(QEMUFile *f, const uint32_t *pv)
+static always_inline void qemu_put_be32s(QEMUFile *f, const uint32_t *pv)
 {
     qemu_put_be32(f, *pv);
 }
 
-static inline void qemu_put_be16s(QEMUFile *f, const uint16_t *pv)
+static always_inline void qemu_put_be16s(QEMUFile *f, const uint16_t *pv)
 {
     qemu_put_be16(f, *pv);
 }
 
-static inline void qemu_put_8s(QEMUFile *f, const uint8_t *pv)
+static always_inline void qemu_put_8s(QEMUFile *f, const uint8_t *pv)
 {
     qemu_put_byte(f, *pv);
 }
 
-static inline void qemu_get_be64s(QEMUFile *f, uint64_t *pv)
+static always_inline void qemu_get_be64s(QEMUFile *f, uint64_t *pv)
 {
     *pv = qemu_get_be64(f);
 }
 
-static inline void qemu_get_be32s(QEMUFile *f, uint32_t *pv)
+static always_inline void qemu_get_be32s(QEMUFile *f, uint32_t *pv)
 {
     *pv = qemu_get_be32(f);
 }
 
-static inline void qemu_get_be16s(QEMUFile *f, uint16_t *pv)
+static always_inline void qemu_get_be16s(QEMUFile *f, uint16_t *pv)
 {
     *pv = qemu_get_be16(f);
 }
 
-static inline void qemu_get_8s(QEMUFile *f, uint8_t *pv)
+static always_inline void qemu_get_8s(QEMUFile *f, uint8_t *pv)
 {
     *pv = qemu_get_byte(f);
 }
@@ -1021,7 +993,7 @@ struct clk_setup_t {
     clk_setup_cb cb;
     void *opaque;
 };
-static inline void clk_setup (clk_setup_t *clk, uint32_t freq)
+static always_inline void clk_setup (clk_setup_t *clk, uint32_t freq)
 {
     if (clk->cb != NULL)
         (*clk->cb)(clk->opaque, freq);
@@ -1054,16 +1026,16 @@ extern QEMUMachine ss5_machine, ss10_mac
 void *iommu_init(target_phys_addr_t addr);
 void sparc_iommu_memory_rw(void *opaque, target_phys_addr_t addr,
                                  uint8_t *buf, int len, int is_write);
-static inline void sparc_iommu_memory_read(void *opaque,
-                                           target_phys_addr_t addr,
-                                           uint8_t *buf, int len)
+static always_inline void sparc_iommu_memory_read(void *opaque,
+                                                  target_phys_addr_t addr,
+                                                  uint8_t *buf, int len)
 {
     sparc_iommu_memory_rw(opaque, addr, buf, len, 0);
 }
 
-static inline void sparc_iommu_memory_write(void *opaque,
-                                            target_phys_addr_t addr,
-                                            uint8_t *buf, int len)
+static always_inline void sparc_iommu_memory_write(void *opaque,
+                                                   target_phys_addr_t addr,
+                                                   uint8_t *buf, int len)
 {
     sparc_iommu_memory_rw(opaque, addr, buf, len, 1);
 }

Index: Makefile.target
===================================================================
RCS file: /sources/qemu/qemu/Makefile.target,v
retrieving revision 1.223

Makefile.target +4 -0
diff -u -d -d -p -r1.223 Makefile.target
@@ -117,6 +116,10 @@ OP_CFLAGS+=$(call cc-option, -fno-align-
 OP_CFLAGS+=$(call cc-option, -fno-align-jumps, "")
 OP_CFLAGS+=$(call cc-option, -fno-align-functions, $(call cc-option, -malign-functions=0, ""))
 OP_CFLAGS+=$(call cc-option, -fno-section-anchors, "")
+OP_CFLAGS+=$(call cc-option, -fno-unit-at-a-time, "")
+OP_CFLAGS+=$(call cc-option, --param inline-unit-growth=1000000, "")
+OP_CFLAGS+=$(call cc-option, -finline-limit=100000, "")
+OP_CFLAGS+=$(call cc-option, --param large-function-growth=100000, "")
 
 ifeq ($(ARCH),i386)
 HELPER_CFLAGS+=-fomit-frame-pointer
#1 lore raw

Hi,

Quoted 16 lines · J. Mayer

Some may have experienced of having some Qemu builds crashing, apparently at random places, but in a reproducable way.
I found one reason for this crashes: it appears that with the growth of the op.c file, there may be cases where we could reach the inlining limits of gcc. In such a case, gcc would not inline some declared "inline" function but would emit a call and provide a separate function. Unfortunately, this is not acceptable in op.o context as it will slowdown the emulation and because the call is likely to break the specific compilation rules (ie reserved registers) used while compiling op.o
I found some workaround to avoid this behavior and I'd like to get opinions about it.
The first idea is to change all occurences of 'inline' with 'always_inline' in all headers included in op.c. It then appeared to me that always_inline is not globally declared and that the definition is duplicated in vl.h and exec-all.h. But it's not declared in

Quoted 8 lines

darwin-user/qemu.h and linux-user/qemu.h, which is not consistent with the declaration in vl.h. Further investigations showed me that the osdep.h header is the one that is actually included everywhere. Even if those are more compiler than OS dependent, I decided to move the definitions for glue, tostring, likely, unlikely, always_inline and REGPARM to this header so they can be globally used. I also changed the include orders in darwin-user/qemu.h to be sure this header will be included first. This patch is attached in common_defs.diff.

I had also noticed that glue and tostring definitions were present in three places in qemu and it seemed wrong to me, so I'm in favour of this patch. I can't say much about the other patches.

After armv6/armv7 support was merged on Sunday, I had a consistent segfaults in the generated code and I tracked it down to cpsr_write function not being inlined properly because it grew in size. Changing the inline to always_inline helped but we decided to instead move the function to target-arm/helper.c. I don't know which approach is better, the performance hit shouldn't be notable (in case of ARM cpsr).

Quoted 18 lines

Giving this patch, I've been able to replace all occurence of 'inline' with 'always_inline' in all headers included from op.c (given the generated .d file). Some would say I'd better add a #define inline always_inline somewhere. I personnally dislike this solution as this kind of macro as it tends to expand recursivally (always_inline definition contains the inline word) and this may lead to compilation warnings or errors in some context; one could do tests using the linux kernel headers to get convinced that it can happen.
Then, I choosed to replace 'inline' by 'always_inline', which is more invasive but have less risks of side effects. The diff is attached in always_inline.diff.
The last thing that helps solve the problem is to change the inlining limits of gcc, at least to compile the op.o file.Unfortunatelly, there is no way to disable those limits (I checked in the source code), then I put them to an arbitrary high level. I also added the -funit-at-a-time switch, as this kind of optimisation would not be relevant in op.o context. The diff is attached in gcc_inline_limits.diff.

Quoted 7 lines

Please comment.

--
J. Mayer <[email protected]>
Never organized

Regards

author
#2 lore raw
Quoted 32 lines · andrzej zaborowski

Hi,

Quoted 16 lines · J. Mayer

Some may have experienced of having some Qemu builds crashing, apparently at random places, but in a reproducable way.
I found one reason for this crashes: it appears that with the growth of the op.c file, there may be cases where we could reach the inlining limits of gcc. In such a case, gcc would not inline some declared "inline" function but would emit a call and provide a separate function. Unfortunately, this is not acceptable in op.o context as it will slowdown the emulation and because the call is likely to break the specific compilation rules (ie reserved registers) used while compiling op.o
I found some workaround to avoid this behavior and I'd like to get opinions about it.
The first idea is to change all occurences of 'inline' with 'always_inline' in all headers included in op.c. It then appeared to me that always_inline is not globally declared and that the definition is duplicated in vl.h and exec-all.h. But it's not declared in

Quoted 8 lines

darwin-user/qemu.h and linux-user/qemu.h, which is not consistent with the declaration in vl.h. Further investigations showed me that the osdep.h header is the one that is actually included everywhere. Even if those are more compiler than OS dependent, I decided to move the definitions for glue, tostring, likely, unlikely, always_inline and REGPARM to this header so they can be globally used. I also changed the include orders in darwin-user/qemu.h to be sure this header will be included first. This patch is attached in common_defs.diff.

I had also noticed that glue and tostring definitions were present in three places in qemu and it seemed wrong to me, so I'm in favour of this patch. I can't say much about the other patches.

Quoted 7 lines

After armv6/armv7 support was merged on Sunday, I had a consistent segfaults in the generated code and I tracked it down to cpsr_write function not being inlined properly because it grew in size. Changing the inline to always_inline helped but we decided to instead move the function to target-arm/helper.c. I don't know which approach is better, the performance hit shouldn't be notable (in case of ARM cpsr).

When the problem appears in rarely used or large functions, moving them to helpers might be the proper solution. When it appears in load/store functions (which is the case I experienced), this would not be acceptable, as those functions are used very often and are really time critical. Furthermore, this problem is likely to be encountered more often as the targets features increase and then need to be solved, even if using more helpers might be a work-around to temporary hide the problem.

[...]

Signature · 2 lines
J. Mayer <[email protected]>
Never organized
#3 lore raw
In reply to J. Mayer

J. Mayer kirjoitti:

Quoted 10 lines

Some may have experienced of having some Qemu builds crashing, apparently at random places, but in a reproducable way.
I found one reason for this crashes: it appears that with the growth of the op.c file, there may be cases where we could reach the inlining limits of gcc. In such a case, gcc would not inline some declared "inline" function but would emit a call and provide a separate function. Unfortunately, this is not acceptable in op.o context as it will slowdown the emulation and because the call is likely to break the specific compilation rules (ie reserved registers) used while compiling op.o

Does -winline give a warning when this happens?

-- Heikki Lindholm

#4 lore raw
Quoted 12 lines · Heikki Lindholm

J. Mayer kirjoitti:

Quoted 10 lines

Some may have experienced of having some Qemu builds crashing, apparently at random places, but in a reproducable way.
I found one reason for this crashes: it appears that with the growth of the op.c file, there may be cases where we could reach the inlining limits of gcc. In such a case, gcc would not inline some declared "inline" function but would emit a call and provide a separate function. Unfortunately, this is not acceptable in op.o context as it will slowdown the emulation and because the call is likely to break the specific compilation rules (ie reserved registers) used while compiling op.o

Quoted 1 line

Does -winline give a warning when this happens?

I did not check this, but getting a warning won't help us a lot. The generated Qemu executable would still crash.

Signature · 1 line
Jocelyn Mayer <[email protected]>
#5 lore raw

Jocelyn Mayer kirjoitti:

Quoted 12 lines · Heikki Lindholm
Quoted 11 lines

J. Mayer kirjoitti:

Quoted 10 lines

Some may have experienced of having some Qemu builds crashing, apparently at random places, but in a reproducable way.
I found one reason for this crashes: it appears that with the growth of the op.c file, there may be cases where we could reach the inlining limits of gcc. In such a case, gcc would not inline some declared "inline" function but would emit a call and provide a separate function. Unfortunately, this is not acceptable in op.o context as it will slowdown the emulation and because the call is likely to break the specific compilation rules (ie reserved registers) used while compiling op.o

Quoted 4 lines
Quoted 1 line

Does -winline give a warning when this happens?

I did not check this, but getting a warning won't help us a lot. The generated Qemu executable would still crash.

But a warning makes the problem more noticable. I tried -Winline and it does give warnings, but I'm not sure whether I'm hitting the same behaviour you see. I silenced the lot with --param-max-inline-insns-single=4000

-- Heikki Lindholm

#6 lore raw
Quoted 13 lines · Heikki Lindholm

Jocelyn Mayer kirjoitti:

Quoted 12 lines · Heikki Lindholm
Quoted 11 lines

J. Mayer kirjoitti:

Quoted 10 lines

Some may have experienced of having some Qemu builds crashing, apparently at random places, but in a reproducable way.
I found one reason for this crashes: it appears that with the growth of the op.c file, there may be cases where we could reach the inlining limits of gcc. In such a case, gcc would not inline some declared "inline" function but would emit a call and provide a separate function. Unfortunately, this is not acceptable in op.o context as it will slowdown the emulation and because the call is likely to break the specific compilation rules (ie reserved registers) used while compiling op.o

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Does -winline give a warning when this happens?

I did not check this, but getting a warning won't help us a lot. The generated Qemu executable would still crash.

But a warning makes the problem more noticable.

yes, sure.

Quoted 4 lines

I tried -Winline and it
does give warnings, but I'm not sure whether I'm hitting the same behaviour you see. I silenced the lot with --param-max-inline-insns-single=4000

This is not sufficient. You will still have problems if triggering the growth limit of a single function or the whole compilation unit. Furthermore, 4000 is not a lot, I think it should be set to a very high value we could never reach in real cases.

I did a build with current CVS and the -Winline switch set, using gcc-3.4.6 on a x86_64 host. This showed me more problems than I expected:

while compiling slirp code, I got a lot of warnings from slirp headers:
warning: inlining failed in call to 'slirp_remque': function body not available
Those seem to be real bugs to be fixed.

from linux-user/syscall.c, I got a lot of warnings "--param large-function-growth limit reached" in tswap32/tswap64 functions. This does not seem to be critical, as syscall is actually a slow operation.

from target-i386/helper.c, I got a gew warnings "--param max-inline-insns-single limit reached"

from target-arm/translate.c, got a few "--param max-inline-insns-single limit reached"

from target-sparc/translate.c, got a few "--param max-inline-insns-single limit reached after inlining into the callee"

from target-mips/translate.c: got a lot of "--param inline-unit-growth limit reached" from gen-op.h, which can be quite a problem from target-mips/op_helper.c: a lot of " --param inline-unit-growth
limit reached", mostly for load/store functions, which may be
problematic.

from target-alpha/translate.c: got a lot of "--param inline-unit-growth limit reached"

More warnings are issued for full system emulation:
from target-i386/translate.c: a lot of "--param large-function-growth limit reached" from softmmu_header.h, which means code fetch is done via function, which is not great.

from hw/pxa2xx.c: "--param large-function-growth limit reached". Should not be problematic.

from target-ppc/helper.c: lots of "--param inline-unit-growth limit reached" for load/store functions, which may be problematic

I noticed no warnings from target-ppc/op.c. But I've seen the bug triggered with the build I done before commiting wy latest patches (with those patches applied to a clean repository...) for the PowerPC 64 target, resulting in an emulator that would crash while entering user mode (with the CDROM images I tested).
I'm not sure if there could be some cases gcc won't warn even when not inlining an declared inline function, or if the bug what not triggered this time, for an unknown reason as I just did a build from a checkout with just the -Winline flag added (then the build should be quite equivalent to the one I did this morning before commiting).

Signature · 1 line
Jocelyn Mayer <[email protected]>
#7 lore raw
In reply to Heikki Lindholm
Quoted 12 lines · Heikki Lindholm

J. Mayer kirjoitti:

Quoted 10 lines

Some may have experienced of having some Qemu builds crashing, apparently at random places, but in a reproducable way.
I found one reason for this crashes: it appears that with the growth of the op.c file, there may be cases where we could reach the inlining limits of gcc. In such a case, gcc would not inline some declared "inline" function but would emit a call and provide a separate function. Unfortunately, this is not acceptable in op.o context as it will slowdown the emulation and because the call is likely to break the specific compilation rules (ie reserved registers) used while compiling op.o

Quoted 1 line

Does -winline give a warning when this happens?

And can it be repaired with __attribute__((__always_inline__))?

-- Jamie

#8 lore raw
Quoted 13 lines · Jamie Lokier
Quoted 12 lines · Heikki Lindholm

J. Mayer kirjoitti:

Quoted 10 lines

Some may have experienced of having some Qemu builds crashing, apparently at random places, but in a reproducable way.
I found one reason for this crashes: it appears that with the growth of the op.c file, there may be cases where we could reach the inlining limits of gcc. In such a case, gcc would not inline some declared "inline" function but would emit a call and provide a separate function. Unfortunately, this is not acceptable in op.o context as it will slowdown the emulation and because the call is likely to break the specific compilation rules (ie reserved registers) used while compiling op.o

Quoted 3 lines
Quoted 1 line

Does -winline give a warning when this happens?

And can it be repaired with __attribute__((__always_inline__))?

As I already reported in the previous message, this helps solve the problem but is not sufficient. Please refer to the proposed patches and the rest of the discussion.

Further invastigation also showed me that there may be problems while compiling translate-op.c. It seems the solution would be to change the gcc inlining limits into the BASE_CFLAGS too _and_ define functions in dyngen.h as always_inline. This would also avoid most inline related warnings during the compilation (but maybe not solve all cases, as seen in the op.c case).
Reading gcc source code showed me there are cases where a function could be not inline when marked 'inline' without generating any warning message. What I don't know is if those cases are likely to happen during normal compilations, but as the op.c compilation seems to be buggy and never emit those warnings, I guess we can reach those cases.

Signature · 1 line
Jocelyn Mayer <[email protected]>
#9 lore raw
In reply to J. Mayer
Quoted 5 lines

Then, I choosed to replace 'inline' by 'always_inline', which is more invasive but have less risks of side effects. The diff is attached in always_inline.diff.
The last thing that helps solve the problem is to change the inlining limits of gcc, at least to compile the op.o file.

Presumably we only need one of the last two patches? It seems rather pointless to have always_inline *and* change the inlining heuristics.

I'm ok with using always_inline for op.o (and things it uses directly) as this is required for correctness. I'm not convinced that that using always_inline everywhere is such a good idea.

Paul

#10 lore raw
Quoted 10 lines · Paul Brook
Quoted 5 lines

Then, I choosed to replace 'inline' by 'always_inline', which is more invasive but have less risks of side effects. The diff is attached in always_inline.diff.
The last thing that helps solve the problem is to change the inlining limits of gcc, at least to compile the op.o file.

Presumably we only need one of the last two patches? It seems rather pointless to have always_inline *and* change the inlining heuristics.

From the tests I made, it seems that adding always_inline helps but

unfortunatelly does not solve all cases. Should check in the gcc source code why it is so...

Quoted 3 lines

I'm ok with using always_inline for op.o (and things it uses directly) as this is required for correctness. I'm not convinced that that using always_inline everywhere is such a good idea.

That's exactly what I did: I changed 'inline' to 'always_inline' in headers that are included by op.c, I did not made any change in other headers. If some of the functions I changed should not be changed, that means that there is another bug, ie those functions should not be used by op.c in any way then should be moved to other headers, or maybe some of those headers should not be included directly or indirectly in op.c... If you see such cases, then we may better fix those issues first...

Signature · 1 line
Jocelyn Mayer <[email protected]>
#11 lore raw
Quoted 15 lines · Jocelyn Mayer
Quoted 10 lines · Paul Brook
Quoted 5 lines

Then, I choosed to replace 'inline' by 'always_inline', which is more invasive but have less risks of side effects. The diff is attached in always_inline.diff.
The last thing that helps solve the problem is to change the inlining limits of gcc, at least to compile the op.o file.

Presumably we only need one of the last two patches? It seems rather pointless to have always_inline *and* change the inlining heuristics.

From the tests I made, it seems that adding always_inline helps but

unfortunatelly does not solve all cases. Should check in the gcc source code why it is so...

Quoted 7 lines
Quoted 3 lines

I'm ok with using always_inline for op.o (and things it uses directly) as this is required for correctness. I'm not convinced that that using always_inline everywhere is such a good idea.

That's exactly what I did: I changed 'inline' to 'always_inline' in headers that are included by op.c, I did not made any change in other headers.

I think a line like

#define inline __attribute__ (( always_inline )) inline

in dyngen-exec.h should be enough?

Regards

author
#12 lore raw
Quoted 24 lines · andrzej zaborowski
Quoted 15 lines · Jocelyn Mayer
Quoted 10 lines · Paul Brook
Quoted 5 lines

Then, I choosed to replace 'inline' by 'always_inline', which is more invasive but have less risks of side effects. The diff is attached in always_inline.diff.
The last thing that helps solve the problem is to change the inlining limits of gcc, at least to compile the op.o file.

Presumably we only need one of the last two patches? It seems rather pointless to have always_inline *and* change the inlining heuristics.

From the tests I made, it seems that adding always_inline helps but

unfortunatelly does not solve all cases. Should check in the gcc source code why it is so...

Quoted 7 lines
Quoted 3 lines

I'm ok with using always_inline for op.o (and things it uses directly) as this is required for correctness. I'm not convinced that that using always_inline everywhere is such a good idea.

That's exactly what I did: I changed 'inline' to 'always_inline' in headers that are included by op.c, I did not made any change in other headers.

Quoted 5 lines

I think a line like

#define inline __attribute__ (( always_inline )) inline

in dyngen-exec.h should be

As I already pointed it in the first message of the thread, this kind of define would expand recursivelly, which is particullary ugly, and which can in some cases lead to compiler warnings or errors. I already had this kind of problems using the linux kernel headers which preciselly uses this definitition.
But, once again, adding always_inline to functions does not completelly solve the problem (please read the thread !) or at least does not solves it with all gcc versions. The inline growth limits tweaking seems needed too.

Signature · 2 lines
J. Mayer <[email protected]>
Never organized
#13 lore raw
Quoted 9 lines · J. Mayer
Quoted 5 lines · andrzej zaborowski

I think a line like

#define inline __attribute__ (( always_inline )) inline

in dyngen-exec.h should be

As I already pointed it in the first message of the thread, this kind of define would expand recursivelly, [...]

No. A macro is not expanded within its own expansion. See ISO C99:

6.10.3.4 Rescanning and further replacement
[...]
2 If the name of the macro being replaced is found during this
scan of the replacement list (not including the rest of the source file's preprocessing tokens), it is not replaced.

If it still bothers you, you could write it as

        #define inline __attribute__ (( always_inline )) __inline__

since GCC accepts __inline__ as a synonym for inline.

Signature · 2 lines
Ben Pfaff 
http://benpfaff.org
author
#14 lore raw
Quoted 7 lines · Ben Pfaff
Quoted 5 lines · J. Mayer
Quoted 4 lines · andrzej zaborowski

I think a line like

#define inline __attribute__ (( always_inline )) inline

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

in dyngen-exec.h should be

As I already pointed it in the first message of the thread, this kind of define would expand recursivelly, [...]

No. A macro is not expanded within its own expansion. See ISO C99:

I just take a look of what happens in *real life* while compiling the linux kernel which uses such a definition.... As I reported, I had compilation problems due to this behavior and did inspect the preprocessor output and saw this result. I did not check if it happens only with some versions of gcc or if this behavior has been changed with newer releases, I have to admit.

Quoted 10 lines

6.10.3.4 Rescanning and further replacement
[...]
2 If the name of the macro being replaced is found during this
scan of the replacement list (not including the rest of the source file's preprocessing tokens), it is not replaced.

If it still bothers you, you could write it as

        #define inline __attribute__ (( always_inline )) __inline__

since GCC accepts __inline__ as a synonym for inline.

You're right, this would be a good solution to avoid many changes in the code.

Signature · 2 lines
J. Mayer <[email protected]>
Never organized
#15 lore raw
In reply to J. Mayer
Quoted 30 lines · J. Mayer
Quoted 28 lines · andrzej zaborowski
Quoted 15 lines · Jocelyn Mayer
Quoted 10 lines · Paul Brook
Quoted 5 lines

Then, I choosed to replace 'inline' by 'always_inline', which is more invasive but have less risks of side effects. The diff is attached in always_inline.diff.
The last thing that helps solve the problem is to change the inlining limits of gcc, at least to compile the op.o file.

Presumably we only need one of the last two patches? It seems rather pointless to have always_inline *and* change the inlining heuristics.

From the tests I made, it seems that adding always_inline helps but

unfortunatelly does not solve all cases. Should check in the gcc source code why it is so...

Quoted 7 lines
Quoted 3 lines

I'm ok with using always_inline for op.o (and things it uses directly) as this is required for correctness. I'm not convinced that that using always_inline everywhere is such a good idea.

That's exactly what I did: I changed 'inline' to 'always_inline' in headers that are included by op.c, I did not made any change in other headers.

I think a line like

#define inline __attribute__ (( always_inline )) inline

Quoted 7 lines
Quoted 1 line

in dyngen-exec.h should be

As I already pointed it in the first message of the thread, this kind of define would expand recursivelly, which is particullary ugly, and which can in some cases lead to compiler warnings or errors. I already had this kind of problems using the linux kernel headers which preciselly uses this definitition.

My point here is that you can use dyngen-exec.h for the macro so that the functions are only always_inline'd when used in op.c, not in other files, I think that's what pbrook mean too. For example cpsr_write from target-arm/exec.h was used in op.c as well as in vl.c. There's no problem if it isn't inlined in vl.c, the fix should only affect op.c which is a special case, for other files let gcc decide in the way it was designed by gcc authors.

Quoted 3 lines

But, once again, adding always_inline to functions does not completelly solve the problem (please read the thread !) or at least does not solves it with all gcc versions.

Yes, I'm not saying anything about the other part. Regards

author
#16 lore raw
Quoted 39 lines · andrzej zaborowski
Quoted 30 lines · J. Mayer
Quoted 28 lines · andrzej zaborowski
Quoted 15 lines · Jocelyn Mayer
Quoted 10 lines · Paul Brook
Quoted 5 lines

Then, I choosed to replace 'inline' by 'always_inline', which is more invasive but have less risks of side effects. The diff is attached in always_inline.diff.
The last thing that helps solve the problem is to change the inlining limits of gcc, at least to compile the op.o file.

Presumably we only need one of the last two patches? It seems rather pointless to have always_inline *and* change the inlining heuristics.

From the tests I made, it seems that adding always_inline helps but

unfortunatelly does not solve all cases. Should check in the gcc source code why it is so...

Quoted 7 lines
Quoted 3 lines

I'm ok with using always_inline for op.o (and things it uses directly) as this is required for correctness. I'm not convinced that that using always_inline everywhere is such a good idea.

That's exactly what I did: I changed 'inline' to 'always_inline' in headers that are included by op.c, I did not made any change in other headers.

I think a line like

#define inline __attribute__ (( always_inline )) inline

Quoted 7 lines
Quoted 1 line

in dyngen-exec.h should be

As I already pointed it in the first message of the thread, this kind of define would expand recursivelly, which is particullary ugly, and which can in some cases lead to compiler warnings or errors. I already had this kind of problems using the linux kernel headers which preciselly uses this definitition.

Quoted 7 lines

My point here is that you can use dyngen-exec.h for the macro so that the functions are only always_inline'd when used in op.c, not in other files, I think that's what pbrook mean too. For example cpsr_write from target-arm/exec.h was used in op.c as well as in vl.c. There's no problem if it isn't inlined in vl.c, the fix should only affect op.c which is a special case, for other files let gcc decide in the way it was designed by gcc authors.

The problem in op.c is that not inlining lead to crashes. But not inlining functions in many other places would lead to great performances issues.
dyngen-exec.h is included only in cpu-exec.c, host-utils.c, op.c and op_helpers.c (from what I see in the .d files).
But, for example, not inlining code fetch or gen_op_xxx in translate.c is a very bad news for code translation efficiency. Not inlining in exec.c also have a great performance impact.
It seems to me that there are much more cases we want always_inline rule to be applied than cases for which it's not so important. And it never hurts if the inlining is not done in the later case but it can have a great impact if inlining is not done for most functions that are declared inline in Qemu.

[...]

Signature · 2 lines
J. Mayer <[email protected]>
Never organized