anitazha / rpms / ndctl

Forked from rpms/ndctl 2 years ago
Clone
e0018b
From 06ae7118aaa74aa2139004fa12b4910bc15236de Mon Sep 17 00:00:00 2001
e0018b
From: Dan Williams <dan.j.williams@intel.com>
e0018b
Date: Thu, 14 Jul 2022 10:02:21 -0700
e0018b
Subject: [PATCH 180/217] cxl/lib: Maintain decoders in id order
e0018b
e0018b
Given that decoder instance order is fundamental to the DPA translation
e0018b
sequence for endpoint decoders, enforce that cxl_decoder_for_each() returns
e0018b
decoders in instance order. Otherwise, they show up in readddir() order
e0018b
which is not predictable.
e0018b
e0018b
Add a list_add_sorted() to generically handle inserting into a sorted list.
e0018b
e0018b
Link: https://lore.kernel.org/r/165781814167.1555691.14895625637451030942.stgit@dwillia2-xfh.jf.intel.com
e0018b
Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>
e0018b
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
e0018b
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
e0018b
---
e0018b
 cxl/lib/libcxl.c |  8 +++++++-
e0018b
 util/list.h      | 39 +++++++++++++++++++++++++++++++++++++++
e0018b
 2 files changed, 46 insertions(+), 1 deletion(-)
e0018b
 create mode 100644 util/list.h
e0018b
e0018b
diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
e0018b
index 6f4d64d..ea597f6 100644
e0018b
--- a/cxl/lib/libcxl.c
e0018b
+++ b/cxl/lib/libcxl.c
e0018b
@@ -19,6 +19,7 @@
e0018b
 #include <ccan/short_types/short_types.h>
e0018b
 
e0018b
 #include <util/log.h>
e0018b
+#include <util/list.h>
e0018b
 #include <util/size.h>
e0018b
 #include <util/sysfs.h>
e0018b
 #include <util/bitmap.h>
e0018b
@@ -909,6 +910,11 @@ cxl_endpoint_get_memdev(struct cxl_endpoint *endpoint)
e0018b
 	return NULL;
e0018b
 }
e0018b
 
e0018b
+static int decoder_id_cmp(struct cxl_decoder *d1, struct cxl_decoder *d2)
e0018b
+{
e0018b
+	return d1->id - d2->id;
e0018b
+}
e0018b
+
e0018b
 static void *add_cxl_decoder(void *parent, int id, const char *cxldecoder_base)
e0018b
 {
e0018b
 	const char *devname = devpath_to_devname(cxldecoder_base);
e0018b
@@ -1050,7 +1056,7 @@ static void *add_cxl_decoder(void *parent, int id, const char *cxldecoder_base)
e0018b
 			return decoder_dup;
e0018b
 		}
e0018b
 
e0018b
-	list_add(&port->decoders, &decoder->list);
e0018b
+	list_add_sorted(&port->decoders, decoder, list, decoder_id_cmp);
e0018b
 
e0018b
 	free(path);
e0018b
 	return decoder;
e0018b
diff --git a/util/list.h b/util/list.h
e0018b
new file mode 100644
e0018b
index 0000000..cb77271
e0018b
--- /dev/null
e0018b
+++ b/util/list.h
e0018b
@@ -0,0 +1,39 @@
e0018b
+/* SPDX-License-Identifier: GPL-2.0 */
e0018b
+/* Copyright (C) 2022 Intel Corporation. All rights reserved. */
e0018b
+#ifndef _NDCTL_LIST_H_
e0018b
+#define _NDCTL_LIST_H_
e0018b
+
e0018b
+#include <ccan/list/list.h>
e0018b
+
e0018b
+#define list_add_sorted(head, n, node, cmp)                                    \
e0018b
+	do {                                                                   \
e0018b
+		struct list_head *__head = (head);                             \
e0018b
+		typeof(n) __iter, __next;                                      \
e0018b
+		typeof(n) __new = (n);                                         \
e0018b
+                                                                               \
e0018b
+		if (list_empty(__head)) {                                      \
e0018b
+			list_add(__head, &__new->node);                        \
e0018b
+			break;                                                 \
e0018b
+		}                                                              \
e0018b
+                                                                               \
e0018b
+		list_for_each (__head, __iter, node) {                         \
e0018b
+			if (cmp(__new, __iter) < 0) {                          \
e0018b
+				list_add_before(__head, &__iter->node,         \
e0018b
+						&__new->node);                 \
e0018b
+				break;                                         \
e0018b
+			}                                                      \
e0018b
+			__next = list_next(__head, __iter, node);              \
e0018b
+			if (!__next) {                                         \
e0018b
+				list_add_after(__head, &__iter->node,          \
e0018b
+					       &__new->node);                  \
e0018b
+				break;                                         \
e0018b
+			}                                                      \
e0018b
+			if (cmp(__new, __next) < 0) {                          \
e0018b
+				list_add_before(__head, &__next->node,         \
e0018b
+						&__new->node);                 \
e0018b
+				break;                                         \
e0018b
+			}                                                      \
e0018b
+		}                                                              \
e0018b
+	} while (0)
e0018b
+
e0018b
+#endif /* _NDCTL_LIST_H_ */
e0018b
-- 
e0018b
2.27.0
e0018b