seq_file: add RCU versions of new hlist/list iterators (v3)
authorstephen hemminger <shemminger@vyatta.com>
Mon, 22 Feb 2010 07:57:17 +0000 (07:57 +0000)
committerDavid S. Miller <davem@davemloft.net>
Mon, 22 Feb 2010 23:45:54 +0000 (15:45 -0800)
Many usages of seq_file use RCU protected lists, so non RCU
iterators will not work safely.

Signed-off-by: Stephen Hemminger <shemminger@vyatta.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
fs/seq_file.c
include/linux/rculist.h
include/linux/seq_file.h

index f65b16f02da3de0f0502d3bf5494e776d85d0de0..5afd554efad32cbcb620838178f84ec0b5debc9b 100644 (file)
@@ -750,3 +750,74 @@ struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
                return node->next;
 }
 EXPORT_SYMBOL(seq_hlist_next);
+
+/**
+ * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
+ * @head: the head of the hlist
+ * @pos:  the start position of the sequence
+ *
+ * Called at seq_file->op->start().
+ *
+ * This list-traversal primitive may safely run concurrently with
+ * the _rcu list-mutation primitives such as hlist_add_head_rcu()
+ * as long as the traversal is guarded by rcu_read_lock().
+ */
+struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
+                                      loff_t pos)
+{
+       struct hlist_node *node;
+
+       __hlist_for_each_rcu(node, head)
+               if (pos-- == 0)
+                       return node;
+       return NULL;
+}
+EXPORT_SYMBOL(seq_hlist_start_rcu);
+
+/**
+ * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
+ * @head: the head of the hlist
+ * @pos:  the start position of the sequence
+ *
+ * Called at seq_file->op->start(). Call this function if you want to
+ * print a header at the top of the output.
+ *
+ * This list-traversal primitive may safely run concurrently with
+ * the _rcu list-mutation primitives such as hlist_add_head_rcu()
+ * as long as the traversal is guarded by rcu_read_lock().
+ */
+struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
+                                           loff_t pos)
+{
+       if (!pos)
+               return SEQ_START_TOKEN;
+
+       return seq_hlist_start_rcu(head, pos - 1);
+}
+EXPORT_SYMBOL(seq_hlist_start_head_rcu);
+
+/**
+ * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
+ * @v:    the current iterator
+ * @head: the head of the hlist
+ * @pos:  the current posision
+ *
+ * Called at seq_file->op->next().
+ *
+ * This list-traversal primitive may safely run concurrently with
+ * the _rcu list-mutation primitives such as hlist_add_head_rcu()
+ * as long as the traversal is guarded by rcu_read_lock().
+ */
+struct hlist_node *seq_hlist_next_rcu(void *v,
+                                     struct hlist_head *head,
+                                     loff_t *ppos)
+{
+       struct hlist_node *node = v;
+
+       ++*ppos;
+       if (v == SEQ_START_TOKEN)
+               return rcu_dereference(head->first);
+       else
+               return rcu_dereference(node->next);
+}
+EXPORT_SYMBOL(seq_hlist_next_rcu);
index 1bf0f708c4fcd61950e8b6e1ef8e504588317257..701fe9cb552a646a2a7b837816fe45d247c15717 100644 (file)
@@ -406,6 +406,11 @@ static inline void hlist_add_after_rcu(struct hlist_node *prev,
                n->next->pprev = &n->next;
 }
 
+#define __hlist_for_each_rcu(pos, head)                        \
+       for (pos = rcu_dereference((head)->first);      \
+            pos && ({ prefetch(pos->next); 1; });      \
+            pos = rcu_dereference(pos->next))
+
 /**
  * hlist_for_each_entry_rcu - iterate over rcu list of given type
  * @tpos:      the type * to use as a loop cursor.
index c95bcdc18f4c1ba093c7a3a5a364b1f3171efab7..03c0232b4169dfccf378be1420fdcec29b9c134b 100644 (file)
@@ -140,10 +140,17 @@ extern struct list_head *seq_list_next(void *v, struct list_head *head,
  */
 
 extern struct hlist_node *seq_hlist_start(struct hlist_head *head,
-               loff_t pos);
+                                         loff_t pos);
 extern struct hlist_node *seq_hlist_start_head(struct hlist_head *head,
-               loff_t pos);
+                                              loff_t pos);
 extern struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
-               loff_t *ppos);
-
+                                        loff_t *ppos);
+
+extern struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
+                                             loff_t pos);
+extern struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
+                                                  loff_t pos);
+extern struct hlist_node *seq_hlist_next_rcu(void *v,
+                                                  struct hlist_head *head,
+                                                  loff_t *ppos);
 #endif