Skip to contentSkip to conversation

selftests: timens: use 'llabs()' over 'abs()'

PATCH Superseded Anders Roxell 4 participants 11 messages Nov 5, 2021 – Nov 20, 2021 Kselftest

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 selftests: timens: use 'llabs()' over 'abs()'

When building selftests/timens with clang, the compiler warn about the function abs() see below:

timerfd.c:64:7: error: absolute value function 'abs' given an argument of type 'long long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]

                if (abs(elapsed - 3600) > 60) {
                    ^
timerfd.c:64:7: note: use function 'llabs' instead
                if (abs(elapsed - 3600) > 60) {
                    ^~~
                    llabs

The note indicates what to do, Rework to use the function 'llabs()'.

---
 tools/testing/selftests/timens/timer.c   | 2 +-
 tools/testing/selftests/timens/timerfd.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
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;
Signature · 1 line
2.33.0
#1 lore raw

PATCH 2/2 selftests: timens: exec: use 'labs()' over 'abs()'

When building selftests/timens with clang, the compiler warn about the function abs() see below:

exec.c:33:8: error: absolute value function 'abs' given an argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]

                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^
exec.c:33:8: note: use function 'labs' instead
                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^~~
                            labs

The note indicates what to do, Rework to use the function 'labs()'.

---
 tools/testing/selftests/timens/exec.c | 6 +++---
 1 file changed, 3 insertions(+), 3 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);
 		}
Signature · 1 line
2.33.0
#2 lore raw

Re: [PATCH 2/2] selftests: timens: exec: use 'labs()' over 'abs()'

Quoted 11 lines · Anders Roxell

When building selftests/timens with clang, the compiler warn about the function abs() see below:

exec.c:33:8: error: absolute value function 'abs' given an argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]

                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^
exec.c:33:8: note: use function 'labs' instead
                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^~~
                            labs

Careful.

Isn't the tv_sec member of struct timespec a time_t which is 32b on 32b hosts and 64b on 64b hosts? If I'm recalling that correctly, then this patch results in a harmless (though unnecessary) sign extension for 32b targets. That should be fine, but someone like Arnd should triple check if my concern is valid or not.

So I'm in favor of this patch (dispatching to abs or labs based on 64b host) would hurt readability.

Quoted 42 lines

The note indicates what to do, Rework to use the function 'labs()'.

---
 tools/testing/selftests/timens/exec.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/timens/exec.c +3 -4
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);
                }
--

2.33.0

Signature · 2 lines
Thanks,
~Nick Desaulniers
#3 lore raw

Re: [PATCH 2/2] selftests: timens: exec: use 'labs()' over 'abs()'

Quoted 14 lines · Nick Desaulniers
Quoted 11 lines · Anders Roxell

When building selftests/timens with clang, the compiler warn about the function abs() see below:

exec.c:33:8: error: absolute value function 'abs' given an argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]

                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^
exec.c:33:8: note: use function 'labs' instead
                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^~~
                            labs
Quoted 7 lines

Careful.

Isn't the tv_sec member of struct timespec a time_t which is 32b on 32b hosts and 64b on 64b hosts? If I'm recalling that correctly, then this patch results in a harmless (though unnecessary) sign extension for 32b targets. That should be fine, but someone like Arnd should triple check if my concern is valid or not.

It could actually be 'int', 'long' or 'long long' depending on the architecture and C library. Maybe we need a temporary variable of type 'long long' to hold the difference, and pass that to llabs()?

Arnd

#4 lore raw

Re: [PATCH 2/2] selftests: timens: exec: use 'labs()' over 'abs()'

Quoted 24 lines · Arnd Bergmann
Quoted 14 lines · Nick Desaulniers
Quoted 11 lines · Anders Roxell

When building selftests/timens with clang, the compiler warn about the function abs() see below:

exec.c:33:8: error: absolute value function 'abs' given an argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]

                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^
exec.c:33:8: note: use function 'labs' instead
                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^~~
                            labs
Quoted 7 lines

Careful.

Isn't the tv_sec member of struct timespec a time_t which is 32b on 32b hosts and 64b on 64b hosts? If I'm recalling that correctly, then this patch results in a harmless (though unnecessary) sign extension for 32b targets. That should be fine, but someone like Arnd should triple check if my concern is valid or not.

Quoted 3 lines

It could actually be 'int', 'long' or 'long long' depending on the architecture and C library. Maybe we need a temporary variable of type 'long long' to hold the difference, and pass that to llabs()?

Yeah, that SGTM. Thanks for the review!

