Skip to contentSkip to conversation

tools: use xen-tools/libs.h for common definitions

PATCH Superseded Juergen Gross 4 participants 11 messages Feb 27, 2023 – Mar 6, 2023 Arm/actions semi architecture

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover tools: use xen-tools/libs.h for common definitions

There are some macros defined multiple times in tools. Use only a single header file for defining those macros and drop the copies.

V2:
- add patch 1 (Andrew Cooper)

Juergen Gross (4):
tools: rename xen-tools/libs.h file to common-macros.h
tools: add container_of() macro to xen-tools/common-macros.h
tools: get rid of additional min() and max() definitions
tools: add offsetof() to xen-tools/common-macros.h

 tools/console/daemon/io.c                     |  2 +-
 tools/firmware/hvmloader/util.h               | 11 ++--------
 tools/firmware/include/stddef.h               |  4 ++--
 tools/fuzz/cpu-policy/afl-policy-fuzzer.c     |  2 +-
 .../xen-tools/{libs.h => common-macros.h}     | 22 ++++++++++++++++---
 tools/libfsimage/Rules.mk                     |  2 ++
 tools/libfsimage/xfs/fsys_xfs.c               |  4 +---
 tools/libs/call/buffer.c                      |  2 +-
 tools/libs/ctrl/xc_pm.c                       |  2 +-
 tools/libs/ctrl/xc_private.h                  |  2 +-
 tools/libs/foreignmemory/linux.c              |  2 +-
 tools/libs/gnttab/freebsd.c                   |  2 +-
 tools/libs/gnttab/linux.c                     |  2 +-
 tools/libs/guest/xg_core_arm.c                |  2 +-
 tools/libs/guest/xg_cpuid_x86.c               |  2 +-
 tools/libs/guest/xg_dom_arm.c                 |  2 +-
 tools/libs/guest/xg_dom_bzimageloader.c       |  2 +-
 tools/libs/guest/xg_dom_x86.c                 |  2 +-
 tools/libs/guest/xg_sr_common.c               |  2 +-
 tools/libs/light/libxl_internal.h             |  2 +-
 tools/libs/light/libxl_psr.c                  |  2 +-
 tools/libs/stat/xenstat_linux.c               |  2 +-
 tools/libs/vchan/init.c                       |  7 +-----
 tools/misc/xen-access.c                       |  2 +-
 tools/misc/xen-cpuid.c                        |  2 +-
 tools/misc/xen-diag.c                         |  2 +-
 tools/misc/xen-hptool.c                       |  2 +-
 tools/misc/xen-livepatch.c                    |  2 +-
 tools/misc/xen-mfndump.c                      |  2 +-
 tools/misc/xenpm.c                            |  2 +-
 tools/ocaml/libs/mmap/xenmmap_stubs.c         |  2 +-
 tools/ocaml/libs/xc/xenctrl_stubs.c           |  2 +-
 tools/python/xen/lowlevel/xc/xc.c             |  2 +-
 tools/tests/cpu-policy/test-cpu-policy.c      |  2 +-
 .../paging-mempool/test-paging-mempool.c      |  2 +-
 tools/tests/resource/test-resource.c          |  2 +-
 tools/tests/tsx/test-tsx.c                    |  2 +-
 tools/tests/vhpet/emul.h                      | 16 +-------------
 tools/tests/vpci/emul.h                       | 22 +------------------
 tools/tests/x86_emulator/x86-emulate.h        |  7 +-----
 tools/tests/xenstore/test-xenstore.c          |  2 +-
 tools/xenstore/list.h                         |  6 ++---
 tools/xenstore/utils.h                        |  2 +-
 tools/xentrace/analyze.h                      |  2 +-
 tools/xl/xl_cmdtable.c                        |  2 +-
 xen/lib/x86/private.h                         |  2 +-
 46 files changed, 68 insertions(+), 105 deletions(-)
 rename tools/include/xen-tools/{libs.h => common-macros.h} (74%)
Signature · 1 line
2.35.3
#1 lore raw

PATCH v2 1/4 tools: rename xen-tools/libs.h file to common-macros.h

In order to better reflect the contents of the header and to make it more appropriate to use it for different runtime environments like programs, libraries, and firmware, rename the libs.h include file to common-macros.h. Additionally ad a comment pointing out the need to be self-contained.

---
V2:
- new patch

