Skip to contentSkip to conversation

futex: hashbucket as list of futex instead of waiters

PATCH/RFC Gerd Gerats 1 participant 1 message Aug 23, 2017

lore
Share & tools
lore
#0 lore raw

When using futex as a condition variable, for example: to manage a threadpool, there may be a lot of threads inside the futex_wait to sleep on this futex. The futex_hash_bucket consists therefore of many struct futex_q for the same futex.

On bad luck another futex, used as mutex, hashed into the same bucket. Every futex_wake on this mutex, has to scan the whole chain of above waiter to find the futex_q for this mutex. For non-unusual threadpool sizes of more than 20, this should be a considerable effort.

I therefore suggest to include in the hash-bucketchain only one futex_q per futex and to queue additional waiter in an extrachain at the 'top' futex_q entry. Thus different futex are isolated from each other, the cost of a hash collision is reduced.

To show the idea, I added a sample patch. Here, the plist is exchanged for a futex-specific implementation. kernel/fplist.h is certainly not not the right place.

---
 kernel/fplist.h | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/futex.c  | 105 ++++++++++++++++++++------------------
 2 files changed, 208 insertions(+), 50 deletions(-)
 create mode 100644 kernel/fplist.h
kernel/fplist.h new file +153 -0
new file mode 100644
index 000000000000..262abe70b31c
@@ -0,0 +1,153 @@
+#ifndef _LINUX_FPLIST_H_
+#define _LINUX_FPLIST_H_
+
+/*
+ * futex specific priority-sorted list
+ *
+ * based on include/linux/plist.h
+ *
+ * Simple ASCII art explanation:
+ *
+ * fl:futx_list
+ * pl:prio_list
+ * nl:node_list
+ *
+ * +------+
+ * |      v
+ * |     |fl|        HEAD
+ * |      ^
+ * |      |
+ * |      v
+ * |  +--------+
+ * |  +->|pl|<-+
+ * |     |10|   (prio)
+ * |     |  |
+ * |  +->|nl|<-+
+ * |  +--------+
+ * |      ^
+ * |      |
+ * |      v
+ * |  +------------------------------------+
+ * |  +->|pl|<->|pl|<--------------->|pl|<-+
+ * |     |10|   |21|   |21|   |21|   |40|   (prio)
+ * |     |  |   |  |   |  |   |  |   |  |
+ * |  +->|nl|<->|nl|<->|nl|<->|nl|<->|nl|<-+
+ * |  +------------------------------------+
+ * |      ^
+ * +------+
+ */
+
+#include <linux/kernel.h>
+#include <linux/list.h>
+
+struct fplist_head {
+	struct list_head        futx_list;
+};
+
+struct fplist_node {
+	int			prio;
+	struct list_head	futx_list;	// top level futex list
+	// per futex list
+	struct list_head	prio_list;
+	struct list_head	node_list;
+};
+
+#define FPLIST_HEAD_INIT(head)				\
+{							\
+	.futx_list = LIST_HEAD_INIT((head).futx_list)	\
+}
+
+#define FPLIST_HEAD(head) \
+	struct fplist_head head = FPLIST_HEAD_INIT(head)
+
+static inline bool fplist_node_empty(struct fplist_node *node)
+{
+	return list_empty(&node->node_list) && list_empty(&node->futx_list);
+}
+
+static inline void fplist_head_init(struct fplist_head *head)
+{
+	INIT_LIST_HEAD(&head->futx_list);
+}
+
+static void fplist_del(struct fplist_node *node, struct fplist_head *head)
+{
+	struct fplist_node *next;
+
+	if (list_empty(&node->node_list)) {
+		/* last/only in ring, remove from futex ring */
+		list_del_init(&node->futx_list);
+		return;
+	}
+	next = list_entry(node->node_list.next,
+			struct fplist_node, node_list);
+	if (!list_empty(&node->prio_list)) {
+		/* add the next node into prio_list */
+		if (list_empty(&next->prio_list))
+			list_add(&next->prio_list, &node->prio_list);
+		list_del_init(&node->prio_list);
+	}
+	if (!list_empty(&node->futx_list))
+		list_replace_init(&node->futx_list, &next->futx_list);
+	list_del_init(&node->node_list);
+}
+
+static void fplist_add_first(struct fplist_node *node, struct fplist_head *head)
+{
+	list_add_tail(&node->futx_list, &head->futx_list);
+	INIT_LIST_HEAD(&node->prio_list);
+	INIT_LIST_HEAD(&node->node_list);
+}
+
+static void
+fplist_add_insert(struct fplist_node *node, struct fplist_node *top,
+		struct fplist_head *head)
+{
+	struct fplist_node *iter, *prev = NULL;
+	struct list_head *node_next = &top->node_list;
+
+	INIT_LIST_HEAD(&node->futx_list);
+	INIT_LIST_HEAD(&node->prio_list);
+
+	iter = top;
+
+	do {
+		if (node->prio < iter->prio) {
+			node_next = &iter->node_list;
+			break;
+		}
+
+		prev = iter;
+		iter = list_entry(iter->prio_list.next,
+				struct fplist_node, prio_list);
+	} while (iter != top);
+
+	if (!prev || prev->prio != node->prio)
+		list_add_tail(&node->prio_list, &iter->prio_list);
+	if (!prev)
+		list_replace_init(&top->futx_list, &node->futx_list);
+	list_add_tail(&node->node_list, node_next);
+}
+
+#define fplist_for_each_entry(pos, head, member)	\
+	 list_for_each_entry(pos, &(head)->futx_list, member.futx_list)
+
+static inline struct fplist_node*
+___fplist_for_each_next(struct fplist_node *node) {
+	struct fplist_node *next;
+
+	next = list_entry(node->node_list.next, struct fplist_node, node_list);
+	return list_empty(&next->futx_list) ? next : 0;
+}
+
+#define __fplist_for_each_next(pos, member) ({ \
+	struct fplist_node *next__ = ___fplist_for_each_next(&(pos)->member); \
+	next__ ? list_entry(next__, typeof(*(pos)), member) : 0; \
+	})
+
+#define fplist_for_each_entry_safe(pos, n, top, member) \
+	for (pos = (top); \
+		(pos && ((n = __fplist_for_each_next(pos, member)), 1)); \
+		pos = n)
+
+#endif
kernel/futex.c +55 -50

224 lines not on this page · raw message

Signature · 1 line
2.14.1