public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] rcu: introduce list_last_or_null_rcu
@ 2015-05-28 20:35 Dan Streetman
  2015-05-28 20:36 ` [PATCH 2/2] list: introduce list_last_entry_or_null Dan Streetman
                   ` (2 more replies)
  0 siblings, 3 replies; 23+ messages in thread
From: Dan Streetman @ 2015-05-28 20:35 UTC (permalink / raw)
  To: Andrew Morton, Paul E. McKenney, Josh Triplett
  Cc: Steven Rostedt, Mathieu Desnoyers, Lai Jiangshan, linux-kernel,
	Dan Streetman

Add list_last_or_null_rcu(), to simplify getting the last entry from a
rcu-protected list.  The standard list_last_entry() can't be used as it
is not rcu-protected; the list may be modified concurrently.  And the
->prev pointer can't be used, as only the ->next pointers are protected
by rcu.

This simply iterates forward through the entire list, to get to the last
entry.  If the list is empty, it returns NULL.

Signed-off-by: Dan Streetman <ddstreet@ieee.org>
---
 include/linux/rculist.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/include/linux/rculist.h b/include/linux/rculist.h
index a18b16f..954fde5 100644
--- a/include/linux/rculist.h
+++ b/include/linux/rculist.h
@@ -293,6 +293,27 @@ static inline void list_splice_init_rcu(struct list_head *list,
 })
 
 /**
+ * list_last_or_null_rcu - get the last element from a list
+ * @ptr:        the list head to take the element from.
+ * @type:       the type of the struct this is embedded in.
+ * @member:     the name of the list_head within the struct.
+ *
+ * Note that if the list is empty, it returns NULL.
+ *
+ * This primitive may safely run concurrently with the _rcu list-mutation
+ * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
+ */
+#define list_last_or_null_rcu(ptr, type, member) \
+({ \
+	struct list_head *__ptr = (ptr); \
+	struct list_head *__last = __ptr; \
+	struct list_head *__entry = list_next_rcu(__ptr); \
+	for (; __entry != __ptr; __entry = list_next_rcu(__entry)) \
+		__last = __entry; \
+	likely(__ptr != __last) ? list_entry_rcu(__last, type, member) : NULL; \
+})
+
+/**
  * list_for_each_entry_rcu	-	iterate over rcu list of given type
  * @pos:	the type * to use as a loop cursor.
  * @head:	the head for your list.
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2015-06-01 22:12 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-05-28 20:35 [PATCH 1/2] rcu: introduce list_last_or_null_rcu Dan Streetman
2015-05-28 20:36 ` [PATCH 2/2] list: introduce list_last_entry_or_null Dan Streetman
2015-05-28 20:39 ` [PATCH 1/2] rcu: introduce list_last_or_null_rcu josh
2015-05-28 20:42   ` Dan Streetman
2015-05-28 20:44     ` Dan Streetman
2015-05-28 21:06       ` Paul E. McKenney
2015-05-28 21:10       ` josh
2015-05-28 21:05     ` Paul E. McKenney
2015-05-28 21:14       ` Dan Streetman
2015-05-28 21:17         ` Paul E. McKenney
2015-05-28 21:19           ` Dan Streetman
2015-05-28 21:28             ` Steven Rostedt
2015-05-28 21:30               ` Dan Streetman
2015-05-28 21:33                 ` Dan Streetman
2015-05-28 21:05 ` Steven Rostedt
2015-05-28 21:12   ` Dan Streetman
2015-05-28 21:16     ` Paul E. McKenney
2015-05-28 21:24       ` Dan Streetman
2015-05-28 22:29         ` Paul E. McKenney
2015-05-28 23:22           ` Dan Streetman
2015-05-29 13:40           ` Dan Streetman
2015-06-01 18:17             ` Paul E. McKenney
2015-06-01 22:11               ` Dan Streetman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox