[CRIU] [PATCH 19/23] zdtm: use architecture-specific atomic operations

Alexander Kartashov alekskartashov at parallels.com
Mon Jan 14 02:26:01 EST 2013


The test static/session01 fails to compile on ARM
because it uses x86-specific atomic operations.

It seems unfeasible to implement architecture-specific
operations for the test-suite so the build system is modified
to use the same atomic operation implementation both for
crtools and its test suite.

The header test/zdtm/lib/lock.h is modified to use
other declaration of the type atomic_t.

Signed-off-by: Alexander Kartashov <alekskartashov at parallels.com>
---
 Makefile                          |    5 +++--
 test/zdtm/lib/atomic.h            |   43 +------------------------------------
 test/zdtm/lib/lock.h              |   26 +++++++++++-----------
 test/zdtm/lib/zdtmtst.h           |    2 ++
 test/zdtm/live/static/Makefile    |    3 ++-
 test/zdtm/live/static/session01.c |    1 +
 6 files changed, 22 insertions(+), 58 deletions(-)

diff --git a/Makefile b/Makefile
index e3137c6..1c9b12c 100644
--- a/Makefile
+++ b/Makefile
@@ -38,7 +38,7 @@ ifeq ($(uname_M),i386)
 endif
 ifeq ($(uname_M),x86_64)
 	ARCH         := x86
-	DEFINES      := -DCONFIG_X86_64
+	ARCH_DEFINES := -DCONFIG_X86_64
 	LDARCH       := i386:x86-64
 endif
 
@@ -51,6 +51,7 @@ CFLAGS		+= -I$(SRC_DIR)/include -I$(SRC_DIR)/pie -I$(ARCH_DIR) -iquote $(ARCH_DI
 
 LIBS		:= -lrt -lpthread -lprotobuf-c
 
+DEFINES		+= $(ARCH_DEFINES)
 DEFINES		+= -D_FILE_OFFSET_BITS=64
 DEFINES		+= -D_GNU_SOURCE
 
@@ -75,7 +76,7 @@ CFLAGS		+= $(WARNINGS) $(DEFINES)
 SYSCALL-LIB	= $(SRC_DIR)/arch/$(ARCH)/syscalls.o
 PROTOBUF-LIB	= $(SRC_DIR)/protobuf/protobuf-lib.o
 
-export E Q CC ECHO MAKE CFLAGS LIBS ARCH DEFINES MAKEFLAGS SRC_DIR SYSCALL-LIB SH ARCH_DIR OBJCOPY LDARCH
+export E Q CC ECHO MAKE CFLAGS LIBS ARCH DEFINES MAKEFLAGS SRC_DIR SYSCALL-LIB SH ARCH_DIR OBJCOPY LDARCH ARCH_DEFINES
 
 
 PROGRAM		:= crtools
diff --git a/test/zdtm/lib/atomic.h b/test/zdtm/lib/atomic.h
index 3debc48..0da77a8 100644
--- a/test/zdtm/lib/atomic.h
+++ b/test/zdtm/lib/atomic.h
@@ -1,42 +1 @@
-#ifndef ATOMIC_H__
-#define ATOMIC_H__
-
-#define atomic_set(mem, v)					\
-	({							\
-		asm volatile ("lock xchg %0, %1\n"		\
-				: "+r" (v), "+m" (*mem)		\
-				:				\
-				: "cc", "memory");		\
-	})
-
-#define atomic_get(mem)						\
-	({							\
-		uint32_t ret__ = 0;					\
-		asm volatile ("lock xadd %0, %1\n"		\
-				: "+r" (ret__),	"+m" (*mem)	\
-				:				\
-				: "cc", "memory");		\
-		ret__;						\
-	})
-
-#define atomic_inc(mem)						\
-	({							\
-		uint32_t ret__ = 1;					\
-		asm volatile ("lock xadd %0, %1\n"		\
-				: "+r" (ret__),	"+m" (*mem)	\
-				:				\
-				: "cc", "memory");		\
-		ret__;						\
-	})
-
-#define atomic_dec(mem)						\
-	({							\
-		uint32_t ret__ = -1;					\
-		asm volatile ("lock xadd %0, %1\n"		\
-				: "+r" (ret__),	"+m" (*mem)	\
-				:				\
-				: "cc", "memory");		\
-		ret__;						\
-	})
-
-#endif /* ATOMIC_H__ */
+#include "asm/atomic.h"
diff --git a/test/zdtm/lib/lock.h b/test/zdtm/lib/lock.h
index 20d7cbd..19da579 100644
--- a/test/zdtm/lib/lock.h
+++ b/test/zdtm/lib/lock.h
@@ -16,7 +16,7 @@
 		}							\
 	} while (0)
 typedef struct {
-	uint32_t	raw;
+	atomic_t	raw;
 } futex_t;
 
 #define FUTEX_ABORT_FLAG	(0x80000000)
