Skip to contentSkip to conversation

selftest/timerns: fix clang build failures for abs() calls

PATCH In review John Hubbard 3 participants 3 messages May 3, 2024 Kselftest

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftest/timerns: fix clang build failures for abs() calls

First of all, in order to build with clang at all, one must first apply Valentin Obst's build fix for LLVM [1]. Once that is done, then when building with clang, via:

make LLVM=1 -C tools/testing/selftests

...then clang warns about mismatches between the expected and required integer length being supplied to abs(3).

Fix this by using the correct variant of abs(3): labs(3) or llabs(3), in these cases.

[1] https://lore.kernel.org/all/20240329-selftests-libmk-llvm-rfc-v1-1-2f9ed7d1c49f@valentinobst.de/

---
 tools/testing/selftests/timens/exec.c       | 6 +++---
 tools/testing/selftests/timens/timer.c      | 2 +-
 tools/testing/selftests/timens/timerfd.c    | 2 +-
 tools/testing/selftests/timens/vfork_exec.c | 4 ++--
 4 files changed, 7 insertions(+), 7 deletions(-)
tools/testing/selftests/timens/exec.c +3 -3
index e40dc5be2f66..d12ff955de0d 100644
@@ -30,7 +30,7 @@ int main(int argc, char *argv[])
 
 		for (i = 0; i < 2; i++) {
 			_gettime(CLOCK_MONOTONIC, &tst, i);
-			if (abs(tst.tv_sec - now.tv_sec) > 5)
+			if (labs(tst.tv_sec - now.tv_sec) > 5)
 				return pr_fail("%ld %ld\n", now.tv_sec, tst.tv_sec);
 		}
 		return 0;
@@ -50,7 +50,7 @@ int main(int argc, char *argv[])
 
 	for (i = 0; i < 2; i++) {
 		_gettime(CLOCK_MONOTONIC, &tst, i);
-		if (abs(tst.tv_sec - now.tv_sec) > 5)
+		if (labs(tst.tv_sec - now.tv_sec) > 5)
 			return pr_fail("%ld %ld\n",
 					now.tv_sec, tst.tv_sec);
 	}
@@ -70,7 +70,7 @@ int main(int argc, char *argv[])
 		/* Check that a child process is in the new timens. */
 		for (i = 0; i < 2; i++) {
 			_gettime(CLOCK_MONOTONIC, &tst, i);
-			if (abs(tst.tv_sec - now.tv_sec - OFFSET) > 5)
+			if (labs(tst.tv_sec - now.tv_sec - OFFSET) > 5)
 				return pr_fail("%ld %ld\n",
 						now.tv_sec + OFFSET, tst.tv_sec);
 		}
tools/testing/selftests/timens/timer.c +1 -1
index 5e7f0051bd7b..5b939f59dfa4 100644
@@ -56,7 +56,7 @@ int run_test(int clockid, struct timespec now)
 			return pr_perror("timerfd_gettime");
 
 		elapsed = new_value.it_value.tv_sec;