---
 tools/console/daemon/io.c                          |  2 +-
 tools/fuzz/cpu-policy/afl-policy-fuzzer.c          |  2 +-
 .../include/xen-tools/{libs.h => common-macros.h}  | 14 +++++++++++---
 tools/libs/call/buffer.c                           |  2 +-
 tools/libs/ctrl/xc_pm.c                            |  2 +-
 tools/libs/ctrl/xc_private.h                       |  2 +-
 tools/libs/foreignmemory/linux.c                   |  2 +-
 tools/libs/gnttab/freebsd.c                        |  2 +-
 tools/libs/gnttab/linux.c                          |  2 +-
 tools/libs/guest/xg_core_arm.c                     |  2 +-
 tools/libs/guest/xg_cpuid_x86.c                    |  2 +-
 tools/libs/guest/xg_dom_arm.c                      |  2 +-
 tools/libs/guest/xg_dom_bzimageloader.c            |  2 +-
 tools/libs/guest/xg_dom_x86.c                      |  2 +-
 tools/libs/guest/xg_sr_common.c                    |  2 +-
 tools/libs/light/libxl_internal.h                  |  2 +-
 tools/libs/light/libxl_psr.c                       |  2 +-
 tools/libs/stat/xenstat_linux.c                    |  2 +-
 tools/misc/xen-access.c                            |  2 +-
 tools/misc/xen-cpuid.c                             |  2 +-
 tools/misc/xen-diag.c                              |  2 +-
 tools/misc/xen-hptool.c                            |  2 +-
 tools/misc/xen-livepatch.c                         |  2 +-
 tools/misc/xen-mfndump.c                           |  2 +-
 tools/misc/xenpm.c                                 |  2 +-
 tools/ocaml/libs/mmap/xenmmap_stubs.c              |  2 +-
 tools/ocaml/libs/xc/xenctrl_stubs.c                |  2 +-
 tools/python/xen/lowlevel/xc/xc.c                  |  2 +-
 tools/tests/cpu-policy/test-cpu-policy.c           |  2 +-
 tools/tests/paging-mempool/test-paging-mempool.c   |  2 +-
 tools/tests/resource/test-resource.c               |  2 +-
 tools/tests/tsx/test-tsx.c                         |  2 +-
 tools/tests/vhpet/emul.h                           |  2 +-
 tools/tests/x86_emulator/x86-emulate.h             |  2 +-
 tools/tests/xenstore/test-xenstore.c               |  2 +-
 tools/xenstore/utils.h                             |  2 +-
 tools/xentrace/analyze.h                           |  2 +-
 tools/xl/xl_cmdtable.c                             |  2 +-
 xen/lib/x86/private.h                              |  2 +-
 39 files changed, 49 insertions(+), 41 deletions(-)
 rename tools/include/xen-tools/{libs.h => common-macros.h} (83%)
tools/console/daemon/io.c +1 -1
index 682c1f4008..6bfe96715b 100644
@@ -50,7 +50,7 @@
 #include <sys/ioctl.h>
 #include <libutil.h>
 #endif
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 /* Each 10 bits takes ~ 3 digits, plus one, plus one for nul terminator. */
 #define MAX_STRLEN(x) ((sizeof(x) * CHAR_BIT + CHAR_BIT-1) / 10 * 3 + 2)
tools/fuzz/cpu-policy/afl-policy-fuzzer.c +1 -1
index 79a849a044..7d0f274c6c 100644
@@ -8,7 +8,7 @@
 
 #include <getopt.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 #include <xen/lib/x86/cpuid.h>
 #include <xen/lib/x86/msr.h>
 #include <xen/domctl.h>
tools/include/xen-tools/common-macros.h renamed from tools/include/xen-tools/libs.h +11 -3
similarity index 83%
rename from tools/include/xen-tools/libs.h
rename to tools/include/xen-tools/common-macros.h
index bafc90e2f6..f39cd96008 100644
@@ -1,5 +1,13 @@
-#ifndef __XEN_TOOLS_LIBS__
-#define __XEN_TOOLS_LIBS__
+#ifndef __XEN_TOOLS_COMMON_MACROS__
+#define __XEN_TOOLS_COMMON_MACROS__
+
+/*
+ * Caution:
+ *
+ * This header must be completely self-contained. There are no external
+ * references to variables or functions allowed, as the file might be included
+ * for different runtime environments, such as firmware or normal programs.
+ */
 
 #ifndef BUILD_BUG_ON
 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
@@ -67,4 +75,4 @@
 #define __must_check __attribute__((__warn_unused_result__))
 #endif
 
-#endif	/* __XEN_TOOLS_LIBS__ */
+#endif	/* __XEN_TOOLS_COMMON_MACROS__ */
tools/libs/call/buffer.c +1 -1
index 085674d882..2579b8c719 100644
@@ -16,7 +16,7 @@
 #include <errno.h>
 #include <string.h>
 #include <pthread.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "private.h"
 
tools/libs/ctrl/xc_pm.c +1 -1
index 76d7eb7f26..c3a9864bf7 100644
@@ -21,7 +21,7 @@
 #include <stdbool.h>
 #include "xc_private.h"
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 /*
  * Get PM statistic info
tools/libs/ctrl/xc_private.h +1 -1
index ed960c6f30..80dc464c93 100644
@@ -39,7 +39,7 @@
 
 #include <xen/sys/privcmd.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #if defined(HAVE_VALGRIND_MEMCHECK_H) && !defined(NDEBUG) && !defined(__MINIOS__)
 /* Compile in Valgrind client requests? */
tools/libs/foreignmemory/linux.c +1 -1
index 9062117407..12f959765a 100644
@@ -25,7 +25,7 @@
 
 #include <sys/mman.h>
 #include <sys/ioctl.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "private.h"
 
