Blame SOURCES/0179-ccan-list-Import-latest-list-helpers.patch

26ccd9
From aaf1059e1c96b8052851030d4db971708e98e4d8 Mon Sep 17 00:00:00 2001
26ccd9
From: Dan Williams <dan.j.williams@intel.com>
26ccd9
Date: Thu, 14 Jul 2022 10:02:15 -0700
26ccd9
Subject: [PATCH 179/217] ccan/list: Import latest list helpers
26ccd9
26ccd9
Pick up the definition of list_add_{before,after} and other updates from
26ccd9
ccan at commit 52b86922f846 ("ccan/base64: fix GCC warning.").
26ccd9
26ccd9
Link: https://lore.kernel.org/r/165781813572.1555691.15909358688944168922.stgit@dwillia2-xfh.jf.intel.com
26ccd9
Reported-by: Ira Weiny <ira.weiny@intel.com>
26ccd9
Reviewed-by: Ira Weiny <ira.weiny@intel.com>
26ccd9
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
26ccd9
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
---
26ccd9
 ccan/list/list.h   | 258 ++++++++++++++++++++++++++++++++++++++-------
26ccd9
 ndctl/lib/inject.c |   1 -
26ccd9
 util/list.h        |  40 -------
26ccd9
 3 files changed, 222 insertions(+), 77 deletions(-)
26ccd9
 delete mode 100644 util/list.h
