From: Waiman Long <longman@redhat.com>
To: 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>,
Daniel Bristot de Oliveira <bristot@redhat.com>,
Valentin Schneider <vschneid@redhat.com>
Cc: linux-kernel@vger.kernel.org, Phil Auld <pauld@redhat.com>,
kernel test robot <oliver.sang@intel.com>,
aubrey.li@linux.intel.com, yu.c.chen@intel.com,
Waiman Long <longman@redhat.com>
Subject: [PATCH] sched: Don't call any kfree*() API in do_set_cpus_allowed()
Date: Mon, 30 Oct 2023 20:14:18 -0400 [thread overview]
Message-ID: <20231031001418.274187-1-longman@redhat.com> (raw)
Commit 851a723e45d1 ("sched: Always clear user_cpus_ptr in
do_set_cpus_allowed()") added a kfree() call to free any user
provided affinity mask, if present. It was changed later to use
kfree_rcu() in commit 9a5418bc48ba ("sched/core: Use kfree_rcu()
in do_set_cpus_allowed()") to avoid a circular locking dependency
problem.
It turns out that even kfree_rcu() isn't safe for avoiding
circular locking problem. As reported by kernel test robot,
the following circular locking dependency still exists:
&rdp->nocb_lock --> rcu_node_0 --> &rq->__lock
So no kfree*() API can be used in do_set_cpus_allowed(). To prevent
memory leakage, the unused user provided affinity mask is now saved in a
lockless list to be reused later by subsequent sched_setaffinity() calls.
Without kfree_rcu(), the internal cpumask_rcuhead union can be removed
too as a lockless list entry only holds a single pointer.
Fixes: 851a723e45d1 ("sched: Always clear user_cpus_ptr in do_set_cpus_allowed()")
Reported-by: kernel test robot <oliver.sang@intel.com>
Closes: https://lore.kernel.org/oe-lkp/202310302207.a25f1a30-oliver.sang@intel.com
Signed-off-by: Waiman Long <longman@redhat.com>
---
kernel/sched/core.c | 31 ++++++++++++++++++-------------
1 file changed, 18 insertions(+), 13 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 802551e0009b..f536d11a284e 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2789,6 +2789,11 @@ __do_set_cpus_allowed(struct task_struct *p, struct affinity_context *ctx)
set_next_task(rq, p);
}
+/*
+ * A lockless list of free cpumasks to be used for user cpumasks.
+ */
+static LLIST_HEAD(cpumask_free_lhead);
+
/*
* Used for kthread_bind() and select_fallback_rq(), in both cases the user
* affinity (if any) should be destroyed too.
@@ -2800,29 +2805,29 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
.user_mask = NULL,
.flags = SCA_USER, /* clear the user requested mask */
};
- union cpumask_rcuhead {
- cpumask_t cpumask;
- struct rcu_head rcu;
- };
__do_set_cpus_allowed(p, &ac);
/*
- * Because this is called with p->pi_lock held, it is not possible
- * to use kfree() here (when PREEMPT_RT=y), therefore punt to using
- * kfree_rcu().
+ * We can't call any kfree*() API here as p->pi_lock and/or rq lock
+ * may be held. So we save it in a llist to be reused in the next
+ * sched_setaffinity() call.
*/
- kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);
+ if (ac.user_mask)
+ llist_add((struct llist_node *)ac.user_mask, &cpumask_free_lhead);
}
static cpumask_t *alloc_user_cpus_ptr(int node)
{
- /*
- * See do_set_cpus_allowed() above for the rcu_head usage.
- */
- int size = max_t(int, cpumask_size(), sizeof(struct rcu_head));
+ struct cpumask *pmask = NULL;
+
+ if (!llist_empty(&cpumask_free_lhead))
+ pmask = (struct cpumask *)llist_del_first(&cpumask_free_lhead);
+
+ if (!pmask)
+ pmask = kmalloc_node(cpumask_size(), GFP_KERNEL, node);
- return kmalloc_node(size, GFP_KERNEL, node);
+ return pmask;
}
int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,
--
2.39.3
next reply other threads:[~2023-10-31 0:15 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-31 0:14 Waiman Long [this message]
2023-10-31 8:53 ` [PATCH] sched: Don't call any kfree*() API in do_set_cpus_allowed() Peter Zijlstra
2023-10-31 10:52 ` Frederic Weisbecker
2023-10-31 12:18 ` Peter Zijlstra
2023-10-31 14:29 ` Paul E. McKenney
2023-10-31 20:02 ` [PATCH] rcu: Break rcu_node_0 --> &rq->__lock order Peter Zijlstra
2023-10-31 21:49 ` Paul E. McKenney
2023-11-01 0:07 ` Waiman Long
2023-11-01 11:21 ` Z qiang
2023-11-01 14:38 ` Peter Zijlstra
2023-11-01 16:52 ` Paul E. McKenney
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=20231031001418.274187-1-longman@redhat.com \
--to=longman@redhat.com \
--cc=aubrey.li@linux.intel.com \
--cc=bristot@redhat.com \
--cc=bsegall@google.com \
--cc=dietmar.eggemann@arm.com \
--cc=juri.lelli@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=oliver.sang@intel.com \
--cc=pauld@redhat.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=yu.c.chen@intel.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