tools/libs/gnttab/freebsd.c +1 -1
index e42ac3fbf3..d69d928a16 100644
@@ -31,7 +31,7 @@
 #include <xen/sys/gntdev.h>
 
 #include <xenctrl.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "private.h"
 
tools/libs/gnttab/linux.c +1 -1
index 5628fd5719..7286c1d4fe 100644
@@ -33,7 +33,7 @@
 #include <xen/sys/gntalloc.h>
 
 #include <xenctrl.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "private.h"
 
tools/libs/guest/xg_core_arm.c +1 -1
index aaabd07558..de30cf0c31 100644
@@ -19,7 +19,7 @@
 #include "xc_private.h"
 #include "xg_core.h"
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 int
 xc_core_arch_gpfn_may_present(struct xc_core_arch_context *arch_ctxt,
tools/libs/guest/xg_cpuid_x86.c +1 -1
index 57f81eb0a0..4542878bbe 100644
@@ -24,7 +24,7 @@
 #include <limits.h>
 #include "xg_private.h"
 #include <xen/hvm/params.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 enum {
 #define XEN_CPUFEATURE(name, value) X86_FEATURE_##name = value,
tools/libs/guest/xg_dom_arm.c +1 -1
index 5e3b76355e..d616dccbd6 100644
@@ -21,7 +21,7 @@
 
 #include <xen/xen.h>
 #include <xen/io/protocols.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "xg_private.h"
 
tools/libs/guest/xg_dom_bzimageloader.c +1 -1
index 764ddbb43e..c6ee6d83e7 100644
@@ -34,7 +34,7 @@
 #include "xg_private.h"
 #include "xg_dom_decompress.h"
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #ifndef __MINIOS__
 
tools/libs/guest/xg_dom_x86.c +1 -1
index 9328fbf804..811d176488 100644
@@ -35,7 +35,7 @@
 #include <xen/arch-x86/hvm/start_info.h>
 #include <xen/io/protocols.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "xg_private.h"
 #include "xenctrl.h"
tools/libs/guest/xg_sr_common.c +1 -1
index 17567ab133..7ccdc3b1f6 100644
@@ -2,7 +2,7 @@
 
 #include "xg_sr_common.h"
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static const char *const dhdr_types[] =
 {
tools/libs/light/libxl_internal.h +1 -1
index ad982d691a..5244fde623 100644
@@ -58,7 +58,7 @@
 #include <xenguest.h>
 #include <xenhypfs.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "xentoollog.h"
 
tools/libs/light/libxl_psr.c +1 -1
index 9ced7d1715..712a55a73d 100644
@@ -16,7 +16,7 @@
 #include "libxl_osdeps.h" /* must come before any other headers */
 #include "libxl_internal.h"
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #define IA32_QM_CTR_ERROR_MASK         (0x3ul << 62)
 
tools/libs/stat/xenstat_linux.c +1 -1
index ebcd65f24a..05d0b07553 100644
@@ -30,7 +30,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <regex.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "xenstat_priv.h"
 
tools/misc/xen-access.c +1 -1
index 0731c20b83..4097eebe6f 100644
@@ -43,7 +43,7 @@
 #include <xenevtchn.h>
 #include <xen/vm_event.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #if defined(__arm__) || defined(__aarch64__)
 #include <xen/arch-arm.h>
tools/misc/xen-cpuid.c +1 -1
index 3ab820571d..8cf6a5ecfe 100644
@@ -10,7 +10,7 @@
 #include <xenctrl.h>
 #include <xenguest.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static uint32_t nr_features;
 
tools/misc/xen-diag.c +1 -1
index 4778914820..cd0c2c1fc6 100644
@@ -11,7 +11,7 @@
 #include <xenctrl.h>
 
 #include <xen/errno.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static xc_interface *xch;
 
tools/misc/xen-hptool.c +1 -1
index 7f17f24942..6d6aec8bad 100644
@@ -5,7 +5,7 @@
 #include <xenctrl.h>
 #include <xenguest.h>
 #include <xenstore.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static xc_interface *xch;
 
tools/misc/xen-livepatch.c +1 -1
index 16a4a47ab7..5bf9d9a32b 100644
@@ -15,7 +15,7 @@
 #include <xenstore.h>
 
 #include <xen/errno.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static xc_interface *xch;
 
tools/misc/xen-mfndump.c +1 -1
index 62121bd241..b32c95e262 100644
@@ -8,7 +8,7 @@
 #include <xenctrl.h>
 #include <xenguest.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #define M2P_SIZE(_m)    ROUNDUP(((_m) * sizeof(xen_pfn_t)), 21)
 #define is_mapped(pfn_type) (!((pfn_type) & 0x80000000UL))
tools/misc/xenpm.c +1 -1
index 4f8cde690a..1bb6187e56 100644
@@ -28,7 +28,7 @@
 #include <inttypes.h>
 #include <sys/time.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #define MAX_PKG_RESIDENCIES 12
 #define MAX_CORE_RESIDENCIES 8
tools/ocaml/libs/mmap/xenmmap_stubs.c +1 -1
index d623ad390e..c85b1fcce7 100644
@@ -21,7 +21,7 @@
 #include <errno.h>
 #include "mmap_stubs.h"
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include <caml/mlvalues.h>
 #include <caml/memory.h>
tools/ocaml/libs/xc/xenctrl_stubs.c +1 -1
index ed1cbafdb4..6ec9ed6d1e 100644
@@ -33,7 +33,7 @@
 #define XC_WANT_COMPAT_MAP_FOREIGN_API
 #include <xenctrl.h>
 #include <xenguest.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "mmap_stubs.h"
 
tools/python/xen/lowlevel/xc/xc.c +1 -1
index cfb2734a99..224671bc02 100644
@@ -22,7 +22,7 @@
 #include <xen/hvm/hvm_info_table.h>
 #include <xen/hvm/params.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 /* Needed for Python versions earlier than 2.3. */
 #ifndef PyMODINIT_FUNC
tools/tests/cpu-policy/test-cpu-policy.c +1 -1
index ed450a0997..d3f24fd6d2 100644
@@ -7,7 +7,7 @@
 #include <string.h>
 #include <err.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 #include <xen/asm/x86-vendors.h>
 #include <xen/lib/x86/cpu-policy.h>
 #include <xen/domctl.h>
tools/tests/paging-mempool/test-paging-mempool.c +1 -1
index b2dfe9b074..1ebc13455a 100644
@@ -8,7 +8,7 @@
 #include <xenctrl.h>
 #include <xenforeignmemory.h>
 #include <xengnttab.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static unsigned int nr_failures;
 #define fail(fmt, ...)                          \
tools/tests/resource/test-resource.c +1 -1
index 0a950072f9..7ae88ea348 100644
@@ -7,7 +7,7 @@
 #include <xenctrl.h>
 #include <xenforeignmemory.h>
 #include <xengnttab.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static unsigned int nr_failures;
 #define fail(fmt, ...)                          \
tools/tests/tsx/test-tsx.c +1 -1
index f11e8c54e0..d6d98c299b 100644
@@ -28,7 +28,7 @@
 
 #include <xenctrl.h>
 #include <xenguest.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "xg_private.h"
 
tools/tests/vhpet/emul.h +1 -1
index b022cc0eab..dfeb10f74e 100644
@@ -24,7 +24,7 @@
 
 #include "hpet.h"
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #define NR_CPUS 8
 
tools/tests/x86_emulator/x86-emulate.h +1 -1
index 18ae40d017..46d4e43cea 100644
@@ -39,7 +39,7 @@
 #include <xen/asm/x86-defns.h>
 #include <xen/asm/x86-vendors.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #define BUG() abort()
 #define ASSERT assert
tools/tests/xenstore/test-xenstore.c +1 -1
index d3574b3fa2..d491dac53b 100644
@@ -31,7 +31,7 @@
 #include <time.h>
 #include <xenstore.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #define TEST_PATH "xenstore-test"
 #define WRITE_BUFFERS_N    10
tools/xenstore/utils.h +1 -1
index 9d012b97c1..028ecb9d7a 100644
@@ -5,7 +5,7 @@
 #include <stdint.h>
 #include <stdio.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "xenstore_lib.h"
 
tools/xentrace/analyze.h +1 -1
index f9d98d8da4..6fefa19639 100644
@@ -3,7 +3,7 @@
 
 #include <stdint.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #define TRC_GEN_MAIN     0
 #define TRC_SCHED_MAIN   1
tools/xl/xl_cmdtable.c +1 -1
index 35182ca196..ccf4d83584 100644
@@ -15,7 +15,7 @@
 #include <string.h>
 
 #include <libxl.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "xl.h"
 
xen/lib/x86/private.h +1 -1
index 2d53bd3ced..60bb82a400 100644
@@ -26,7 +26,7 @@
 #include <xen/asm/msr-index.h>
 #include <xen/asm/x86-vendors.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static inline bool test_bit(unsigned int bit, const void *vaddr)
 {
Signature · 1 line
2.35.3
#2 lore raw

Re: [PATCH v2 1/4] tools: rename xen-tools/libs.h file to common-macros.h

Quoted 5 lines · Juergen Gross
tools/include/xen-tools/common-macros.h renamed from tools/include/xen-tools/libs.h +0 -2
@@ -1,5 +1,13 @@
-#ifndef __XEN_TOOLS_LIBS__
-#define __XEN_TOOLS_LIBS__

tools/include/xen-tools/common-macros.h line 10

Quoted 10 lines
tools/include/xen-tools/common-macros.h renamed from tools/include/xen-tools/libs.h +10 -0
+#ifndef __XEN_TOOLS_COMMON_MACROS__
+#define __XEN_TOOLS_COMMON_MACROS__
+
+/*
+ * Caution:
+ *
+ * This header must be completely self-contained. There are no external
+ * references to variables or functions allowed, as the file might be included
+ * for different runtime environments, such as firmware or normal programs.
+ */

May I ask to go a tiny step further: s/normal/target and build host/ or something alike?

Jan

#3 lore raw

Re: [PATCH v2 1/4] tools: rename xen-tools/libs.h file to common-macros.h

Quoted 11 lines · Jan Beulich
Quoted 10 lines · Juergen Gross
tools/include/xen-tools/common-macros.h renamed from tools/include/xen-tools/libs.h +5 -2
@@ -1,5 +1,13 @@
-#ifndef __XEN_TOOLS_LIBS__
-#define __XEN_TOOLS_LIBS__
+#ifndef __XEN_TOOLS_COMMON_MACROS__
+#define __XEN_TOOLS_COMMON_MACROS__
+
+/*
+ * Caution:
Quoted 8 lines

tools/include/xen-tools/common-macros.h line 10

Quoted 5 lines
tools/include/xen-tools/common-macros.h renamed from tools/include/xen-tools/libs.h +5 -0
+ *
+ * This header must be completely self-contained. There are no external
+ * references to variables or functions allowed, as the file might be included
+ * for different runtime environments, such as firmware or normal programs.
+ */

May I ask to go a tiny step further: s/normal/target and build host/ or something alike?

Fine with me.

Juergen

Acked-by on v2 1/4
#4 lore raw

Re: [PATCH v2 1/4] tools: rename xen-tools/libs.h file to common-macros.h

In reply to Juergen Gross
Quoted 6 lines · Juergen Gross

In order to better reflect the contents of the header and to make it more appropriate to use it for different runtime environments like programs, libraries, and firmware, rename the libs.h include file to common-macros.h. Additionally ad a comment pointing out the need to be self-contained.

Quoted 6 lines

---
V2:
- new patch
---

For this (assuming header rename itself will be acked):

Quoted 1 line
 tools/python/xen/lowlevel/xc/xc.c                  |  2 +-
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#5 lore raw

PATCH v2 2/4 tools: add container_of() macro to xen-tools/common-macros.h

In reply to Juergen Gross

Instead of having 4 identical copies of the definition of a container_of() macro in different tools header files, add that macro to xen-tools/common-macros.h and use that instead.

Delete the other copies of that macro.

---
There is a similar macro CONTAINER_OF() defined in tools/include/xentoolcore_internal.h, which allows to not only use a type for the 2nd parameter, but a variable, too.
I'd like to get rid of that macro as well, but there are lots of use cases especially in libxl. Any thoughts regarding that macro? I could either:
- don't touch it at all
- enhance container_of() like CONTAINER_OF() and replace all use cases of CONTAINER_OF() with container_of()
- replace the few CONTAINER_OF() users outside libxl with container_of() and define CONTAINER_OF() in e.g. libxl_internal.h
- replace all CONTAINER_OF() use cases with container_of(), including the change from (.., var, ..) to (.., type, ...).

---
 tools/include/xen-tools/common-macros.h | 4 ++++
 tools/tests/vhpet/emul.h                | 3 ---
 tools/tests/vpci/emul.h                 | 6 +-----
 tools/tests/x86_emulator/x86-emulate.h  | 5 -----
 tools/xenstore/list.h                   | 6 ++----
 5 files changed, 7 insertions(+), 17 deletions(-)
tools/include/xen-tools/common-macros.h +4 -0
index f39cd96008..3e73b33c91 100644
@@ -75,4 +75,8 @@
 #define __must_check __attribute__((__warn_unused_result__))
 #endif
 
+#define container_of(ptr, type, member) ({                \
+    typeof( ((type *)0)->member ) *__mptr = (ptr);        \
+    (type *)( (char *)__mptr - offsetof(type,member) );})
+
 #endif	/* __XEN_TOOLS_COMMON_MACROS__ */
tools/tests/vhpet/emul.h +0 -3
index dfeb10f74e..610641ab0c 100644
@@ -125,9 +125,6 @@ enum
 #define max_t(type, x, y) \
     ({ type __x = (x); type __y = (y); __x > __y ? __x : __y; })
 #define offsetof(t, m) ((unsigned long )&((t *)0)->m)
-#define container_of(ptr, type, member) ({              \
-        typeof( ((type *)0)->member ) *__mptr = (ptr);  \
-        (type *)( (char *)__mptr - offsetof(type,member) ); })
 
 struct domain;
 
