[-next v2 4/4] cpuset: add helpers for cpus read and cpuset_mutex locks

Chen Ridong posted 4 patches 4 months ago
There is a newer version of this series
[-next v2 4/4] cpuset: add helpers for cpus read and cpuset_mutex locks
Posted by Chen Ridong 4 months ago
From: Chen Ridong <chenridong@huawei.com>

cpuset: add helpers for cpus_read_lock and cpuset_mutex

Replace repetitive locking patterns with new helpers:
- cpus_read_cpuset_lock()
- cpus_read_cpuset_unlock()

This makes the code cleaner and ensures consistent lock ordering.

Signed-off-by: Chen Ridong <chenridong@huawei.com>
---
 kernel/cgroup/cpuset-internal.h |  2 ++
 kernel/cgroup/cpuset-v1.c       | 12 +++------
 kernel/cgroup/cpuset.c          | 48 +++++++++++++++------------------
 3 files changed, 28 insertions(+), 34 deletions(-)

diff --git a/kernel/cgroup/cpuset-internal.h b/kernel/cgroup/cpuset-internal.h
index 75b3aef39231..6fb00c96044d 100644
--- a/kernel/cgroup/cpuset-internal.h
+++ b/kernel/cgroup/cpuset-internal.h
@@ -276,6 +276,8 @@ int cpuset_update_flag(cpuset_flagbits_t bit, struct cpuset *cs, int turning_on)
 ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
 				    char *buf, size_t nbytes, loff_t off);
 int cpuset_common_seq_show(struct seq_file *sf, void *v);
