From: Keith Busch <kbusch@meta.com>
To: <linux-nvme@lists.infradead.org>, <linux-kernel@vger.kernel.org>,
<linux-kselftest@vger.kernel.org>
Cc: <hch@lst.de>, <sagi@grimberg.me>, <paulmck@kernel.org>,
<davidgow@google.com>, <akpm@linux-foundation.org>,
<venkat88@linux.vnet.ibm.com>, Keith Busch <kbusch@kernel.org>
Subject: [PATCH 1/2] list: introduce a new cutting helper
Date: Wed, 12 Jun 2024 08:51:34 -0700 [thread overview]
Message-ID: <20240612155135.3060667-1-kbusch@meta.com> (raw)
From: Keith Busch <kbusch@kernel.org>
Provide a helper to remove elements from a list to the end, and place
those elements in a new list.
Signed-off-by: Keith Busch <kbusch@kernel.org>
---
include/linux/list.h | 20 ++++++++++++++++++++
lib/list-test.c | 29 +++++++++++++++++++++++++++++
2 files changed, 49 insertions(+)
diff --git a/include/linux/list.h b/include/linux/list.h
index 5f4b0a39cf46a..f22850e854820 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -520,6 +520,26 @@ static inline void list_cut_before(struct list_head *list,
entry->prev = head;
}
+/**
+ * list_cut - cut a list into two from the entry
+ * @list: a new list to add all removed entries
+ * @head: a list with entries
+ * @entry: an entry within head, could be the head itself
+ *
+ * This helper removes elements from @head starting at @entry until the end,
+ * and appends them to @lists.
+ */
+static inline void list_cut(struct list_head *list,
+ struct list_head *head, struct list_head *entry)
+{
+ list->next = entry;
+ list->prev = head->prev;
+ head->prev = entry->prev;
+ entry->prev->next = head;
+ entry->prev = list;
+ list->prev->next = list;
+}
+
static inline void __list_splice(const struct list_head *list,
struct list_head *prev,
struct list_head *next)
diff --git a/lib/list-test.c b/lib/list-test.c
index 0cc27de9cec88..1507f46cf1ade 100644
--- a/lib/list-test.c
+++ b/lib/list-test.c
@@ -382,6 +382,34 @@ static void list_test_list_is_singular(struct kunit *test)
KUNIT_EXPECT_FALSE(test, list_is_singular(&list));
}
+static void list_test_list_cut(struct kunit *test)
+{
+ struct list_head entries[3], *cur;
+ LIST_HEAD(list1);
+ LIST_HEAD(list2);
+ int i = 0;
+
+ list_add_tail(&entries[0], &list1);
+ list_add_tail(&entries[1], &list1);
+ list_add_tail(&entries[2], &list1);
+
+ /* before: [list1] -> entries[0] -> entries[1] -> entries[2] */
+ list_cut(&list2, &list1, &entries[1]);
+ /* after: [list1] -> entries[0], [list2] -> entries[1] -> entries[2] */
+
+ list_for_each(cur, &list1) {
+ KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
+ i++;
+ }
+
+ KUNIT_EXPECT_EQ(test, i, 1);
+
+ list_for_each(cur, &list2) {
+ KUNIT_EXPECT_PTR_EQ(test, cur, &entries[i]);
+ i++;
+ }
+}
+
static void list_test_list_cut_position(struct kunit *test)
{
struct list_head entries[3], *cur;
@@ -780,6 +808,7 @@ static struct kunit_case list_test_cases[] = {
KUNIT_CASE(list_test_list_is_singular),
KUNIT_CASE(list_test_list_cut_position),
KUNIT_CASE(list_test_list_cut_before),
+ KUNIT_CASE(list_test_list_cut),
KUNIT_CASE(list_test_list_splice),
KUNIT_CASE(list_test_list_splice_tail),
KUNIT_CASE(list_test_list_splice_init),
--
2.43.0
next reply other threads:[~2024-06-12 15:52 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-12 15:51 Keith Busch [this message]
2024-06-12 15:51 ` [PATCH 2/2] nvme: fix namespace removal list Keith Busch
2024-06-12 17:20 ` Chaitanya Kulkarni
2024-06-12 17:24 ` Keith Busch
2024-06-12 17:19 ` [PATCH 1/2] list: introduce a new cutting helper Chaitanya Kulkarni
2024-06-13 4:56 ` Nilay Shroff
2024-06-13 8:10 ` Christoph Hellwig
2024-06-13 13:00 ` Keith Busch
2024-06-13 12:56 ` Keith Busch
2024-06-13 13:41 ` Nilay Shroff
2024-06-13 14:36 ` Keith Busch
2024-06-13 14:43 ` Paul E. McKenney
2024-06-13 14:47 ` Keith Busch
2024-06-13 15:15 ` Paul E. McKenney
2024-06-13 15:40 ` Keith Busch
2024-06-13 16:01 ` Paul E. McKenney
2024-06-13 16:10 ` Keith Busch
2024-06-13 17:39 ` Paul E. McKenney
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240612155135.3060667-1-kbusch@meta.com \
--to=kbusch@meta.com \
--cc=akpm@linux-foundation.org \
--cc=davidgow@google.com \
--cc=hch@lst.de \
--cc=kbusch@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=paulmck@kernel.org \
--cc=sagi@grimberg.me \
--cc=venkat88@linux.vnet.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox