Skip to contentSkip to conversation

RCU preemption priority boosting

RFC PATCH New Steven Rostedt 1 participant 1 message Oct 3, 2007

Subsystems (4)
lore
Share & tools
lore

Steven Rostedt

author reviewer
#0 lore raw

RFC PATCH 1/1 RCU preemption priority boosting

** RFC not for inclusion **

Although this patch is against the -rt tree, it is also applicable to mainline after Paul's RCU preempt patches make it in.

This patch adds RCU preemption boosting to RCU readers that were preempted (or sleep due to spin_locks in -rt).

The approach I took is similar to priority inheritance. When a task does a synchronize_rcu, all sleeping RCU readers will inherit the priority of the caller. Any new RCU reader that schedules out will also boost its priority until the end of the grace period.

The way this works is that there's a global rcu_boost_prio variable that keeps track of what the RCU readers that schedule out should boost their priority to.

Along with that is a rcu_boost_dat structure per cpu that keeps its own copy of the rcu_boost_prio. When a RCU reader is preempted and schedules, it adds itself to a "toboost" link list in the local CPU rcu_boost_dat. If the rcu_boost_prio is lower (higher in priority) than the priority of the RCU reader that is about to sleep, the RCU reader will increase its priority to that of the rcu_boost_prio.

The RCU reader will keep this priority until it releases the last of the RCU read locks that it held. At that time the RCU reader will lower its priority back to what it was originally.

When a call is made to synchronize_rcu, if the rcu_boost_prio is higher than (lower in priority) the caller's priority, then the caller will set the rcu_boost_prio to its priority. An iteration is done to all the CPUs rcu_boost_dat. If there are any RCU readers already on the "boosted" link list a rcu_boost_dat they are moved back to the "toboost" list. Then each task is moved back to the the "boosted" list after it has its priority boosted to the new rcu_boost_prio. This is done until there are no more tasks on the "toboost" list.

Each iteration releases all locks and reacquires them and checks to see if another task of higher priority didn't come in and start boosting them higher. If this happened, then the lower priority task simply exits.

Note, the RCU readers do not lower their priority after the grace period that caused the boosting happens. So there is a slight priority leak. But the leak is bounded to the time the RCU read lock is held. If a high priority task is calling synchronize_rcu, then that priority will be of risk to having this leak. But that should not be a problem since:
1) RCU read-side critical sections are usually short.
2) In the RT point of view, a RCU synchronize_rcu must take into account any RCU read-side critical sections.

Other things that can be added to this patch:

1) Priority inheritance. Right now if we increase the priority of a RCU reader that is blocked on a spin_lock mutex (-rt only) we do not push up the priority chain of the locks. This can be added with little pain, but will only be done if we see that it is a problem.

2) There's a time where a high priority process called synchronize_rcu, and just before the grace period ended a slightly lower priority process also called the sychronize_rcu. When the grace period ends for the high priority process, new RCU sleepers will not get the priority of the lower priority process that is doing the synchronize_rcu. This is a small window and shouldn't be a problem. But it can be trivially fixed.

Comments?

-- Steve

include/linux/init_task.h +13 -0
Index: linux-2.6.23-rc9-rt1/include/linux/init_task.h
===================================================================
@@ -90,6 +90,17 @@ extern struct nsproxy init_nsproxy;
 	.signalfd_wqh	= __WAIT_QUEUE_HEAD_INITIALIZER(sighand.signalfd_wqh),	\
 }
 
+#ifdef CONFIG_PREEMPT_RCU_BOOST
+#define INIT_RCU_BOOST_PRIO .rcu_prio	= MAX_PRIO,
+#define INIT_PREEMPT_RCU_BOOST(tsk)					\
+	.rcub_rbdp	= NULL,						\
+	.rcub_state	= RCU_BOOST_IDLE,				\
+	.rcub_entry	= LIST_HEAD_INIT(tsk.rcub_entry),
+#else /* #ifdef CONFIG_PREEMPT_RCU_BOOST */
+#define INIT_RCU_BOOST_PRIO
+#define INIT_PREEMPT_RCU_BOOST(tsk)
+#endif /* #else #ifdef CONFIG_PREEMPT_RCU_BOOST */
+
 extern struct group_info init_groups;
 
 #define INIT_STRUCT_PID {						\
@@ -129,6 +140,7 @@ extern struct group_info init_groups;
 	.static_prio	= MAX_PRIO-20,					\
 	.normal_prio	= MAX_PRIO-20,					\
 	.policy		= SCHED_NORMAL,					\
+	INIT_RCU_BOOST_PRIO						\
 	.cpus_allowed	= CPU_MASK_ALL,					\
 	.mm		= NULL,						\
 	.active_mm	= &init_mm,					\
