Skip to contentSkip to conversation

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

PATCH In review Juergen Gross 3 participants 13 messages Mar 23, 2023

lore
Share & tools
lore
#0 lore raw

v5 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, or use stddef.h for offsetof().

V2:
- add patch 1 (Andrew Cooper)

V3:
- address comments

V4:
- patch 1 of V3 already applied
- new patches 3-5

V5:
- swap patches 1 and 2 in order to avoid build error

Juergen Gross (5):
tools: get rid of additional min() and max() definitions
tools: add container_of() macro to xen-tools/common-macros.h tools/hvmloader: remove private offsetof() definition tools/libfsimage: remove private offsetof() definition tools/libs/vchan: remove private offsetof() definition

 tools/firmware/hvmloader/util.h         | 11 ++---------
 tools/include/xen-tools/common-macros.h |  5 +++++
 tools/libfsimage/xfs/fsys_xfs.c         |  4 +---
 tools/libs/vchan/init.c                 |  8 ++------
 tools/tests/vpci/Makefile               |  2 +-
 tools/tests/vpci/emul.h                 | 22 +---------------------
 tools/tests/x86_emulator/x86-emulate.h  |  5 -----
 tools/xenstore/list.h                   |  6 ++----
 8 files changed, 14 insertions(+), 49 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

PATCH v5 1/5 tools: get rid of additional min() and max() definitions

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.

---
V5:
- change patch sequence

---
 tools/firmware/hvmloader/util.h |  8 ++------
 tools/libs/vchan/init.c         |  3 +--
 tools/tests/vpci/Makefile       |  2 +-
 tools/tests/vpci/emul.h         | 18 ++----------------
 4 files changed, 6 insertions(+), 25 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/vpci/Makefile +1 -1
index 5075bc2be2..62f21f341a 100644
@@ -11,7 +11,7 @@ run: $(TARGET)
 	./$(TARGET)
 
 $(TARGET): vpci.c vpci.h list.h main.c emul.h
-	$(HOSTCC) -g -o $@ vpci.c main.c
+	$(HOSTCC) $(CFLAGS_xeninclude) -g -o $@ vpci.c main.c
 
 .PHONY: clean
 clean:
tools/tests/vpci/emul.h +2 -16
index f03e3a56d1..d26e00b9d7 100644
@@ -27,6 +27,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include <xen-tools/common-macros.h>
+
 #define container_of(ptr, type, member) ({                      \
         typeof(((type *)0)->member) *mptr = (ptr);              \
                                                                 \
@@ -110,22 +112,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
Acked-by on v5 1/5
#2 lore raw

Re: [PATCH v5 1/5] tools: get rid of additional min() and max() definitions

Quoted 7 lines · 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.

Acked-by on v5 1/5
#3 lore raw

Re: [PATCH v5 1/5] tools: get rid of additional min() and max() definitions

In reply to Juergen Gross
Quoted 7 lines · 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.

Thanks,

Signature · 1 line
Anthony PERARD
#4 lore raw

PATCH v5 2/5 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.

---
V5:
- change patch sequence
- use offsetof() definition from tools/tests/x86_emulator/x86-emulate.h as template (Jan Beulich)

---
 tools/include/xen-tools/common-macros.h | 5 +++++
 tools/tests/vpci/emul.h                 | 6 ------
 tools/tests/x86_emulator/x86-emulate.h  | 5 -----
 tools/xenstore/list.h                   | 6 ++----
 4 files changed, 7 insertions(+), 15 deletions(-)
tools/include/xen-tools/common-macros.h +5 -0
index a372b9ecf2..76b55bf620 100644
@@ -76,4 +76,9 @@
 #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/vpci/emul.h +0 -6
index d26e00b9d7..8c5bcadd5f 100644
@@ -29,12 +29,6 @@
 
 #include <xen-tools/common-macros.h>
 
-#define container_of(ptr, type, member) ({                      \
-        typeof(((type *)0)->member) *mptr = (ptr);              \
-                                                                \
-        (type *)((char *)mptr - offsetof(type, member));        \
-})
-
 #define smp_wmb()
 #define prefetch(x) __builtin_prefetch(x)
 #define ASSERT(x) assert(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
Acked-by on v5 2/5
#5 lore raw

Re: [PATCH v5 2/5] tools: add container_of() macro to xen-tools/common-macros.h

Quoted 1 line · Juergen Gross

Instead of having 4 identical copies of the definition of a

Nit: 3 ;-), no more copies in removed vhpet tests.

Quoted 7 lines

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.

Thanks,

Signature · 1 line
Anthony PERARD
#6 lore raw

PATCH v5 3/5 tools/hvmloader: remove private offsetof() definition

In reply to Juergen Gross

util.h contains a definition of offsetof(), which isn't used.

Remove it.

---
V4:
- new patch

---
 tools/firmware/hvmloader/util.h | 3 ---
 1 file changed, 3 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)
 
Signature · 1 line
2.35.3
#7 lore raw

Re: [PATCH v5 3/5] tools/hvmloader: remove private offsetof() definition

Quoted 1 line · Juergen Gross

util.h contains a definition of offsetof(), which isn't used.

Coming back to my comment on the earlier version: "not used" isn't true, but the earlier "not needed" also didn't make clear why it was not needed (misleading me into making the [wrong] suggestion): The macro is used, just not in hvmloader/ but in files living in libacpi/ and compiled here (as well as elsewhere).

New suggestion (which I'm happy to apply while committing): "... which isn't needed, as firmware/include/stddef.h's doesn't really need overriding". (The same, btw, is true for NULL, so I guess I'll make a follow-up patch to remove that one as well.)

Jan

#8 lore raw

Re: [PATCH v5 3/5] tools/hvmloader: remove private offsetof() definition

Quoted 8 lines · Jan Beulich
Quoted 1 line · Juergen Gross

util.h contains a definition of offsetof(), which isn't used.

Coming back to my comment on the earlier version: "not used" isn't true, but the earlier "not needed" also didn't make clear why it was not needed (misleading me into making the [wrong] suggestion): The macro is used, just not in hvmloader/ but in files living in libacpi/ and compiled here (as well as elsewhere).

Ah, okay, I only looked in hvmloader for users.

Quoted 4 lines

New suggestion (which I'm happy to apply while committing): "... which isn't needed, as firmware/include/stddef.h's doesn't really need overriding". (The same, btw, is true for NULL, so I guess I'll make a follow-up patch to remove that one as well.)

Fine with me.

Juergen

#9 lore raw

PATCH v5 4/5 tools/libfsimage: remove private offsetof() definition

In reply to Juergen Gross

xfs/fsys_xfs.c is defining offsetof privately. Remove that definition and just use stddef.h instead.

---
V4:
- new patch

---
 tools/libfsimage/xfs/fsys_xfs.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
tools/libfsimage/xfs/fsys_xfs.c +1 -3
index d735a88e55..b8b4ca928c 100644
@@ -17,6 +17,7 @@
  *  along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <stddef.h>
 #include <xenfsimage_grub.h>
 #include "xfs.h"
 
@@ -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)
 {
Signature · 1 line
2.35.3
#11 lore raw

PATCH v5 5/5 tools/libs/vchan: remove private offsetof() definition

In reply to Juergen Gross

vchan/init.c is defining offsetof privately. Remove that definition and just use stddef.h instead.

---
V4:
- new patch

---
 tools/libs/vchan/init.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
tools/libs/vchan/init.c +1 -4
index 021e1f29e1..a081dd4c9a 100644
@@ -32,6 +32,7 @@
 #include <sys/mman.h>
 #include <sys/ioctl.h>
 #include <sys/user.h>
+#include <stddef.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdint.h>
@@ -69,10 +70,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;
Signature · 1 line
2.35.3