
From: Hans Reiser <reiser@namesys.com>

This patch is by Dipankar Sarma <dipankar@in.ibm.com>. His description:

  This patch introduces a new interface - rcu_barrier() which waits until
  all the RCUs queued until this call have been completed.

Reiser4 needs this patch, because we do more than just freeing memory object
in our RCU callback: we also remove it from the list hanging off super-block. 
This means, that before freeing reiser4-specific portion of super-block
(during umount) we have to wait until all pending RCU callbacks are executed.

The only change of reiser4 made to the original patch, is exporting of
rcu_barrier().

Signed-off-by: Andrew Morton <akpm@osdl.org>
---

 25-akpm/include/linux/rcupdate.h |    2 +
 25-akpm/kernel/rcupdate.c        |   41 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)

diff -puN include/linux/rcupdate.h~reiser4-rcu-barrier include/linux/rcupdate.h
--- 25/include/linux/rcupdate.h~reiser4-rcu-barrier	2004-11-30 01:24:02.541259392 -0800
+++ 25-akpm/include/linux/rcupdate.h	2004-11-30 01:24:02.546258632 -0800
@@ -99,6 +99,7 @@ struct rcu_data {
 	struct rcu_head *donelist;
 	struct rcu_head **donetail;
 	int cpu;
+	struct rcu_head barrier;
 };
 
 DECLARE_PER_CPU(struct rcu_data, rcu_data);
@@ -263,6 +264,7 @@ extern void FASTCALL(call_rcu(struct rcu
 extern void FASTCALL(call_rcu_bh(struct rcu_head *head,
 				void (*func)(struct rcu_head *head)));
 extern void synchronize_kernel(void);
+extern void rcu_barrier(void);
 
 #endif /* __KERNEL__ */
 #endif /* __LINUX_RCUPDATE_H */
diff -puN kernel/rcupdate.c~reiser4-rcu-barrier kernel/rcupdate.c
--- 25/kernel/rcupdate.c~reiser4-rcu-barrier	2004-11-30 01:24:02.542259240 -0800
+++ 25-akpm/kernel/rcupdate.c	2004-11-30 01:24:02.547258480 -0800
@@ -72,6 +72,10 @@ DEFINE_PER_CPU(struct rcu_data, rcu_bh_d
 static DEFINE_PER_CPU(struct tasklet_struct, rcu_tasklet) = {NULL};
 static int maxbatch = 10;
 
+static atomic_t rcu_barrier_cpu_count;
+static struct semaphore rcu_barrier_sema;
+static struct completion rcu_barrier_completion;
+
 /**
  * call_rcu - Queue an RCU callback for invocation after a grace period.
  * @head: structure to be used for queueing the RCU updates.
@@ -129,6 +133,42 @@ void fastcall call_rcu_bh(struct rcu_hea
 	local_irq_restore(flags);
 }
 
+static void rcu_barrier_callback(struct rcu_head *notused)
+{
+	if (atomic_dec_and_test(&rcu_barrier_cpu_count))
+		complete(&rcu_barrier_completion);
+}
+
+/*
+ * Called with preemption disabled, and from cross-cpu IRQ context.
+ */
+static void rcu_barrier_func(void *notused)
+{
+	int cpu = smp_processor_id();
+	struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
+	struct rcu_head *head;
+
+	head = &rdp->barrier;
+	atomic_inc(&rcu_barrier_cpu_count);
+	call_rcu(head, rcu_barrier_callback);
+}
+
+/**
+ * rcu_barrier - Wait until all the in-flight RCUs are complete.
+ */
+void rcu_barrier(void)
+{
+	BUG_ON(in_interrupt());
+	/* Take cpucontrol semaphore to protect against CPU hotplug */
+	down(&rcu_barrier_sema);
+	init_completion(&rcu_barrier_completion);
+	atomic_set(&rcu_barrier_cpu_count, 0);
+	on_each_cpu(rcu_barrier_func, NULL, 0, 1);
+	wait_for_completion(&rcu_barrier_completion);
+	up(&rcu_barrier_sema);
+}
+EXPORT_SYMBOL(rcu_barrier);
+
 /*
  * Invoke the completed RCU callbacks. They are expected to be in
  * a per-cpu list.
@@ -419,6 +459,7 @@ static struct notifier_block __devinitda
  */
 void __init rcu_init(void)
 {
+	sema_init(&rcu_barrier_sema, 1);
 	rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
 			(void *)(long)smp_processor_id());
 	/* Register notifier for non-boot CPUs */
_