@@ -173,6 +185,7 @@ extern struct group_info init_groups;
 	},								\
 	INIT_TRACE_IRQFLAGS						\
 	INIT_LOCKDEP							\
+	INIT_PREEMPT_RCU_BOOST(tsk)					\
 }
 
 
include/linux/rcupdate.h +46 -0

51 lines not on this page · raw message

include/linux/rcupreempt.h +20 -0
Index: linux-2.6.23-rc9-rt1/include/linux/rcupreempt.h
===================================================================
@@ -42,6 +42,26 @@
 #include <linux/cpumask.h>
 #include <linux/seqlock.h>
 
+#ifdef CONFIG_PREEMPT_RCU_BOOST
+/*
+ * Task state with respect to being RCU-boosted.  This state is changed
+ * by the task itself in response to the following three events:
+ * 1. Preemption (or block on lock) while in RCU read-side critical section.
+ * 2. Outermost rcu_read_unlock() for blocked RCU read-side critical section.
+ *
+ * The RCU-boost task also updates the state when boosting priority.
+ */
+enum rcu_boost_state {
+	RCU_BOOST_IDLE = 0,	   /* Not yet blocked if in RCU read-side. */
+	RCU_BOOST_BLOCKED = 1,	   /* Blocked from RCU read-side. */
+	RCU_BOOSTED = 2,	   /* Boosting complete. */
+	RCU_BOOST_INVALID = 3,	   /* For bogus state sightings. */
+};
+
+#define N_RCU_BOOST_STATE (RCU_BOOST_INVALID + 1)
+
+#endif /* #ifdef CONFIG_PREEMPT_RCU_BOOST */
+
 /*
  * Someone might want to pass call_rcu_bh as a function pointer.
  * So this needs to just be a rename and not a macro function.
include/linux/sched.h +25 -0
Index: linux-2.6.23-rc9-rt1/include/linux/sched.h
===================================================================
@@ -733,6 +733,22 @@ struct signal_struct {
 #define SIGNAL_STOP_CONTINUED	0x00000004 /* SIGCONT since WCONTINUED reap */
 #define SIGNAL_GROUP_EXIT	0x00000008 /* group exit in progress */
 
+#ifdef CONFIG_PREEMPT_RCU_BOOST
+#define set_rcu_prio(p, prio) /* cpp to avoid #include hell */ \
+	do { \
+		(p)->rcu_prio = (prio); \
+	} while (0)
+#define get_rcu_prio(p) (p)->rcu_prio  /* cpp to avoid #include hell */
+#else /* #ifdef CONFIG_PREEMPT_RCU_BOOST */
+static inline void set_rcu_prio(struct task_struct *p, int prio)
+{
+}
+static inline int get_rcu_prio(struct task_struct *p)
+{
+	return MAX_PRIO;
+}
+#endif /* #else #ifdef CONFIG_PREEMPT_RCU_BOOST */
+
 /*
  * Some day this will be a full-fledged user tracking system..
  */
