Skip to contentSkip to conversation

tst_atomic: make tst_atomic_inc take a signed integer

PATCH Superseded Jan Stancek 2 participants 9 messages Apr 14, 2016 – Apr 15, 2016 Library code

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 tst_atomic: make tst_atomic_inc take a signed integer

This is a preparation for upcoming patches, which add atomic_add_return(), based on kernel function of same name. Kernel's atomic_t is int based, so let's match that.

---
 doc/test-writing-guidelines.txt |  2 +-
 include/tst_atomic.h            |  2 +-
 lib/newlib_tests/test08.c       |  2 +-
 lib/tst_test.c                  | 16 ++++++++--------
 4 files changed, 11 insertions(+), 11 deletions(-)

Changes in v2:
- fixed tst_atomic_inc return type
- fixed tst_exit printf flags

doc/test-writing-guidelines.txt +1 -1
index d0b14084c90b..c0ef33afd5bb 100644
@@ -780,7 +780,7 @@ the user supplied cleanup to the test library.
 
 static void cleanup(void)
 {
-	static unsigned int flag;
+	static int flag;
 
 	if (tst_atomic_inc(&flag) != 1)
 		pthread_exit(NULL);
include/tst_atomic.h +1 -1
index 75c713d38d2a..2ec9f91318ea 100644
@@ -18,7 +18,7 @@
 #ifndef TST_ATOMIC_H__
 #define TST_ATOMIC_H__
 
-static inline unsigned int tst_atomic_inc(unsigned int *v)
+static inline int tst_atomic_inc(int *v)
 {
 	return __sync_add_and_fetch(v, 1);
 }
lib/newlib_tests/test08.c +1 -1
index 8fefc182650d..0a2023119a71 100644
@@ -35,7 +35,7 @@ static void setup(void)
 
 static void cleanup(void)
 {
-	static unsigned int flag;
+	static int flag;
 
 	/* Avoid subsequent threads to enter the cleanup */
 	if (tst_atomic_inc(&flag) != 1)
lib/tst_test.c +8 -8
index 37265fe50951..b8ec246414d1 100644
@@ -43,10 +43,10 @@ static float duration = -1;
 static pid_t main_pid;
 
 struct results {
-	unsigned int passed;
-	unsigned int skipped;
-	unsigned int failed;
-	unsigned int warnings;
+	int passed;
+	int skipped;
+	int failed;
+	int warnings;
 };
 
 static struct results *results;
@@ -442,10 +442,10 @@ static void do_exit(void)
 	int ret = 0;
 
 	printf("\nSummary:\n");
-	printf("passed   %u\n", results->passed);
-	printf("failed   %u\n", results->failed);
-	printf("skipped  %u\n", results->skipped);
-	printf("warnings %u\n", results->warnings);
+	printf("passed   %d\n", results->passed);
+	printf("failed   %d\n", results->failed);
+	printf("skipped  %d\n", results->skipped);
+	printf("warnings %d\n", results->warnings);
 
 	if (results->failed)
 		ret |= TFAIL;
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 2/4 m4: add a check for __sync_add_and_fetch

Compilers older than gcc 4.1.2 do not provide __sync_add_and_fetch, newer ones usually do, but it still may be missing for some targets.

---
 configure.ac                 |  1 +
 include/tst_atomic.h         | 14 +++++++++++++-
 m4/ltp-sync_add_and_fetch.m4 | 29 +++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 m4/ltp-sync_add_and_fetch.m4

Changes in v2:
- no changes. I kept single definition of tst_atomic_inc() out of all #ifdefs as opposed to making 2 definitions for both (!)HAVE_SYNC_ADD_AND_FETCH cases.
Now that we have a more generic version (atomic_add_return), that is compiler/arch dependent, it seems more tidy to me to keep just one definition of atomic_inc.

configure.ac +1 -0
index 2fb1ebc4a9c1..92ed5ec0e500 100644
@@ -185,5 +185,6 @@ LTP_CHECK_KCMP_TYPE
 LTP_CHECK_PREADV
 LTP_CHECK_PWRITEV
 LTP_CHECK_EPOLL_PWAIT
+LTP_CHECK_SYNC_ADD_AND_FETCH
 
 AC_OUTPUT
include/tst_atomic.h +13 -1
index 2ec9f91318ea..046eb160ad28 100644
@@ -18,9 +18,21 @@
 #ifndef TST_ATOMIC_H__
 #define TST_ATOMIC_H__
 
+#include "config.h"
+
+#if HAVE_SYNC_ADD_AND_FETCH == 1
+static inline __attribute__((always_inline)) int atomic_add_return(int i, int *v)
+{
+	return __sync_add_and_fetch(v, i);
+}
+#else
+#error Your compiler does not provide __sync_add_and_fetch and LTP\
+ implementation is missing for your architecture.
+#endif
+
 static inline int tst_atomic_inc(int *v)
 {
-	return __sync_add_and_fetch(v, 1);
+	return atomic_add_return(1, v);
 }
 
 #endif	/* TST_ATOMIC_H__ */
m4/ltp-sync_add_and_fetch.m4 new file +29 -0
new file mode 100644
index 000000000000..b9e222589267
@@ -0,0 +1,29 @@
+dnl
+dnl Copyright (c) Linux Test Project, 2016
+dnl
+dnl This program is free software;  you can redistribute it and/or modify
+dnl it under the terms of the GNU General Public License as published by
+dnl the Free Software Foundation; either version 2 of the License, or
+dnl (at your option) any later version.
+dnl
+dnl This program is distributed in the hope that it will be useful,
+dnl but WITHOUT ANY WARRANTY;  without even the implied warranty of
+dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+dnl the GNU General Public License for more details.
+dnl
+
+AC_DEFUN([LTP_CHECK_SYNC_ADD_AND_FETCH],[dnl
+	AC_MSG_CHECKING([for __sync_add_and_fetch])
+	AC_LINK_IFELSE([AC_LANG_SOURCE([
+int main(void) {
+	int i = 0;
+	return __sync_add_and_fetch(&i, 1);
+}])],[has_saac="yes"])
+
+if test "x$has_saac" = xyes; then
+	AC_DEFINE(HAVE_SYNC_ADD_AND_FETCH,1,[Define to 1 if you have __sync_add_and_fetch])
+	AC_MSG_RESULT(yes)
+else
+	AC_MSG_RESULT(no)
+fi
+])
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v2 2/4 m4: add a check for __sync_add_and_fetch

Hi!

include/tst_atomic.h line 24

Quoted 2 lines
include/tst_atomic.h +2 -0
+#if HAVE_SYNC_ADD_AND_FETCH == 1
+static inline __attribute__((always_inline)) int atomic_add_return(int i, int *v)

As this is publicly exported interface I would prefer adding tst_ prefix to the function name.

Quoted 13 lines
include/tst_atomic.h +9 -1
+{
+	return __sync_add_and_fetch(v, i);
+}
+#else
+#error Your compiler does not provide __sync_add_and_fetch and LTP\
+ implementation is missing for your architecture.
+#endif
+
 static inline int tst_atomic_inc(int *v)
 {
-	return __sync_add_and_fetch(v, 1);
+	return atomic_add_return(1, v);
 }
Signature · 2 lines
Cyril Hrubis
[email protected]
#3 lore raw

PATCH v2 3/4 tst_atomic: add atomic_add_return for x86/64, ppc/64 and s390/x

In reply to Jan Stancek

In case __sync_add_and_fetch is not provided by compiler, we try to supply our own implementation.

It has been taken from kernel 4.5 sources, by compiling a small piece of code (atomic_add_return()) with "gcc -E" and applying some formatting to make it more readable/pretty.

---
 include/tst_atomic.h | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 88 insertions(+), 1 deletion(-)

Changes in v2:
- atomic_add_return for x86 simplified, with original kernel macro kept as comment.
- added comments were assembly inline was taken from

include/tst_atomic.h +88 -1
index 046eb160ad28..e6b432d63ea1 100644
@@ -21,11 +21,98 @@
 #include "config.h"
 
 #if HAVE_SYNC_ADD_AND_FETCH == 1
+#define HAVE_ATOMIC_ADD_RETURN 1
 static inline __attribute__((always_inline)) int atomic_add_return(int i, int *v)
 {
 	return __sync_add_and_fetch(v, i);
 }
-#else
+
+#else /* HAVE_SYNC_ADD_AND_FETCH == 1 */
+
+#if defined(__i386__) || defined(__x86_64__)
+#define HAVE_ATOMIC_ADD_RETURN 1
+static inline __attribute__((always_inline)) int atomic_add_return(int i, int *v)
+{
+	int __ret = i;
+
+	/*
+	 * taken from arch/x86/include/asm/cmpxchg.h
+	 * Since we always pass int sized parameter, we can simplify it
+	 * and cherry-pick only that specific case.
+	 *
+	switch (sizeof(*v)) {
+	case 1:
+		asm volatile ("lock; xaddb %b0, %1\n"
+			: "+q" (__ret), "+m" (*v) : : "memory", "cc");
+		break;
+	case 2:
+		asm volatile ("lock; xaddw %w0, %1\n"
+			: "+r" (__ret), "+m" (*v) : : "memory", "cc");
+		break;
+	case 4:
+		asm volatile ("lock; xaddl %0, %1\n"
+			: "+r" (__ret), "+m" (*v) : : "memory", "cc");
+		break;
+	case 8:
+		asm volatile ("lock; xaddq %q0, %1\n"
+			: "+r" (__ret), "+m" (*v) : : "memory", "cc");
+		break;
+	default:
+		__xadd_wrong_size();
+	}
+	*/
+	asm volatile ("lock; xaddl %0, %1\n"
+		: "+r" (__ret), "+m" (*v) : : "memory", "cc");
+
+	return i + __ret;
+}
+#endif
+
+#if defined(__powerpc__) || defined(__powerpc64__)
+#define HAVE_ATOMIC_ADD_RETURN 1
+static inline __attribute__((always_inline)) int atomic_add_return(int i, int *v)
+{
+	int t;
+
+	/* taken from arch/powerpc/include/asm/atomic.h */
+	asm volatile(
+		"	sync\n"
+		"1:	lwarx	%0,0,%2		# atomic_add_return\n"
+		"	add %0,%1,%0\n"
+		"	stwcx.	%0,0,%2 \n"
+		"	bne-	1b\n"
+		"	sync\n"
+		: "=&r" (t)
+		: "r" (i), "r" (v)
+		: "cc", "memory");
+
+	return t;
+}
+#endif
+
+#if defined(__s390__) || defined(__s390x__)
+#define HAVE_ATOMIC_ADD_RETURN 1
+static inline __attribute__((always_inline)) int atomic_add_return(int i, int *v)
+{
+	int old_val, new_val;
+
+	/* taken from arch/s390/include/asm/atomic.h */
+	asm volatile(
+		"	l	%0,%2\n"
+		"0:	lr	%1,%0\n"
+		"	ar	%1,%3\n"
+		"	cs	%0,%1,%2\n"
+		"	jl	0b"
+		: "=&d" (old_val), "=&d" (new_val), "+Q" (*v)
+		: "d" (i)
+		: "cc", "memory");
+
+	return old_val + i;
+}
+#endif
+#endif /* HAVE_SYNC_ADD_AND_FETCH == 1 */
+
+#if !defined(HAVE_ATOMIC_ADD_RETURN)
 #error Your compiler does not provide __sync_add_and_fetch and LTP\
  implementation is missing for your architecture.
 #endif
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH v2 3/4 tst_atomic: add atomic_add_return for x86/64, ppc/64 and s390/x

Hi!

Quoted 4 lines

#if HAVE_SYNC_ADD_AND_FETCH == 1

include/tst_atomic.h +1 -0
+#define HAVE_ATOMIC_ADD_RETURN 1
 static inline __attribute__((always_inline)) int atomic_add_return(int i, int *v)
 {

include/tst_atomic.h line 35

Quoted 10 lines
include/tst_atomic.h +7 -1
 	return __sync_add_and_fetch(v, i);
 }
-#else
+
+#else /* HAVE_SYNC_ADD_AND_FETCH == 1 */
+
+#if defined(__i386__) || defined(__x86_64__)
+#define HAVE_ATOMIC_ADD_RETURN 1
+static inline __attribute__((always_inline)) int atomic_add_return(int i, int *v)
+{

Maybe we can make the ifdefs a bit more readable by indenting them with spaces after the hash.

#else

# if defined(__i386__) || defined(__x86_64__)
#  define HAVE_ATOMIC_ADD_RETURN 1

...

# endif

Otherwise it looks good.

Signature · 2 lines
Cyril Hrubis
[email protected]
#5 lore raw

PATCH v2 3/4 tst_atomic: add atomic_add_return for x86/64, ppc/64 and s390/x

----- Original Message -----

Quoted 27 lines

From: "Cyril Hrubis" <[email protected]>
To: "Jan Stancek" <[email protected]>
Cc: [email protected]
Sent: Thursday, 14 April, 2016 4:36:23 PM
Subject: Re: [LTP] [PATCH v2 3/4] tst_atomic: add atomic_add_return for x86/64, ppc/64 and s390/x

Hi!

Quoted 14 lines

#if HAVE_SYNC_ADD_AND_FETCH == 1

include/tst_atomic.h +7 -1
+#define HAVE_ATOMIC_ADD_RETURN 1
 static inline __attribute__((always_inline)) int atomic_add_return(int i,
 int *v)
 {
 	return __sync_add_and_fetch(v, i);
 }
-#else
+
+#else /* HAVE_SYNC_ADD_AND_FETCH == 1 */
+
+#if defined(__i386__) || defined(__x86_64__)
+#define HAVE_ATOMIC_ADD_RETURN 1
+static inline __attribute__((always_inline)) int atomic_add_return(int i,
Quoted 2 lines

int *v)
+{

Maybe we can make the ifdefs a bit more readable by indenting them with spaces after the hash.

Quoted 7 lines

#else

# if defined(__i386__) || defined(__x86_64__)
#  define HAVE_ATOMIC_ADD_RETURN 1

...

# endif

I have re-arranged this in v3, so that there is less nesting, and also dropped HAVE_ATOMIC_ADD_RETURN.

I also made some other small tweaks, changes are described in each patch.

#6 lore raw

PATCH v2 4/4 tst_atomic: add test for tst_atomic_inc

In reply to Jan Stancek

Spawn a lot of threads and run tst_atomic_inc in a loop. At the end we expect value to be THREADS * ITERATIONS.

---
 lib/newlib_tests/Makefile |  1 +
 lib/newlib_tests/test09.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)
 create mode 100644 lib/newlib_tests/test09.c

Changes in v2:
- removed .forks_child

lib/newlib_tests/Makefile +1 -0
index 5771712fab10..0e4eeb83b6d4 100644
@@ -6,5 +6,6 @@ CFLAGS			+= -W -Wall
 LDLIBS			+= -lltp
 
 test08: CFLAGS+=-pthread
+test09: CFLAGS+=-pthread
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
lib/newlib_tests/test09.c new file +61 -0

61 lines not on this page · raw message

Signature · 1 line
1.8.3.1