cgroup/cpuset: simplify the percpu kthreads check in update_tasks_cpumask()

Message ID 20230704113049.1019118-1-linmiaohe@huawei.com
State New
Headers
Series cgroup/cpuset: simplify the percpu kthreads check in update_tasks_cpumask() |

Commit Message

Miaohe Lin July 4, 2023, 11:30 a.m. UTC
  kthread_is_per_cpu() can be called directly without checking whether
PF_KTHREAD is set in task->flags. So remove PF_KTHREAD check to make
code more concise.

Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
---
 kernel/cgroup/cpuset.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Waiman Long July 5, 2023, 3:14 a.m. UTC | #1
On 7/4/23 07:30, Miaohe Lin wrote:
> kthread_is_per_cpu() can be called directly without checking whether
> PF_KTHREAD is set in task->flags. So remove PF_KTHREAD check to make
> code more concise.
>
> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
> ---
>   kernel/cgroup/cpuset.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> index 58e6f18f01c1..601c40da8e03 100644
> --- a/kernel/cgroup/cpuset.c
> +++ b/kernel/cgroup/cpuset.c
> @@ -1230,7 +1230,7 @@ static void update_tasks_cpumask(struct cpuset *cs, struct cpumask *new_cpus)
>   			/*
>   			 * Percpu kthreads in top_cpuset are ignored
>   			 */
> -			if ((task->flags & PF_KTHREAD) && kthread_is_per_cpu(task))
> +			if (kthread_is_per_cpu(task))
>   				continue;
>   			cpumask_andnot(new_cpus, possible_mask, cs->subparts_cpus);
>   		} else {

The initial intention was to ignore only percpu kthreads. The current 
code likely ignore all the kthreads. Removing the PF_KTHREAD flag, 
however, may introduce unexpected regression at this point. I would like 
to hold off for now until more investigation are done.

Thanks,
Longman
  
Miaohe Lin July 5, 2023, 5:56 a.m. UTC | #2
On 2023/7/5 11:14, Waiman Long wrote:
> On 7/4/23 07:30, Miaohe Lin wrote:
>> kthread_is_per_cpu() can be called directly without checking whether
>> PF_KTHREAD is set in task->flags. So remove PF_KTHREAD check to make
>> code more concise.
>>
>> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
>> ---
>>   kernel/cgroup/cpuset.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
>> index 58e6f18f01c1..601c40da8e03 100644
>> --- a/kernel/cgroup/cpuset.c
>> +++ b/kernel/cgroup/cpuset.c
>> @@ -1230,7 +1230,7 @@ static void update_tasks_cpumask(struct cpuset *cs, struct cpumask *new_cpus)
>>               /*
>>                * Percpu kthreads in top_cpuset are ignored
>>                */
>> -            if ((task->flags & PF_KTHREAD) && kthread_is_per_cpu(task))
>> +            if (kthread_is_per_cpu(task))
>>                   continue;
>>               cpumask_andnot(new_cpus, possible_mask, cs->subparts_cpus);
>>           } else {
> 
> The initial intention was to ignore only percpu kthreads. The current code likely ignore all the kthreads. Removing the PF_KTHREAD flag, however, may introduce unexpected regression at this point. I would like to hold off for now until more investigation are done.

IMHO, the current code will ignore only percpu kthreads:
  1.If PF_KTHREAD is set in task->flags, this patch doesn't make any difference.
  2.If PF_KTHREAD is not set in task->flags, kthread_is_per_cpu will *always return false*. So this patch doesn't make any functional change.

    bool kthread_is_per_cpu(struct task_struct *p)
    {
        struct kthread *kthread = __to_kthread(p);
	if (!kthread)
		return false;
        ....
    }

    static inline struct kthread *__to_kthread(struct task_struct *p)
    {
	void *kthread = p->worker_private;
	if (kthread && !(p->flags & PF_KTHREAD))
			 ^^^^^^^^^^^^^^^^^^^^^^
			 PF_KTHREAD is not set, so kthread = NULL.
		kthread = NULL;
	return kthread;
    }

Or am I miss something? Thanks for comment and review.
  
Waiman Long July 5, 2023, 2:49 p.m. UTC | #3
On 7/5/23 01:56, Miaohe Lin wrote:
> On 2023/7/5 11:14, Waiman Long wrote:
>> On 7/4/23 07:30, Miaohe Lin wrote:
>>> kthread_is_per_cpu() can be called directly without checking whether
>>> PF_KTHREAD is set in task->flags. So remove PF_KTHREAD check to make
>>> code more concise.
>>>
>>> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
>>> ---
>>>    kernel/cgroup/cpuset.c | 2 +-
>>>    1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
>>> index 58e6f18f01c1..601c40da8e03 100644
>>> --- a/kernel/cgroup/cpuset.c
>>> +++ b/kernel/cgroup/cpuset.c
>>> @@ -1230,7 +1230,7 @@ static void update_tasks_cpumask(struct cpuset *cs, struct cpumask *new_cpus)
>>>                /*
>>>                 * Percpu kthreads in top_cpuset are ignored
>>>                 */
>>> -            if ((task->flags & PF_KTHREAD) && kthread_is_per_cpu(task))
>>> +            if (kthread_is_per_cpu(task))
>>>                    continue;
>>>                cpumask_andnot(new_cpus, possible_mask, cs->subparts_cpus);
>>>            } else {
>> The initial intention was to ignore only percpu kthreads. The current code likely ignore all the kthreads. Removing the PF_KTHREAD flag, however, may introduce unexpected regression at this point. I would like to hold off for now until more investigation are done.
> IMHO, the current code will ignore only percpu kthreads:
>    1.If PF_KTHREAD is set in task->flags, this patch doesn't make any difference.
>    2.If PF_KTHREAD is not set in task->flags, kthread_is_per_cpu will *always return false*. So this patch doesn't make any functional change.
>
>      bool kthread_is_per_cpu(struct task_struct *p)
>      {
>          struct kthread *kthread = __to_kthread(p);
> 	if (!kthread)
> 		return false;
>          ....
>      }
>
>      static inline struct kthread *__to_kthread(struct task_struct *p)
>      {
> 	void *kthread = p->worker_private;
> 	if (kthread && !(p->flags & PF_KTHREAD))
> 			 ^^^^^^^^^^^^^^^^^^^^^^
> 			 PF_KTHREAD is not set, so kthread = NULL.
> 		kthread = NULL;
> 	return kthread;
>      }
>
> Or am I miss something? Thanks for comment and review.

Yes, you are right. I was that conscious when I reviewed the patch last 
night :-)

Reviewed-by: Waiman Long <longman@redhat.com>
  
Tejun Heo July 10, 2023, 8:25 p.m. UTC | #4
On Tue, Jul 04, 2023 at 07:30:49PM +0800, Miaohe Lin wrote:
> kthread_is_per_cpu() can be called directly without checking whether
> PF_KTHREAD is set in task->flags. So remove PF_KTHREAD check to make
> code more concise.
> 
> Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>

Applied to cgroup/for-6.6.

Thanks.
  

Patch

diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index 58e6f18f01c1..601c40da8e03 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -1230,7 +1230,7 @@  static void update_tasks_cpumask(struct cpuset *cs, struct cpumask *new_cpus)
 			/*
 			 * Percpu kthreads in top_cpuset are ignored
 			 */
-			if ((task->flags & PF_KTHREAD) && kthread_is_per_cpu(task))
+			if (kthread_is_per_cpu(task))
 				continue;
 			cpumask_andnot(new_cpus, possible_mask, cs->subparts_cpus);
 		} else {