@@ -1112,6 +1128,9 @@ struct task_struct {
 #endif
 
 	int prio, static_prio, normal_prio;
+#ifdef CONFIG_PREEMPT_RCU_BOOST
+	int rcu_prio;
+#endif
 	struct list_head run_list;
 	struct sched_class *sched_class;
 	struct sched_entity se;
@@ -1138,6 +1157,12 @@ struct task_struct {
 #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
 	struct sched_info sched_info;
 #endif
+#ifdef CONFIG_PREEMPT_RCU_BOOST
+	struct rcu_boost_dat *rcub_rbdp;
+	enum rcu_boost_state rcub_state;
+	struct list_head rcub_entry;
+	unsigned long rcu_preempt_counter;
+#endif
 
 	struct list_head tasks;
 	/*
kernel/Kconfig.preempt +13 -0
Index: linux-2.6.23-rc9-rt1/kernel/Kconfig.preempt
===================================================================
@@ -171,6 +171,19 @@ config PREEMPT_RCU
 
 	  Say N if you are unsure.
 
+config PREEMPT_RCU_BOOST
+	bool "Enable priority boosting of RCU read-side critical sections"
+	depends on PREEMPT_RCU
+	help
+	  This option permits priority boosting of RCU read-side critical
+	  sections tat have been preempted and a RT process is waiting
+	  on a synchronize_rcu.
+
+	  An RCU thread is also created that periodically wakes up and
+	  performs a synchronize_rcu to make sure that all readers eventually
+	  do complete to prevent an indefinite delay of grace periods and
+	  possible OOM problems.
+
 endchoice
 
 config RCU_TRACE
kernel/Makefile +1 -0
Index: linux-2.6.23-rc9-rt1/kernel/Makefile
===================================================================
@@ -58,6 +58,7 @@ obj-$(CONFIG_SECCOMP) += seccomp.o
 obj-$(CONFIG_RCU_TORTURE_TEST) += rcutorture.o
 obj-$(CONFIG_CLASSIC_RCU) += rcuclassic.o
 obj-$(CONFIG_PREEMPT_RCU) += rcuclassic.o rcupreempt.o
+obj-$(CONFIG_PREEMPT_RCU_BOOST) += rcupreempt-boost.o
 ifeq ($(CONFIG_PREEMPT_RCU),y)
 obj-$(CONFIG_RCU_TRACE) += rcupreempt_trace.o
 endif
kernel/fork.c +7 -1
Index: linux-2.6.23-rc9-rt1/kernel/fork.c
===================================================================
@@ -1081,7 +1081,13 @@ static struct task_struct *copy_process(
 #ifdef CONFIG_PREEMPT_RCU
 	p->rcu_read_lock_nesting = 0;
 	p->rcu_flipctr_idx = 0;
-#endif /* #ifdef CONFIG_PREEMPT_RCU */
+#ifdef CONFIG_PREEMPT_RCU_BOOST
+	p->rcu_prio = MAX_PRIO;
+	p->rcub_rbdp = NULL;
+	p->rcub_state = RCU_BOOST_IDLE;
+	INIT_LIST_HEAD(&p->rcub_entry);
+#endif
+#endif /* CONFIG_PREEMPT_RCU */
 	p->vfork_done = NULL;
 	spin_lock_init(&p->alloc_lock);
 
kernel/rcupreempt-boost.c new file +538 -0

538 lines not on this page · raw message

kernel/rcupreempt.c +2 -0
Index: linux-2.6.23-rc9-rt1/kernel/rcupreempt.c
===================================================================
@@ -310,6 +310,8 @@ void __rcu_read_unlock(void)
 
 		ACCESS_ONCE(__get_cpu_var(rcu_flipctr)[idx])--;
 		local_irq_restore(oldirq);
+
+		__rcu_preempt_unboost();
 	}
 }
 EXPORT_SYMBOL_GPL(__rcu_read_unlock);
kernel/rtmutex.c +4 -3
Index: linux-2.6.23-rc9-rt1/kernel/rtmutex.c
===================================================================
@@ -121,11 +121,12 @@ static inline void init_lists(struct rt_
  */
 int rt_mutex_getprio(struct task_struct *task)
 {
+	int prio = min(task->normal_prio, get_rcu_prio(task));
+
 	if (likely(!task_has_pi_waiters(task)))
-		return task->normal_prio;
+		return prio;
 
-	return min(task_top_pi_waiter(task)->pi_list_entry.prio,
-		   task->normal_prio);
+	return min(task_top_pi_waiter(task)->pi_list_entry.prio, prio);
 }
 
 /*
kernel/sched.c +2 -0
Index: linux-2.6.23-rc9-rt1/kernel/sched.c
===================================================================
@@ -3882,6 +3882,8 @@ asmlinkage void __sched __schedule(void)
 	struct rq *rq;
 	int cpu;
 
+	rcu_preempt_boost();
+
 	preempt_disable();
 	cpu = smp_processor_id();
 	rq = cpu_rq(cpu);
kernel/rcupdate.c +3 -0
Index: linux-2.6.23-rc9-rt1/kernel/rcupdate.c
===================================================================
@@ -84,8 +84,11 @@ void synchronize_rcu(void)
 	/* Will wake me after RCU finished */
 	call_rcu(&rcu.head, wakeme_after_rcu);
 
+	rcu_boost_readers();
+
 	/* Wait for it */
 	wait_for_completion(&rcu.completion);
+	rcu_unboost_readers();
 }
 EXPORT_SYMBOL_GPL(synchronize_rcu);
 
kernel/rcupreempt_trace.c +7 -0
Index: linux-2.6.23-rc9-rt1/kernel/rcupreempt_trace.c
===================================================================
@@ -296,8 +296,14 @@ static int rcupreempt_debugfs_init(void)
 						NULL, &rcuctrs_fops);
 	if (!ctrsdir)
 		goto free_out;
+
+	if (!rcu_trace_boost_create(rcudir))
+		goto free_out;
+
 	return 0;
 free_out:
+	if (ctrsdir)
+		debugfs_remove(ctrsdir);
 	if (statdir)
 		debugfs_remove(statdir);
 	if (gpdir)
@@ -323,6 +329,7 @@ static int __init rcupreempt_trace_init(
 
 static void __exit rcupreempt_trace_cleanup(void)
 {
+	rcu_trace_boost_destroy();
 	debugfs_remove(statdir);
 	debugfs_remove(gpdir);
 	debugfs_remove(ctrsdir);