+void cpus_read_cpuset_lock(void);
+void cpus_read_cpuset_unlock(void);
 
 /*
  * cpuset-v1.c
diff --git a/kernel/cgroup/cpuset-v1.c b/kernel/cgroup/cpuset-v1.c
index b69a7db67090..f3d2d116c842 100644
--- a/kernel/cgroup/cpuset-v1.c
+++ b/kernel/cgroup/cpuset-v1.c
@@ -169,8 +169,7 @@ static int cpuset_write_s64(struct cgroup_subsys_state *css, struct cftype *cft,
 	cpuset_filetype_t type = cft->private;
 	int retval = -ENODEV;
 
-	cpus_read_lock();
-	cpuset_lock();
+	cpus_read_cpuset_lock();
 	if (!is_cpuset_online(cs))
 		goto out_unlock;
 
@@ -184,8 +183,7 @@ static int cpuset_write_s64(struct cgroup_subsys_state *css, struct cftype *cft,
 		break;
 	}
 out_unlock:
-	cpuset_unlock();
-	cpus_read_unlock();
+	cpus_read_cpuset_unlock();
 	return retval;
 }
 
@@ -454,8 +452,7 @@ static int cpuset_write_u64(struct cgroup_subsys_state *css, struct cftype *cft,
 	cpuset_filetype_t type = cft->private;
 	int retval = 0;
 
-	cpus_read_lock();
-	cpuset_lock();
+	cpus_read_cpuset_lock();
 	if (!is_cpuset_online(cs)) {
 		retval = -ENODEV;
 		goto out_unlock;
@@ -498,8 +495,7 @@ static int cpuset_write_u64(struct cgroup_subsys_state *css, struct cftype *cft,
 		break;
 	}
 out_unlock:
-	cpuset_unlock();
-	cpus_read_unlock();
+	cpus_read_cpuset_unlock();
 	return retval;
 }
 
diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index 3c5e44f824d1..9c0e8f297aaf 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -260,6 +260,18 @@ void cpuset_unlock(void)
 	mutex_unlock(&cpuset_mutex);
 }
 
+void cpus_read_cpuset_lock(void)
+{
+	cpus_read_lock();
+	mutex_lock(&cpuset_mutex);
+}
+
+void cpus_read_cpuset_unlock(void)
+{
+	mutex_unlock(&cpuset_mutex);
+	cpus_read_unlock();
+}
+
 static DEFINE_SPINLOCK(callback_lock);
 
 void cpuset_callback_lock_irq(void)
@@ -3233,8 +3245,7 @@ ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
 	int retval = -ENODEV;
 
 	buf = strstrip(buf);
-	cpus_read_lock();
-	mutex_lock(&cpuset_mutex);
+	cpus_read_cpuset_lock();
 	if (!is_cpuset_online(cs))
 		goto out_unlock;
 
@@ -3263,8 +3274,7 @@ ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
 	if (force_sd_rebuild)
 		rebuild_sched_domains_locked();
 out_unlock:
-	mutex_unlock(&cpuset_mutex);
-	cpus_read_unlock();
+	cpus_read_cpuset_unlock();
 	flush_workqueue(cpuset_migrate_mm_wq);
 	return retval ?: nbytes;
 }
@@ -3367,12 +3377,10 @@ static ssize_t cpuset_partition_write(struct kernfs_open_file *of, char *buf,
 	else
 		return -EINVAL;
 
-	cpus_read_lock();
-	mutex_lock(&cpuset_mutex);
+	cpus_read_cpuset_lock();
 	if (is_cpuset_online(cs))
 		retval = update_prstate(cs, val);
-	mutex_unlock(&cpuset_mutex);
-	cpus_read_unlock();
+	cpus_read_cpuset_unlock();
 	return retval ?: nbytes;
 }
 
@@ -3497,9 +3505,7 @@ static int cpuset_css_online(struct cgroup_subsys_state *css)
 	if (!parent)
 		return 0;
 
-	cpus_read_lock();
-	mutex_lock(&cpuset_mutex);
-
+	cpus_read_cpuset_lock();
 	if (is_spread_page(parent))
 		set_bit(CS_SPREAD_PAGE, &cs->flags);
 	if (is_spread_slab(parent))
@@ -3551,8 +3557,7 @@ static int cpuset_css_online(struct cgroup_subsys_state *css)
 	cpumask_copy(cs->effective_cpus, parent->cpus_allowed);
 	spin_unlock_irq(&callback_lock);
 out_unlock:
-	mutex_unlock(&cpuset_mutex);
-	cpus_read_unlock();
+	cpus_read_cpuset_unlock();
 	return 0;
 }
 
@@ -3567,16 +3572,12 @@ static void cpuset_css_offline(struct cgroup_subsys_state *css)
 {
 	struct cpuset *cs = css_cs(css);
 
-	cpus_read_lock();
-	mutex_lock(&cpuset_mutex);
-
+	cpus_read_cpuset_lock();
 	if (!cpuset_v2() && is_sched_load_balance(cs))
 		cpuset_update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
 
 	cpuset_dec();
-
-	mutex_unlock(&cpuset_mutex);
-	cpus_read_unlock();
+	cpus_read_cpuset_unlock();
 }
 
 /*
@@ -3588,16 +3589,11 @@ static void cpuset_css_killed(struct cgroup_subsys_state *css)
 {
 	struct cpuset *cs = css_cs(css);
 
-	cpus_read_lock();
-	mutex_lock(&cpuset_mutex);
-
+	cpus_read_cpuset_lock();
 	/* Reset valid partition back to member */
 	if (is_partition_valid(cs))
 		update_prstate(cs, PRS_MEMBER);
-
-	mutex_unlock(&cpuset_mutex);
-	cpus_read_unlock();
-
+	cpus_read_cpuset_unlock();
 }
 
 static void cpuset_css_free(struct cgroup_subsys_state *css)
-- 
2.34.1
Re: [-next v2 4/4] cpuset: add helpers for cpus read and cpuset_mutex locks
Posted by Waiman Long 4 months ago
On 8/13/25 4:29 AM, Chen Ridong wrote:
> From: Chen Ridong <chenridong@huawei.com>
>
> cpuset: add helpers for cpus_read_lock and cpuset_mutex
>
> Replace repetitive locking patterns with new helpers:
> - cpus_read_cpuset_lock()
> - cpus_read_cpuset_unlock()
>
> This makes the code cleaner and ensures consistent lock ordering.
>
> Signed-off-by: Chen Ridong <chenridong@huawei.com>
> ---
>   kernel/cgroup/cpuset-internal.h |  2 ++
>   kernel/cgroup/cpuset-v1.c       | 12 +++------
>   kernel/cgroup/cpuset.c          | 48 +++++++++++++++------------------
>   3 files changed, 28 insertions(+), 34 deletions(-)
>
> diff --git a/kernel/cgroup/cpuset-internal.h b/kernel/cgroup/cpuset-internal.h
> index 75b3aef39231..6fb00c96044d 100644
> --- a/kernel/cgroup/cpuset-internal.h
> +++ b/kernel/cgroup/cpuset-internal.h
> @@ -276,6 +276,8 @@ int cpuset_update_flag(cpuset_flagbits_t bit, struct cpuset *cs, int turning_on)
>   ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
>   				    char *buf, size_t nbytes, loff_t off);
>   int cpuset_common_seq_show(struct seq_file *sf, void *v);
> +void cpus_read_cpuset_lock(void);
> +void cpus_read_cpuset_unlock(void);

The names are not intuitive. I would prefer just extend the 
cpuset_lock/unlock to include cpus_read_lock/unlock and we use 
cpuset_lock/unlock consistently in the cpuset code. Also, there is now 
no external user of cpuset_lock/unlock, we may as well remove them from 
include/linux/cpuset.h.

Cheers,
Longman
Re: [-next v2 4/4] cpuset: add helpers for cpus read and cpuset_mutex locks
Posted by Chen Ridong 4 months ago

On 2025/8/14 4:09, Waiman Long wrote:
> On 8/13/25 4:29 AM, Chen Ridong wrote:
>> From: Chen Ridong <chenridong@huawei.com>
>>
>> cpuset: add helpers for cpus_read_lock and cpuset_mutex
>>
>> Replace repetitive locking patterns with new helpers:
>> - cpus_read_cpuset_lock()
>> - cpus_read_cpuset_unlock()
>>
>> This makes the code cleaner and ensures consistent lock ordering.
>>
>> Signed-off-by: Chen Ridong <chenridong@huawei.com>
>> ---
>>   kernel/cgroup/cpuset-internal.h |  2 ++
>>   kernel/cgroup/cpuset-v1.c       | 12 +++------
>>   kernel/cgroup/cpuset.c          | 48 +++++++++++++++------------------
>>   3 files changed, 28 insertions(+), 34 deletions(-)
>>
>> diff --git a/kernel/cgroup/cpuset-internal.h b/kernel/cgroup/cpuset-internal.h
>> index 75b3aef39231..6fb00c96044d 100644
>> --- a/kernel/cgroup/cpuset-internal.h
>> +++ b/kernel/cgroup/cpuset-internal.h
>> @@ -276,6 +276,8 @@ int cpuset_update_flag(cpuset_flagbits_t bit, struct cpuset *cs, int turning_on)
>>   ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
>>                       char *buf, size_t nbytes, loff_t off);
>>   int cpuset_common_seq_show(struct seq_file *sf, void *v);
>> +void cpus_read_cpuset_lock(void);
>> +void cpus_read_cpuset_unlock(void);
> 
> The names are not intuitive. I would prefer just extend the cpuset_lock/unlock to include
> cpus_read_lock/unlock and we use cpuset_lock/unlock consistently in the cpuset code. Also, there is
> now no external user of cpuset_lock/unlock, we may as well remove them from include/linux/cpuset.h.
> 
> Cheers,
> Longman

I like the idea and have considered it.
However, I noticed that cpuset_locked is being used in __sched_setscheduler.

-- 
Best regards,
Ridong

Re: [-next v2 4/4] cpuset: add helpers for cpus read and cpuset_mutex locks
Posted by Waiman Long 4 months ago
On 8/13/25 8:44 PM, Chen Ridong wrote:
>
> On 2025/8/14 4:09, Waiman Long wrote:
>> On 8/13/25 4:29 AM, Chen Ridong wrote:
>>> From: Chen Ridong <chenridong@huawei.com>
>>>
>>> cpuset: add helpers for cpus_read_lock and cpuset_mutex
>>>
>>> Replace repetitive locking patterns with new helpers:
>>> - cpus_read_cpuset_lock()
>>> - cpus_read_cpuset_unlock()
>>>
>>> This makes the code cleaner and ensures consistent lock ordering.
>>>
>>> Signed-off-by: Chen Ridong <chenridong@huawei.com>
>>> ---
>>>    kernel/cgroup/cpuset-internal.h |  2 ++
>>>    kernel/cgroup/cpuset-v1.c       | 12 +++------
>>>    kernel/cgroup/cpuset.c          | 48 +++++++++++++++------------------
>>>    3 files changed, 28 insertions(+), 34 deletions(-)
>>>
>>> diff --git a/kernel/cgroup/cpuset-internal.h b/kernel/cgroup/cpuset-internal.h
>>> index 75b3aef39231..6fb00c96044d 100644
>>> --- a/kernel/cgroup/cpuset-internal.h
>>> +++ b/kernel/cgroup/cpuset-internal.h
>>> @@ -276,6 +276,8 @@ int cpuset_update_flag(cpuset_flagbits_t bit, struct cpuset *cs, int turning_on)
>>>    ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
>>>                        char *buf, size_t nbytes, loff_t off);
>>>    int cpuset_common_seq_show(struct seq_file *sf, void *v);
>>> +void cpus_read_cpuset_lock(void);
>>> +void cpus_read_cpuset_unlock(void);
>> The names are not intuitive. I would prefer just extend the cpuset_lock/unlock to include
>> cpus_read_lock/unlock and we use cpuset_lock/unlock consistently in the cpuset code. Also, there is
>> now no external user of cpuset_lock/unlock, we may as well remove them from include/linux/cpuset.h.
>>
>> Cheers,
>> Longman
> I like the idea and have considered it.
> However, I noticed that cpuset_locked is being used in __sched_setscheduler.

