Alternative RCU implementation]
Hi Everyone,
I sent this to the LKML on Friday, but it seems to have been lost so I'm sending it again.
The attached patch against linux-2.6.8.1-mm4 is an experimental implementation of RCU.
It uses an active synchronization between the rcu_read_lock(), rcu_read_unlock(), and the code which starts a new RCU batch. A RCU batch can be started at an arbitrary point, and it will complete without waiting for a timer driven poll. This should help avoid large batches and their adverse cache and latency effects.
I did this work because Concurrent encourages its customers to isolate critical realtime processes to their own cpu and shield them from other processes and interrupts. This includes disabling the local timer interrupt. The current RCU code relies on the local timer to recognize quiescent states. If it is disabled on any cpu, RCU callbacks are never called and the system bleeds memory and hangs on calls to synchronize_kernel().
The main idea from the patch is to synchronize between the read-side critical section and the code that starts an rcu batch. Any code which is not protected by rcu_read_lock()/rcu_read_unlock() is assumed to be a quiescent state.
Here is a slightly simplified version of the code:
static inline void rcu_read_lock(void)
{
...
r->flags = IN_RCU_READ_LOCK;
}static inline void rcu_read_unlock(void)
{
...
flags = xchg(&r->flags, 0);
if (flags & DO_RCU_COMPLETION)
rcu_quiescent(cpu);
...
}void rcu_poll_other_cpus(void)
{
for (cpu = first_cpu(cpu_online_map); cpu < NR_CPUS;
cpu = next_cpu(cpu, cpu_online_map)) {
r = &per_cpu(rcu_data, cpu);
flags = cmpxchg(&r->flags, IN_RCU_READ_LOCK,
IN_RCU_READ_LOCK | DO_RCU_COMPLETION);
if (!flags)
cpu_clear(cpu, rcu_state.rcu_cpu_mask);
}
if (cpus_empty(rcu_state.rcu_cpu_mask))
rcu_grace_period_complete();
}I move the work normally done by the local timer to the code which starts the RCU batch and to the rcu_read_unlock(). I added code to rcu_call to maintain a count of callbacks queued in the nxtlist and to start a new batch when the count exceeds a limit.
The patch borrows the krcud daemon which Dipankar proposed earlier this year. This is useful because I don't want to poll for the RCU batch completions. I may recognize that a grace period has completed in either the code which starts the batch called from call_rcu() or in rcu_quiescent() called from rcu_read_unlock(). Since I want to avoid timer based polling to recognize RCU batch completions, I have the processor which completes the grace period call rcu_wake_daemon() for each processor which has callbacks queued.
I expect the current patch to scale reasonably well. The code in rcu_poll_other_cpus() does a cmpxchg() to determine if each of the other cpus is in the read side critical section. If they are, it exchanges in a value which requests that the rcu_read_unlock() call rcu_quiescent() to help complete the grace period. Only processors caught in the read-side critical section need to participate in the rendezvous. Currently rcu_poll_other_cpus() clears the rcu_cpu_mask bits as it finds cpus not in the read-side critical section. This could be optimized to reduce cache line bouncing by saving this state in a local bitmap. To do this cleanly would require an atomic cpu_and operation. It would only need to be atomic on a word by word basis.
The fraction of time that a processor spends in the read-side critical section effects the overhead of starting a batch. In the case where rcu_poll_other_cpus() finds the other processor is not in the read-side critical section the overhead is a single cache miss. If the other processor is in the critical section, there will be 3-4 cache misses. One for the rcu_poll_other_cpus() read and cmpxchg(), one for rcu_read_unlock() to reread its rcu_data structure, and one to update the rcu_cpu_mask.
I just started testing and so far only on a dual processor Opteron. The early results are encouraging. I wrote a couple quick tests which exercise the dcache paths. One simply loops doing 60 million opens and closes. The second creates a file and then renames it 10 million times. There isn't a noticeable difference running these programs on the standard kernel or with my changes. Running the tests with Oprofile shows that rcu_read_lock() and rcu_read_unlock() combined used about 1% of the cpu.
Jim Houston - Concurrent Computer Corp.
--
include/linux/rcupdate.h +107 -78
@@ -40,7 +40,7 @@#include <linux/threads.h> #include <linux/percpu.h> #include <linux/cpumask.h> -#include <linux/seqlock.h> +#include <linux/interrupt.h> /** * struct rcu_head - callback structure for use with RCU
@@ -58,15 +58,18 @@ (ptr)->next =NULL ; (ptr)->func =NULL ; \ }while (0 ) - - -/* Global control variables for rcupdate callback mechanism. */ -struct rcu_ctrlblk { -long cur;/* Current batch number. */ -long completed;/* Number of the last completed batch */ -int next_pending;/* Is the next batch already waiting? */ -seqcount_t lock;/* For atomic reads of cur and next_pending. */ -} ____cacheline_maxaligned_in_smp; +/* +* The rcu_batch variable contains the current batch number +* and the following flags. The RCU_NEXT_PENDING bit requests that +* a new batch should start when the current batch completes. The +* RCU_COMPLETE bit indicates that the most recent batch has completed +* and RCU processing has stopped. +*/ +extern long rcu_batch; +#define RCU_BATCH_MASK (~3) +#define RCU_INCREMENT 4 +#define RCU_COMPLETE 2 +#define RCU_NEXT_PENDING 1/* Is batch a before batch b ? */ static inline int rcu_batch_before (long a,long b)
@@ -80,81 +83,85 @@return (a - b) >0 ; } +static inline int rcu_batch_complete (long batch) +{ +return (!rcu_batch_before ((rcu_batch&~RCU_NEXT_PENDING ), batch)); +} + +struct rcu_list { +struct rcu_head *head; +struct rcu_head **tail; +}; + +static inline void rcu_list_init (struct rcu_list *l) +{ + l->head =NULL ; + l->tail = &l->head; +} + +static inline void rcu_list_add (struct rcu_list *l,struct rcu_head *h) +{ + *l->tail = h; + l->tail = &h->next; +} + +static inline void rcu_list_move (struct rcu_list *to,struct rcu_list *from) +{ +if (from->head) { + *to->tail = from->head; + to->tail = from->tail; +rcu_list_init (from); + } +} +/* * Per-CPU data for Read-Copy UPdate. * nxtlist - new callbacks are added here * curlist - current batch for which quiescent cycle started if any */ struct rcu_data { -/* 1) quiescent state handling : */ -long quiescbatch;/* Batch # for grace period */ -long qsctr;/* User-mode/idle loop etc. */ -long last_qsctr;/* value of qsctr at beginning */ -/* of rcu grace period */ -int qs_pending;/* core waits for quiesc state */ - -/* 2) batch handling */ -long batch;/* Batch # for current RCU batch */ -struct rcu_head *nxtlist; -struct rcu_head **nxttail; -struct rcu_head *curlist; -struct rcu_head **curtail; -struct rcu_head *donelist; -struct rcu_head **donetail; -int cpu; +/* 1) batch handling */ +long batch;/* batch # for current RCU batch */ +unsigned long nxtbatch;/* batch # for next queue */ +struct rcu_list nxt; +struct rcu_list cur; +struct rcu_list done; +long nxtcount;/* number of callbacks queued */ +struct task_struct *krcud; + +/* 2) synchronization between rcu_read_lock and rcu_start_batch. */ +int nest_count;/* count of rcu_read_lock nesting */ +int flags;struct rcu_head barrier; }; -DECLARE_PER_CPU(struct rcu_data, rcu_data); -DECLARE_PER_CPU(struct rcu_data, rcu_bh_data); -extern struct rcu_ctrlblk rcu_ctrlblk; -extern struct rcu_ctrlblk rcu_bh_ctrlblk; -/* -* Increment the quiscent state counter. +* Flags values used to synchronize between rcu_read_lock/rcu_read_unlock +* and the rcu_start_batch. Only processors executing rcu_read_lock +* protected code get invited to the rendezvous. */ -static inline void rcu_qsctr_inc (int cpu) -{ -struct rcu_data *rdp = &per_cpu (rcu_data, cpu); - rdp->qsctr++; -} -static inline void rcu_bh_qsctr_inc (int cpu) -{ -struct rcu_data *rdp = &per_cpu (rcu_bh_data, cpu); - rdp->qsctr++; -} - -static inline int __rcu_pending (struct rcu_ctrlblk *rcp, -struct rcu_data *rdp) -{ -/* This cpu has pending rcu entries and the grace period -* for them has completed. -*/ -if (rdp->curlist && !rcu_batch_before (rcp->completed, rdp->batch)) -return 1 ; - -/* This cpu has no pending entries, but there are new entries */ -if (!rdp->curlist && rdp->nxtlist) -return 1 ; +#define IN_RCU_READ_LOCK 1 +#define DO_RCU_COMPLETION 2 -/* This cpu has finished callbacks to invoke */ -if (rdp->donelist) -return 1 ; +DECLARE_PER_CPU(struct rcu_data, rcu_data); -/* The rcu core waits for a quiescent state from the cpu */ -if (rdp->quiescbatch != rcp->cur || rdp->qs_pending) -return 1 ; +extern void rcu_init (void ); +extern void rcu_restart_cpu (int cpu); +extern void rcu_quiescent (int cpu); +extern void rcu_poll (int cpu); -/* nothing to do */ -return 0 ; -} - -static inline int rcu_pending (int cpu) -{ -return __rcu_pending (&rcu_ctrlblk, &per_cpu (rcu_data, cpu)) || -__rcu_pending (&rcu_bh_ctrlblk, &per_cpu (rcu_bh_data, cpu)); -} +#undef INLINE +#if INLINE_RCU_READ_LOCK +#define INLINE static inline +#else +#ifdef RCUPDATE_C +#define INLINE +#endif +extern void rcu_read_lock (void ); +extern void rcu_read_unlock (void ); +#endif +#ifdef INLINE /** * rcu_read_lock - mark the beginning of an RCU read-side critical section. *
@@ -184,14 +191,38 @@* * It is illegal to block while in an RCU read-side critical section. */ -#define rcu_read_lock () preempt_disable() +INLINE void rcu_read_lock(void) +{ +struct rcu_data *r; + +preempt_disable (); + r = &per_cpu (rcu_data,smp_processor_id ()); +if (r->nest_count++ ==0 ) + r->flags =IN_RCU_READ_LOCK ; +}/** * rcu_read_unlock - marks the end of an RCU read-side critical section. +* Check if a RCU batch was started while we were in the critical +* section. If so, call rcu_quiescent() join the rendezvous. * * See rcu_read_lock() for more information. */ -#define rcu_read_unlock () preempt_enable() +INLINE void rcu_read_unlock(void) +{ +struct rcu_data *r; +int cpu, flags; + + cpu =smp_processor_id (); + r = &per_cpu (rcu_data, cpu); +if (--r->nest_count ==0 ) { + flags =xchg (&r->flags,0 ); +if (flags &DO_RCU_COMPLETION ) +rcu_quiescent (cpu); + } +preempt_enable (); +} +#endif /* * So where is rcu_write_lock()? It does not exist, as there is no
@@ -213,15 +244,17 @@* disabling softirqs. Read-side critical sections in interrupt context * can use just rcu_read_lock(). * +* Hack alert. I'm not sure if I understand the reason this interface +* is needed and if it is still needed with my implementation of RCU. */ -#define rcu_read_lock_bh () local_bh_disable() +#define rcu_read_lock_bh () rcu_read_lock()/* * rcu_read_unlock_bh - marks the end of a softirq-only RCU critical section * * See rcu_read_lock_bh() for more information. */ -#define rcu_read_unlock_bh () local_bh_enable() +#define rcu_read_unlock_bh () rcu_read_unlock()/** * rcu_dereference - fetch an RCU-protected pointer in an
@@ -239,10 +272,6 @@ (_________p1); \ }) -extern void rcu_init (void ); -extern void rcu_check_callbacks (int cpu,int user); -extern void rcu_restart_cpu (int cpu); -/* Exported interfaces */ extern void FASTCALL (call_rcu (struct rcu_head *head,void (*func)(struct rcu_head *head)));
init/main.c +3 -1
@@ -510,10 +510,10 @@ __stop___param - __start___param, &unknown_bootoption);sort_main_extable (); -rcu_init ();init_IRQ ();pidhash_init ();init_timers (); +rcu_init ();softirq_init ();time_init ();
@@ -648,12 +648,14 @@static void do_pre_smp_initcalls (void ) { +extern int spawn_krcud (void );extern int spawn_ksoftirqd (void );#ifdef CONFIG_SMP extern int migration_init (void );migration_init ();#endif +spawn_krcud ();spawn_ksoftirqd (); }
Binary files linux-2.6.8.1-mm4.orig/kernel/config_data.gz and linux-2.6.8.1-mm4/kernel/config_data.gz differ
kernel/rcupdate.c +376 -261
771 lines not on this page · raw message
kernel/sched.c +6 -0
@@ -36,7 +36,9 @@#include <linux/delay.h> #include <linux/smp.h> #include <linux/timer.h> +#if 0 #include <linux/rcupdate.h> +#endif #include <linux/cpu.h> #include <linux/cpuset.h> #include <linux/percpu.h>
@@ -2270,8 +2272,10 @@ rq->timestamp_last_tick =clock_us ();#endif +#if 0 if (rcu_pending (cpu))rcu_check_callbacks (cpu, user_ticks); +#endif /* note: this timer irq context must be accounted for as well */ if (hardirq_count () -HARDIRQ_OFFSET ) {
@@ -2583,7 +2587,9 @@ switch_tasks:prefetch (next);clear_tsk_need_resched (prev); +#if 0 rcu_qsctr_inc (task_cpu (prev)); +#endif sched_info_switch (prev, next);if (likely (prev != next)) {
kernel/softirq.c +2 -0
@@ -93,7 +93,9 @@ do { if (pending & 1) { h->action(h); +#if 0 rcu_bh_qsctr_inc(cpu); +#endif } h++; pending >>= 1;
Quoted 4 lines · Jim Houston
Hi Everyone,
I sent this to the LKML on Friday, but it seems to have been lost so I'm sending it again.
Hello, Jim,
I was wondering what happened when my reply showed up without any connection to your original. Here is my reply again, just to get thing tied in.
Quoted 9 lines
The attached patch against linux-2.6.8.1-mm4 is an experimental implementation of RCU.
It uses an active synchronization between the
rcu_read_lock(),rcu_read_unlock(), and the code which starts a new RCU batch. A RCU batch can be started at an arbitrary point, and it will complete without waiting for a timer driven poll. This should help avoid large batches and their adverse cache and latency effects.
Quoted 7 lines
I did this work because Concurrent encourages its customers to isolate critical realtime processes to their own cpu and shield them from other processes and interrupts. This includes disabling the local timer interrupt. The current RCU code relies on the local timer to recognize quiescent states. If it is disabled on any cpu, RCU callbacks are never called and the system bleeds memory and hangs on calls to
synchronize_kernel().
Interesting approach, but I have some concern...
Are these critical realtime processes user-mode only, or do they also execute kernel code? If they are user-mode only, a much more straightforward approach might be to have RCU pretend that they do not exist.
This approach would have the added benefit of keeping rcu_read_unlock() atomic-instruction free. In some cases, the overhead of the atomic exchange would overwhelm that of the read-side RCU critical section.
Taking this further, if the realtime CPUs are not allowed to execute in the kernel at all, you can avoid overhead from smp_call_function() and the like -- and avoid confusing those parts of the kernel that expect to be able to send IPIs and the like to the realtime CPU (or do you leave IPIs enabled on the realtime CPU?).
Thanx, Paul
[ . . . ]