Signature · 2 lines
Thanks,
~Nick Desaulniers
#5 lore raw

PATCH v2 1/1 selftests: timens: exec: use 'labs()' over 'abs()'

In reply to Arnd Bergmann

When building selftests/timens with clang, the compiler warn about the function abs() see below:

exec.c:33:8: error: absolute value function 'abs' given an argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]

                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^
exec.c:33:8: note: use function 'labs' instead
                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^~~
                            labs

Rework to store the time difference in a 'long long' and pass that to llabs(), since the variable can be an 'int', 'long' or 'long long' depending on the architecture and C library.

---
 tools/testing/selftests/timens/exec.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
tools/testing/selftests/timens/exec.c +7 -3
index e40dc5be2f66..04439e6ac8a2 100644
@@ -21,6 +21,7 @@
 int main(int argc, char *argv[])
 {
 	struct timespec now, tst;
+	long long timediff;
 	int status, i;
 	pid_t pid;
 
@@ -30,7 +31,8 @@ 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)
+			timediff = tst.tv_sec - now.tv_sec;
+			if (llabs(timediff) > 5)
 				return pr_fail("%ld %ld\n", now.tv_sec, tst.tv_sec);
 		}
 		return 0;
@@ -50,7 +52,8 @@ 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)
+		timediff = tst.tv_sec - now.tv_sec;
+		if (llabs(timediff) > 5)
 			return pr_fail("%ld %ld\n",
 					now.tv_sec, tst.tv_sec);
 	}
@@ -70,7 +73,8 @@ 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)
+			timediff = tst.tv_sec - now.tv_sec - OFFSET;
+			if (llabs(timediff) > 5)
 				return pr_fail("%ld %ld\n",
 						now.tv_sec + OFFSET, tst.tv_sec);
 		}
Signature · 1 line
2.33.0
Reviewed-by on v2 1/1
#6 lore raw

Re: [PATCHv2] selftests: timens: exec: use 'labs()' over 'abs()'

Quoted 12 lines · Anders Roxell

When building selftests/timens with clang, the compiler warn about the function abs() see below:

exec.c:33:8: error: absolute value function 'abs' given an argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]

                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^
exec.c:33:8: note: use function 'labs' instead
                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^~~
                            labs
Quoted 6 lines

Rework to store the time difference in a 'long long' and pass that to llabs(), since the variable can be an 'int', 'long' or 'long long' depending on the architecture and C library.

Thanks for the patch!

Quoted 49 lines
---
 tools/testing/selftests/timens/exec.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
tools/testing/selftests/timens/exec.c +7 -4
index e40dc5be2f66..04439e6ac8a2 100644
@@ -21,6 +21,7 @@
 int main(int argc, char *argv[])
 {
        struct timespec now, tst;
+       long long timediff;
        int status, i;
        pid_t pid;
 
@@ -30,7 +31,8 @@ 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)
+                       timediff = tst.tv_sec - now.tv_sec;
+                       if (llabs(timediff) > 5)
                                return pr_fail("%ld %ld\n", now.tv_sec, tst.tv_sec);
                }
                return 0;
@@ -50,7 +52,8 @@ 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)
+               timediff = tst.tv_sec - now.tv_sec;
+               if (llabs(timediff) > 5)
                        return pr_fail("%ld %ld\n",
                                        now.tv_sec, tst.tv_sec);
        }
@@ -70,7 +73,8 @@ 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)
+                       timediff = tst.tv_sec - now.tv_sec - OFFSET;
+                       if (llabs(timediff) > 5)
                                return pr_fail("%ld %ld\n",
                                                now.tv_sec + OFFSET, tst.tv_sec);
                }
--

2.33.0

Signature · 2 lines
Thanks,
~Nick Desaulniers
#7 lore raw

Re: [PATCHv2] selftests: timens: exec: use 'labs()' over 'abs()'

Quoted 18 lines · Nick Desaulniers
Quoted 16 lines · Anders Roxell

When building selftests/timens with clang, the compiler warn about the function abs() see below:

exec.c:33:8: error: absolute value function 'abs' given an argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]

                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^
exec.c:33:8: note: use function 'labs' instead
                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^~~
                            labs

Rework to store the time difference in a 'long long' and pass that to llabs(), since the variable can be an 'int', 'long' or 'long long' depending on the architecture and C library.

Quoted 5 lines
Quoted 2 lines

Thanks for the patch!

ah, gmail doesn't do a great job at showing the subject when a v2 is sent in-reply-to. Should the oneline mention llabs rather than labs now?

