[next] cgroup/cpuset: Cleanup signedness issue in cpu_exclusive_check()

Message ID 20230927065801.2139969-1-harshit.m.mogalapalli@oracle.com
State New
Headers
Series [next] cgroup/cpuset: Cleanup signedness issue in cpu_exclusive_check() |

Commit Message

Harshit Mogalapalli Sept. 27, 2023, 6:58 a.m. UTC
  Smatch complains about returning negative error codes from a type
bool function.

kernel/cgroup/cpuset.c:705 cpu_exclusive_check() warn:
	signedness bug returning '(-22)'

The code works correctly, but it is confusing.  The current behavior is
that cpu_exclusive_check() returns true if it's *NOT* exclusive.  Rename
it to cpusets_are_exclusive() and reverse the returns so it returns true
if it is exclusive and false if it's not.  Update both callers as well.

Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <error27@gmail.com>
Closes: https://lore.kernel.org/r/202309201706.2LhKdM6o-lkp@intel.com/
Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
---
This is based on sattic analysis, only compile tested
---
 kernel/cgroup/cpuset.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
  

Comments

Kamalesh Babulal Sept. 27, 2023, 9:37 a.m. UTC | #1
On 9/27/23 12:28, Harshit Mogalapalli wrote:
> Smatch complains about returning negative error codes from a type
> bool function.
> 
> kernel/cgroup/cpuset.c:705 cpu_exclusive_check() warn:
> 	signedness bug returning '(-22)'
> 
> The code works correctly, but it is confusing.  The current behavior is
> that cpu_exclusive_check() returns true if it's *NOT* exclusive.  Rename
> it to cpusets_are_exclusive() and reverse the returns so it returns true
> if it is exclusive and false if it's not.  Update both callers as well.
> 
> Reported-by: kernel test robot <lkp@intel.com>
> Reported-by: Dan Carpenter <error27@gmail.com>
> Closes: https://lore.kernel.org/r/202309201706.2LhKdM6o-lkp@intel.com/
> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>

The patch looks good to me, returning true on exclusive cpusets is more
intuitive. Renaming cpu_exclusive_check() to is_cpuset_exclusive() is
one other option, though, there is is_cpu_exclusive() function, which
sounds similar, and tests for the cpu exclusive bit in a given cpuset's
flag.

I don't have a strong opinion between the original function name and
the proposed rename.

Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>