tools/tests/vpci/emul.h +1 -5
index f03e3a56d1..7169a2ea02 100644
@@ -27,11 +27,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#define container_of(ptr, type, member) ({                      \
-        typeof(((type *)0)->member) *mptr = (ptr);              \
-                                                                \
-        (type *)((char *)mptr - offsetof(type, member));        \
-})
+#include <xen-tools/common-macros.h>
 
 #define smp_wmb()
 #define prefetch(x) __builtin_prefetch(x)
tools/tests/x86_emulator/x86-emulate.h +0 -5
index 46d4e43cea..1af986f78d 100644
@@ -56,11 +56,6 @@
 
 #define cf_check /* No Control Flow Integriy checking */
 
-#define container_of(ptr, type, member) ({             \
-    typeof(((type *)0)->member) *mptr__ = (ptr);       \
-    (type *)((char *)mptr__ - offsetof(type, member)); \
-})
-
 #define AC_(n,t) (n##t)
 #define _AC(n,t) AC_(n,t)
 
tools/xenstore/list.h +2 -4
index b17d13e0ec..a464a38b61 100644
@@ -3,6 +3,8 @@
 /* Taken from Linux kernel code, but de-kernelized for userspace. */
 #include <stddef.h>
 
+#include <xen-tools/common-macros.h>
+
 #undef LIST_HEAD_INIT
 #undef LIST_HEAD
 #undef INIT_LIST_HEAD
