Message ID | 20221115131926.3409974-1-qiang1.zhang@intel.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2719257wru; Tue, 15 Nov 2022 05:16:50 -0800 (PST) X-Google-Smtp-Source: AA0mqf6qBADxIlbNdVtSKYVPhpT99jC6Hh0I751GHykklim5+NqMuid929t5nxZF+4yZ2WZfkTzv X-Received: by 2002:a17:90b:2406:b0:211:906a:f8ef with SMTP id nr6-20020a17090b240600b00211906af8efmr2246448pjb.71.1668518210012; Tue, 15 Nov 2022 05:16:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668518210; cv=none; d=google.com; s=arc-20160816; b=URlVtMDp/fmi8mOjBWg00wRENg8q+z6RJtrRqQ2pVzNYtUxFVKj3HYdtrKis1RBF2f Glh+T2RMV46ucrzU7fcQUdm4RIBy83NRwlgwnyMnyJRf71my3qLc7VI5T6XrhX2x4mLF Z5GA7cQ1xbgmkVg8eoClhz597SytQQOJg4LB3g6FlH7YIkrUAL+daMAvYtFFLREK6vb+ mcEcnwaLmfdRjf94RCP7SGkaVQ90akWev/V+OLb9nX4e/dOMzj3u8nh3Gwvc2ONb+beD rUJFIMmk1JGPtO6n8cBcPDE2rXkrTNqfFiT0bfnMklufGUA+Co3ZFj2xYb1Y2jlswNCi sn5A== 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=6Min09Rk12NgZBMgljA0Dt7ol1SuIA2cq3o6DzrFJAU=; b=tOwqE/ZtSLs6M44LPiyzbEY5FOlbd5G/kHyXNzR9voAhymAk1zqBEZCkdTFEzJ9ZIf /EgFQCN96FYXor11hRKdoNwjVe2XTNHPKPnlV0FB63cr81iQ3MhXwPwqEUMoEVY0+VPd 3j+hMGOegMjAmVL5J+LxLpYmk0gCN3bZrMeMDYE0Zr7bKQkCu5pJ6an5CaSe+WFSns0N ho3W923Wjy39hJWHZXHbiSNPBqQAyBF8AVcpL1HuJYyecJyY3IRgXQrA23Ky1v2W5dHP x3OGm0gD4HdZdELIv9N1lUKFhRRRrKRSH+G/I+Lkqk0t0hfWsEnQQ6+mEY4ekqJliiKy H9jg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HtIx8PUn; 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=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a11-20020a056a001d0b00b0056e8a852e3esi11538822pfx.219.2022.11.15.05.16.34; Tue, 15 Nov 2022 05:16:49 -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=@intel.com header.s=Intel header.b=HtIx8PUn; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230192AbiKONNz (ORCPT <rfc822;lkml4gm@gmail.com> + 99 others); Tue, 15 Nov 2022 08:13:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232222AbiKONNp (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 15 Nov 2022 08:13:45 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6554B1D2; Tue, 15 Nov 2022 05:13:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1668518019; x=1700054019; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=KJFXj3YWKvBgdKbZAu8eoga4jTJKbrFgb7rok5iDpxA=; b=HtIx8PUnM7oNMLWloO2Yg3latnTs7KgnJOSzZT6+9kW2Lr+Cv+7HVLrQ qJuV1ULwqyYCapdtI+LYAITqsDud4XPS83bsXWBHo2WEH4cI3KcLbKRjk et5JWV2kmKo3sgAzlPff7Gq2+xMEeHOh1VJ2Pe5ib8yHvGoeNfHydcUHk kiZy4MvnvXgjj+AvY2jU1mfBJF5Z16vyHOnm9nb9MNrHppnBu7b8dpXHR EsSHSNwuSIeixGzS6yWDDK0q2btRU/64oDZ0WDiCDFGEQybrlilQuJGOC JKzBzxbIvHdwTB4ZO99G0JD7YiV6azABaXhlEFNyydQqlrbO6aEPBz8zW Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10531"; a="309875258" X-IronPort-AV: E=Sophos;i="5.96,166,1665471600"; d="scan'208";a="309875258" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Nov 2022 05:13:31 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10531"; a="744586943" X-IronPort-AV: E=Sophos;i="5.96,166,1665471600"; d="scan'208";a="744586943" Received: from zq-optiplex-7090.bj.intel.com ([10.238.156.129]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Nov 2022 05:13:28 -0800 From: Zqiang <qiang1.zhang@intel.com> To: paulmck@kernel.org, frederic@kernel.org, joel@joelfernandes.org Cc: rcu@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] rcu: Avoid invalid wakeup for rcuc kthreads in RCU_KTHREAD_OFFCPU status Date: Tue, 15 Nov 2022 21:19:26 +0800 Message-Id: <20221115131926.3409974-1-qiang1.zhang@intel.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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?1749568150323509264?= X-GMAIL-MSGID: =?utf-8?q?1749568150323509264?= |
Series |
rcu: Avoid invalid wakeup for rcuc kthreads in RCU_KTHREAD_OFFCPU status
|
|
Commit Message
Zqiang
Nov. 15, 2022, 1:19 p.m. UTC
For CONFIG_PREEMPT_RT=y kernel, the "use_softirq=0" will be set, the
RCU_SOFTIRQ processing is moved to per-CPU rcuc kthreads which created
by smpboot_register_percpu_thread(). when CPU is going offline, the
corresponding rcu_data.rcu_cpu_kthread_status is set RCU_KTHREAD_OFFCPU,
and the rcuc kthreads enter TASK_PARKED state, kthreads in TASK_PARKED
state only accept kthread_unpark() to wakeup.
Therefore, This commit avoid invoke wake_up_process() to rcuc kthreads
in TASK_PARKED state.
Signed-off-by: Zqiang <qiang1.zhang@intel.com>
---
kernel/rcu/tree.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
Comments
On Tue, Nov 15, 2022 at 09:19:26PM +0800, Zqiang wrote: > For CONFIG_PREEMPT_RT=y kernel, the "use_softirq=0" will be set, the > RCU_SOFTIRQ processing is moved to per-CPU rcuc kthreads which created > by smpboot_register_percpu_thread(). when CPU is going offline, the > corresponding rcu_data.rcu_cpu_kthread_status is set RCU_KTHREAD_OFFCPU, > and the rcuc kthreads enter TASK_PARKED state, kthreads in TASK_PARKED > state only accept kthread_unpark() to wakeup. > > Therefore, This commit avoid invoke wake_up_process() to rcuc kthreads > in TASK_PARKED state. > > Signed-off-by: Zqiang <qiang1.zhang@intel.com> > --- > kernel/rcu/tree.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c > index 3ccad468887e..49dd87356851 100644 > --- a/kernel/rcu/tree.c > +++ b/kernel/rcu/tree.c > @@ -2375,7 +2375,8 @@ static void rcu_wake_cond(struct task_struct *t, int status) > * If the thread is yielding, only wake it when this > * is invoked from idle > */ > - if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current))) > + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)) && > + status != RCU_KTHREAD_OFFCPU) > wake_up_process(t); There is a tiny window where this can happen (between CPUHP_TEARDOWN_CPU and CPUHP_AP_SMPBOOT_THREADS) and it can't cause a spurious unpark because wake_up_process() only wakes up from TASK_[UN]INTERRUPTIBLE states. And even if it did, the KTHREAD_SHOULD_PARK bit would still be on. And more important! On unpark time RCU_KTHREAD_OFFCPU isn't cleared. Only the rcuc kthread does it, and after your patch it couldn't be awaken to perform that, unless rcuc is lucky enough to have rcu_data.rcu_cpu_has_work = 1 by the time it unparks and that isn't guaranteed. So rcuc may sleep forever. OTOH one cleanup that could be done is to make rcu_cpu_kthread_park() to use __this_cpu_write as it's guaranteed that cpu == smp_processor_id(). Thanks.
On Tue, Nov 15, 2022 at 09:19:26PM +0800, Zqiang wrote: > For CONFIG_PREEMPT_RT=y kernel, the "use_softirq=0" will be set, the > RCU_SOFTIRQ processing is moved to per-CPU rcuc kthreads which created > by smpboot_register_percpu_thread(). when CPU is going offline, the > corresponding rcu_data.rcu_cpu_kthread_status is set RCU_KTHREAD_OFFCPU, > and the rcuc kthreads enter TASK_PARKED state, kthreads in TASK_PARKED > state only accept kthread_unpark() to wakeup. > > Therefore, This commit avoid invoke wake_up_process() to rcuc kthreads > in TASK_PARKED state. > > Signed-off-by: Zqiang <qiang1.zhang@intel.com> > --- > kernel/rcu/tree.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c > index 3ccad468887e..49dd87356851 100644 > --- a/kernel/rcu/tree.c > +++ b/kernel/rcu/tree.c > @@ -2375,7 +2375,8 @@ static void rcu_wake_cond(struct task_struct *t, int status) > * If the thread is yielding, only wake it when this > * is invoked from idle > */ > - if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current))) > + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)) && > + status != RCU_KTHREAD_OFFCPU) > wake_up_process(t); >There is a tiny window where this can happen (between CPUHP_TEARDOWN_CPU >and CPUHP_AP_SMPBOOT_THREADS) and it can't cause a spurious unpark because >wake_up_process() only wakes up from TASK_[UN]INTERRUPTIBLE states. And even >if it did, the KTHREAD_SHOULD_PARK bit would still be on. Yes even if it did, because KTHREAD_SHOULD_PARK bit would still be on, this kthreads will schedule out again. > >And more important! On unpark time RCU_KTHREAD_OFFCPU isn't cleared. Only the >rcuc kthread does it, and after your patch it couldn't be awaken to perform >that, unless rcuc is lucky enough to have rcu_data.rcu_cpu_has_work = 1 >by the time it unparks and that isn't guaranteed. So rcuc may sleep forever. Thanks for review, yes I should register an unpark function to clear RCU_KTHREAD_OFFCPU. Is the following modification more appropriate? diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c index 3ccad468887e..a2248af0ccda 100644 --- a/kernel/rcu/tree.c +++ b/kernel/rcu/tree.c @@ -2375,7 +2375,8 @@ static void rcu_wake_cond(struct task_struct *t, int status) * If the thread is yielding, only wake it when this * is invoked from idle */ - if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current))) + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)) && + status != RCU_KTHREAD_OFFCPU) wake_up_process(t); } @@ -2407,7 +2408,14 @@ static void invoke_rcu_core(void) static void rcu_cpu_kthread_park(unsigned int cpu) { - per_cpu(rcu_data.rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU; + WARN_ON_ONCE(cpu != smp_processor_id()); + __this_cpu_write(rcu_data.rcu_cpu_kthread_status, RCU_KTHREAD_OFFCPU); +} + +static void rcu_cpu_kthread_unpark(unsigned int cpu) +{ + WARN_ON_ONCE(cpu != smp_processor_id()); + __this_cpu_write(rcu_data.rcu_cpu_kthread_status, RCU_KTHREAD_ONCPU); } static int rcu_cpu_kthread_should_run(unsigned int cpu) @@ -2460,6 +2468,7 @@ static struct smp_hotplug_thread rcu_cpu_thread_spec = { .thread_comm = "rcuc/%u", .setup = rcu_cpu_kthread_setup, .park = rcu_cpu_kthread_park, + .unpark = rcu_cpu_kthread_unpark, }; /* diff --git a/kernel/rcu/tree.h b/kernel/rcu/tree.h index fcb5d696eb17..c4b9606968db 100644 --- a/kernel/rcu/tree.h +++ b/kernel/rcu/tree.h @@ -36,8 +36,9 @@ struct rcu_exp_work { #define RCU_KTHREAD_RUNNING 1 #define RCU_KTHREAD_WAITING 2 #define RCU_KTHREAD_OFFCPU 3 -#define RCU_KTHREAD_YIELDING 4 -#define RCU_KTHREAD_MAX 4 +#define RCU_KTHREAD_ONCPU 4 +#define RCU_KTHREAD_YIELDING 5 +#define RCU_KTHREAD_MAX 5 Thanks Zqiang > >OTOH one cleanup that could be done is to make rcu_cpu_kthread_park() to use >__this_cpu_write as it's guaranteed that cpu == smp_processor_id(). > >Thanks.
On Wed, Nov 16, 2022 at 02:07:28PM +0000, Zhang, Qiang1 wrote: > On Tue, Nov 15, 2022 at 09:19:26PM +0800, Zqiang wrote: > >And more important! On unpark time RCU_KTHREAD_OFFCPU isn't cleared. Only the > >rcuc kthread does it, and after your patch it couldn't be awaken to perform > >that, unless rcuc is lucky enough to have rcu_data.rcu_cpu_has_work = 1 > >by the time it unparks and that isn't guaranteed. So rcuc may sleep forever. > > Thanks for review, yes I should register an unpark function to clear RCU_KTHREAD_OFFCPU. > Is the following modification more appropriate? > > diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c > index 3ccad468887e..a2248af0ccda 100644 > --- a/kernel/rcu/tree.c > +++ b/kernel/rcu/tree.c > @@ -2375,7 +2375,8 @@ static void rcu_wake_cond(struct task_struct *t, int status) > * If the thread is yielding, only wake it when this > * is invoked from idle > */ > - if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current))) > + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)) && > + status != RCU_KTHREAD_OFFCPU) > wake_up_process(t); > } > > @@ -2407,7 +2408,14 @@ static void invoke_rcu_core(void) > > static void rcu_cpu_kthread_park(unsigned int cpu) > { > - per_cpu(rcu_data.rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU; > + WARN_ON_ONCE(cpu != smp_processor_id()); > + __this_cpu_write(rcu_data.rcu_cpu_kthread_status, RCU_KTHREAD_OFFCPU); > +} > + > +static void rcu_cpu_kthread_unpark(unsigned int cpu) > +{ > + WARN_ON_ONCE(cpu != smp_processor_id()); > + __this_cpu_write(rcu_data.rcu_cpu_kthread_status, RCU_KTHREAD_ONCPU); > } > > static int rcu_cpu_kthread_should_run(unsigned int cpu) > @@ -2460,6 +2468,7 @@ static struct smp_hotplug_thread rcu_cpu_thread_spec = { > .thread_comm = "rcuc/%u", > .setup = rcu_cpu_kthread_setup, > .park = rcu_cpu_kthread_park, > + .unpark = rcu_cpu_kthread_unpark, Well, personally I don't think it's worth the burden because wake_up_process() already does an early exit if it's not dealing with a TASK_[UN]INTERRUPTIBLE task and the window is so short and rare that it doesn't look like a good candidate for extra optimization; Thanks.
On Wed, Nov 16, 2022 at 02:07:28PM +0000, Zhang, Qiang1 wrote: > On Tue, Nov 15, 2022 at 09:19:26PM +0800, Zqiang wrote: > >And more important! On unpark time RCU_KTHREAD_OFFCPU isn't cleared. Only the > >rcuc kthread does it, and after your patch it couldn't be awaken to perform > >that, unless rcuc is lucky enough to have rcu_data.rcu_cpu_has_work = 1 > >by the time it unparks and that isn't guaranteed. So rcuc may sleep forever. > > Thanks for review, yes I should register an unpark function to clear RCU_KTHREAD_OFFCPU. > Is the following modification more appropriate? > > diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c > index 3ccad468887e..a2248af0ccda 100644 > --- a/kernel/rcu/tree.c > +++ b/kernel/rcu/tree.c > @@ -2375,7 +2375,8 @@ static void rcu_wake_cond(struct task_struct *t, int status) > * If the thread is yielding, only wake it when this > * is invoked from idle > */ > - if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current))) > + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)) && > + status != RCU_KTHREAD_OFFCPU) > wake_up_process(t); > } > > @@ -2407,7 +2408,14 @@ static void invoke_rcu_core(void) > > static void rcu_cpu_kthread_park(unsigned int cpu) > { > - per_cpu(rcu_data.rcu_cpu_kthread_status, cpu) = RCU_KTHREAD_OFFCPU; > + WARN_ON_ONCE(cpu != smp_processor_id()); > + __this_cpu_write(rcu_data.rcu_cpu_kthread_status, RCU_KTHREAD_OFFCPU); > +} > + > +static void rcu_cpu_kthread_unpark(unsigned int cpu) > +{ > + WARN_ON_ONCE(cpu != smp_processor_id()); > + __this_cpu_write(rcu_data.rcu_cpu_kthread_status, RCU_KTHREAD_ONCPU); > } > > static int rcu_cpu_kthread_should_run(unsigned int cpu) > @@ -2460,6 +2468,7 @@ static struct smp_hotplug_thread rcu_cpu_thread_spec = { > .thread_comm = "rcuc/%u", > .setup = rcu_cpu_kthread_setup, > .park = rcu_cpu_kthread_park, > + .unpark = rcu_cpu_kthread_unpark, >Well, personally I don't think it's worth the burden because wake_up_process() >already does an early exit if it's not dealing with a TASK_[UN]INTERRUPTIBLE task and >the window is so short and rare that it doesn't look like a good candidate for >extra optimization; Agree, in try_to_wake_up(), acquire rcuc task->pi_lock(there's almost no lock contention here) and then check tasks state, for TASK_[UN]INTERRUPTIBLE task, not dealing and early exit. Thanks Zqiang > >Thanks.
diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c index 3ccad468887e..49dd87356851 100644 --- a/kernel/rcu/tree.c +++ b/kernel/rcu/tree.c @@ -2375,7 +2375,8 @@ static void rcu_wake_cond(struct task_struct *t, int status) * If the thread is yielding, only wake it when this * is invoked from idle */ - if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current))) + if (t && (status != RCU_KTHREAD_YIELDING || is_idle_task(current)) && + status != RCU_KTHREAD_OFFCPU) wake_up_process(t); }