Right, I overloooked the cpuset_lock() call in kernel/sched/syscall.c. 
So we can't remove it from include/linux/cpuset.h.

This call is invoked to ensure cpusets information is stable. However, 
it doesn't hurt if the cpus_read_lock() is also acquired as a result. 
Alternatively, we can use a name like cpuset_full_lock() to include 
cpus_read_lock().

Cheers,
Longman

Re: [-next v2 4/4] cpuset: add helpers for cpus read and cpuset_mutex locks
Posted by Waiman Long 4 months ago
On 8/13/25 11:13 PM, Waiman Long wrote:
> On 8/13/25 8:44 PM, Chen Ridong wrote:
>>
>> On 2025/8/14 4:09, Waiman Long wrote:
>>> On 8/13/25 4:29 AM, Chen Ridong wrote:
>>>> From: Chen Ridong <chenridong@huawei.com>
>>>>
>>>> cpuset: add helpers for cpus_read_lock and cpuset_mutex
>>>>
>>>> Replace repetitive locking patterns with new helpers:
>>>> - cpus_read_cpuset_lock()
>>>> - cpus_read_cpuset_unlock()
>>>>
>>>> This makes the code cleaner and ensures consistent lock ordering.
>>>>
>>>> Signed-off-by: Chen Ridong <chenridong@huawei.com>
>>>> ---
>>>>    kernel/cgroup/cpuset-internal.h |  2 ++
>>>>    kernel/cgroup/cpuset-v1.c       | 12 +++------
>>>>    kernel/cgroup/cpuset.c          | 48 
>>>> +++++++++++++++------------------
>>>>    3 files changed, 28 insertions(+), 34 deletions(-)
>>>>
>>>> diff --git a/kernel/cgroup/cpuset-internal.h 
>>>> b/kernel/cgroup/cpuset-internal.h
>>>> index 75b3aef39231..6fb00c96044d 100644
>>>> --- a/kernel/cgroup/cpuset-internal.h
>>>> +++ b/kernel/cgroup/cpuset-internal.h
>>>> @@ -276,6 +276,8 @@ int cpuset_update_flag(cpuset_flagbits_t bit, 
>>>> struct cpuset *cs, int turning_on)
>>>>    ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
>>>>                        char *buf, size_t nbytes, loff_t off);
>>>>    int cpuset_common_seq_show(struct seq_file *sf, void *v);
>>>> +void cpus_read_cpuset_lock(void);
>>>> +void cpus_read_cpuset_unlock(void);
>>> The names are not intuitive. I would prefer just extend the 
>>> cpuset_lock/unlock to include
>>> cpus_read_lock/unlock and we use cpuset_lock/unlock consistently in 
>>> the cpuset code. Also, there is
>>> now no external user of cpuset_lock/unlock, we may as well remove 
>>> them from include/linux/cpuset.h.
>>>
>>> Cheers,
>>> Longman
>> I like the idea and have considered it.
>> However, I noticed that cpuset_locked is being used in 
>> __sched_setscheduler.
>
> Right, I overloooked the cpuset_lock() call in kernel/sched/syscall.c. 
> So we can't remove it from include/linux/cpuset.h.
>
> This call is invoked to ensure cpusets information is stable. However, 
> it doesn't hurt if the cpus_read_lock() is also acquired as a result. 
> Alternatively, we can use a name like cpuset_full_lock() to include 
> cpus_read_lock().

