diff mbox series

[2/2] selftests: cgroup: use function 'labs()' over 'abs()'

Message ID 20211105162530.3307666-2-anders.roxell@linaro.org (mailing list archive)
State New
Headers show
Series [1/2] selftests: cgroup: build error multiple outpt files | expand

Commit Message

Anders Roxell Nov. 5, 2021, 4:25 p.m. UTC
When building selftests/cgroup with clang, the compiler warn about the
function abs() see below:

In file included from test_memcontrol.c:21:
./cgroup_util.h:16:9: warning: absolute value function 'abs' given an argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Wabsolute-value]
        return abs(a - b) <= (a + b) / 100 * err;
               ^
./cgroup_util.h:16:9: note: use function 'labs' instead
        return abs(a - b) <= (a + b) / 100 * err;
               ^~~
               labs

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

Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
---
 tools/testing/selftests/cgroup/cgroup_util.h | 2 +-
 tools/testing/selftests/cgroup/test_kmem.c   | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

Comments

Nick Desaulniers Nov. 5, 2021, 8:15 p.m. UTC | #1
On Fri, Nov 5, 2021 at 9:25 AM Anders Roxell <anders.roxell@linaro.org> wrote:
>
> When building selftests/cgroup with clang, the compiler warn about the
> function abs() see below:
>
> In file included from test_memcontrol.c:21:
> ./cgroup_util.h:16:9: warning: absolute value function 'abs' given an argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Wabsolute-value]
>         return abs(a - b) <= (a + b) / 100 * err;
>                ^
> ./cgroup_util.h:16:9: note: use function 'labs' instead
>         return abs(a - b) <= (a + b) / 100 * err;
>                ^~~
>                labs
>
> The note indicates what to do, Rework to use the function 'labs()'.
>
> Signed-off-by: Anders Roxell <anders.roxell@linaro.org>

Thanks for the patch!
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

> ---
>  tools/testing/selftests/cgroup/cgroup_util.h | 2 +-
>  tools/testing/selftests/cgroup/test_kmem.c   | 4 ++--
>  2 files changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/tools/testing/selftests/cgroup/cgroup_util.h b/tools/testing/selftests/cgroup/cgroup_util.h
> index 82e59cdf16e7..76b35d9dffb5 100644
> --- a/tools/testing/selftests/cgroup/cgroup_util.h
> +++ b/tools/testing/selftests/cgroup/cgroup_util.h
> @@ -13,7 +13,7 @@
>   */
>  static inline int values_close(long a, long b, int err)
>  {
> -       return abs(a - b) <= (a + b) / 100 * err;
> +       return labs(a - b) <= (a + b) / 100 * err;
>  }
>
>  extern int cg_find_unified_root(char *root, size_t len);
> diff --git a/tools/testing/selftests/cgroup/test_kmem.c b/tools/testing/selftests/cgroup/test_kmem.c
> index 22b31ebb3513..d65bb8fe876a 100644
> --- a/tools/testing/selftests/cgroup/test_kmem.c
> +++ b/tools/testing/selftests/cgroup/test_kmem.c
> @@ -192,7 +192,7 @@ static int test_kmem_memcg_deletion(const char *root)
>                 goto cleanup;
>
>         sum = slab + anon + file + kernel_stack + pagetables + percpu + sock;
> -       if (abs(sum - current) < MAX_VMSTAT_ERROR) {
> +       if (labs(sum - current) < MAX_VMSTAT_ERROR) {
>                 ret = KSFT_PASS;
>         } else {
>                 printf("memory.current = %ld\n", current);
> @@ -383,7 +383,7 @@ static int test_percpu_basic(const char *root)
>         current = cg_read_long(parent, "memory.current");
>         percpu = cg_read_key_long(parent, "memory.stat", "percpu ");
>
> -       if (current > 0 && percpu > 0 && abs(current - percpu) <
> +       if (current > 0 && percpu > 0 && labs(current - percpu) <
>             MAX_VMSTAT_ERROR)
>                 ret = KSFT_PASS;
>         else
> --
> 2.33.0
>
Christian Brauner Nov. 9, 2021, 3:02 p.m. UTC | #2
On Fri, Nov 05, 2021 at 05:25:30PM +0100, Anders Roxell wrote:
> When building selftests/cgroup with clang, the compiler warn about the
> function abs() see below:
> 
> In file included from test_memcontrol.c:21:
> ./cgroup_util.h:16:9: warning: absolute value function 'abs' given an argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Wabsolute-value]
>         return abs(a - b) <= (a + b) / 100 * err;
>                ^
> ./cgroup_util.h:16:9: note: use function 'labs' instead
>         return abs(a - b) <= (a + b) / 100 * err;
>                ^~~
>                labs
> 
> The note indicates what to do, Rework to use the function 'labs()'.
> 
> Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
> ---

Thanks!
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
diff mbox series

Patch

diff --git a/tools/testing/selftests/cgroup/cgroup_util.h b/tools/testing/selftests/cgroup/cgroup_util.h
index 82e59cdf16e7..76b35d9dffb5 100644
--- a/tools/testing/selftests/cgroup/cgroup_util.h
+++ b/tools/testing/selftests/cgroup/cgroup_util.h
@@ -13,7 +13,7 @@ 
  */
 static inline int values_close(long a, long b, int err)
 {
-	return abs(a - b) <= (a + b) / 100 * err;
+	return labs(a - b) <= (a + b) / 100 * err;
 }
 
 extern int cg_find_unified_root(char *root, size_t len);
diff --git a/tools/testing/selftests/cgroup/test_kmem.c b/tools/testing/selftests/cgroup/test_kmem.c
index 22b31ebb3513..d65bb8fe876a 100644
--- a/tools/testing/selftests/cgroup/test_kmem.c
+++ b/tools/testing/selftests/cgroup/test_kmem.c
@@ -192,7 +192,7 @@  static int test_kmem_memcg_deletion(const char *root)
 		goto cleanup;
 
 	sum = slab + anon + file + kernel_stack + pagetables + percpu + sock;
-	if (abs(sum - current) < MAX_VMSTAT_ERROR) {
+	if (labs(sum - current) < MAX_VMSTAT_ERROR) {
 		ret = KSFT_PASS;
 	} else {
 		printf("memory.current = %ld\n", current);
@@ -383,7 +383,7 @@  static int test_percpu_basic(const char *root)
 	current = cg_read_long(parent, "memory.current");
 	percpu = cg_read_key_long(parent, "memory.stat", "percpu ");
 
-	if (current > 0 && percpu > 0 && abs(current - percpu) <
+	if (current > 0 && percpu > 0 && labs(current - percpu) <
 	    MAX_VMSTAT_ERROR)
 		ret = KSFT_PASS;
 	else