26ccd9
26ccd9
diff --git a/ccan/list/list.h b/ccan/list/list.h
26ccd9
index 3ebd1b2..15f5fb7 100644
26ccd9
--- a/ccan/list/list.h
26ccd9
+++ b/ccan/list/list.h
26ccd9
@@ -95,8 +95,8 @@ struct list_node *list_check_node(const struct list_node *n,
26ccd9
 #define list_debug(h, loc) list_check((h), loc)
26ccd9
 #define list_debug_node(n, loc) list_check_node((n), loc)
26ccd9
 #else
26ccd9
-#define list_debug(h, loc) (h)
26ccd9
-#define list_debug_node(n, loc) (n)
26ccd9
+#define list_debug(h, loc) ((void)loc, h)
26ccd9
+#define list_debug_node(n, loc) ((void)loc, n)
26ccd9
 #endif
26ccd9
 
26ccd9
 /**
26ccd9
@@ -111,7 +111,7 @@ struct list_node *list_check_node(const struct list_node *n,
26ccd9
  * Example:
26ccd9
  *	static struct list_head my_list = LIST_HEAD_INIT(my_list);
26ccd9
  */
26ccd9
-#define LIST_HEAD_INIT(name) { { &name.n, &name.n } }
26ccd9
+#define LIST_HEAD_INIT(name) { { &(name).n, &(name).n } }
26ccd9
 
26ccd9
 /**
26ccd9
  * LIST_HEAD - define and initialize an empty list_head
26ccd9
@@ -145,6 +145,48 @@ static inline void list_head_init(struct list_head *h)
26ccd9
 	h->n.next = h->n.prev = &h->n;
26ccd9
 }
26ccd9
 
26ccd9
+/**
26ccd9
+ * list_node_init - initialize a list_node
26ccd9
+ * @n: the list_node to link to itself.
26ccd9
+ *
26ccd9
+ * You don't need to use this normally!  But it lets you list_del(@n)
26ccd9
+ * safely.
26ccd9
+ */
26ccd9
+static inline void list_node_init(struct list_node *n)
26ccd9
+{
26ccd9
+	n->next = n->prev = n;
26ccd9
+}
26ccd9
+
26ccd9
+/**
26ccd9
+ * list_add_after - add an entry after an existing node in a linked list
26ccd9
+ * @h: the list_head to add the node to (for debugging)
26ccd9
+ * @p: the existing list_node to add the node after
26ccd9
+ * @n: the new list_node to add to the list.
26ccd9
+ *
26ccd9
+ * The existing list_node must already be a member of the list.
26ccd9
+ * The new list_node does not need to be initialized; it will be overwritten.
26ccd9
+ *
26ccd9
+ * Example:
26ccd9
+ *	struct child c1, c2, c3;
26ccd9
+ *	LIST_HEAD(h);
26ccd9
+ *
26ccd9
+ *	list_add_tail(&h, &c1.list);
26ccd9
+ *	list_add_tail(&h, &c3.list);
26ccd9
+ *	list_add_after(&h, &c1.list, &c2.list);
26ccd9
+ */
26ccd9
+#define list_add_after(h, p, n) list_add_after_(h, p, n, LIST_LOC)
26ccd9
+static inline void list_add_after_(struct list_head *h,
26ccd9
+				   struct list_node *p,
26ccd9
+				   struct list_node *n,
26ccd9
+				   const char *abortstr)
26ccd9
+{
26ccd9
+	n->next = p->next;
26ccd9
+	n->prev = p;
26ccd9
+	p->next->prev = n;
26ccd9
+	p->next = n;
26ccd9
+	(void)list_debug(h, abortstr);
26ccd9
+}
26ccd9
+
26ccd9
 /**
26ccd9
  * list_add - add an entry at the start of a linked list.
26ccd9
  * @h: the list_head to add the node to
26ccd9
@@ -163,10 +205,34 @@ static inline void list_add_(struct list_head *h,
26ccd9
 			     struct list_node *n,
26ccd9
 			     const char *abortstr)
26ccd9
 {
26ccd9
-	n->next = h->n.next;
26ccd9
-	n->prev = &h->n;
26ccd9
-	h->n.next->prev = n;
26ccd9
-	h->n.next = n;
26ccd9
+	list_add_after_(h, &h->n, n, abortstr);
26ccd9
+}
26ccd9
+
26ccd9
+/**
26ccd9
+ * list_add_before - add an entry before an existing node in a linked list
26ccd9
+ * @h: the list_head to add the node to (for debugging)
26ccd9
+ * @p: the existing list_node to add the node before
26ccd9
+ * @n: the new list_node to add to the list.
26ccd9
+ *
26ccd9
+ * The existing list_node must already be a member of the list.
26ccd9
+ * The new list_node does not need to be initialized; it will be overwritten.
26ccd9
+ *
26ccd9
+ * Example:
26ccd9
+ *	list_head_init(&h);
26ccd9
+ *	list_add_tail(&h, &c1.list);
26ccd9
+ *	list_add_tail(&h, &c3.list);
26ccd9
+ *	list_add_before(&h, &c3.list, &c2.list);
26ccd9
+ */
26ccd9
+#define list_add_before(h, p, n) list_add_before_(h, p, n, LIST_LOC)
26ccd9
+static inline void list_add_before_(struct list_head *h,
26ccd9
+				    struct list_node *p,
26ccd9
+				    struct list_node *n,
26ccd9
+				    const char *abortstr)
26ccd9
+{
26ccd9
+	n->next = p;
26ccd9
+	n->prev = p->prev;
26ccd9
+	p->prev->next = n;
26ccd9
+	p->prev = n;
26ccd9
 	(void)list_debug(h, abortstr);
26ccd9
 }
26ccd9
 
26ccd9
@@ -185,11 +251,7 @@ static inline void list_add_tail_(struct list_head *h,
26ccd9
 				  struct list_node *n,
26ccd9
 				  const char *abortstr)
26ccd9
 {
26ccd9
-	n->next = &h->n;
26ccd9
-	n->prev = h->n.prev;
26ccd9
-	h->n.prev->next = n;
26ccd9
-	h->n.prev = n;
26ccd9
-	(void)list_debug(h, abortstr);
26ccd9
+	list_add_before_(h, &h->n, n, abortstr);
26ccd9
 }
26ccd9
 
26ccd9
 /**
26ccd9
@@ -229,6 +291,21 @@ static inline bool list_empty_nodebug(const struct list_head *h)
26ccd9
 }
26ccd9
 #endif
26ccd9
 
26ccd9
+/**
26ccd9
+ * list_empty_nocheck - is a list empty?
26ccd9
+ * @h: the list_head
26ccd9
+ *
26ccd9
+ * If the list is empty, returns true. This doesn't perform any
26ccd9
+ * debug check for list consistency, so it can be called without
26ccd9
+ * locks, racing with the list being modified. This is ok for
26ccd9
+ * checks where an incorrect result is not an issue (optimized
26ccd9
+ * bail out path for example).
26ccd9
+ */
26ccd9
+static inline bool list_empty_nocheck(const struct list_head *h)
26ccd9
+{
26ccd9
+	return h->n.next == &h->n;
26ccd9
+}
26ccd9
+
26ccd9
 /**
26ccd9
  * list_del - delete an entry from an (unknown) linked list.
26ccd9
  * @n: the list_node to delete from the list.
26ccd9
@@ -237,7 +314,7 @@ static inline bool list_empty_nodebug(const struct list_head *h)
26ccd9
  * another list, but not deleted again.
26ccd9
  *
26ccd9
  * See also:
26ccd9
- *	list_del_from()
26ccd9
+ *	list_del_from(), list_del_init()
26ccd9
  *
26ccd9
  * Example:
26ccd9
  *	list_del(&child->list);
26ccd9
@@ -255,6 +332,27 @@ static inline void list_del_(struct list_node *n, const char* abortstr)
26ccd9
 #endif
26ccd9
 }
26ccd9
 
26ccd9
+/**
26ccd9
+ * list_del_init - delete a node, and reset it so it can be deleted again.
26ccd9
+ * @n: the list_node to be deleted.
26ccd9
+ *
26ccd9
+ * list_del(@n) or list_del_init() again after this will be safe,
26ccd9
+ * which can be useful in some cases.
26ccd9
+ *
26ccd9
+ * See also:
26ccd9
+ *	list_del_from(), list_del()
26ccd9
+ *
26ccd9
+ * Example:
26ccd9
+ *	list_del_init(&child->list);
26ccd9
+ *	parent->num_children--;
26ccd9
+ */
26ccd9
+#define list_del_init(n) list_del_init_(n, LIST_LOC)
26ccd9
+static inline void list_del_init_(struct list_node *n, const char *abortstr)
26ccd9
+{
26ccd9
+	list_del_(n, abortstr);
26ccd9
+	list_node_init(n);
26ccd9
+}
26ccd9
+
26ccd9
 /**
26ccd9
  * list_del_from - delete an entry from a known linked list.
26ccd9
  * @h: the list_head the node is in.
26ccd9
@@ -285,6 +383,39 @@ static inline void list_del_from(struct list_head *h, struct list_node *n)
26ccd9
 	list_del(n);
26ccd9
 }
26ccd9
 
26ccd9
+/**
26ccd9
+ * list_swap - swap out an entry from an (unknown) linked list for a new one.
26ccd9
+ * @o: the list_node to replace from the list.
26ccd9
+ * @n: the list_node to insert in place of the old one.
26ccd9
+ *
26ccd9
+ * Note that this leaves @o in an undefined state; it can be added to
26ccd9
+ * another list, but not deleted/swapped again.
26ccd9
+ *
26ccd9
+ * See also:
26ccd9
+ *	list_del()
26ccd9
+ *
26ccd9
+ * Example:
26ccd9
+ *	struct child x1, x2;
26ccd9
+ *	LIST_HEAD(xh);
26ccd9
+ *
26ccd9
+ *	list_add(&xh, &x1.list);
26ccd9
+ *	list_swap(&x1.list, &x2.list);
26ccd9
+ */
26ccd9
+#define list_swap(o, n) list_swap_(o, n, LIST_LOC)
26ccd9
+static inline void list_swap_(struct list_node *o,
26ccd9
+			      struct list_node *n,
26ccd9
+			      const char* abortstr)
26ccd9
+{
26ccd9
+	(void)list_debug_node(o, abortstr);
26ccd9
+	*n = *o;
26ccd9
+	n->next->prev = n;
26ccd9
+	n->prev->next = n;
26ccd9
+#ifdef CCAN_LIST_DEBUG
26ccd9
+	/* Catch use-after-del. */
26ccd9
+	o->next = o->prev = NULL;
26ccd9
+#endif
26ccd9
+}
26ccd9
+
26ccd9
 /**
26ccd9
  * list_entry - convert a list_node back into the structure containing it.
26ccd9
  * @n: the list_node
26ccd9
@@ -406,9 +537,29 @@ static inline const void *list_tail_(const struct list_head *h, size_t off)
26ccd9
  *		printf("Name: %s\n", child->name);
26ccd9
  */
26ccd9
 #define list_for_each_rev(h, i, member)					\
26ccd9
-	for (i = container_of_var(list_debug(h,	LIST_LOC)->n.prev, i, member); \
26ccd9
-	     &i->member != &(h)->n;					\
26ccd9
-	     i = container_of_var(i->member.prev, i, member))
26ccd9
+	list_for_each_rev_off(h, i, list_off_var_(i, member))
26ccd9
+
26ccd9
+/**
26ccd9
+ * list_for_each_rev_safe - iterate through a list backwards,
26ccd9
+ * maybe during deletion
26ccd9
+ * @h: the list_head
26ccd9
+ * @i: the structure containing the list_node
26ccd9
+ * @nxt: the structure containing the list_node
26ccd9
+ * @member: the list_node member of the structure
26ccd9
+ *
26ccd9
+ * This is a convenient wrapper to iterate @i over the entire list backwards.
26ccd9
+ * It's a for loop, so you can break and continue as normal.  The extra
26ccd9
+ * variable * @nxt is used to hold the next element, so you can delete @i
26ccd9
+ * from the list.
26ccd9
+ *
26ccd9
+ * Example:
26ccd9
+ *	struct child *next;
26ccd9
+ *	list_for_each_rev_safe(&parent->children, child, next, list) {
26ccd9
+ *		printf("Name: %s\n", child->name);
26ccd9
+ *	}
26ccd9
+ */
26ccd9
+#define list_for_each_rev_safe(h, i, nxt, member)			\
26ccd9
+	list_for_each_rev_safe_off(h, i, nxt, list_off_var_(i, member))
26ccd9
 
26ccd9
 /**
26ccd9
  * list_for_each_safe - iterate through a list, maybe during deletion
26ccd9
@@ -422,7 +573,6 @@ static inline const void *list_tail_(const struct list_head *h, size_t off)
26ccd9
  * @nxt is used to hold the next element, so you can delete @i from the list.
26ccd9
  *
26ccd9
  * Example:
26ccd9
- *	struct child *next;
26ccd9
  *	list_for_each_safe(&parent->children, child, next, list) {
26ccd9
  *		list_del(&child->list);
26ccd9
  *		parent->num_children--;
26ccd9
@@ -537,10 +687,28 @@ static inline void list_prepend_list_(struct list_head *to,
26ccd9
 	list_head_init(from);
26ccd9
 }
26ccd9
 
26ccd9
+/* internal macros, do not use directly */
26ccd9
+#define list_for_each_off_dir_(h, i, off, dir)				\
26ccd9
+	for (i = list_node_to_off_(list_debug(h, LIST_LOC)->n.dir,	\
26ccd9
+				   (off));				\
26ccd9
+	list_node_from_off_((void *)i, (off)) != &(h)->n;		\
26ccd9
+	i = list_node_to_off_(list_node_from_off_((void *)i, (off))->dir, \
26ccd9
+			      (off)))
26ccd9
+
26ccd9
+#define list_for_each_safe_off_dir_(h, i, nxt, off, dir)		\
26ccd9
+	for (i = list_node_to_off_(list_debug(h, LIST_LOC)->n.dir,	\
26ccd9
+				   (off)),				\
26ccd9
+	nxt = list_node_to_off_(list_node_from_off_(i, (off))->dir,	\
26ccd9
+				(off));					\
26ccd9
+	list_node_from_off_(i, (off)) != &(h)->n;			\
26ccd9
+	i = nxt,							\
26ccd9
+	nxt = list_node_to_off_(list_node_from_off_(i, (off))->dir,	\
26ccd9
+				(off)))
26ccd9
+
26ccd9
 /**
26ccd9
  * list_for_each_off - iterate through a list of memory regions.
26ccd9
  * @h: the list_head
26ccd9
- * @i: the pointer to a memory region wich contains list node data.
26ccd9
+ * @i: the pointer to a memory region which contains list node data.
26ccd9
  * @off: offset(relative to @i) at which list node data resides.
26ccd9
  *
26ccd9
  * This is a low-level wrapper to iterate @i over the entire list, used to
26ccd9
@@ -548,12 +716,12 @@ static inline void list_prepend_list_(struct list_head *to,
26ccd9
  * so you can break and continue as normal.
26ccd9
  *
26ccd9
  * WARNING! Being the low-level macro that it is, this wrapper doesn't know
26ccd9
- * nor care about the type of @i. The only assumtion made is that @i points
26ccd9
+ * nor care about the type of @i. The only assumption made is that @i points
26ccd9
  * to a chunk of memory that at some @offset, relative to @i, contains a
26ccd9
- * properly filled `struct node_list' which in turn contains pointers to
26ccd9
- * memory chunks and it's turtles all the way down. Whith all that in mind
26ccd9
+ * properly filled `struct list_node' which in turn contains pointers to
26ccd9
+ * memory chunks and it's turtles all the way down. With all that in mind
26ccd9
  * remember that given the wrong pointer/offset couple this macro will
26ccd9
- * happilly churn all you memory untill SEGFAULT stops it, in other words
26ccd9
+ * happily churn all you memory until SEGFAULT stops it, in other words
26ccd9
  * caveat emptor.
26ccd9
  *
26ccd9
  * It is worth mentioning that one of legitimate use-cases for that wrapper
26ccd9
@@ -567,17 +735,24 @@ static inline void list_prepend_list_(struct list_head *to,
26ccd9
  *		printf("Name: %s\n", child->name);
26ccd9
  */
26ccd9
 #define list_for_each_off(h, i, off)                                    \
26ccd9
-	for (i = list_node_to_off_(list_debug(h, LIST_LOC)->n.next,	\
26ccd9
-				   (off));				\
26ccd9
-       list_node_from_off_((void *)i, (off)) != &(h)->n;                \
26ccd9
-       i = list_node_to_off_(list_node_from_off_((void *)i, (off))->next, \
26ccd9
-                             (off)))
26ccd9
+	list_for_each_off_dir_((h),(i),(off),next)
26ccd9
+
26ccd9
+/**
26ccd9
+ * list_for_each_rev_off - iterate through a list of memory regions backwards
26ccd9
+ * @h: the list_head
26ccd9
+ * @i: the pointer to a memory region which contains list node data.
26ccd9
+ * @off: offset(relative to @i) at which list node data resides.
26ccd9
+ *
26ccd9
+ * See list_for_each_off for details
26ccd9
+ */
26ccd9
+#define list_for_each_rev_off(h, i, off)                                    \
26ccd9
+	list_for_each_off_dir_((h),(i),(off),prev)
26ccd9
 
26ccd9
 /**
26ccd9
  * list_for_each_safe_off - iterate through a list of memory regions, maybe
26ccd9
  * during deletion
26ccd9
  * @h: the list_head
26ccd9
- * @i: the pointer to a memory region wich contains list node data.
26ccd9
+ * @i: the pointer to a memory region which contains list node data.
26ccd9
  * @nxt: the structure containing the list_node
26ccd9
  * @off: offset(relative to @i) at which list node data resides.
26ccd9
  *
26ccd9
@@ -590,15 +765,26 @@ static inline void list_prepend_list_(struct list_head *to,
26ccd9
  *		printf("Name: %s\n", child->name);
26ccd9
  */
26ccd9
 #define list_for_each_safe_off(h, i, nxt, off)                          \
26ccd9
-	for (i = list_node_to_off_(list_debug(h, LIST_LOC)->n.next,	\
26ccd9
-				   (off)),				\
26ccd9
-         nxt = list_node_to_off_(list_node_from_off_(i, (off))->next,   \
26ccd9
-                                 (off));                                \
26ccd9
-       list_node_from_off_(i, (off)) != &(h)->n;                        \
26ccd9
-       i = nxt,                                                         \
26ccd9
-         nxt = list_node_to_off_(list_node_from_off_(i, (off))->next,   \
26ccd9
-                                 (off)))
26ccd9
+	list_for_each_safe_off_dir_((h),(i),(nxt),(off),next)
26ccd9
 
26ccd9
+/**
26ccd9
+ * list_for_each_rev_safe_off - iterate backwards through a list of
26ccd9
+ * memory regions, maybe during deletion
26ccd9
+ * @h: the list_head
26ccd9
+ * @i: the pointer to a memory region which contains list node data.
26ccd9
+ * @nxt: the structure containing the list_node
26ccd9
+ * @off: offset(relative to @i) at which list node data resides.
26ccd9
+ *
26ccd9
+ * For details see `list_for_each_rev_off' and `list_for_each_rev_safe'
26ccd9
+ * descriptions.
26ccd9
+ *
26ccd9
+ * Example:
26ccd9
+ *	list_for_each_rev_safe_off(&parent->children, child,
26ccd9
+ *		next, offsetof(struct child, list))
26ccd9
+ *		printf("Name: %s\n", child->name);
26ccd9
+ */
26ccd9
+#define list_for_each_rev_safe_off(h, i, nxt, off)                      \
26ccd9
+	list_for_each_safe_off_dir_((h),(i),(nxt),(off),prev)
26ccd9
 
26ccd9
 /* Other -off variants. */
26ccd9
 #define list_entry_off(n, type, off)		\
26ccd9
diff --git a/ndctl/lib/inject.c b/ndctl/lib/inject.c
26ccd9
index d61c02c..3486ffe 100644
26ccd9
--- a/ndctl/lib/inject.c
26ccd9
+++ b/ndctl/lib/inject.c
26ccd9
@@ -2,7 +2,6 @@
26ccd9
 // Copyright (C) 2014-2020, Intel Corporation. All rights reserved.
26ccd9
 #include <stdlib.h>
26ccd9
 #include <limits.h>
26ccd9
-#include <util/list.h>
26ccd9
 #include <util/size.h>
26ccd9
 #include <ndctl/libndctl.h>
26ccd9
 #include <ccan/list/list.h>
26ccd9
diff --git a/util/list.h b/util/list.h
26ccd9
deleted file mode 100644
26ccd9
index 1ea9c59..0000000
26ccd9
--- a/util/list.h
26ccd9
+++ /dev/null
26ccd9
@@ -1,40 +0,0 @@
26ccd9
-/* SPDX-License-Identifier: GPL-2.0 */
26ccd9
-/* Copyright (C) 2015-2020 Intel Corporation. All rights reserved. */
26ccd9
-#ifndef _NDCTL_LIST_H_
26ccd9
-#define _NDCTL_LIST_H_
26ccd9
-
26ccd9
-#include <ccan/list/list.h>
26ccd9
-
26ccd9
-/**
26ccd9
- * list_add_after - add an entry after the given node in the linked list.
26ccd9
- * @h: the list_head to add the node to
26ccd9
- * @l: the list_node after which to add to
26ccd9
- * @n: the list_node to add to the list.
26ccd9
- *
26ccd9
- * The list_node does not need to be initialized; it will be overwritten.
26ccd9
- * Example:
26ccd9
- *	struct child *child = malloc(sizeof(*child));
26ccd9
- *
26ccd9
- *	child->name = "geoffrey";
26ccd9
- *	list_add_after(&parent->children, &child1->list, &child->list);
26ccd9
- *	parent->num_children++;
26ccd9
- */
26ccd9
-#define list_add_after(h, l, n) list_add_after_(h, l, n, LIST_LOC)
26ccd9
-static inline void list_add_after_(struct list_head *h,
26ccd9
-				   struct list_node *l,
26ccd9
-				   struct list_node *n,
26ccd9
-				   const char *abortstr)
26ccd9
-{
26ccd9
-	if (l->next == &h->n) {
26ccd9
-		/* l is the last element, this becomes a list_add_tail */
26ccd9
-		list_add_tail(h, n);
26ccd9
-		return;
26ccd9
-	}
26ccd9
-	n->next = l->next;
26ccd9
-	n->prev = l;
26ccd9
-	l->next->prev = n;
26ccd9
-	l->next = n;
26ccd9
-	(void)list_debug(h, abortstr);
26ccd9
-}
26ccd9
-
26ccd9
-#endif /* _NDCTL_LIST_H_ */
26ccd9
-- 
26ccd9
2.27.0
26ccd9