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>
Cc: Phil Auld <pauld@redhat.com>,
linux-kernel@vger.kernel.org, Waiman Long <longman@redhat.com>
Subject: [PATCH-tip] sched: Don't call kfree() in do_set_cpus_allowed()
Date: Fri, 18 Nov 2022 14:33:02 -0500 [thread overview]
Message-ID: <20221118193302.522399-1-longman@redhat.com> (raw)
Commit 851a723e45d1 ("sched: Always clear user_cpus_ptr in
do_set_cpus_allowed()") may call kfree() if user_cpus_ptr was previously
set. Unfortunately, some of the callers of do_set_cpus_allowed()
may not be in a context where kfree() can be safely called. So the
following splats may be printed:
WARNING: possible circular locking dependency detected
BUG: sleeping function called from invalid context
To avoid these problems without leaking memory, the free cpumask is now
put into a lockless list to be reused in a later sched_setaffinity()
call instead.
Fixes: 851a723e45d1 ("sched: Always clear user_cpus_ptr in do_set_cpus_allowed()")
Signed-off-by: Waiman Long <longman@redhat.com>
---
kernel/sched/core.c | 26 +++++++++++++++++++++++---
1 file changed, 23 insertions(+), 3 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 78b2d5cabcc5..8df51b08bb38 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2527,6 +2527,11 @@ int push_cpu_stop(void *arg)
return 0;
}
+/*
+ * A lockless list of user cpumask available to be reused.
+ */
+static LLIST_HEAD(free_cpumasks);
+
/*
* sched_class::set_cpus_allowed must do the below, but is not required to
* actually call this function.
@@ -2606,7 +2611,14 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
};
__do_set_cpus_allowed(p, &ac);
- kfree(ac.user_mask);
+ if (ac.user_mask) {
+ /*
+ * We may not be in a context where kfree() can be called.
+ * Put the free user_mask in free_cpumasks to be freed or
+ * used later.
+ */
+ llist_add((struct llist_node *)ac.user_mask, &free_cpumasks);
+ }
}
int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,
@@ -8194,7 +8206,7 @@ __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx)
long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
{
struct affinity_context ac;
- struct cpumask *user_mask;
+ struct cpumask *user_mask = NULL;
struct task_struct *p;
int retval;
@@ -8229,7 +8241,15 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
if (retval)
goto out_put_task;
- user_mask = kmalloc(cpumask_size(), GFP_KERNEL);
+ /*
+ * Use the element in the free_cpumasks, if available.
+ */
+ if (!llist_empty(&free_cpumasks))
+ user_mask = (struct cpumask *)llist_del_first(&free_cpumasks);
+
+ if (!user_mask)
+ user_mask = kmalloc(cpumask_size(), GFP_KERNEL);
+
if (!user_mask) {
retval = -ENOMEM;
goto out_put_task;
--
2.31.1
next reply other threads:[~2022-11-18 19:34 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-18 19:33 Waiman Long [this message]
2022-11-21 10:38 ` [PATCH-tip] sched: Don't call kfree() in do_set_cpus_allowed() Peter Zijlstra
2022-11-21 15:04 ` Waiman Long
2022-11-22 12:37 ` Peter Zijlstra
2022-11-22 15:23 ` Waiman Long
2022-11-22 16:33 ` Paul E. McKenney
2022-11-22 19:24 ` Peter Zijlstra
2022-11-22 19:30 ` Waiman Long
2022-11-22 19:58 ` Peter Zijlstra
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=20221118193302.522399-1-longman@redhat.com \
--to=longman@redhat.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=pauld@redhat.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=vincent.guittot@linaro.org \
/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;
as well as URLs for NNTP newsgroup(s).