mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ridong Chen <ridong.chen@linux.dev>
To: Waiman Long <longman@redhat.com>, Ingo Molnar <mingo@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	Tejun Heo <tj@kernel.org>,
	Frederic Weisbecker <frederic@kernel.org>
Cc: linux-kernel@vger.kernel.org,
	Guopeng Zhang <guopeng.zhang@linux.dev>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Subject: Re: [PATCH v2] sched/isolation: Enable cpuset_mutex/cpus_read_lock synchronization before freeing old housekeeping cpumask
Date: Tue, 17 Nov 2026 16:34:34 +0800	[thread overview]
Message-ID: <054a53a3-c038-48fe-ac01-fc24fd8065d0@linux.dev> (raw)
In-Reply-To: <20261117051853.2429410-1-longman@redhat.com>



On 11/17/2026 1:18 PM, Waiman Long wrote:
> Commit a84097e625f2 ("cgroup/cpuset: Call housekeeping_update() without
> holding cpus_read_lock") adds a new cpuset_top_mutex on top but drops
> cpus_read_lock and cpuset_mutex before calling housekeeping_update()
> which calls other functions that may acquire cpus_read_lock leading
> to a possibility of deadlock if cpus_read_lock isn't dropped before
> calling housekeeping_update().
> 
> This commit also changes lockdep_is_cpuset_held() to return true if
> either cpuset_mutex or cpuset_top_mutex is held. lockdep_is_cpuset_held()
> is used by housekeeping_dereference_check() as one of the conditions
> that rcu_read_lock() is not needed to access HK_TYPE_DOMAIN
> cpumask which can be changed by housekeeping_update() at run
> time. The housekeeping_dereference_check() function also uses
> lockdep_is_cpus_write_held() as another condition that can skip the
> use of rcu_read_lock().
> 
> In reality, both cpuset_mutex and cpus_write_lock are now no longer
> valid conditions to skip rcu_read_lock(). It is certainly possible
> that there can be a parallel housekeeping_update() call going on if
> only cpuset_mutex or cpus_write_lock is held. This can lead to a
> potential use-after-free situation when HK_TYPE_DOMAIN housekeeping
> cpumask is being accessed.
> 
> One possible solution is to hold rcu_read_lock for functions that may
> be called with only cpuset_mutex or cpus_write_lock is held. Quite a
> large number of functions will need to be modified if rcu_read_lock()
> has to be added. A far simpler solution is to synchronize with other
> cpuset_mutex and cpus_write_lock critical sections before freeing the old
> housekeeping cpumask. This will ensure that code within those critical
> sections will not have a UaF situation when accessing housekeeping
> cpumask. This commit implements the latter solution and make the code
> in lockdep_is_cpuset_held() and housekeeping_dereference_check() correct.
> 
> Fixes: a84097e625f2 ("cgroup/cpuset: Call housekeeping_update() without holding cpus_read_lock")
> Signed-off-by: Waiman Long <longman@redhat.com>
> ---
>   kernel/sched/isolation.c | 10 ++++++++++
>   1 file changed, 10 insertions(+)
> 
> diff --git a/kernel/sched/isolation.c b/kernel/sched/isolation.c
> index 156025ef81b7..5634d6a597bd 100644
> --- a/kernel/sched/isolation.c
> +++ b/kernel/sched/isolation.c
> @@ -159,6 +159,16 @@ int housekeeping_update(struct cpumask *isol_mask)
>   	err = kthreads_update_housekeeping();
>   	WARN_ON_ONCE(err < 0);
>   
> +	/*
> +	 * Synchronize with cpuset_mutex and cpus_read_lock to make sure that
> +	 * cpuset_mutex and cpus_write_lock critical sections afterward will
> +	 * not have access to the old housekeeping cpumask.
> +	 */
> +	cpus_read_lock();
> +	cpuset_lock();
> +	cpuset_unlock();
> +	cpus_read_unlock();
> +
>   	kfree(old);
>   
>   	return 0;

Hi Waiman,

Taking the locks and releasing them right away looks a bit odd to me.
It is not obvious what the empty critical section protects until one
reads the commit message.

Let me check that I understand the problem correctly.
housekeeping_dereference_check() lets readers holding cpus_write_lock
or cpuset_mutex access HK_TYPE_DOMAIN without rcu_read_lock(). After
commit a84097e625f2, the pointer is switched and the old cpumask is
freed after cpuset_mutex and cpus_read_lock have been dropped, and
synchronize_rcu() does not wait for those readers. For example:

   CPU A                                  CPU B
   rebuild_sched_domains()                cpuset_update_sd_hk_unlock()
                                            mutex_unlock(&cpuset_mutex)
                                            cpus_read_unlock()
     mutex_lock(&cpuset_mutex)
     p = housekeeping_cpumask(DOMAIN)
                                            housekeeping_update()
                                              rcu_assign_pointer()
                                              synchronize_rcu()
                                              kfree(old)
     access p <- UAF

Here CPU A reads the mask in asym_cpu_capacity_scan() or
dl_get_task_effective_cpus().

If that is right, the root cause is that the pointer is now switched
outside of those locks. So instead of adding a lock barrier before
kfree(), how about switching the pointer before the locks are
dropped? housekeeping_update() could be split into two parts:

  - housekeeping_publish(): allocate, check and publish the new mask,
    called with cpus_read_lock and cpuset_mutex held.

  - housekeeping_update(): synchronize_rcu(), the workqueue flushes and
    kfree(old), called after the locks are dropped as it is now.

Then a reader holding cpuset_mutex either finishes before the pointer
is switched, or only sees the new mask:

   CPU A                                  CPU B
                                          cpuset_update_sd_hk_unlock()
                                            housekeeping_publish()
                                              rcu_assign_pointer()
     mutex_lock(&cpuset_mutex) <- wait
                                            mutex_unlock(&cpuset_mutex)
                                            cpus_read_unlock()
     p = housekeeping_cpumask(DOMAIN)       housekeeping_update()
         <- new cpumask                       synchronize_rcu()
                                              kfree(old)

The same applies to cpus_write_lock readers, as cpus_read_lock is held
until the new mask is published. The workqueue flushes still run
without those locks, so the deadlock fixed by a84097e625f2 does not come
back.

I will send a patch in reply to this mail. Could you take a look and
tell me whether this approach works for you?

-- 
Best regards
Ridong


  parent reply	other threads:[~2026-11-17  8:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  5:18 Waiman Long
2026-11-17  6:43 ` Guopeng Zhang
2026-11-17  8:34 ` Ridong Chen [this message]
2026-11-17  8:39   ` [PATCH] sched/isolation: Publish new HK_TYPE_DOMAIN cpumask under cpuset_mutex Ridong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=054a53a3-c038-48fe-ac01-fc24fd8065d0@linux.dev \
    --to=ridong.chen@linux.dev \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=frederic@kernel.org \
    --cc=guopeng.zhang@linux.dev \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=tj@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®