All of lore.kernel.org
 help / color / mirror / Atom feed
* [patch 014/102] llist: introduce llist_entry_safe()
@ 2016-10-11 20:51 akpm
       [not found] ` <CA+55aFxr2uZADh--vtLYXjcLjNGO5t4jmTWEVZWbRuaJwiocug@mail.gmail.com>
  0 siblings, 1 reply; 19+ messages in thread
From: akpm @ 2016-10-11 20:51 UTC (permalink / raw)
  To: torvalds, mm-commits, akpm, glider, dvyukov, edumazet, kcc, mingo

From: Alexander Potapenko <glider@google.com>
Subject: llist: introduce llist_entry_safe()

Currently llist_for_each_entry() and llist_for_each_entry_safe() iterate
until &pos->member != NULL.  But when building the kernel with Clang, the
compiler assumes &pos->member cannot be NULL if the member's offset is
greater than 0.  Therefore the loop condition is always true, and the
loops become infinite.

To work around this, introduce llist_entry_safe(), which returns NULL for
NULL pointers, and additionally check that pos is not NULL in the list
iterators before dereferencing it.

Link: http://lkml.kernel.org/r/1474636978-41435-3-git-send-email-glider@google.com
Signed-off-by: Alexander Potapenko <glider@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Kostya Serebryany <kcc@google.com>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 include/linux/llist.h |   26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff -puN include/linux/llist.h~llist-introduce-llist_entry_safe include/linux/llist.h
--- a/include/linux/llist.h~llist-introduce-llist_entry_safe
+++ a/include/linux/llist.h
@@ -88,6 +88,16 @@ static inline void init_llist_head(struc
 	container_of(ptr, type, member)
 
 /**
+ * llist_entry_safe - get the struct of this entry without overflowing
+ * @ptr:	the &struct llist_node pointer.
+ * @type:	the type of the struct this is embedded in.
+ * @member:	the name of the llist_node within the struct.
+ */
+#define llist_entry_safe(ptr, type, member)		\
+	container_of_safe(ptr, type, member)
+
+
+/**
  * llist_for_each - iterate over some deleted entries of a lock-less list
  * @pos:	the &struct llist_node to use as a loop cursor
  * @node:	the first entry of deleted list entries
@@ -120,9 +130,10 @@ static inline void init_llist_head(struc
  * reverse the order by yourself before traversing.
  */
 #define llist_for_each_entry(pos, node, member)				\
-	for ((pos) = llist_entry((node), typeof(*(pos)), member);	\
-	     &(pos)->member != NULL;					\
-	     (pos) = llist_entry((pos)->member.next, typeof(*(pos)), member))
+	for ((pos) = llist_entry_safe((node), typeof(*(pos)), member);	\
+	     pos != NULL && &(pos)->member != NULL;			\
+	     (pos) = llist_entry_safe((pos)->member.next, \
+					typeof(*(pos)), member))
 
 /**
  * llist_for_each_entry_safe - iterate over some deleted entries of lock-less list of given type
@@ -141,10 +152,11 @@ static inline void init_llist_head(struc
  * you want to traverse from the oldest to the newest, you must
  * reverse the order by yourself before traversing.
  */
-#define llist_for_each_entry_safe(pos, n, node, member)			       \
-	for (pos = llist_entry((node), typeof(*pos), member);		       \
-	     &pos->member != NULL &&					       \
-	        (n = llist_entry(pos->member.next, typeof(*n), member), true); \
+#define llist_for_each_entry_safe(pos, n, node, member)			     \
+	for (pos = llist_entry_safe((node), typeof(*pos), member);	     \
+	     pos != NULL && &pos->member != NULL &&			     \
+		(n = llist_entry_safe(pos->member.next, typeof(*n), member), \
+		 true); \
 	     pos = n)
 
 /**
_

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

end of thread, other threads:[~2019-10-18 10:10 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-11 20:51 [patch 014/102] llist: introduce llist_entry_safe() akpm
     [not found] ` <CA+55aFxr2uZADh--vtLYXjcLjNGO5t4jmTWEVZWbRuaJwiocug@mail.gmail.com>
     [not found]   ` <CA+55aFxQRf+U0z6mrAd5QQLWgB2A_mRjY7g9vpZHCSuyjrdhxQ@mail.gmail.com>
2019-10-16 22:23     ` Linus Torvalds
2019-10-16 22:40       ` Linus Torvalds
2019-10-16 23:13         ` Kirill A. Shutemov
2019-10-16 23:11       ` Kirill A. Shutemov
2019-10-16 23:29         ` Linus Torvalds
2019-10-17  0:16           ` Kirill A. Shutemov
2019-10-17  5:20             ` Masahiro Yamada
2019-10-17  8:50               ` Arnd Bergmann
2019-10-17 15:17               ` Linus Torvalds
2019-10-17 12:53           ` Arnd Bergmann
2019-10-17 12:56             ` [PATCH] [RFC, EXPERIMENTAL] allow building with --std=gnu99 Arnd Bergmann
2019-10-17 15:05               ` Linus Torvalds
2019-10-17 15:37                 ` Kirill A. Shutemov
2019-10-17 15:47                   ` Arnd Bergmann
2019-10-17 15:56                     ` Linus Torvalds
2019-10-17 16:16                       ` Kirill A. Shutemov
2019-10-18  7:56                         ` Arnd Bergmann
2019-10-18 10:10                           ` Kirill A. Shutemov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.