From: Maurizio Lombardi <mlombard@redhat.com>
To: kbusch@kernel.org
Cc: hch@lst.de, hare@suse.de, chaitanyak@nvidia.com,
bvanassche@acm.org, linux-scsi@vger.kernel.org,
linux-nvme@lists.infradead.org,
James.Bottomley@HansenPartnership.com, mlombard@arkamax.eu,
jmeneghi@redhat.com, emilne@redhat.com, bgurney@redhat.com
Subject: [PATCH V3 1/3] lib: Introduce completion chain helper
Date: Wed, 25 Feb 2026 17:12:01 +0100 [thread overview]
Message-ID: <20260225161203.76168-2-mlombard@redhat.com> (raw)
In-Reply-To: <20260225161203.76168-1-mlombard@redhat.com>
Introduce a new helper library, the completion chain, designed to serialize
asynchronous operations that must complete in a strict First-In, First-Out
(FIFO) order.
Certain workflows, particularly in storage drivers, require operations to
complete in the same sequence they were submitted.
This helper provides a generic mechanism to enforce this ordering.
compl_chain: The main structure representing the queue of operations
compl_chain_entry: An entry embedded in a per-operation structure
The typical usage pattern is:
* An operation is enqueued by calling compl_chain_add().
* The worker thread for the operation calls
compl_chain_wait(), which blocks until the previously
enqueued operation has finished.
* After the work is done, the thread calls compl_chain_complete().
This signals the next operation in the chain that it can now
proceed and removes the current entry from the list.
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Maurizio Lombardi <mlombard@redhat.com>
---
include/linux/compl_chain.h | 35 +++++++++++
lib/Makefile | 2 +-
lib/compl_chain.c | 118 ++++++++++++++++++++++++++++++++++++
3 files changed, 154 insertions(+), 1 deletion(-)
create mode 100644 include/linux/compl_chain.h
create mode 100644 lib/compl_chain.c
diff --git a/include/linux/compl_chain.h b/include/linux/compl_chain.h
new file mode 100644
index 000000000000..a2bf271144e0
--- /dev/null
+++ b/include/linux/compl_chain.h
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_COMPLETION_CHAIN_H
+#define _LINUX_COMPLETION_CHAIN_H
+
+#include <linux/list.h>
+#include <linux/completion.h>
+#include <linux/spinlock.h>
+
+struct compl_chain {
+ spinlock_t lock;
+ struct list_head list;
+};
+
+#define COMPL_CHAIN_INIT(name) \
+ { .lock = __SPIN_LOCK_UNLOCKED((name).lock), \
+ .list = LIST_HEAD_INIT((name).list) }
+
+#define DEFINE_COMPL_CHAIN(name) \
+ struct compl_chain name = COMPL_CHAIN_INIT(name)
+
+struct compl_chain_entry {
+ struct compl_chain *chain;
+ struct list_head list;
+ struct completion prev_finished;
+};
+
+void compl_chain_init(struct compl_chain *chain);
+void compl_chain_add(struct compl_chain *chain,
+ struct compl_chain_entry *entry);
+void compl_chain_wait(struct compl_chain_entry *entry);
+void compl_chain_complete(struct compl_chain_entry *entry);
+bool compl_chain_pending(struct compl_chain_entry *entry);
+void compl_chain_flush(struct compl_chain *chain);
+
+#endif /* _LINUX_COMPLETION_CHAIN_H */
diff --git a/lib/Makefile b/lib/Makefile
index 1b9ee167517f..c3ccd82bb190 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -58,7 +58,7 @@ obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
bsearch.o find_bit.o llist.o lwq.o memweight.o kfifo.o \
percpu-refcount.o rhashtable.o base64.o \
once.o refcount.o rcuref.o usercopy.o errseq.o bucket_locks.o \
- generic-radix-tree.o bitmap-str.o
+ generic-radix-tree.o bitmap-str.o compl_chain.o
obj-y += string_helpers.o
obj-y += hexdump.o
obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
diff --git a/lib/compl_chain.c b/lib/compl_chain.c
new file mode 100644
index 000000000000..b1cb43753f52
--- /dev/null
+++ b/lib/compl_chain.c
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Author: Maurizio Lombardi <mlombard@redhat.com>
+ */
+#include <linux/compl_chain.h>
+
+/**
+ * compl_chain_init - Initialize a completion chain
+ * @chain: The completion chain to be initialized.
+ *
+ * Initializes a compl_chain structure
+ */
+void compl_chain_init(struct compl_chain *chain)
+{
+ spin_lock_init(&chain->lock);
+ INIT_LIST_HEAD(&chain->list);
+}
+EXPORT_SYMBOL_GPL(compl_chain_init);
+
+/**
+ * compl_chain_add - Add a new entry to the tail of the chain
+ * @chain: The completion chain to add the entry to.
+ * @entry: The entry to be enqueued.
+ *
+ * Adds a new entry to the end of the queue.
+ * If the chain is empty when this entry is added, it is immediately marked
+ * as ready to run, as there is no preceding entry to wait for.
+ */
+void compl_chain_add(struct compl_chain *chain,
+ struct compl_chain_entry *entry)
+{
+ init_completion(&entry->prev_finished);
+ INIT_LIST_HEAD(&entry->list);
+
+ WRITE_ONCE(entry->chain, chain);
+
+ spin_lock(&chain->lock);
+ if (list_empty(&chain->list))
+ complete_all(&entry->prev_finished);
+ list_add_tail(&entry->list, &chain->list);
+ spin_unlock(&chain->lock);
+}
+EXPORT_SYMBOL_GPL(compl_chain_add);
+
+/**
+ * compl_chain_wait - Wait for the preceding operation to finish
+ * @entry: The entry for the current operation.
+ *
+ * Blocks the current execution thread until compl_chain_complete()
+ * is executed against the previous entry in the chain.
+ */
+void compl_chain_wait(struct compl_chain_entry *entry)
+{
+ WARN_ON(!entry->chain);
+
+ wait_for_completion(&entry->prev_finished);
+}
+EXPORT_SYMBOL_GPL(compl_chain_wait);
+
+/**
+ * compl_chain_complete - Mark an entry as completed and signal the next one
+ * @entry: The entry to mark as completed.
+ *
+ * Removes the current entry from the chain and signals the next waiting
+ * entry (if one exists) that it is now allowed to proceed.
+ */
+void compl_chain_complete(struct compl_chain_entry *entry)
+{
+ struct compl_chain *chain = entry->chain;
+
+ WARN_ON(!chain);
+
+ wait_for_completion(&entry->prev_finished);
+
+ spin_lock(&chain->lock);
+ list_del(&entry->list);
+ if (!list_empty(&chain->list)) {
+ struct compl_chain_entry *next =
+ list_first_entry(&chain->list,
+ struct compl_chain_entry, list);
+ complete_all(&next->prev_finished);
+ }
+ spin_unlock(&chain->lock);
+
+ WRITE_ONCE(entry->chain, NULL);
+}
+EXPORT_SYMBOL_GPL(compl_chain_complete);
+
+/**
+ * compl_chain_pending - Check if an entry is pending
+ * @entry: The entry to check.
+ *
+ * Returns true if an entry has been added to a chain and hasn't yet
+ * been completed.
+ */
+bool compl_chain_pending(struct compl_chain_entry *entry)
+{
+ return READ_ONCE(entry->chain) != NULL;
+}
+EXPORT_SYMBOL_GPL(compl_chain_pending);
+
+/**
+ * compl_chain_flush - Wait for all entries currently in the chain to finish
+ * @chain: The completion chain to flush.
+ *
+ * Enqueues a dummy entry into the chain and immediately calls
+ * compl_chain_complete() against it. Because operations execute in strict
+ * FIFO order, this acts as a barrier, blocking the calling thread until
+ * all previously enqueued entries have finished.
+ */
+void compl_chain_flush(struct compl_chain *chain)
+{
+ struct compl_chain_entry dummy_entry;
+
+ compl_chain_add(chain, &dummy_entry);
+ compl_chain_complete(&dummy_entry);
+}
+EXPORT_SYMBOL_GPL(compl_chain_flush);
--
2.53.0
next prev parent reply other threads:[~2026-02-25 16:12 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-25 16:12 [PATCH V3 0/3] Ensure ordered namespace registration during async scan Maurizio Lombardi
2026-02-25 16:12 ` Maurizio Lombardi [this message]
2026-02-25 16:12 ` [PATCH V3 2/3] nvme-core: register namespaces in order " Maurizio Lombardi
2026-02-25 21:37 ` kernel test robot
2026-02-25 16:12 ` [PATCH V3 3/3] scsi: Convert async scanning to use the completion chain helper Maurizio Lombardi
2026-02-25 21:41 ` [PATCH V3 0/3] Ensure ordered namespace registration during async scan Keith Busch
2026-02-26 8:07 ` Maurizio Lombardi
2026-02-26 15:09 ` Keith Busch
2026-02-26 16:35 ` John Meneghini
2026-02-26 18:15 ` Keith Busch
2026-03-02 7:16 ` Hannes Reinecke
2026-03-02 17:12 ` Keith Busch
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=20260225161203.76168-2-mlombard@redhat.com \
--to=mlombard@redhat.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=bgurney@redhat.com \
--cc=bvanassche@acm.org \
--cc=chaitanyak@nvidia.com \
--cc=emilne@redhat.com \
--cc=hare@suse.de \
--cc=hch@lst.de \
--cc=jmeneghi@redhat.com \
--cc=kbusch@kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=linux-scsi@vger.kernel.org \
--cc=mlombard@arkamax.eu \
/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