[PATCH 11/33] cpuset: Provide lockdep check for cpuset lock held

Frederic Weisbecker posted 33 patches 5 months, 3 weeks ago
There is a newer version of this series
[PATCH 11/33] cpuset: Provide lockdep check for cpuset lock held
Posted by Frederic Weisbecker 5 months, 3 weeks ago
cpuset modifies partitions, including isolated, while holding the cpuset
mutex.

This means that holding the cpuset mutex is safe to synchronize against
housekeeping cpumask changes.

Provide a lockdep check to validate that.

Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
---
 include/linux/cpuset.h | 2 ++
 kernel/cgroup/cpuset.c | 7 +++++++
 2 files changed, 9 insertions(+)

diff --git a/include/linux/cpuset.h b/include/linux/cpuset.h
index 2ddb256187b5..051d36fec578 100644
--- a/include/linux/cpuset.h
+++ b/include/linux/cpuset.h
@@ -18,6 +18,8 @@
 #include <linux/mmu_context.h>
 #include <linux/jump_label.h>
 
+extern bool lockdep_is_cpuset_held(void);
+
 #ifdef CONFIG_CPUSETS
 
 /*
diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index 8595f1eadf23..aa1ac7bcf2ea 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -279,6 +279,13 @@ void cpuset_full_unlock(void)
 	cpus_read_unlock();
 }
 
+#ifdef CONFIG_LOCKDEP
+bool lockdep_is_cpuset_held(void)
+{
+	return lockdep_is_held(&cpuset_mutex);
+}
+#endif
+
 static DEFINE_SPINLOCK(callback_lock);
 
 void cpuset_callback_lock_irq(void)
-- 
2.51.0
Re: [PATCH 11/33] cpuset: Provide lockdep check for cpuset lock held
Posted by Chen Ridong 5 months, 3 weeks ago

On 2025/10/14 4:31, Frederic Weisbecker wrote:
> cpuset modifies partitions, including isolated, while holding the cpuset
> mutex.
> 
> This means that holding the cpuset mutex is safe to synchronize against
> housekeeping cpumask changes.
> 
> Provide a lockdep check to validate that.
> 
> Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
> ---
>  include/linux/cpuset.h | 2 ++
>  kernel/cgroup/cpuset.c | 7 +++++++
>  2 files changed, 9 insertions(+)
> 
> diff --git a/include/linux/cpuset.h b/include/linux/cpuset.h
> index 2ddb256187b5..051d36fec578 100644
> --- a/include/linux/cpuset.h
> +++ b/include/linux/cpuset.h
> @@ -18,6 +18,8 @@
>  #include <linux/mmu_context.h>
>  #include <linux/jump_label.h>
>  
> +extern bool lockdep_is_cpuset_held(void);
> +
>  #ifdef CONFIG_CPUSETS
>  
>  /*
> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> index 8595f1eadf23..aa1ac7bcf2ea 100644
> --- a/kernel/cgroup/cpuset.c
> +++ b/kernel/cgroup/cpuset.c
> @@ -279,6 +279,13 @@ void cpuset_full_unlock(void)
>  	cpus_read_unlock();
>  }
>  
> +#ifdef CONFIG_LOCKDEP
> +bool lockdep_is_cpuset_held(void)
> +{
> +	return lockdep_is_held(&cpuset_mutex);
> +}
> +#endif
> +
>  static DEFINE_SPINLOCK(callback_lock);
>  
>  void cpuset_callback_lock_irq(void)

Is the lockdep_is_cpuset_held function actually being used?
If CONFIG_LOCKDEP is disabled, compilation would fail with an "undefined reference to
lockdep_is_cpuset_held" error.

-- 
Best regards,
Ridong
Re: [PATCH 11/33] cpuset: Provide lockdep check for cpuset lock held
Posted by Frederic Weisbecker 5 months ago
Le Tue, Oct 14, 2025 at 09:29:25PM +0800, Chen Ridong a écrit :
> 
> 
> On 2025/10/14 4:31, Frederic Weisbecker wrote:
> > cpuset modifies partitions, including isolated, while holding the cpuset
> > mutex.
> > 
> > This means that holding the cpuset mutex is safe to synchronize against
> > housekeeping cpumask changes.
> > 
> > Provide a lockdep check to validate that.
> > 
> > Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
> > ---
> >  include/linux/cpuset.h | 2 ++
> >  kernel/cgroup/cpuset.c | 7 +++++++
> >  2 files changed, 9 insertions(+)
> > 
> > diff --git a/include/linux/cpuset.h b/include/linux/cpuset.h
> > index 2ddb256187b5..051d36fec578 100644
> > --- a/include/linux/cpuset.h
> > +++ b/include/linux/cpuset.h
> > @@ -18,6 +18,8 @@
> >  #include <linux/mmu_context.h>
> >  #include <linux/jump_label.h>
> >  
> > +extern bool lockdep_is_cpuset_held(void);
> > +
> >  #ifdef CONFIG_CPUSETS
> >  
> >  /*
> > diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> > index 8595f1eadf23..aa1ac7bcf2ea 100644
> > --- a/kernel/cgroup/cpuset.c
> > +++ b/kernel/cgroup/cpuset.c
> > @@ -279,6 +279,13 @@ void cpuset_full_unlock(void)
> >  	cpus_read_unlock();
> >  }
> >  
> > +#ifdef CONFIG_LOCKDEP
> > +bool lockdep_is_cpuset_held(void)
> > +{
> > +	return lockdep_is_held(&cpuset_mutex);
> > +}
> > +#endif
> > +
> >  static DEFINE_SPINLOCK(callback_lock);
> >  
> >  void cpuset_callback_lock_irq(void)
> 
> Is the lockdep_is_cpuset_held function actually being used?
> If CONFIG_LOCKDEP is disabled, compilation would fail with an "undefined reference to
> lockdep_is_cpuset_held" error.

Although counter-intuitive, this is how the lockdep_is_held() functions family
do work.

This allows this kind of trick:

if (IS_ENABLED(CONFIG_LOCKDEP))
   WARN_ON_ONCE(!lockdep_is_held(&some_lock))

This works during the compilation because the prototype of lockdep_is_held()
is declared. And since the IS_ENABLED() is resolved during compilation as well,
the conditional code is wiped out and therefore not linked. As a result the
linker doesn't even look for the definition of lockdep_is_held() and we don't
need to define an off case that would return a wrong assumption.

Thanks.

-- 
Frederic Weisbecker
SUSE Labs
Re: [PATCH 11/33] cpuset: Provide lockdep check for cpuset lock held
Posted by Chen Ridong 5 months ago

On 2025/11/1 0:08, Frederic Weisbecker wrote:
> Le Tue, Oct 14, 2025 at 09:29:25PM +0800, Chen Ridong a écrit :
>>
>>
>> On 2025/10/14 4:31, Frederic Weisbecker wrote:
>>> cpuset modifies partitions, including isolated, while holding the cpuset
>>> mutex.
>>>
>>> This means that holding the cpuset mutex is safe to synchronize against
>>> housekeeping cpumask changes.
>>>
>>> Provide a lockdep check to validate that.
>>>
>>> Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
>>> ---
>>>  include/linux/cpuset.h | 2 ++
>>>  kernel/cgroup/cpuset.c | 7 +++++++
>>>  2 files changed, 9 insertions(+)
>>>
>>> diff --git a/include/linux/cpuset.h b/include/linux/cpuset.h
>>> index 2ddb256187b5..051d36fec578 100644
>>> --- a/include/linux/cpuset.h
>>> +++ b/include/linux/cpuset.h
>>> @@ -18,6 +18,8 @@
>>>  #include <linux/mmu_context.h>
>>>  #include <linux/jump_label.h>
>>>  
>>> +extern bool lockdep_is_cpuset_held(void);
>>> +
>>>  #ifdef CONFIG_CPUSETS
>>>  
>>>  /*
>>> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
>>> index 8595f1eadf23..aa1ac7bcf2ea 100644
>>> --- a/kernel/cgroup/cpuset.c
>>> +++ b/kernel/cgroup/cpuset.c
>>> @@ -279,6 +279,13 @@ void cpuset_full_unlock(void)
>>>  	cpus_read_unlock();
>>>  }
>>>  
>>> +#ifdef CONFIG_LOCKDEP
>>> +bool lockdep_is_cpuset_held(void)
>>> +{
>>> +	return lockdep_is_held(&cpuset_mutex);
>>> +}
>>> +#endif
>>> +
>>>  static DEFINE_SPINLOCK(callback_lock);
>>>  
>>>  void cpuset_callback_lock_irq(void)
>>
>> Is the lockdep_is_cpuset_held function actually being used?
>> If CONFIG_LOCKDEP is disabled, compilation would fail with an "undefined reference to
>> lockdep_is_cpuset_held" error.
> 
> Although counter-intuitive, this is how the lockdep_is_held() functions family
> do work.
> 
> This allows this kind of trick:
> 
> if (IS_ENABLED(CONFIG_LOCKDEP))
>    WARN_ON_ONCE(!lockdep_is_held(&some_lock))
> 
> This works during the compilation because the prototype of lockdep_is_held()
> is declared. And since the IS_ENABLED() is resolved during compilation as well,
> the conditional code is wiped out and therefore not linked. As a result the
> linker doesn't even look for the definition of lockdep_is_held() and we don't
> need to define an off case that would return a wrong assumption.
> 
> Thanks.
> 

Thank you for your explanation

-- 
Best regards,
Ridong