Message ID | 20221118193302.522399-1-longman@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp373446wrr; Fri, 18 Nov 2022 11:34:56 -0800 (PST) X-Google-Smtp-Source: AA0mqf42j1R6kXkscl21AyE5IBPsRI28S4fFW4QKVJd7S19HVvRiYLR9m68kknwLsygPwuuZ7rkH X-Received: by 2002:a17:906:6bda:b0:7ae:4a7f:3281 with SMTP id t26-20020a1709066bda00b007ae4a7f3281mr6921612ejs.50.1668800096016; Fri, 18 Nov 2022 11:34:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668800096; cv=none; d=google.com; s=arc-20160816; b=V08AtLFSbsru7p8IxjdjrTeLdhhK96fOZV/PnWdiyFk7j0hktVSKQKBMf1NQW8CJa7 eMMK4mC+YlNRUZBbTfFJ2UJu0BGOQAVyKp5DMFRFrGy1RpwXea9VO7AwbvjJj9je+6Nv qVMgj+10fTrv+J2MpSZEcIYwOO1iVHWbVCfOyOZOgOs4sxWHtKpcCs7Ut4fKQ3+0rhfO 01kKN2BmyjXPvQbSLxJLVN+iOqnq2uiV/hMM7f3ucSNsbUoXSPREcmiM6dchbSIUdwN9 yplccP1DLEXw4VeShNBO9KrXfOXkqpKdi7I59828Bv5gmSzaJS8hGtGTQB7qh4DHgvfo phBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=vm4lYSuI39T5xePAN2iEqNP90UK7XrasUEw23g3xMyk=; b=h3d00EBHFVqc4Fto+uiX2vxzM3oX9zBM20ig42x9httXHJImw7pafjGEZjSRH/Frm9 5lnw52GMf1K4w5JGLT+gvWqsiJ92n8tE9p53UltGkz8nnZ6sjCcWyygH1h2aRbDJ1I3U VJJtMu93/mrwl/FCHfxOvefEgrR9zzY+YhQ+oJ8hqBP9kg07XI8f6khb2EBfpJjwmT9k Qtc6sX+aORSkezFJuEyL47MDsiCFMIVrqjxuMw4A3IVsNfgS3OQSbOEEs5vqoAwlZRQC GzM5gsfJJDT77rWwKBM5gjJMPvBr70v3qfJjfVJ2yhyP/D+vFFQvfLGu9BEIATotn0fh pU1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CslY047q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hv11-20020a17090760cb00b007ad6a0afbc6si3482912ejc.7.2022.11.18.11.34.32; Fri, 18 Nov 2022 11:34:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CslY047q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235374AbiKRTeS (ORCPT <rfc822;kkmonlee@gmail.com> + 99 others); Fri, 18 Nov 2022 14:34:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234867AbiKRTeQ (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 18 Nov 2022 14:34:16 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6A9478D4B for <linux-kernel@vger.kernel.org>; Fri, 18 Nov 2022 11:33:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668799999; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=vm4lYSuI39T5xePAN2iEqNP90UK7XrasUEw23g3xMyk=; b=CslY047qvblMjaIpMwnebuyi9Ml/WL7HrSs9pga0XgLwkTvMddTMZ+DQ+pOQiuC3mrRR4s y5gV6bNuNe2VHOvXVEESfISawSZ7Cqs60MGRvxBotic/FNXe350Ad+LBSkosJ+CA8Z1TQw +BwdVm6u6HzOayw/xyqdECn36A8H7hU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-385-a0HxK2E1MZyeMrjtuV38kw-1; Fri, 18 Nov 2022 14:33:16 -0500 X-MC-Unique: a0HxK2E1MZyeMrjtuV38kw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5031385A588; Fri, 18 Nov 2022 19:33:16 +0000 (UTC) Received: from llong.com (unknown [10.22.18.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id A092E140EBF3; Fri, 18 Nov 2022 19:33:15 +0000 (UTC) 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 Message-Id: <20221118193302.522399-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749863729282211531?= X-GMAIL-MSGID: =?utf-8?q?1749863729282211531?= |
Series |
[PATCH-tip] sched: Don't call kfree() in do_set_cpus_allowed()
|
|
Commit Message
Waiman Long
Nov. 18, 2022, 7:33 p.m. UTC
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(-)
Comments
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? > 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?
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
On Mon, Nov 21, 2022 at 10:04:33AM -0500, Waiman Long wrote: > > 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. Oh, I see .. > Maybe. One thing that I am not clear about is why user_cpus_ptr is set in > the first place. Perhaps someone set an affinity on kthreadd ? But I'm thinking this exact problem is also possible (rather more likely even) with select_fallback_rq() that too holds pi_lock (which account for both other users of this function). Bah. And the allocation is just the one long in size (for small configs) which is just enough space for a single linked list like you had. Urgh. The below is yuck too, and I'm not sure Paul wants us to use kvfree_call_rcu() without its wrapper. --- diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 78b2d5cabcc5..0d0af0fc7fcf 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2606,7 +2606,12 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) }; __do_set_cpus_allowed(p, &ac); - kfree(ac.user_mask); + /* + * 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(). + */ + kvfree_call_rcu((struct rcu_head *)ac.user_mask, (rcu_callback_t)0); } int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, @@ -8196,7 +8201,7 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) struct affinity_context ac; struct cpumask *user_mask; struct task_struct *p; - int retval; + int retval, size; rcu_read_lock(); @@ -8229,7 +8234,11 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) if (retval) goto out_put_task; - user_mask = kmalloc(cpumask_size(), GFP_KERNEL); + /* + * See do_set_cpus_allowed() for the rcu_head usage. + */ + size = max_t(int, cpumask_size(), sizeof(struct rcu_head)); + user_mask = kmalloc(size, GFP_KERNEL); if (!user_mask) { retval = -ENOMEM; goto out_put_task;
On 11/22/22 07:37, Peter Zijlstra wrote: > On Mon, Nov 21, 2022 at 10:04:33AM -0500, Waiman Long wrote: >> 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. > Oh, I see .. > >> Maybe. One thing that I am not clear about is why user_cpus_ptr is set in >> the first place. > Perhaps someone set an affinity on kthreadd ? > > But I'm thinking this exact problem is also possible (rather more likely > even) with select_fallback_rq() that too holds pi_lock (which account > for both other users of this function). > > Bah. > > And the allocation is just the one long in size (for small configs) > which is just enough space for a single linked list like you had. That is exactly the reason why I use lockless list. > > Urgh. > > The below is yuck too, and I'm not sure Paul wants us to use > kvfree_call_rcu() without its wrapper. > > --- > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index 78b2d5cabcc5..0d0af0fc7fcf 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -2606,7 +2606,12 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) > }; > > __do_set_cpus_allowed(p, &ac); > - kfree(ac.user_mask); > + /* > + * 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(). > + */ > + kvfree_call_rcu((struct rcu_head *)ac.user_mask, (rcu_callback_t)0); > } I guess you need to do a NULL check before calling kvfree_call_rcu() as I don't think kvfree_call_rcu() does that. Also it is unlikely that we need to call it. > > int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, > @@ -8196,7 +8201,7 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > struct affinity_context ac; > struct cpumask *user_mask; > struct task_struct *p; > - int retval; > + int retval, size; > > rcu_read_lock(); > > @@ -8229,7 +8234,11 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > if (retval) > goto out_put_task; > > - user_mask = kmalloc(cpumask_size(), GFP_KERNEL); > + /* > + * See do_set_cpus_allowed() for the rcu_head usage. > + */ > + size = max_t(int, cpumask_size(), sizeof(struct rcu_head)); > + user_mask = kmalloc(size, GFP_KERNEL); > if (!user_mask) { > retval = -ENOMEM; > goto out_put_task; I guess that will work too. Just like you, I am a bit uneasy to call into kvfree_call_rcu() directly as it may change in the future. How about iff --git a/kernel/sched/core.c b/kernel/sched/core.c index 78b2d5cabcc5..5fac4aa6ac7f 100644 --- a/kernel/sched/core.c diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 78b2d5cabcc5..5fac4aa6ac7f 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2593,6 +2593,11 @@ __do_set_cpus_allowed(struct task_struct *p, struct affinity_context *ctx) set_next_task(rq, p); } +union cpumask_rcuhead { + void *cpumask; + struct rcu_head rcu; +}; + /* * Used for kthread_bind() and select_fallback_rq(), in both cases the user * affinity (if any) should be destroyed too. @@ -2606,7 +2611,12 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) }; __do_set_cpus_allowed(p, &ac); - kfree(ac.user_mask); + /* + * 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(). + */ + kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu); } int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, @@ -8196,7 +8206,7 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) struct affinity_context ac; struct cpumask *user_mask; struct task_struct *p; - int retval; + int retval, size; rcu_read_lock(); @@ -8229,7 +8239,11 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) if (retval) goto out_put_task; - user_mask = kmalloc(cpumask_size(), GFP_KERNEL); + /* + * See do_set_cpus_allowed() for the rcu_head usage. + */ + size = max_t(int, cpumask_size(), sizeof(union cpumask_rcuhead)); + user_mask = kmalloc(size, GFP_KERNEL); if (!user_mask) { retval = -ENOMEM; goto out_put_task; Cheers, Longman
On Tue, Nov 22, 2022 at 10:23:43AM -0500, Waiman Long wrote: > On 11/22/22 07:37, Peter Zijlstra wrote: > > On Mon, Nov 21, 2022 at 10:04:33AM -0500, Waiman Long wrote: > > > 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. > > Oh, I see .. > > > > > Maybe. One thing that I am not clear about is why user_cpus_ptr is set in > > > the first place. > > Perhaps someone set an affinity on kthreadd ? > > > > But I'm thinking this exact problem is also possible (rather more likely > > even) with select_fallback_rq() that too holds pi_lock (which account > > for both other users of this function). > > > > Bah. > > > > And the allocation is just the one long in size (for small configs) > > which is just enough space for a single linked list like you had. > That is exactly the reason why I use lockless list. > > > > Urgh. > > > > The below is yuck too, and I'm not sure Paul wants us to use > > kvfree_call_rcu() without its wrapper. > > > > --- > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > > index 78b2d5cabcc5..0d0af0fc7fcf 100644 > > --- a/kernel/sched/core.c > > +++ b/kernel/sched/core.c > > @@ -2606,7 +2606,12 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) > > }; > > __do_set_cpus_allowed(p, &ac); > > - kfree(ac.user_mask); > > + /* > > + * 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(). > > + */ > > + kvfree_call_rcu((struct rcu_head *)ac.user_mask, (rcu_callback_t)0); > > } > > I guess you need to do a NULL check before calling kvfree_call_rcu() as I > don't think kvfree_call_rcu() does that. Also it is unlikely that we need to > call it. Indeed, the NULL check is in kvfree_rcu_arg_2(). By the time you get to kvfree_call_rcu, the pointer is assumed to be non-NULL. > > int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, > > @@ -8196,7 +8201,7 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > > struct affinity_context ac; > > struct cpumask *user_mask; > > struct task_struct *p; > > - int retval; > > + int retval, size; > > rcu_read_lock(); > > @@ -8229,7 +8234,11 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) > > if (retval) > > goto out_put_task; > > - user_mask = kmalloc(cpumask_size(), GFP_KERNEL); > > + /* > > + * See do_set_cpus_allowed() for the rcu_head usage. > > + */ > > + size = max_t(int, cpumask_size(), sizeof(struct rcu_head)); > > + user_mask = kmalloc(size, GFP_KERNEL); > > if (!user_mask) { > > retval = -ENOMEM; > > goto out_put_task; > > I guess that will work too. Just like you, I am a bit uneasy to call into > kvfree_call_rcu() directly as it may change in the future. How about > > iff --git a/kernel/sched/core.c b/kernel/sched/core.c > index 78b2d5cabcc5..5fac4aa6ac7f 100644 > --- a/kernel/sched/core.c > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index 78b2d5cabcc5..5fac4aa6ac7f 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -2593,6 +2593,11 @@ __do_set_cpus_allowed(struct task_struct *p, struct > affinity_context *ctx) > set_next_task(rq, p); > } > > +union cpumask_rcuhead { > + void *cpumask; > + struct rcu_head rcu; > +}; > + > /* > * Used for kthread_bind() and select_fallback_rq(), in both cases the user > * affinity (if any) should be destroyed too. > @@ -2606,7 +2611,12 @@ void do_set_cpus_allowed(struct task_struct *p, const > struct cpumask *new_mask) > }; > > __do_set_cpus_allowed(p, &ac); > - kfree(ac.user_mask); > + /* > + * 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(). > + */ > + kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu); This looks plausible to me. Thanx, Paul > } > > int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, > @@ -8196,7 +8206,7 @@ long sched_setaffinity(pid_t pid, const struct cpumask > *in_mask) > struct affinity_context ac; > struct cpumask *user_mask; > struct task_struct *p; > - int retval; > + int retval, size; > > rcu_read_lock(); > > @@ -8229,7 +8239,11 @@ long sched_setaffinity(pid_t pid, const struct > cpumask *in_mask) > if (retval) > goto out_put_task; > > - user_mask = kmalloc(cpumask_size(), GFP_KERNEL); > + /* > + * See do_set_cpus_allowed() for the rcu_head usage. > + */ > + size = max_t(int, cpumask_size(), sizeof(union cpumask_rcuhead)); > + user_mask = kmalloc(size, GFP_KERNEL); > if (!user_mask) { > retval = -ENOMEM; > goto out_put_task; > > Cheers, > Longman > >
On Tue, Nov 22, 2022 at 10:23:43AM -0500, Waiman Long wrote: > index 78b2d5cabcc5..5fac4aa6ac7f 100644 > --- a/kernel/sched/core.c > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index 78b2d5cabcc5..5fac4aa6ac7f 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -2593,6 +2593,11 @@ __do_set_cpus_allowed(struct task_struct *p, struct > affinity_context *ctx) > set_next_task(rq, p); > } > > +union cpumask_rcuhead { > + void *cpumask; > + struct rcu_head rcu; > +}; > + Hehe; I had this union too; I just figured it'd be nice to not have to spend these 4 lines to express this. Esp. since we're casting pointers *anyway*. > /* > * Used for kthread_bind() and select_fallback_rq(), in both cases the user > * affinity (if any) should be destroyed too. > @@ -2606,7 +2611,12 @@ void do_set_cpus_allowed(struct task_struct *p, const > struct cpumask *new_mask) > }; > > __do_set_cpus_allowed(p, &ac); > - kfree(ac.user_mask); > + /* > + * 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(). > + */ > + kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu); > } > > int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, > @@ -8196,7 +8206,7 @@ long sched_setaffinity(pid_t pid, const struct cpumask > *in_mask) > struct affinity_context ac; > struct cpumask *user_mask; > struct task_struct *p; > - int retval; > + int retval, size; > > rcu_read_lock(); > > @@ -8229,7 +8239,11 @@ long sched_setaffinity(pid_t pid, const struct > cpumask *in_mask) > if (retval) > goto out_put_task; > > - user_mask = kmalloc(cpumask_size(), GFP_KERNEL); > + /* > + * See do_set_cpus_allowed() for the rcu_head usage. > + */ > + size = max_t(int, cpumask_size(), sizeof(union cpumask_rcuhead)); > + user_mask = kmalloc(size, GFP_KERNEL); > if (!user_mask) { > retval = -ENOMEM; > goto out_put_task; > We also should fix the allocation in dup_user_cpus_ptr() -- perhaps pull the thing into a helper.
On 11/22/22 14:24, Peter Zijlstra wrote: > On Tue, Nov 22, 2022 at 10:23:43AM -0500, Waiman Long wrote: >> index 78b2d5cabcc5..5fac4aa6ac7f 100644 >> --- a/kernel/sched/core.c >> diff --git a/kernel/sched/core.c b/kernel/sched/core.c >> index 78b2d5cabcc5..5fac4aa6ac7f 100644 >> --- a/kernel/sched/core.c >> +++ b/kernel/sched/core.c >> @@ -2593,6 +2593,11 @@ __do_set_cpus_allowed(struct task_struct *p, struct >> affinity_context *ctx) >> set_next_task(rq, p); >> } >> >> +union cpumask_rcuhead { >> + void *cpumask; >> + struct rcu_head rcu; >> +}; >> + > Hehe; I had this union too; I just figured it'd be nice to not have to > spend these 4 lines to express this. Esp. since we're casting pointers > *anyway*. Well, that is true. As long as the NULL check is there, I am OK with calling kvfree_call_rcu() directly if Paul doesn't object. >> /* >> * Used for kthread_bind() and select_fallback_rq(), in both cases the user >> * affinity (if any) should be destroyed too. >> @@ -2606,7 +2611,12 @@ void do_set_cpus_allowed(struct task_struct *p, const >> struct cpumask *new_mask) >> }; >> >> __do_set_cpus_allowed(p, &ac); >> - kfree(ac.user_mask); >> + /* >> + * 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(). >> + */ >> + kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu); >> } >> >> int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, >> @@ -8196,7 +8206,7 @@ long sched_setaffinity(pid_t pid, const struct cpumask >> *in_mask) >> struct affinity_context ac; >> struct cpumask *user_mask; >> struct task_struct *p; >> - int retval; >> + int retval, size; >> >> rcu_read_lock(); >> >> @@ -8229,7 +8239,11 @@ long sched_setaffinity(pid_t pid, const struct >> cpumask *in_mask) >> if (retval) >> goto out_put_task; >> >> - user_mask = kmalloc(cpumask_size(), GFP_KERNEL); >> + /* >> + * See do_set_cpus_allowed() for the rcu_head usage. >> + */ >> + size = max_t(int, cpumask_size(), sizeof(union cpumask_rcuhead)); >> + user_mask = kmalloc(size, GFP_KERNEL); >> if (!user_mask) { >> retval = -ENOMEM; >> goto out_put_task; >> > We also should fix the allocation in dup_user_cpus_ptr() -- perhaps pull > the thing into a helper. > I have just sent out a new patch to fix that before I saw your email. I do forgot to put -tip in the subject line. Cheers, Longman
On Tue, Nov 22, 2022 at 02:30:38PM -0500, Waiman Long wrote: > I have just sent out a new patch to fix that before I saw your email. I do > forgot to put -tip in the subject line. I found it; I'll stare at it in the morning. Things don't seem to want to make much sense anymore today :-)
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;