From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754700AbbE1UgC (ORCPT ); Thu, 28 May 2015 16:36:02 -0400 Received: from mail-ig0-f175.google.com ([209.85.213.175]:36944 "EHLO mail-ig0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753372AbbE1Ufy (ORCPT ); Thu, 28 May 2015 16:35:54 -0400 From: Dan Streetman To: Andrew Morton , "Paul E. McKenney" , Josh Triplett Cc: Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , linux-kernel@vger.kernel.org, Dan Streetman Subject: [PATCH 1/2] rcu: introduce list_last_or_null_rcu Date: Thu, 28 May 2015 16:35:27 -0400 Message-Id: <1432845328-27932-1-git-send-email-ddstreet@ieee.org> X-Mailer: git-send-email 2.1.0 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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