> ---
> This is based on sattic analysis, only compile tested
> ---
>  kernel/cgroup/cpuset.c | 14 +++++++-------
>  1 file changed, 7 insertions(+), 7 deletions(-)
> 
> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> index 15f399153a2e..afefddd33c3e 100644
> --- a/kernel/cgroup/cpuset.c
> +++ b/kernel/cgroup/cpuset.c
> @@ -719,18 +719,18 @@ static inline struct cpumask *fetch_xcpus(struct cpuset *cs)
>  }
>  
>  /*
> - * cpu_exclusive_check() - check if two cpusets are exclusive
> + * cpusets_are_exclusive() - check if two cpusets are exclusive
>   *
> - * Return 0 if exclusive, -EINVAL if not
> + * Return true if exclusive, false if not
>   */
> -static inline bool cpu_exclusive_check(struct cpuset *cs1, struct cpuset *cs2)
> +static inline bool cpusets_are_exclusive(struct cpuset *cs1, struct cpuset *cs2)
>  {
>  	struct cpumask *xcpus1 = fetch_xcpus(cs1);
>  	struct cpumask *xcpus2 = fetch_xcpus(cs2);
>  
>  	if (cpumask_intersects(xcpus1, xcpus2))
> -		return -EINVAL;
> -	return 0;
> +		return false;
> +	return true;
>  }
>  
>  /*
> @@ -833,7 +833,7 @@ static int validate_change(struct cpuset *cur, struct cpuset *trial)
>  	cpuset_for_each_child(c, css, par) {
>  		if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
>  		    c != cur) {
> -			if (cpu_exclusive_check(trial, c))
> +			if (!cpusets_are_exclusive(trial, c))
>  				goto out;
>  		}
>  		if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
> @@ -1864,7 +1864,7 @@ static int update_parent_effective_cpumask(struct cpuset *cs, int cmd,
>  			cpuset_for_each_child(child, css, parent) {
>  				if (child == cs)
>  					continue;
> -				if (cpu_exclusive_check(cs, child)) {
> +				if (!cpusets_are_exclusive(cs, child)) {
>  					exclusive = false;
>  					break;
>  				}
  
Waiman Long Sept. 27, 2023, 1:13 p.m. UTC | #2
On 9/27/23 02:58, Harshit Mogalapalli wrote:
> Smatch complains about returning negative error codes from a type
> bool function.
>
> kernel/cgroup/cpuset.c:705 cpu_exclusive_check() warn:
> 	signedness bug returning '(-22)'
>
> The code works correctly, but it is confusing.  The current behavior is
> that cpu_exclusive_check() returns true if it's *NOT* exclusive.  Rename
> it to cpusets_are_exclusive() and reverse the returns so it returns true
> if it is exclusive and false if it's not.  Update both callers as well.
>
> Reported-by: kernel test robot <lkp@intel.com>
> Reported-by: Dan Carpenter <error27@gmail.com>
> Closes: https://lore.kernel.org/r/202309201706.2LhKdM6o-lkp@intel.com/
> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
> ---
> This is based on sattic analysis, only compile tested
> ---
>   kernel/cgroup/cpuset.c | 14 +++++++-------
>   1 file changed, 7 insertions(+), 7 deletions(-)
>
> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> index 15f399153a2e..afefddd33c3e 100644
> --- a/kernel/cgroup/cpuset.c
> +++ b/kernel/cgroup/cpuset.c
> @@ -719,18 +719,18 @@ static inline struct cpumask *fetch_xcpus(struct cpuset *cs)
>   }
>   
>   /*
> - * cpu_exclusive_check() - check if two cpusets are exclusive
> + * cpusets_are_exclusive() - check if two cpusets are exclusive
>    *
> - * Return 0 if exclusive, -EINVAL if not
> + * Return true if exclusive, false if not
>    */
> -static inline bool cpu_exclusive_check(struct cpuset *cs1, struct cpuset *cs2)
> +static inline bool cpusets_are_exclusive(struct cpuset *cs1, struct cpuset *cs2)
>   {
>   	struct cpumask *xcpus1 = fetch_xcpus(cs1);
>   	struct cpumask *xcpus2 = fetch_xcpus(cs2);
>   
>   	if (cpumask_intersects(xcpus1, xcpus2))
> -		return -EINVAL;
> -	return 0;
> +		return false;
> +	return true;
>   }
>   
>   /*
> @@ -833,7 +833,7 @@ static int validate_change(struct cpuset *cur, struct cpuset *trial)
>   	cpuset_for_each_child(c, css, par) {
>   		if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
>   		    c != cur) {
> -			if (cpu_exclusive_check(trial, c))
> +			if (!cpusets_are_exclusive(trial, c))
>   				goto out;
>   		}
>   		if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
> @@ -1864,7 +1864,7 @@ static int update_parent_effective_cpumask(struct cpuset *cs, int cmd,
>   			cpuset_for_each_child(child, css, parent) {
>   				if (child == cs)
>   					continue;
> -				if (cpu_exclusive_check(cs, child)) {
> +				if (!cpusets_are_exclusive(cs, child)) {
>   					exclusive = false;
>   					break;
>   				}

Thanks for fixing that.

Acked-by: Waiman Long <longman@redhat.com>
  
Tejun Heo Oct. 4, 2023, 6:59 p.m. UTC | #3
On Tue, Sep 26, 2023 at 11:58:01PM -0700, Harshit Mogalapalli wrote:
> Smatch complains about returning negative error codes from a type
> bool function.
> 
> kernel/cgroup/cpuset.c:705 cpu_exclusive_check() warn:
> 	signedness bug returning '(-22)'
> 
> The code works correctly, but it is confusing.  The current behavior is
> that cpu_exclusive_check() returns true if it's *NOT* exclusive.  Rename
> it to cpusets_are_exclusive() and reverse the returns so it returns true
> if it is exclusive and false if it's not.  Update both callers as well.
> 
> Reported-by: kernel test robot <lkp@intel.com>
> Reported-by: Dan Carpenter <error27@gmail.com>
> Closes: https://lore.kernel.org/r/202309201706.2LhKdM6o-lkp@intel.com/
> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>

Applied to cgroup/for-6.7.

Thanks.
  

Patch

diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index 15f399153a2e..afefddd33c3e 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -719,18 +719,18 @@  static inline struct cpumask *fetch_xcpus(struct cpuset *cs)
 }
 
 /*
- * cpu_exclusive_check() - check if two cpusets are exclusive
+ * cpusets_are_exclusive() - check if two cpusets are exclusive
  *
- * Return 0 if exclusive, -EINVAL if not
+ * Return true if exclusive, false if not
  */
-static inline bool cpu_exclusive_check(struct cpuset *cs1, struct cpuset *cs2)
+static inline bool cpusets_are_exclusive(struct cpuset *cs1, struct cpuset *cs2)
 {
 	struct cpumask *xcpus1 = fetch_xcpus(cs1);
 	struct cpumask *xcpus2 = fetch_xcpus(cs2);
 
 	if (cpumask_intersects(xcpus1, xcpus2))
-		return -EINVAL;
-	return 0;
+		return false;
+	return true;
 }
 
 /*
@@ -833,7 +833,7 @@  static int validate_change(struct cpuset *cur, struct cpuset *trial)
 	cpuset_for_each_child(c, css, par) {
 		if ((is_cpu_exclusive(trial) || is_cpu_exclusive(c)) &&
 		    c != cur) {
-			if (cpu_exclusive_check(trial, c))
+			if (!cpusets_are_exclusive(trial, c))
 				goto out;
 		}
 		if ((is_mem_exclusive(trial) || is_mem_exclusive(c)) &&
@@ -1864,7 +1864,7 @@  static int update_parent_effective_cpumask(struct cpuset *cs, int cmd,
 			cpuset_for_each_child(child, css, parent) {
 				if (child == cs)
 					continue;
-				if (cpu_exclusive_check(cs, child)) {
+				if (!cpusets_are_exclusive(cs, child)) {
 					exclusive = false;
 					break;
 				}