I have a correction. According to commit d74b27d63a8b ("cgroup/cpuset: 
Change cpuset_rwsem and hotplug lock order") , sched_scheduler() can be 
called while holding cpus_hotplug_lock. So we should keep cpuset_lock() 
as it is.

Cheers,
Longman

Re: [-next v2 4/4] cpuset: add helpers for cpus read and cpuset_mutex locks
Posted by Chen Ridong 4 months ago

On 2025/8/14 11:27, Waiman Long wrote:
> On 8/13/25 11:13 PM, Waiman Long wrote:
>> On 8/13/25 8:44 PM, Chen Ridong wrote:
>>>
>>> On 2025/8/14 4:09, Waiman Long wrote:
>>>> On 8/13/25 4:29 AM, Chen Ridong wrote:
>>>>> From: Chen Ridong <chenridong@huawei.com>
>>>>>
>>>>> cpuset: add helpers for cpus_read_lock and cpuset_mutex
>>>>>
>>>>> Replace repetitive locking patterns with new helpers:
>>>>> - cpus_read_cpuset_lock()
>>>>> - cpus_read_cpuset_unlock()
>>>>>
>>>>> This makes the code cleaner and ensures consistent lock ordering.
>>>>>
>>>>> Signed-off-by: Chen Ridong <chenridong@huawei.com>
>>>>> ---
>>>>>    kernel/cgroup/cpuset-internal.h |  2 ++
>>>>>    kernel/cgroup/cpuset-v1.c       | 12 +++------
>>>>>    kernel/cgroup/cpuset.c          | 48 +++++++++++++++------------------
>>>>>    3 files changed, 28 insertions(+), 34 deletions(-)
>>>>>
>>>>> diff --git a/kernel/cgroup/cpuset-internal.h b/kernel/cgroup/cpuset-internal.h
>>>>> index 75b3aef39231..6fb00c96044d 100644
>>>>> --- a/kernel/cgroup/cpuset-internal.h
>>>>> +++ b/kernel/cgroup/cpuset-internal.h
>>>>> @@ -276,6 +276,8 @@ int cpuset_update_flag(cpuset_flagbits_t bit, struct cpuset *cs, int
>>>>> turning_on)
>>>>>    ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
>>>>>                        char *buf, size_t nbytes, loff_t off);
>>>>>    int cpuset_common_seq_show(struct seq_file *sf, void *v);
>>>>> +void cpus_read_cpuset_lock(void);
>>>>> +void cpus_read_cpuset_unlock(void);
>>>> The names are not intuitive. I would prefer just extend the cpuset_lock/unlock to include
>>>> cpus_read_lock/unlock and we use cpuset_lock/unlock consistently in the cpuset code. Also, there is
>>>> now no external user of cpuset_lock/unlock, we may as well remove them from include/linux/cpuset.h.
>>>>
>>>> Cheers,
>>>> Longman
>>> I like the idea and have considered it.
>>> However, I noticed that cpuset_locked is being used in __sched_setscheduler.
>>
>> Right, I overloooked the cpuset_lock() call in kernel/sched/syscall.c. So we can't remove it from
>> include/linux/cpuset.h.
>>
>> This call is invoked to ensure cpusets information is stable. However, it doesn't hurt if the
>> cpus_read_lock() is also acquired as a result. Alternatively, we can use a name like
>> cpuset_full_lock() to include cpus_read_lock().
> 
> I have a correction. According to commit d74b27d63a8b ("cgroup/cpuset: Change cpuset_rwsem and
> hotplug lock order") , sched_scheduler() can be called while holding cpus_hotplug_lock. So we should
> keep cpuset_lock() as it is.
> 
> Cheers,
> Longman

Thank you Longman, this is very helpful.

I had considered whether we can add cpus_read_lock() to the cpuset_lock, but based on your
explanation, I now understand this approach would not work.

For clarity, would it be acceptable to rename:
cpus_read_cpuset_lock() -> cpuset_full_lock()
cpus_read_cpuset_unlock() -> cpuset_full_unlock()

-- 
Best regards,
Ridong

Re: [-next v2 4/4] cpuset: add helpers for cpus read and cpuset_mutex locks
Posted by Waiman Long 4 months ago
On 8/13/25 11:58 PM, Chen Ridong wrote:
>
> On 2025/8/14 11:27, Waiman Long wrote:
>> On 8/13/25 11:13 PM, Waiman Long wrote:
>>> On 8/13/25 8:44 PM, Chen Ridong wrote:
>>>> On 2025/8/14 4:09, Waiman Long wrote:
>>>>> On 8/13/25 4:29 AM, Chen Ridong wrote:
>>>>>> From: Chen Ridong <chenridong@huawei.com>
>>>>>>
>>>>>> cpuset: add helpers for cpus_read_lock and cpuset_mutex
>>>>>>
>>>>>> Replace repetitive locking patterns with new helpers:
>>>>>> - cpus_read_cpuset_lock()
>>>>>> - cpus_read_cpuset_unlock()
>>>>>>
>>>>>> This makes the code cleaner and ensures consistent lock ordering.
>>>>>>
>>>>>> Signed-off-by: Chen Ridong <chenridong@huawei.com>
>>>>>> ---
>>>>>>     kernel/cgroup/cpuset-internal.h |  2 ++
>>>>>>     kernel/cgroup/cpuset-v1.c       | 12 +++------
>>>>>>     kernel/cgroup/cpuset.c          | 48 +++++++++++++++------------------
>>>>>>     3 files changed, 28 insertions(+), 34 deletions(-)
>>>>>>
>>>>>> diff --git a/kernel/cgroup/cpuset-internal.h b/kernel/cgroup/cpuset-internal.h
>>>>>> index 75b3aef39231..6fb00c96044d 100644
>>>>>> --- a/kernel/cgroup/cpuset-internal.h
>>>>>> +++ b/kernel/cgroup/cpuset-internal.h
>>>>>> @@ -276,6 +276,8 @@ int cpuset_update_flag(cpuset_flagbits_t bit, struct cpuset *cs, int
>>>>>> turning_on)
>>>>>>     ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
>>>>>>                         char *buf, size_t nbytes, loff_t off);
>>>>>>     int cpuset_common_seq_show(struct seq_file *sf, void *v);
>>>>>> +void cpus_read_cpuset_lock(void);
>>>>>> +void cpus_read_cpuset_unlock(void);
>>>>> The names are not intuitive. I would prefer just extend the cpuset_lock/unlock to include
>>>>> cpus_read_lock/unlock and we use cpuset_lock/unlock consistently in the cpuset code. Also, there is
>>>>> now no external user of cpuset_lock/unlock, we may as well remove them from include/linux/cpuset.h.
>>>>>
>>>>> Cheers,
>>>>> Longman
>>>> I like the idea and have considered it.
>>>> However, I noticed that cpuset_locked is being used in __sched_setscheduler.
>>> Right, I overloooked the cpuset_lock() call in kernel/sched/syscall.c. So we can't remove it from
>>> include/linux/cpuset.h.
>>>
>>> This call is invoked to ensure cpusets information is stable. However, it doesn't hurt if the
>>> cpus_read_lock() is also acquired as a result. Alternatively, we can use a name like
>>> cpuset_full_lock() to include cpus_read_lock().
>> I have a correction. According to commit d74b27d63a8b ("cgroup/cpuset: Change cpuset_rwsem and
>> hotplug lock order") , sched_scheduler() can be called while holding cpus_hotplug_lock. So we should
>> keep cpuset_lock() as it is.
>>
>> Cheers,
>> Longman
> Thank you Longman, this is very helpful.
>
> I had considered whether we can add cpus_read_lock() to the cpuset_lock, but based on your
> explanation, I now understand this approach would not work.
>
> For clarity, would it be acceptable to rename:
> cpus_read_cpuset_lock() -> cpuset_full_lock()
> cpus_read_cpuset_unlock() -> cpuset_full_unlock()

Yes, that is what I want to see. Note that taking both cpus_read_lock() 
and cpuset_mutex are needed to modify cpuset data. Taking just 
cpuset_mutex will prevent other from making changes to the cpuset data, 
but is not enough to make modification.

Cheers,
Longman

>

Re: [-next v2 4/4] cpuset: add helpers for cpus read and cpuset_mutex locks
Posted by Chen Ridong 4 months ago

On 2025/8/16 3:13, Waiman Long wrote:
> On 8/13/25 11:58 PM, Chen Ridong wrote:
>>
>> On 2025/8/14 11:27, Waiman Long wrote:
>>> On 8/13/25 11:13 PM, Waiman Long wrote:
>>>> On 8/13/25 8:44 PM, Chen Ridong wrote:
>>>>> On 2025/8/14 4:09, Waiman Long wrote:
>>>>>> On 8/13/25 4:29 AM, Chen Ridong wrote:
>>>>>>> From: Chen Ridong <chenridong@huawei.com>
>>>>>>>
>>>>>>> cpuset: add helpers for cpus_read_lock and cpuset_mutex
>>>>>>>
>>>>>>> Replace repetitive locking patterns with new helpers:
>>>>>>> - cpus_read_cpuset_lock()
>>>>>>> - cpus_read_cpuset_unlock()
>>>>>>>
>>>>>>> This makes the code cleaner and ensures consistent lock ordering.
>>>>>>>
>>>>>>> Signed-off-by: Chen Ridong <chenridong@huawei.com>
>>>>>>> ---
>>>>>>>     kernel/cgroup/cpuset-internal.h |  2 ++
>>>>>>>     kernel/cgroup/cpuset-v1.c       | 12 +++------
>>>>>>>     kernel/cgroup/cpuset.c          | 48 +++++++++++++++------------------
>>>>>>>     3 files changed, 28 insertions(+), 34 deletions(-)
>>>>>>>
>>>>>>> diff --git a/kernel/cgroup/cpuset-internal.h b/kernel/cgroup/cpuset-internal.h
>>>>>>> index 75b3aef39231..6fb00c96044d 100644
>>>>>>> --- a/kernel/cgroup/cpuset-internal.h
>>>>>>> +++ b/kernel/cgroup/cpuset-internal.h
>>>>>>> @@ -276,6 +276,8 @@ int cpuset_update_flag(cpuset_flagbits_t bit, struct cpuset *cs, int
>>>>>>> turning_on)
>>>>>>>     ssize_t cpuset_write_resmask(struct kernfs_open_file *of,
>>>>>>>                         char *buf, size_t nbytes, loff_t off);
>>>>>>>     int cpuset_common_seq_show(struct seq_file *sf, void *v);
>>>>>>> +void cpus_read_cpuset_lock(void);
>>>>>>> +void cpus_read_cpuset_unlock(void);
>>>>>> The names are not intuitive. I would prefer just extend the cpuset_lock/unlock to include
>>>>>> cpus_read_lock/unlock and we use cpuset_lock/unlock consistently in the cpuset code. Also,
>>>>>> there is
>>>>>> now no external user of cpuset_lock/unlock, we may as well remove them from
>>>>>> include/linux/cpuset.h.
>>>>>>
>>>>>> Cheers,
>>>>>> Longman
>>>>> I like the idea and have considered it.
>>>>> However, I noticed that cpuset_locked is being used in __sched_setscheduler.
>>>> Right, I overloooked the cpuset_lock() call in kernel/sched/syscall.c. So we can't remove it from
>>>> include/linux/cpuset.h.
>>>>
>>>> This call is invoked to ensure cpusets information is stable. However, it doesn't hurt if the
>>>> cpus_read_lock() is also acquired as a result. Alternatively, we can use a name like
>>>> cpuset_full_lock() to include cpus_read_lock().
>>> I have a correction. According to commit d74b27d63a8b ("cgroup/cpuset: Change cpuset_rwsem and
>>> hotplug lock order") , sched_scheduler() can be called while holding cpus_hotplug_lock. So we should
>>> keep cpuset_lock() as it is.
>>>
>>> Cheers,
>>> Longman
>> Thank you Longman, this is very helpful.
>>
>> I had considered whether we can add cpus_read_lock() to the cpuset_lock, but based on your
>> explanation, I now understand this approach would not work.
>>
>> For clarity, would it be acceptable to rename:
>> cpus_read_cpuset_lock() -> cpuset_full_lock()
>> cpus_read_cpuset_unlock() -> cpuset_full_unlock()
> 
> Yes, that is what I want to see. Note that taking both cpus_read_lock() and cpuset_mutex are needed
> to modify cpuset data. Taking just cpuset_mutex will prevent other from making changes to the cpuset
> data, but is not enough to make modification.
> 
> Cheers,
> Longman
> 
>>

See, I will add the comments to clarify how to use these helpers.

-- 
Best regards,
Ridong