Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1284381

[PATCH tip/core/rcu 2/4] list: Use READ_ONCE() when testing for empty lists

From "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
Newsgroups linux.kernel
Subject [PATCH tip/core/rcu 2/4] list: Use READ_ONCE() when testing for empty lists
Date 2015-12-05 01:20 +0100
Message-ID <qC8K0-1nw-37@gated-at.bofh.it> (permalink)
References <qC8JY-1nw-9@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


Most of the list-empty-check macros (list_empty(), hlist_empty(),
hlist_bl_empty(), hlist_nulls_empty(), and hlist_nulls_empty()) use
an unadorned load to check the list header.  Given that these macros
are sometimes invoked without the protection of a lock, this is
not sufficient.  This commit therefore adds READ_ONCE() calls to
them.  This commit does not touch llist_empty() because it already
has the needed ACCESS_ONCE().

Reported-by: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
---
 include/linux/list.h       | 4 ++--
 include/linux/list_bl.h    | 2 +-
 include/linux/list_nulls.h | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/include/linux/list.h b/include/linux/list.h
index d7e31fe398b3..06c2d887a918 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -186,7 +186,7 @@ static inline int list_is_last(const struct list_head *list,
  */
 static inline int list_empty(const struct list_head *head)
 {
-	return head->next == head;
+	return READ_ONCE(head->next) == head;
 }
 
 /**
@@ -608,7 +608,7 @@ static inline int hlist_unhashed(const struct hlist_node *h)
 
 static inline int hlist_empty(const struct hlist_head *h)
 {
-	return !h->first;
+	return !READ_ONCE(h->first);
 }
 
 static inline void __hlist_del(struct hlist_node *n)
diff --git a/include/linux/list_bl.h b/include/linux/list_bl.h
index 8132214e8efd..ee7229a6c06a 100644
--- a/include/linux/list_bl.h
+++ b/include/linux/list_bl.h
@@ -70,7 +70,7 @@ static inline void hlist_bl_set_first(struct hlist_bl_head *h,
 
 static inline int hlist_bl_empty(const struct hlist_bl_head *h)
 {
-	return !((unsigned long)h->first & ~LIST_BL_LOCKMASK);
+	return !((unsigned long)READ_ONCE(h->first) & ~LIST_BL_LOCKMASK);
 }
 
 static inline void hlist_bl_add_head(struct hlist_bl_node *n,
diff --git a/include/linux/list_nulls.h b/include/linux/list_nulls.h
index 444d2b1313bd..b01fe1009084 100644
--- a/include/linux/list_nulls.h
+++ b/include/linux/list_nulls.h
@@ -57,7 +57,7 @@ static inline int hlist_nulls_unhashed(const struct hlist_nulls_node *h)
 
 static inline int hlist_nulls_empty(const struct hlist_nulls_head *h)
 {
-	return is_a_nulls(h->first);
+	return is_a_nulls(READ_ONCE(h->first));
 }
 
 static inline void hlist_nulls_add_head(struct hlist_nulls_node *n,
-- 
2.5.2

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

[PATCH tip/core/rcu 0/4] linked-list updates for 4.5 "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> - 2015-12-05 01:20 +0100
  [PATCH tip/core/rcu 4/4] list: Use WRITE_ONCE() when initializing list_head structures "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> - 2015-12-05 01:20 +0100
  [PATCH tip/core/rcu 1/4] list: Use WRITE_ONCE() when adding to lists and hlists "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> - 2015-12-05 01:20 +0100
  [PATCH tip/core/rcu 3/4] list: Introduces generic list_splice_tail_init_rcu() "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> - 2015-12-05 01:20 +0100
  [PATCH tip/core/rcu 2/4] list: Use READ_ONCE() when testing for empty lists "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> - 2015-12-05 01:20 +0100

csiph-web