@@ -15,10 +17,6 @@
 #define LIST_POISON1  ((void *) 0x00100100)
 #define LIST_POISON2  ((void *) 0x00200200)
 
-#define container_of(ptr, type, member) ({			\
-        typeof( ((type *)0)->member ) *__mptr = (ptr);	\
-        (type *)( (char *)__mptr - offsetof(type,member) );})
-
 /*
  * Simple doubly linked list implementation.
  *
Signature · 1 line
2.35.3
#6 lore raw

PATCH v2 3/4 tools: get rid of additional min() and max() definitions

In reply to Juergen Gross

Defining min(), min_t(), max() and max_t() at other places than xen-tools/common-macros.h isn't needed, as the definitions in said header can be used instead.

Same applies to BUILD_BUG_ON() in hvmloader.

---
 tools/firmware/hvmloader/util.h |  8 ++------
 tools/libs/vchan/init.c         |  3 +--
 tools/tests/vhpet/emul.h        | 11 +----------
 tools/tests/vpci/emul.h         | 16 ----------------
 4 files changed, 4 insertions(+), 34 deletions(-)
tools/firmware/hvmloader/util.h +2 -6
index 8d95eab28a..e04990ee97 100644
@@ -9,6 +9,8 @@
 #include <xen/hvm/hvm_info_table.h>
 #include "e820.h"
 
+#include <xen-tools/common-macros.h>
+
 /* Request un-prefixed values from errno.h. */
 #define XEN_ERRNO(name, value) name = value,
 enum {
@@ -41,12 +43,6 @@ void __assert_failed(const char *assertion, const char *file, int line)
 void __bug(const char *file, int line) __attribute__((noreturn));
 #define BUG() __bug(__FILE__, __LINE__)
 #define BUG_ON(p) do { if (p) BUG(); } while (0)
-#define BUILD_BUG_ON(p) ((void)sizeof(char[1 - 2 * !!(p)]))
-
-#define min_t(type,x,y) \
-        ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
-#define max_t(type,x,y) \
-        ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
 
 #define MB(mb) (mb##ULL << 20)
 #define GB(gb) (gb##ULL << 30)
tools/libs/vchan/init.c +1 -2
index 9195bd3b98..021e1f29e1 100644
@@ -45,6 +45,7 @@
 #include <xen/sys/gntalloc.h>
 #include <xen/sys/gntdev.h>
 #include <libxenvchan.h>
+#include <xen-tools/common-macros.h>
 
 #include "vchan.h"
 
@@ -72,8 +73,6 @@
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 #endif
 
-#define max(a,b) ((a > b) ? a : b)
-
 static int init_gnt_srv(struct libxenvchan *ctrl, int domain)
 {
 	int pages_left = ctrl->read.order >= PAGE_SHIFT ? 1 << (ctrl->read.order - PAGE_SHIFT) : 0;
tools/tests/vhpet/emul.h +1 -10
index 610641ab0c..6af880cd43 100644
@@ -114,16 +114,7 @@ enum
     TASKLET_SOFTIRQ,
     NR_COMMON_SOFTIRQS
 };
-/*
- * ..and if you can't take the strict
- * types, you can specify one yourself.
- *
- * Or not use min/max at all, of course.
- */
-#define min_t(type, x, y) \
-    ({ type __x = (x); type __y = (y); __x < __y ? __x : __y; })
-#define max_t(type, x, y) \
-    ({ type __x = (x); type __y = (y); __x > __y ? __x : __y; })
+
 #define offsetof(t, m) ((unsigned long )&((t *)0)->m)
 
 struct domain;
tools/tests/vpci/emul.h +0 -16
index 7169a2ea02..8c5bcadd5f 100644
@@ -106,22 +106,6 @@ typedef union {
 #define BUG() assert(0)
 #define ASSERT_UNREACHABLE() assert(0)
 
-#define min(x, y) ({                    \
-        const typeof(x) tx = (x);       \
-        const typeof(y) ty = (y);       \
-                                        \
-        (void) (&tx == &ty);            \
-        tx < ty ? tx : ty;              \
-})
-
-#define max(x, y) ({                    \
-        const typeof(x) tx = (x);       \
-        const typeof(y) ty = (y);       \
-                                        \
-        (void) (&tx == &ty);            \
-        tx > ty ? tx : ty;              \
-})
-
 #endif
 
 /*
Signature · 1 line
2.35.3
#7 lore raw

PATCH v2 4/4 tools: add offsetof() to xen-tools/common-macros.h

In reply to Juergen Gross

Instead of having multiple files defining offsetof(), add the definition to tools/include/xen-tools/common-macros.h.

---
 tools/firmware/hvmloader/util.h         | 3 ---
 tools/firmware/include/stddef.h         | 4 ++--
 tools/include/xen-tools/common-macros.h | 4 ++++
 tools/libfsimage/Rules.mk               | 2 ++
 tools/libfsimage/xfs/fsys_xfs.c         | 4 +---
 tools/libs/vchan/init.c                 | 4 ----
 tools/tests/vhpet/emul.h                | 2 --
 7 files changed, 9 insertions(+), 14 deletions(-)
tools/firmware/hvmloader/util.h +0 -3
index e04990ee97..7249773eeb 100644
@@ -30,9 +30,6 @@ enum {
 #define SEL_DATA32          0x0020
 #define SEL_CODE64          0x0028
 
-#undef offsetof
-#define offsetof(t, m) ((unsigned long)&((t *)0)->m)
-
 #undef NULL
 #define NULL ((void*)0)
 
tools/firmware/include/stddef.h +2 -2
index c7f974608a..0afb426a6d 100644
@@ -1,10 +1,10 @@
 #ifndef _STDDEF_H_
 #define _STDDEF_H_
 
+#include <xen-tools/common-macros.h>
+
 typedef __SIZE_TYPE__ size_t;
 
 #define NULL ((void*)0)
 
-#define offsetof(t, m) __builtin_offsetof(t, m)
-
 #endif
tools/include/xen-tools/common-macros.h +4 -0
index 3e73b33c91..15399e9b88 100644
@@ -79,4 +79,8 @@
     typeof( ((type *)0)->member ) *__mptr = (ptr);        \
     (type *)( (char *)__mptr - offsetof(type,member) );})
 
+#ifndef offsetof
+#define offsetof(a, b) __builtin_offsetof(a, b)
+#endif
+
 #endif	/* __XEN_TOOLS_COMMON_MACROS__ */