-		if (abs(elapsed - 3600) > 60) {
+		if (llabs(elapsed - 3600) > 60) {
 			ksft_test_result_fail("clockid: %d elapsed: %lld\n",
 					      clockid, elapsed);
 			return 1;
tools/testing/selftests/timens/timerfd.c +1 -1
index 9edd43d6b2c1..a4196bbd6e33 100644
@@ -61,7 +61,7 @@ int run_test(int clockid, struct timespec now)
 			return pr_perror("timerfd_gettime(%d)", clockid);
 
 		elapsed = new_value.it_value.tv_sec;
-		if (abs(elapsed - 3600) > 60) {
+		if (llabs(elapsed - 3600) > 60) {
 			ksft_test_result_fail("clockid: %d elapsed: %lld\n",
 					      clockid, elapsed);
 			return 1;
tools/testing/selftests/timens/vfork_exec.c +2 -2
index beb7614941fb..5b8907bf451d 100644
@@ -32,7 +32,7 @@ static void *tcheck(void *_args)
 
 	for (i = 0; i < 2; i++) {
 		_gettime(CLOCK_MONOTONIC, &tst, i);
-		if (abs(tst.tv_sec - now->tv_sec) > 5) {
+		if (labs(tst.tv_sec - now->tv_sec) > 5) {
 			pr_fail("%s: in-thread: unexpected value: %ld (%ld)\n",
 				args->tst_name, tst.tv_sec, now->tv_sec);
 			return (void *)1UL;
@@ -64,7 +64,7 @@ static int check(char *tst_name, struct timespec *now)
 
 	for (i = 0; i < 2; i++) {
 		_gettime(CLOCK_MONOTONIC, &tst, i);
-		if (abs(tst.tv_sec - now->tv_sec) > 5)
+		if (labs(tst.tv_sec - now->tv_sec) > 5)
 			return pr_fail("%s: unexpected value: %ld (%ld)\n",
 					tst_name, tst.tv_sec, now->tv_sec);
 	}

base-commit: f03359bca01bf4372cf2c118cd9a987a5951b1c8
prerequisite-patch-id: b901ece2a5b78503e2fb5480f20e304d36a0ea27

Signature · 1 line
2.45.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · John Hubbard

First of all, in order to build with clang at all, one must first apply Valentin Obst's build fix for LLVM [1]. Once that is done, then when building with clang, via:

make LLVM=1 -C tools/testing/selftests

...then clang warns about mismatches between the expected and required integer length being supplied to abs(3).

Quoted 6 lines

Fix this by using the correct variant of abs(3): labs(3) or llabs(3), in these cases.

[1] https://lore.kernel.org/all/20240329-selftests-libmk-llvm-rfc-v1-1-2f9ed7d1c49f@valentinobst.de/

LGTM, even potentially fixes the testing post-2038.

Quoted 92 lines
---
 tools/testing/selftests/timens/exec.c       | 6 +++---
 tools/testing/selftests/timens/timer.c      | 2 +-
 tools/testing/selftests/timens/timerfd.c    | 2 +-
 tools/testing/selftests/timens/vfork_exec.c | 4 ++--
 4 files changed, 7 insertions(+), 7 deletions(-)
tools/testing/selftests/timens/exec.c +3 -3
index e40dc5be2f66..d12ff955de0d 100644
@@ -30,7 +30,7 @@ int main(int argc, char *argv[])
 
                for (i = 0; i < 2; i++) {
                        _gettime(CLOCK_MONOTONIC, &tst, i);
-                       if (abs(tst.tv_sec - now.tv_sec) > 5)
+                       if (labs(tst.tv_sec - now.tv_sec) > 5)
                                return pr_fail("%ld %ld\n", now.tv_sec, tst.tv_sec);
                }
                return 0;
@@ -50,7 +50,7 @@ int main(int argc, char *argv[])
 
        for (i = 0; i < 2; i++) {
                _gettime(CLOCK_MONOTONIC, &tst, i);
-               if (abs(tst.tv_sec - now.tv_sec) > 5)
+               if (labs(tst.tv_sec - now.tv_sec) > 5)
                        return pr_fail("%ld %ld\n",
                                        now.tv_sec, tst.tv_sec);
        }
@@ -70,7 +70,7 @@ int main(int argc, char *argv[])
                /* Check that a child process is in the new timens. */
                for (i = 0; i < 2; i++) {
                        _gettime(CLOCK_MONOTONIC, &tst, i);
-                       if (abs(tst.tv_sec - now.tv_sec - OFFSET) > 5)
+                       if (labs(tst.tv_sec - now.tv_sec - OFFSET) > 5)
                                return pr_fail("%ld %ld\n",
                                                now.tv_sec + OFFSET, tst.tv_sec);
                }
tools/testing/selftests/timens/timer.c +1 -1
index 5e7f0051bd7b..5b939f59dfa4 100644
@@ -56,7 +56,7 @@ int run_test(int clockid, struct timespec now)
                        return pr_perror("timerfd_gettime");
 
                elapsed = new_value.it_value.tv_sec;
-               if (abs(elapsed - 3600) > 60) {
+               if (llabs(elapsed - 3600) > 60) {
                        ksft_test_result_fail("clockid: %d elapsed: %lld\n",
                                              clockid, elapsed);
                        return 1;
tools/testing/selftests/timens/timerfd.c +1 -1
index 9edd43d6b2c1..a4196bbd6e33 100644
@@ -61,7 +61,7 @@ int run_test(int clockid, struct timespec now)
                        return pr_perror("timerfd_gettime(%d)", clockid);
 
                elapsed = new_value.it_value.tv_sec;
-               if (abs(elapsed - 3600) > 60) {
+               if (llabs(elapsed - 3600) > 60) {
                        ksft_test_result_fail("clockid: %d elapsed: %lld\n",
                                              clockid, elapsed);
                        return 1;
tools/testing/selftests/timens/vfork_exec.c +2 -2
index beb7614941fb..5b8907bf451d 100644
@@ -32,7 +32,7 @@ static void *tcheck(void *_args)
 
        for (i = 0; i < 2; i++) {
                _gettime(CLOCK_MONOTONIC, &tst, i);
-               if (abs(tst.tv_sec - now->tv_sec) > 5) {
+               if (labs(tst.tv_sec - now->tv_sec) > 5) {
                        pr_fail("%s: in-thread: unexpected value: %ld (%ld)\n",
                                args->tst_name, tst.tv_sec, now->tv_sec);
                        return (void *)1UL;
@@ -64,7 +64,7 @@ static int check(char *tst_name, struct timespec *now)
 
        for (i = 0; i < 2; i++) {
                _gettime(CLOCK_MONOTONIC, &tst, i);
-               if (abs(tst.tv_sec - now->tv_sec) > 5)
+               if (labs(tst.tv_sec - now->tv_sec) > 5)
                        return pr_fail("%s: unexpected value: %ld (%ld)\n",
                                        tst_name, tst.tv_sec, now->tv_sec);
        }

base-commit: f03359bca01bf4372cf2c118cd9a987a5951b1c8
prerequisite-patch-id: b901ece2a5b78503e2fb5480f20e304d36a0ea27 --
2.45.0

Reviewed-by on v1 1/1
#2 lore raw
In reply to John Hubbard
Quoted 9 lines · John Hubbard

First of all, in order to build with clang at all, one must first apply Valentin Obst's build fix for LLVM [1]. Once that is done, then when building with clang, via:

make LLVM=1 -C tools/testing/selftests

...then clang warns about mismatches between the expected and required integer length being supplied to abs(3).

Quoted 6 lines

Fix this by using the correct variant of abs(3): labs(3) or llabs(3), in these cases.

[1] https://lore.kernel.org/all/20240329-selftests-libmk-llvm-rfc-v1-1-2f9ed7d1c49f@valentinobst.de/

Thanks for fixing!

Quoted 89 lines
---
 tools/testing/selftests/timens/exec.c       | 6 +++---
 tools/testing/selftests/timens/timer.c      | 2 +-
 tools/testing/selftests/timens/timerfd.c    | 2 +-
 tools/testing/selftests/timens/vfork_exec.c | 4 ++--
 4 files changed, 7 insertions(+), 7 deletions(-)
tools/testing/selftests/timens/exec.c +3 -3
index e40dc5be2f66..d12ff955de0d 100644
@@ -30,7 +30,7 @@ int main(int argc, char *argv[])
 
 		for (i = 0; i < 2; i++) {
 			_gettime(CLOCK_MONOTONIC, &tst, i);
-			if (abs(tst.tv_sec - now.tv_sec) > 5)
+			if (labs(tst.tv_sec - now.tv_sec) > 5)
 				return pr_fail("%ld %ld\n", now.tv_sec, tst.tv_sec);
 		}
 		return 0;
@@ -50,7 +50,7 @@ int main(int argc, char *argv[])
 
 	for (i = 0; i < 2; i++) {
 		_gettime(CLOCK_MONOTONIC, &tst, i);
-		if (abs(tst.tv_sec - now.tv_sec) > 5)
+		if (labs(tst.tv_sec - now.tv_sec) > 5)
 			return pr_fail("%ld %ld\n",
 					now.tv_sec, tst.tv_sec);
 	}
@@ -70,7 +70,7 @@ int main(int argc, char *argv[])
 		/* Check that a child process is in the new timens. */
 		for (i = 0; i < 2; i++) {
 			_gettime(CLOCK_MONOTONIC, &tst, i);
-			if (abs(tst.tv_sec - now.tv_sec - OFFSET) > 5)
+			if (labs(tst.tv_sec - now.tv_sec - OFFSET) > 5)
 				return pr_fail("%ld %ld\n",
 						now.tv_sec + OFFSET, tst.tv_sec);
 		}
tools/testing/selftests/timens/timer.c +1 -1
index 5e7f0051bd7b..5b939f59dfa4 100644
@@ -56,7 +56,7 @@ int run_test(int clockid, struct timespec now)
 			return pr_perror("timerfd_gettime");
 
 		elapsed = new_value.it_value.tv_sec;
-		if (abs(elapsed - 3600) > 60) {
+		if (llabs(elapsed - 3600) > 60) {
 			ksft_test_result_fail("clockid: %d elapsed: %lld\n",
 					      clockid, elapsed);
 			return 1;
tools/testing/selftests/timens/timerfd.c +1 -1
index 9edd43d6b2c1..a4196bbd6e33 100644
@@ -61,7 +61,7 @@ int run_test(int clockid, struct timespec now)
 			return pr_perror("timerfd_gettime(%d)", clockid);
 
 		elapsed = new_value.it_value.tv_sec;
-		if (abs(elapsed - 3600) > 60) {
+		if (llabs(elapsed - 3600) > 60) {
 			ksft_test_result_fail("clockid: %d elapsed: %lld\n",
 					      clockid, elapsed);
 			return 1;
tools/testing/selftests/timens/vfork_exec.c +2 -2
index beb7614941fb..5b8907bf451d 100644
@@ -32,7 +32,7 @@ static void *tcheck(void *_args)
 
 	for (i = 0; i < 2; i++) {
 		_gettime(CLOCK_MONOTONIC, &tst, i);
-		if (abs(tst.tv_sec - now->tv_sec) > 5) {
+		if (labs(tst.tv_sec - now->tv_sec) > 5) {
 			pr_fail("%s: in-thread: unexpected value: %ld (%ld)\n",
 				args->tst_name, tst.tv_sec, now->tv_sec);
 			return (void *)1UL;
@@ -64,7 +64,7 @@ static int check(char *tst_name, struct timespec *now)
 
 	for (i = 0; i < 2; i++) {
 		_gettime(CLOCK_MONOTONIC, &tst, i);
-		if (abs(tst.tv_sec - now->tv_sec) > 5)
+		if (labs(tst.tv_sec - now->tv_sec) > 5)
 			return pr_fail("%s: unexpected value: %ld (%ld)\n",
 					tst_name, tst.tv_sec, now->tv_sec);
 	}

base-commit: f03359bca01bf4372cf2c118cd9a987a5951b1c8
prerequisite-patch-id: b901ece2a5b78503e2fb5480f20e304d36a0ea27

Signature · 2 lines
BR,
Muhammad Usama Anjum