Skip to contentSkip to conversation

sched/topology: Add a cpus_read_lock to partition_sched_domains()

PATCH Superseded Sebastian Andrzej Siewior 4 participants 6 messages Aug 12 – Aug 13 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/topology: Add a cpus_read_lock to partition_sched_domains()

A read from /proc/sys/kernel/sched_rt_runtime_us leads to backtrace due to missing cpu_hotplug_lock. The callchain is sched_rt_handler() -> partition_sched_domains() -> sched_cache_set() -> static_key_enable_cpuslocked(&sched_cache_present).

sched_cache_set() itself is also invoked from sched_init_domains() which is early during the boot, holding just the sched_domains_mutex_lock(). Here is no warning because it happens before user space is running (and hotplug operations are not possible).

There is also sched_cache_active_set() which acquires the hotplug lock via before invoking any of the _cpuslocked() functions.

Acquire CPU hotplug lock before in partition_sched_domains(), before sched_domains_mutex before the _cpuslocked() functions are invoked.

Fixes a7660ce1590fc ("sched/cache: Fix has_multi_llcs iff at least one partition has multiple LLCs")
---
 kernel/sched/topology.c | 1 +
 1 file changed, 1 insertion(+)
kernel/sched/topology.c +1 -0
index 622e2e01974c4..ef037d6664d98 100644
@@ -3498,6 +3498,7 @@ static void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new
 void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
 			     struct sched_domain_attr *dattr_new)
 {
+	guard(cpus_read_lock)();
 	sched_domains_mutex_lock();
 	partition_sched_domains_locked(ndoms_new, doms_new, dattr_new);
 	sched_domains_mutex_unlock();
Signature · 1 line
2.55.0
#1 lore raw

Hi Sebastian,

Quoted 27 lines · Sebastian Andrzej Siewior

A read from /proc/sys/kernel/sched_rt_runtime_us leads to backtrace due to missing cpu_hotplug_lock. The callchain is sched_rt_handler() -> partition_sched_domains() -> sched_cache_set() -> static_key_enable_cpuslocked(&sched_cache_present).

sched_cache_set() itself is also invoked from sched_init_domains() which is early during the boot, holding just the sched_domains_mutex_lock(). Here is no warning because it happens before user space is running (and hotplug operations are not possible).

There is also sched_cache_active_set() which acquires the hotplug lock via before invoking any of the _cpuslocked() functions.

Acquire CPU hotplug lock before in partition_sched_domains(), before sched_domains_mutex before the _cpuslocked() functions are invoked.

Fixes a7660ce1590fc ("sched/cache: Fix has_multi_llcs iff at least one partition has multiple LLCs")
---
  kernel/sched/topology.c | 1 +
  1 file changed, 1 insertion(+)
kernel/sched/topology.c +0 -0
index 622e2e01974c4..ef037d6664d98 100644

kernel/sched/topology.c static void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new · line 3504

Quoted 7 lines
kernel/sched/topology.c +1 -0
@@ -3498,6 +3498,7 @@ static void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new
  void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
  			     struct sched_domain_attr *dattr_new)
  {
+	guard(cpus_read_lock)();
  	sched_domains_mutex_lock();
  	partition_sched_domains_locked(ndoms_new, doms_new, dattr_new);
  	sched_domains_mutex_unlock();

Thanks for taking a look at this issue. I found that there is a comment around partition_sched_domains() says: "Call with hotplug lock held"
Not sure if the caller of partition_sched_domains() rather than partition_sched_domains() should grab the cpuhotplug lock? I guess the issue is triggered when CONFIG_CPUSETS=n, in this case rebuild_sched_domains() will not grab cpuhotplug lock, should we add guard(cpus_read_lock) in rebuild_sched_domains() instead?

thanks,
Chenyu

#2 lore raw
Quoted 40 lines · Yu C

Hi Sebastian,

Quoted 27 lines · Sebastian Andrzej Siewior

A read from /proc/sys/kernel/sched_rt_runtime_us leads to backtrace due to missing cpu_hotplug_lock. The callchain is sched_rt_handler() -> partition_sched_domains() -> sched_cache_set() -> static_key_enable_cpuslocked(&sched_cache_present).

sched_cache_set() itself is also invoked from sched_init_domains() which is early during the boot, holding just the sched_domains_mutex_lock(). Here is no warning because it happens before user space is running (and hotplug operations are not possible).

There is also sched_cache_active_set() which acquires the hotplug lock via before invoking any of the _cpuslocked() functions.

Acquire CPU hotplug lock before in partition_sched_domains(), before sched_domains_mutex before the _cpuslocked() functions are invoked.

Fixes a7660ce1590fc ("sched/cache: Fix has_multi_llcs iff at least one partition has multiple LLCs")
---
  kernel/sched/topology.c | 1 +
  1 file changed, 1 insertion(+)
kernel/sched/topology.c +0 -0
index 622e2e01974c4..ef037d6664d98 100644

kernel/sched/topology.c static void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new · line 3504

Quoted 7 lines
kernel/sched/topology.c +1 -0
@@ -3498,6 +3498,7 @@ static void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new
  void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
  			     struct sched_domain_attr *dattr_new)
  {
+	guard(cpus_read_lock)();
  	sched_domains_mutex_lock();
  	partition_sched_domains_locked(ndoms_new, doms_new, dattr_new);
  	sched_domains_mutex_unlock();

Thanks for taking a look at this issue. I found that there is a comment around

Quoted 8 lines

partition_sched_domains() says: "Call with hotplug lock held" Not sure if the caller of partition_sched_domains() rather than partition_sched_domains()
should grab the cpuhotplug lock? I guess the issue is triggered when CONFIG_CPUSETS=n, in this case rebuild_sched_domains() will not grab cpuhotplug lock, should we add guard(cpus_read_lock) in rebuild_sched_domains()
instead?

When CONFIG_CPUSETS=n, rebuild_sched_domains() is stubbed to

static inline void rebuild_sched_domains(void)

{
        partition_sched_domains(1, NULL, NULL);
}

without cpus_read_lock. Are you suggesting adding the lock here? We do acquire the lock for CONFIG_CPUSETS=y.

If we do follow this convention, cpuset_reset_sched_domains() and cpuset_reset_sched_domains() also stubbed to partition_sched_domains(1, NULL, NULL);
Probably should add cpus_read_lock there too for consistency.

Tim

Quoted 4 lines

thanks,
Chenyu

#3 lore raw
Quoted 21 lines · Tim Chen
Quoted 10 lines

kernel/sched/topology.c static void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new · line 3504

Quoted 7 lines

void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
struct sched_domain_attr *dattr_new)
{

kernel/sched/topology.c +1 -0
+	guard(cpus_read_lock)();
  	sched_domains_mutex_lock();
  	partition_sched_domains_locked(ndoms_new, doms_new, dattr_new);
  	sched_domains_mutex_unlock();

Thanks for taking a look at this issue. I found that there is a comment around

Quoted 8 lines

partition_sched_domains() says: "Call with hotplug lock held" Not sure if the caller of partition_sched_domains() rather than partition_sched_domains()
should grab the cpuhotplug lock? I guess the issue is triggered when CONFIG_CPUSETS=n, in this case rebuild_sched_domains() will not grab cpuhotplug lock, should we add guard(cpus_read_lock) in rebuild_sched_domains()
instead?

When CONFIG_CPUSETS=n, rebuild_sched_domains() is stubbed to

Quoted 8 lines

static inline void rebuild_sched_domains(void)

{
        partition_sched_domains(1, NULL, NULL);
}

without cpus_read_lock. Are you suggesting adding the lock here? We do acquire the lock for CONFIG_CPUSETS=y.

Yes, I think so.

Quoted 5 lines

If we do follow this convention, cpuset_reset_sched_domains() and cpuset_reset_sched_domains() also stubbed to partition_sched_domains(1, NULL, NULL);
Probably should add cpus_read_lock there too for consistency.

cpuset_reset_sched_domains() is invoked by cpuset_cpu_inactive() and via CPU-hotplug callback, and in cpuhp_thread_fun:
static void cpuhp_thread_fun(unsigned int cpu)

{
	lockdep_acquire_cpus_lock(); <--- lockdep
	call_back();
}

and according to the comments:
"
/*
* The BP holds the hotplug lock, but we're now running on the AP,
* ensure that anybody asserting the lock is held, will actually find
* it so.
*/
"
it pretend that AP has grabed the lock on behalf of the BP in _cpu_down(), which has grabed the cpu-hotplug write lock:
cpus_write_lock().

So it should be ok to not take hotplug lock in cpuset_reset_sched_domains().

thanks,
Chenyu

Quoted 6 lines

Tim

Quoted 4 lines

thanks,
Chenyu

#4 lore raw
Quoted 58 lines · Chen Yu
Quoted 21 lines · Tim Chen
Quoted 10 lines

kernel/sched/topology.c static void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new · line 3504

Quoted 7 lines

void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
struct sched_domain_attr *dattr_new)
{

kernel/sched/topology.c +1 -0
+	guard(cpus_read_lock)();
  	sched_domains_mutex_lock();
  	partition_sched_domains_locked(ndoms_new, doms_new, dattr_new);
  	sched_domains_mutex_unlock();

Thanks for taking a look at this issue. I found that there is a comment around

Quoted 8 lines

partition_sched_domains() says: "Call with hotplug lock held" Not sure if the caller of partition_sched_domains() rather than partition_sched_domains()
should grab the cpuhotplug lock? I guess the issue is triggered when CONFIG_CPUSETS=n, in this case rebuild_sched_domains() will not grab cpuhotplug lock, should we add guard(cpus_read_lock) in rebuild_sched_domains()
instead?

When CONFIG_CPUSETS=n, rebuild_sched_domains() is stubbed to

Quoted 8 lines

static inline void rebuild_sched_domains(void)

{
        partition_sched_domains(1, NULL, NULL);
}

without cpus_read_lock. Are you suggesting adding the lock here? We do acquire the lock for CONFIG_CPUSETS=y.

Yes, I think so.

Quoted 5 lines

If we do follow this convention, cpuset_reset_sched_domains() and cpuset_reset_sched_domains() also stubbed to partition_sched_domains(1, NULL, NULL);
Probably should add cpus_read_lock there too for consistency.

cpuset_reset_sched_domains() is invoked by cpuset_cpu_inactive() and via CPU-hotplug callback, and in cpuhp_thread_fun:
static void cpuhp_thread_fun(unsigned int cpu)

{
	lockdep_acquire_cpus_lock(); <--- lockdep
	call_back();
}

and according to the comments:
"
/*
* The BP holds the hotplug lock, but we're now running on the AP,
* ensure that anybody asserting the lock is held, will actually find
* it so.
*/
"
it pretend that AP has grabed the lock on behalf of the BP in _cpu_down(), which has grabed the cpu-hotplug write lock:
cpus_write_lock().

Quoted 1 line

So it should be ok to not take hotplug lock in cpuset_reset_sched_domains().

Yes, likewise cpuset_update_active_cpus() that uses partition_sched_domains(1, NULL, NULL) is called from cpu-hotplug path with proper lock, we are good there too.

Tim

#5 lore raw
In reply to Chen, Yu C
Quoted 1 line · Yu C

Hi Sebastian,

Hi,

Quoted 9 lines
Quoted 9 lines
kernel/sched/topology.c +1 -0
@@ -3498,6 +3498,7 @@ static void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new
  void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
  			     struct sched_domain_attr *dattr_new)
  {
+	guard(cpus_read_lock)();
  	sched_domains_mutex_lock();
  	partition_sched_domains_locked(ndoms_new, doms_new, dattr_new);
Quoted 8 lines

kernel/sched/topology.c static void partition_sched_domains_locked(int ndoms_new, cpumask_var_t doms_new · line 3504

Quoted 1 line
kernel/sched/topology.c +0 -0
  	sched_domains_mutex_unlock();

Thanks for taking a look at this issue. I found that there is a comment around
partition_sched_domains() says: "Call with hotplug lock held" Not sure if the caller of partition_sched_domains() rather than partition_sched_domains()
should grab the cpuhotplug lock? I guess the issue is triggered when

How did I miss that part?

Quoted 4 lines

CONFIG_CPUSETS=n, in this case rebuild_sched_domains() will not grab cpuhotplug lock, should we add guard(cpus_read_lock) in rebuild_sched_domains()
instead?

Let me look…

Quoted 2 lines

thanks,
Chenyu

Sebastian