@@ -49,11 +49,11 @@ static inline void futex_set(futex_t *f, uint32_t v)
 		uint32_t tmp;					\
 								\
 		while (1) {					\
-			tmp = (__f)->raw;			\
+			tmp = (__f)->raw.counter;		\
 			if ((tmp & FUTEX_ABORT_FLAG) ||		\
 			    (tmp __cond (__v)))			\
 				break;				\
-			ret = sys_futex(&(__f)->raw, FUTEX_WAIT,\
+			ret = sys_futex(&(__f)->raw.counter, FUTEX_WAIT,\
 					tmp, NULL, NULL, 0);	\
 			if (ret < 0 && errno == EAGAIN)			\
 				continue;			\
@@ -65,7 +65,7 @@ static inline void futex_set(futex_t *f, uint32_t v)
 static inline void futex_set_and_wake(futex_t *f, uint32_t v)
 {
 	atomic_set(&f->raw, v);
-	BUG_ON(sys_futex(&f->raw, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
+	BUG_ON(sys_futex(&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
 }
 
 /* Mark futex @f as wait abort needed and wake up all waiters */
@@ -78,14 +78,14 @@ static inline void futex_abort_and_wake(futex_t *f)
 static inline void futex_dec_and_wake(futex_t *f)
 {
 	atomic_dec(&f->raw);
-	BUG_ON(sys_futex(&f->raw, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
+	BUG_ON(sys_futex(&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
 }
 
 /* Plain increment futex @f value */
-static inline void futex_inc(futex_t *f) { f->raw++; }
+static inline void futex_inc(futex_t *f) { atomic_inc(&f->raw); }
 
 /* Plain decrement futex @f value */
-static inline void futex_dec(futex_t *f) { f->raw--; }
+static inline void futex_dec(futex_t *f) { atomic_dec(&f->raw); }
 
 /* Wait until futex @f value become @v */
 static inline void futex_wait_until(futex_t *f, uint32_t v)
@@ -98,18 +98,18 @@ static inline void futex_wait_while_gt(futex_t *f, uint32_t v)
 /* Wait while futex @f value is @v */
 static inline uint32_t futex_wait_while(futex_t *f, uint32_t v)
 {
-	while (f->raw == v) {
-		int ret = sys_futex(&f->raw, FUTEX_WAIT, v, NULL, NULL, 0);
+	while (f->raw.counter == v) {
+		int ret = sys_futex(&f->raw.counter, FUTEX_WAIT, v, NULL, NULL, 0);
 		if (ret < 0 && errno == EAGAIN)
 			continue;
 		BUG_ON(ret < 0 && errno != EWOULDBLOCK);
 	}
 
-	return f->raw;
+	return f->raw.counter;
 }
 
 typedef struct {
-	uint32_t	raw;
+	atomic_t	raw;
 } mutex_t;
 
 static void inline mutex_init(mutex_t *m)
@@ -124,7 +124,7 @@ static void inline mutex_lock(mutex_t *m)
 	int ret;
 
 	while ((c = atomic_inc(&m->raw))) {
-		ret = sys_futex(&m->raw, FUTEX_WAIT, c + 1, NULL, NULL, 0);
+		ret = sys_futex(&m->raw.counter, FUTEX_WAIT, c + 1, NULL, NULL, 0);
 		BUG_ON(ret < 0 && ret != -EWOULDBLOCK);
 	}
 }
@@ -133,7 +133,7 @@ static void inline mutex_unlock(mutex_t *m)
 {
 	uint32_t c = 0;
 	atomic_set(&m->raw, c);
-	BUG_ON(sys_futex(&m->raw, FUTEX_WAKE, 1, NULL, NULL, 0) < 0);
+	BUG_ON(sys_futex(&m->raw.counter, FUTEX_WAKE, 1, NULL, NULL, 0) < 0);
 }
 
 #endif /* CR_LOCK_H_ */
diff --git a/test/zdtm/lib/zdtmtst.h b/test/zdtm/lib/zdtmtst.h
index 323b70e..4eb3b57 100644
--- a/test/zdtm/lib/zdtmtst.h
+++ b/test/zdtm/lib/zdtmtst.h
@@ -84,8 +84,10 @@ extern int parse_opt_string(char *param, void *arg);
 #include <stdio.h>
 #include <errno.h>
 
+#ifndef __stringify
 #define __stringify_1(x)        #x
 #define __stringify(x)          __stringify_1(x)
+#endif
 
 /* message helpers */
 extern void setup_outfile(void);
diff --git a/test/zdtm/live/static/Makefile b/test/zdtm/live/static/Makefile
index ebd98b0..98edee1 100644
--- a/test/zdtm/live/static/Makefile
+++ b/test/zdtm/live/static/Makefile
@@ -1,7 +1,7 @@
 LIBDIR	= ../../lib
 LIB	= $(LIBDIR)/libzdtmtst.a
 override CPPFLAGS += -I$(LIBDIR)
-CFLAGS	= -g -O2 -Wall -Werror -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0
+CFLAGS	= -g -O2 -Wall -Werror -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 $(ARCH_DEFINES)
 
 TST_NOFILE	=				\
 		busyloop00			\
@@ -218,6 +218,7 @@ posix_timers:		override LDLIBS += -lrt
 socket-tcp6: override CFLAGS += -D ZDTM_IPV6
 socket-tcpbuf6: override CFLAGS += -D ZDTM_IPV6
 socket_listen6: override CFLAGS += -D ZDTM_IPV6
+session01:		override CPPFLAGS += -iquote $(ARCH_DIR)/include -I$(SRC_DIR)/include
 
 $(LIB):	force
 	$(MAKE) -C $(LIBDIR)
diff --git a/test/zdtm/live/static/session01.c b/test/zdtm/live/static/session01.c
index 2fe6aab..d26f7dd 100644
--- a/test/zdtm/live/static/session01.c
+++ b/test/zdtm/live/static/session01.c
@@ -5,6 +5,7 @@
 #include <sys/wait.h>
 #include <sys/mman.h>
 
+#include "asm/types.h"
 #include "zdtmtst.h"
 #include "lock.h"
 
-- 
1.7.10.4



More information about the CRIU mailing list