From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752066AbdANJQA (ORCPT ); Sat, 14 Jan 2017 04:16:00 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:57568 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751692AbdANJNe (ORCPT ); Sat, 14 Jan 2017 04:13:34 -0500 From: "Paul E. McKenney" To: linux-kernel@vger.kernel.org Cc: mingo@kernel.org, jiangshanlai@gmail.com, dipankar@in.ibm.com, akpm@linux-foundation.org, mathieu.desnoyers@efficios.com, josh@joshtriplett.org, tglx@linutronix.de, peterz@infradead.org, rostedt@goodmis.org, dhowells@redhat.com, edumazet@google.com, dvhart@linux.intel.com, fweisbec@gmail.com, oleg@redhat.com, bobby.prani@gmail.com, "Paul E. McKenney" Subject: [PATCH tip/core/rcu 10/20] rcu: Add functions to test for trivial grace periods Date: Sat, 14 Jan 2017 01:13:11 -0800 X-Mailer: git-send-email 2.5.2 In-Reply-To: <20170114091255.GA20854@linux.vnet.ibm.com> References: <20170114091255.GA20854@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 17011409-0016-0000-0000-000005DF11FF X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00006431; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000199; SDB=6.00807466; UDB=6.00393077; IPR=6.00584804; BA=6.00005055; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00013919; XFM=3.00000011; UTC=2017-01-14 09:13:31 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17011409-0017-0000-0000-0000367D378A Message-Id: <1484385201-22227-10-git-send-email-paulmck@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-01-14_01:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1612050000 definitions=main-1701140136 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Under some circumstances, RCU grace periods are zero cost. For RCU-preempt, this is the case during boot, and for RCU-bh and RCU-sched, this is the case if there is only one CPU. This means that RCU users might wish to dispense with grace-period-avoidance strategies when grace periods are zero cost, so this commit adds rcu_trivial_gp(), rcu_bh_trivial_gp(), and rcu_sched_trivial_gp() to test for these conditions. Because the conditions leading to zero-cost grace periods can change at any time (for example, when a second CPU is onlined), these functions should be used as performance hints, and must not be relied on for correctness. For example, even if rcu_trivial_gp() returns true, you are required to invoke synchronize_rcu(). Signed-off-by: Paul E. McKenney --- include/linux/rcupdate.h | 8 ++++++++ include/linux/rcutiny.h | 10 ++++++++++ include/linux/rcutree.h | 2 ++ kernel/rcu/tree.c | 24 ++++++++++++++++++++++++ kernel/rcu/tree_plugin.h | 11 +++++++++++ 5 files changed, 55 insertions(+) diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h index 01f71e1d2e94..a6222478b87d 100644 --- a/include/linux/rcupdate.h +++ b/include/linux/rcupdate.h @@ -293,6 +293,7 @@ void __rcu_read_lock(void); void __rcu_read_unlock(void); void rcu_read_unlock_special(struct task_struct *t); void synchronize_rcu(void); +bool rcu_trivial_gp(void); /* * Defined as a macro as it is a very low level header included from @@ -448,6 +449,13 @@ bool __rcu_is_watching(void); #define RCU_SCHEDULER_INIT 1 #define RCU_SCHEDULER_RUNNING 2 +#ifndef CONFIG_PREEMPT_RCU +static inline bool rcu_trivial_gp(void) +{ + return rcu_sched_trivial_gp(); +} +#endif /* #ifndef CONFIG_PREEMPT_RCU */ + /* * init_rcu_head_on_stack()/destroy_rcu_head_on_stack() are needed for dynamic * initialization and destruction of rcu_head on the stack. rcu_head structures diff --git a/include/linux/rcutiny.h b/include/linux/rcutiny.h index ac81e4063b40..a77dafe79813 100644 --- a/include/linux/rcutiny.h +++ b/include/linux/rcutiny.h @@ -82,6 +82,16 @@ static inline void synchronize_sched_expedited(void) synchronize_sched(); } +static inline bool rcu_sched_trivial_gp(void) +{ + return true; +} + +static inline bool rcu_bh_trivial_gp(void) +{ + return true; +} + static inline void kfree_call_rcu(struct rcu_head *head, rcu_callback_t func) { diff --git a/include/linux/rcutree.h b/include/linux/rcutree.h index 63a4e4cf40a5..fcd61cb08851 100644 --- a/include/linux/rcutree.h +++ b/include/linux/rcutree.h @@ -47,6 +47,8 @@ static inline void rcu_virt_note_context_switch(int cpu) void synchronize_rcu_bh(void); void synchronize_sched_expedited(void); void synchronize_rcu_expedited(void); +bool rcu_sched_trivial_gp(void); +bool rcu_bh_trivial_gp(void); void kfree_call_rcu(struct rcu_head *head, rcu_callback_t func); diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c index d7b63b88434b..ed5a17aca281 100644 --- a/kernel/rcu/tree.c +++ b/kernel/rcu/tree.c @@ -3293,6 +3293,18 @@ void synchronize_sched(void) EXPORT_SYMBOL_GPL(synchronize_sched); /** + * rcu_sched_trivial_gp - Are RCU-sched grace periods trivially zero cost? + * + * Returns true if RCU-sched grace periods are currently zero cost, which + * they are if there is only one CPU. Note that unless you take steps to + * prevent it, the number of CPUs might change at any time. + */ +bool rcu_sched_trivial_gp(void) +{ + return rcu_blocking_is_gp(); +} + +/** * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed. * * Control will return to the caller some time after a full rcu_bh grace @@ -3320,6 +3332,18 @@ void synchronize_rcu_bh(void) EXPORT_SYMBOL_GPL(synchronize_rcu_bh); /** + * rcu_bh_trivial_gp - Are RCU-bh grace periods trivially zero cost? + * + * Returns true if RCU-bh grace periods are currently zero cost, which + * they are if there is only one CPU. Note that unless you take steps to + * prevent it, the number of CPUs might change at any time. + */ +bool rcu_bh_trivial_gp(void) +{ + return rcu_blocking_is_gp(); +} + +/** * get_state_synchronize_rcu - Snapshot current RCU state * * Returns a cookie that is used by a later call to cond_synchronize_rcu() diff --git a/kernel/rcu/tree_plugin.h b/kernel/rcu/tree_plugin.h index 56583e764ebf..e92d67a3fad2 100644 --- a/kernel/rcu/tree_plugin.h +++ b/kernel/rcu/tree_plugin.h @@ -680,6 +680,17 @@ void synchronize_rcu(void) EXPORT_SYMBOL_GPL(synchronize_rcu); /** + * rcu_trivial_gp - Are RCU grace periods trivially zero cost? + * + * Returns true if RCU grace periods are currently zero cost, which + * they are during boot. + */ +bool rcu_trivial_gp(void) +{ + return rcu_scheduler_active == RCU_SCHEDULER_INACTIVE; +} + +/** * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete. * * Note that this primitive does not necessarily wait for an RCU grace period -- 2.5.2