From: Xavier <xavier_qy@163.com>
To: longman@redhat.com, mkoutny@suse.com
Cc: lizefan.x@bytedance.com, tj@kernel.org, hannes@cmpxchg.org,
cgroups@vger.kernel.org, linux-kernel@vger.kernel.org,
Xavier <xavier_qy@163.com>
Subject: [PATCH v4 v4 1/2] Union-Find: add a new module in kernel library
Date: Thu, 20 Jun 2024 16:52:32 +0800 [thread overview]
Message-ID: <20240620085233.205690-2-xavier_qy@163.com> (raw)
In-Reply-To: <20240620085233.205690-1-xavier_qy@163.com>
This patch implements a union-find data structure in the kernel library,
which includes operations for allocating nodes, freeing nodes,
finding the root of a node, and merging two nodes.
Signed-off-by: Xavier <xavier_qy@163.com>
---
MAINTAINERS | 7 +++++++
include/linux/union_find.h | 30 ++++++++++++++++++++++++++++++
lib/Makefile | 2 +-
lib/union_find.c | 38 ++++++++++++++++++++++++++++++++++++++
4 files changed, 76 insertions(+), 1 deletion(-)
create mode 100644 include/linux/union_find.h
create mode 100644 lib/union_find.c
diff --git a/MAINTAINERS b/MAINTAINERS
index d6c90161c7..602d8c6f42 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -23054,6 +23054,13 @@ F: drivers/cdrom/cdrom.c
F: include/linux/cdrom.h
F: include/uapi/linux/cdrom.h
+UNION-FIND
+M: Xavier <xavier_qy@163.com>
+L: linux-kernel@vger.kernel.org
+S: Maintained
+F: include/linux/union_find.h
+F: lib/union_find.c
+
UNIVERSAL FLASH STORAGE HOST CONTROLLER DRIVER
R: Alim Akhtar <alim.akhtar@samsung.com>
R: Avri Altman <avri.altman@wdc.com>
diff --git a/include/linux/union_find.h b/include/linux/union_find.h
new file mode 100644
index 0000000000..67e9f62bb3
--- /dev/null
+++ b/include/linux/union_find.h
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __LINUX_UNION_FIND_H
+#define __LINUX_UNION_FIND_H
+#include <linux/slab.h>
+
+/* Define a union-find node struct */
+struct uf_node {
+ struct uf_node *parent;
+ unsigned int rank;
+};
+
+/* Allocate nodes and initialize to 0 */
+static inline struct uf_node *uf_nodes_alloc(unsigned int node_num)
+{
+ return kzalloc(sizeof(struct uf_node) * node_num, GFP_KERNEL);
+}
+
+/* Free nodes*/
+static inline void uf_nodes_free(struct uf_node *nodes)
+{
+ kfree(nodes);
+}
+
+/* find the root of a node*/
+struct uf_node *uf_find(struct uf_node *node);
+
+/* Merge two intersecting nodes */
+void uf_union(struct uf_node *node1, struct uf_node *node2);
+
+#endif /*__LINUX_UNION_FIND_H*/
diff --git a/lib/Makefile b/lib/Makefile
index 3b17690456..e1769e6f03 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -34,7 +34,7 @@ lib-y := ctype.o string.o vsprintf.o cmdline.o \
is_single_threaded.o plist.o decompress.o kobject_uevent.o \
earlycpio.o seq_buf.o siphash.o dec_and_lock.o \
nmi_backtrace.o win_minmax.o memcat_p.o \
- buildid.o objpool.o
+ buildid.o objpool.o union_find.o
lib-$(CONFIG_PRINTK) += dump_stack.o
lib-$(CONFIG_SMP) += cpumask.o
diff --git a/lib/union_find.c b/lib/union_find.c
new file mode 100644
index 0000000000..2f77bae1ca
--- /dev/null
+++ b/lib/union_find.c
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/union_find.h>
+
+struct uf_node *uf_find(struct uf_node *node)
+{
+ struct uf_node *parent;
+
+ if (!node->parent) {
+ node->parent = node;
+ return node;
+ }
+
+ /*Find the root node and perform path compression at the same time*/
+ while (node->parent != node) {
+ parent = node->parent;
+ node->parent = parent->parent;
+ node = parent;
+ }
+ return node;
+}
+
+/*Function to merge two sets, using union by rank*/
+void uf_union(struct uf_node *node1, struct uf_node *node2)
+{
+ struct uf_node *root1 = uf_find(node1);
+ struct uf_node *root2 = uf_find(node2);
+
+ if (root1 != root2) {
+ if (root1->rank < root2->rank) {
+ root1->parent = root2;
+ } else if (root1->rank > root2->rank) {
+ root2->parent = root1;
+ } else {
+ root2->parent = root1;
+ root1->rank++;
+ }
+ }
+}
--
2.45.2
next prev parent reply other threads:[~2024-06-20 8:55 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-31 2:48 [PATCH v2] cpuset: Optimize the number of iterations in the scheduling domain construction process Xavier
2024-05-31 21:13 ` Waiman Long
2024-06-03 12:31 ` [PATCH v3] cpuset: use Union-Find to optimize the merging of cpumasks Xavier
2024-06-04 15:02 ` Waiman Long
2024-06-10 17:18 ` Michal Koutný
2024-06-20 8:52 ` [PATCH v4 v4 0/2] cpuset: use Union-Find to optimize Xavier
2024-06-20 8:52 ` Xavier [this message]
2024-06-20 14:54 ` [PATCH v4 v4 1/2] Union-Find: add a new module in kernel library Waiman Long
2024-06-21 8:49 ` [PATCH-cpuset v5 0/2] cpuset: use Union-Find to optimize Xavier
2024-06-21 8:49 ` [PATCH-cpuset v5 1/2] Union-Find: add a new module in kernel library Xavier
2024-06-21 21:10 ` Tejun Heo
2024-06-22 7:14 ` [PATCH-cpuset v6 0/2] Add Union-Find and use it to optimize cpuset Xavier
2024-06-22 7:14 ` [PATCH-cpuset v6 1/2] Union-Find: add a new module in kernel library Xavier
2024-06-22 7:14 ` [PATCH-cpuset v6 2/2] cpuset: use Union-Find to optimize the merging of cpumasks Xavier
2024-06-22 16:13 ` [PATCH-cpuset v6 0/2] Add Union-Find and use it to optimize cpuset Tejun Heo
2024-06-23 2:38 ` [PATCH-cpuset v7 " Xavier
2024-06-23 2:39 ` [PATCH-cpuset v7 1/2] Union-Find: add a new module in kernel library Xavier
2024-06-23 2:39 ` [PATCH-cpuset v7 2/2] cpuset: use Union-Find to optimize the merging of cpumasks Xavier
2024-06-27 21:06 ` [PATCH-cpuset v7 0/2] Add Union-Find and use it to optimize cpuset Tejun Heo
2024-06-28 16:13 ` [PATCH-cpuset v8 " Xavier
2024-06-28 16:13 ` [PATCH-cpuset v8 1/2] Union-Find: add a new module in kernel library Xavier
2024-07-01 20:53 ` Tejun Heo
2024-07-02 10:50 ` [PATCH-cpuset v9 0/2] Add Union-Find and use it to optimize cpuset Xavier
2024-07-02 10:50 ` [PATCH-cpuset v9 1/2] Union-Find: add a new module in kernel library Xavier
2024-07-02 10:50 ` [PATCH-cpuset v9 2/2] cpuset: use Union-Find to optimize the merging of cpumasks Xavier
2024-07-03 3:05 ` Waiman Long
2024-07-02 19:22 ` [PATCH-cpuset v9 0/2] Add Union-Find and use it to optimize cpuset Tejun Heo
2024-07-03 0:31 ` Andrew Morton
2024-07-03 17:34 ` Tejun Heo
2024-07-03 6:37 ` [PATCH-cpuset v10 " Xavier
2024-07-03 6:37 ` [PATCH-cpuset v10 1/2] Union-Find: add a new module in kernel library Xavier
2024-07-03 9:40 ` Michal Koutný
2024-07-03 11:20 ` Xavier
2024-07-04 12:12 ` Michal Koutný
2024-07-03 6:37 ` [PATCH-cpuset v10 2/2] cpuset: use Union-Find to optimize the merging of cpumasks Xavier
2024-07-03 9:40 ` Michal Koutný
2024-07-03 10:49 ` Xavier
2024-07-03 16:43 ` Waiman Long
2024-07-04 6:24 ` [PATCH-cpuset v11 0/2] Add Union-Find and use it to optimize cpuset Xavier
2024-07-04 6:24 ` [PATCH-cpuset v11 1/2] Union-Find: add a new module in kernel library Xavier
2024-07-30 23:05 ` Tejun Heo
2024-07-04 6:24 ` [PATCH-cpuset v11 2/2] cpuset: use Union-Find to optimize the merging of cpumasks Xavier
2024-07-30 23:05 ` Tejun Heo
2024-07-08 1:59 ` [PATCH-cpuset v11 0/2] Add Union-Find and use it to optimize cpuset Waiman Long
2024-07-08 18:38 ` Tejun Heo
2024-07-09 2:45 ` Xavier
2024-07-29 2:44 ` Xavier
2024-07-30 23:06 ` Tejun Heo
2024-06-28 16:13 ` [PATCH-cpuset v8 2/2] cpuset: use Union-Find to optimize the merging of cpumasks Xavier
2024-07-01 20:53 ` Tejun Heo
2024-06-21 8:49 ` [PATCH-cpuset v5 " Xavier
2024-06-20 8:52 ` [PATCH v4 v4 " Xavier
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=20240620085233.205690-2-xavier_qy@163.com \
--to=xavier_qy@163.com \
--cc=cgroups@vger.kernel.org \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lizefan.x@bytedance.com \
--cc=longman@redhat.com \
--cc=mkoutny@suse.com \
--cc=tj@kernel.org \
/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
all inboxes | Powered by JetHome®