tools/libfsimage/Rules.mk +2 -0
index cf37d6cb0d..85674f2379 100644
@@ -3,6 +3,8 @@ include $(XEN_ROOT)/tools/libfsimage/common.mk
 FSLIB = fsimage.so
 TARGETS += $(FSLIB)
 
+CFLAGS += $(CFLAGS_xeninclude)
+
 .PHONY: all
 all: $(TARGETS)
 
tools/libfsimage/xfs/fsys_xfs.c +1 -3
index d735a88e55..f562daaef0 100644
@@ -19,6 +19,7 @@
 
 #include <xenfsimage_grub.h>
 #include "xfs.h"
+#include <xen-tools/common-macros.h>
 
 #define MAX_LINK_COUNT	8
 
@@ -182,9 +183,6 @@ fsb2daddr (xfs_fsblock_t fsbno)
 			 (xfs_agblock_t)(fsbno & mask32lo(xfs.agblklog)));
 }
 
-#undef offsetof
-#define offsetof(t,m)	((size_t)&(((t *)0)->m))
-
 static inline int
 btroot_maxrecs (fsi_file_t *ffi)
 {
tools/libs/vchan/init.c +0 -4
index 021e1f29e1..ad4cc66d2c 100644
@@ -69,10 +69,6 @@
 #define MAX_RING_SHIFT 20
 #define MAX_RING_SIZE (1 << MAX_RING_SHIFT)
 
-#ifndef offsetof
-#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
-#endif
-
 static int init_gnt_srv(struct libxenvchan *ctrl, int domain)
 {
 	int pages_left = ctrl->read.order >= PAGE_SHIFT ? 1 << (ctrl->read.order - PAGE_SHIFT) : 0;
tools/tests/vhpet/emul.h +0 -2
index 6af880cd43..7b3ee88fb5 100644
@@ -115,8 +115,6 @@ enum
     NR_COMMON_SOFTIRQS
 };
 
-#define offsetof(t, m) ((unsigned long )&((t *)0)->m)
-
 struct domain;
 
 struct vcpu
Signature · 1 line
2.35.3
#8 lore raw

Re: [PATCH v2 4/4] tools: add offsetof() to xen-tools/common-macros.h

Quoted 5 lines · Juergen Gross
tools/firmware/include/stddef.h +0 -0
@@ -1,10 +1,10 @@
 #ifndef _STDDEF_H_
 #define _STDDEF_H_

tools/firmware/include/stddef.h line 10

Quoted 10 lines
tools/firmware/include/stddef.h +2 -2
 
+#include <xen-tools/common-macros.h>
+
 typedef __SIZE_TYPE__ size_t;
 
 #define NULL ((void*)0)
 
-#define offsetof(t, m) __builtin_offsetof(t, m)
-
 #endif

The C standard is pretty specific about what a header of this name may or (in particular here) may not define. You add much more to the name space than just the replacement offsetof(). If this was a header used by an individual component, this might be fine. But this header is meant to serve all components under firmware/ which care to include it. At present that's hvmloader (which we control, so we can arrange for it to be free of collisions) and rombios (which we do not really control, and which people also may not build routinely anymore).

Jan

#9 lore raw

Re: [PATCH v2 4/4] tools: add offsetof() to xen-tools/common-macros.h

Quoted 13 lines · Jan Beulich
Quoted 12 lines · Juergen Gross
tools/firmware/include/stddef.h +2 -0
@@ -1,10 +1,10 @@
  #ifndef _STDDEF_H_
  #define _STDDEF_H_
 
+#include <xen-tools/common-macros.h>
+
  typedef __SIZE_TYPE__ size_t;
 
  #define NULL ((void*)0)
 
Quoted 13 lines

tools/firmware/include/stddef.h line 10

Quoted 3 lines
tools/firmware/include/stddef.h +0 -2
-#define offsetof(t, m) __builtin_offsetof(t, m)
-
  #endif

The C standard is pretty specific about what a header of this name may or (in particular here) may not define. You add much more to the name space than just the replacement offsetof(). If this was a header used by an individual component, this might be fine. But this header is meant to serve all components under firmware/ which care to include it. At present that's hvmloader (which we control, so we can arrange for it to be free of collisions) and rombios (which we do not really control, and which people also may not build routinely anymore).

Good point.

I have verified that the build is still succeeding without the modification of tools/firmware/include/stddef.h, so I'll drop this hunk.

Juergen

Acked-by on the v2 series
#10 lore raw
In reply to Juergen Gross
Quoted 63 lines · Juergen Gross

There are some macros defined multiple times in tools. Use only a single header file for defining those macros and drop the copies.

V2:
- add patch 1 (Andrew Cooper)

Juergen Gross (4):
tools: rename xen-tools/libs.h file to common-macros.h
tools: add container_of() macro to xen-tools/common-macros.h
tools: get rid of additional min() and max() definitions
tools: add offsetof() to xen-tools/common-macros.h

tools/console/daemon/io.c                     |  2 +-
tools/firmware/hvmloader/util.h               | 11 ++--------
tools/firmware/include/stddef.h               |  4 ++--
tools/fuzz/cpu-policy/afl-policy-fuzzer.c     |  2 +-
.../xen-tools/{libs.h => common-macros.h}     | 22 ++++++++++++++++---
tools/libfsimage/Rules.mk                     |  2 ++
tools/libfsimage/xfs/fsys_xfs.c               |  4 +---
tools/libs/call/buffer.c                      |  2 +-
tools/libs/ctrl/xc_pm.c                       |  2 +-
tools/libs/ctrl/xc_private.h                  |  2 +-
tools/libs/foreignmemory/linux.c              |  2 +-
tools/libs/gnttab/freebsd.c                   |  2 +-
tools/libs/gnttab/linux.c                     |  2 +-
tools/libs/guest/xg_core_arm.c                |  2 +-
tools/libs/guest/xg_cpuid_x86.c               |  2 +-
tools/libs/guest/xg_dom_arm.c                 |  2 +-
tools/libs/guest/xg_dom_bzimageloader.c       |  2 +-
tools/libs/guest/xg_dom_x86.c                 |  2 +-
tools/libs/guest/xg_sr_common.c               |  2 +-
tools/libs/light/libxl_internal.h             |  2 +-
tools/libs/light/libxl_psr.c                  |  2 +-
tools/libs/stat/xenstat_linux.c               |  2 +-
tools/libs/vchan/init.c                       |  7 +-----
tools/misc/xen-access.c                       |  2 +-
tools/misc/xen-cpuid.c                        |  2 +-
tools/misc/xen-diag.c                         |  2 +-
tools/misc/xen-hptool.c                       |  2 +-
tools/misc/xen-livepatch.c                    |  2 +-
tools/misc/xen-mfndump.c                      |  2 +-
tools/misc/xenpm.c                            |  2 +-
tools/ocaml/libs/mmap/xenmmap_stubs.c         |  2 +-
tools/ocaml/libs/xc/xenctrl_stubs.c           |  2 +-
tools/python/xen/lowlevel/xc/xc.c             |  2 +-
tools/tests/cpu-policy/test-cpu-policy.c      |  2 +-
.../paging-mempool/test-paging-mempool.c      |  2 +-
tools/tests/resource/test-resource.c          |  2 +-
tools/tests/tsx/test-tsx.c                    |  2 +-
tools/tests/vhpet/emul.h                      | 16 +-------------
tools/tests/vpci/emul.h                       | 22 +------------------
tools/tests/x86_emulator/x86-emulate.h        |  7 +-----
tools/tests/xenstore/test-xenstore.c          |  2 +-
tools/xenstore/list.h                         |  6 ++---
tools/xenstore/utils.h                        |  2 +-
tools/xentrace/analyze.h                      |  2 +-
tools/xl/xl_cmdtable.c                        |  2 +-
xen/lib/x86/private.h                         |  2 +-
46 files changed, 68 insertions(+), 105 deletions(-)
Quoted 5 lines

rename tools/include/xen-tools/{libs.h => common-macros.h} (74%)

Signature · 1 line
2.35.3