Quoted 55 lines
Quoted 48 lines
---
 tools/testing/selftests/timens/exec.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
tools/testing/selftests/timens/exec.c +7 -4
index e40dc5be2f66..04439e6ac8a2 100644
@@ -21,6 +21,7 @@
 int main(int argc, char *argv[])
 {
        struct timespec now, tst;
+       long long timediff;
        int status, i;
        pid_t pid;
 
@@ -30,7 +31,8 @@ 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)
+                       timediff = tst.tv_sec - now.tv_sec;
+                       if (llabs(timediff) > 5)
                                return pr_fail("%ld %ld\n", now.tv_sec, tst.tv_sec);
                }
                return 0;
@@ -50,7 +52,8 @@ 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)
+               timediff = tst.tv_sec - now.tv_sec;
+               if (llabs(timediff) > 5)
                        return pr_fail("%ld %ld\n",
                                        now.tv_sec, tst.tv_sec);
        }
@@ -70,7 +73,8 @@ 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)
+                       timediff = tst.tv_sec - now.tv_sec - OFFSET;
+                       if (llabs(timediff) > 5)
                                return pr_fail("%ld %ld\n",
                                                now.tv_sec + OFFSET, tst.tv_sec);
                }
--
Quoted 2 lines

2.33.0

--
Thanks,
~Nick Desaulniers

Signature · 2 lines
Thanks,
~Nick Desaulniers
#8 lore raw

Re: [PATCHv2] selftests: timens: exec: use 'labs()' over 'abs()'

Quoted 21 lines · Nick Desaulniers
Quoted 18 lines · Nick Desaulniers
Quoted 16 lines · Anders Roxell

When building selftests/timens with clang, the compiler warn about the function abs() see below:

exec.c:33:8: error: absolute value function 'abs' given an argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]

                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^
exec.c:33:8: note: use function 'labs' instead
                        if (abs(tst.tv_sec - now.tv_sec) > 5)
                            ^~~
                            labs

Rework to store the time difference in a 'long long' and pass that to llabs(), since the variable can be an 'int', 'long' or 'long long' depending on the architecture and C library.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Thanks for the patch!

ah, gmail doesn't do a great job at showing the subject when a v2 is sent in-reply-to.

oh right, sorry.

Quoted 2 lines

Should the oneline mention llabs rather than labs
now?

You are correct, can this be changed when the patch gets applied or should I send a v3?

Cheers,
Anders

Reviewed-by on v1 1/2
#9 lore raw
In reply to Anders Roxell
Quoted 12 lines · Anders Roxell

When building selftests/timens with clang, the compiler warn about the function abs() see below:

timerfd.c:64:7: error: absolute value function 'abs' given an argument of type 'long long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]

                if (abs(elapsed - 3600) > 60) {
                    ^
timerfd.c:64:7: note: use function 'llabs' instead
                if (abs(elapsed - 3600) > 60) {
                    ^~~
                    llabs
Quoted 3 lines

The note indicates what to do, Rework to use the function 'llabs()'.

Thanks for the patch!

Quoted 34 lines
---
 tools/testing/selftests/timens/timer.c   | 2 +-
 tools/testing/selftests/timens/timerfd.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
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 -2
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;
--

2.33.0

Signature · 2 lines
Thanks,
~Nick Desaulniers
maintainer
#10 lore raw
Quoted 45 lines · Nick Desaulniers
Quoted 12 lines · Anders Roxell

When building selftests/timens with clang, the compiler warn about the function abs() see below:

timerfd.c:64:7: error: absolute value function 'abs' given an argument of type 'long long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]

                 if (abs(elapsed - 3600) > 60) {
                     ^
timerfd.c:64:7: note: use function 'llabs' instead
                 if (abs(elapsed - 3600) > 60) {
                     ^~~
                     llabs
Quoted 3 lines

The note indicates what to do, Rework to use the function 'llabs()'.

Thanks for the patch!

Quoted 25 lines
---
  tools/testing/selftests/timens/timer.c   | 2 +-
  tools/testing/selftests/timens/timerfd.c | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
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 +0 -0
index 9edd43d6b2c1..a4196bbd6e33 100644
@@ -61,7 +61,7 @@ int run_test(int clockid, struct timespec now)
                         return pr_perror("timerfd_gettime(%d)", clockid);
 
Quoted 11 lines
Quoted 9 lines
tools/testing/selftests/timens/timerfd.c +1 -2
                 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;
--

2.33.0

Same comment on llabs() define in stdlib.h made earlier in the context of timer test changes.

thanks,
-- Shuah