Re: [PATCH-tip] sched: Don't call kfree() in do_set_cpus_allowed()

From: Waiman Long
Date: Mon Nov 21 2022 - 10:13:30 EST



On 11/21/22 05:38, Peter Zijlstra wrote:
On Fri, Nov 18, 2022 at 02:33:02PM -0500, Waiman Long wrote:
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()
'some' ? There's only 3 or so, which one triggers this?

It happenned at __kthread_bind_mask() where do_set_cpus_allowed() is called with pi_lock held.

[ 1084.820105]  <TASK>
[ 1084.820110]  dump_stack_lvl+0x57/0x81
[ 1084.820117]  check_noncircular+0x103/0x120
[ 10[ 1084.820160]  lock_acquire+0xba/0x230
[ 1084.820164]  ? kfree+0x10f/0x380
[ 1084.820172]  ? do_set_cpus_allowed+0x40/0x60
[ 1084.820181]  rt_spin_lock+0x27/0xe0
[ 1084.820184]  ? kfree+0x10f/0x380
[ 1084.820188]  kfree+0x10f/0x380
[ 1084.820195]  do_set_cpus_allowed+0x40/0x60
[ 1084.820203]  kthread_bind_mask+0x4a/0x70
[ 1084.820211]  create_worker+0xfb/0x1a0
[ 1084.820220]  worker_thread+0x2e3/0x3c0
[ 1084.820226]  ? process_one_work+0x450/0x450
[ 1084.820230]  kthread+0x111/0x130
[ 1084.820236]  ? kthread_complete_and_exit+0x20/0x20
[ 1084.820244]  ret_from_fork+0x22/0x30
[ 1084.820258]  </TASK>
[ 1084.820260] BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:46

It shows up with PREEMPT_RT kernel.


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.
Urgh.. depending on which of the callsites it is, it's probably simpler
to just rework the caller to not use do_set_cpus_allowed(), no?

Maybe. One thing that I am not clear about is why user_cpus_ptr is set in the first place.

Cheers,
Longman