linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Masami Hiramatsu <mhiramat@kernel.org>
To: linux-kernel@vger.kernel.org, Peter Zijlstra <peterz@infradead.org>
Cc: Eddy_Wu@trendmicro.com, x86@kernel.org, davem@davemloft.net,
	rostedt@goodmis.org, naveen.n.rao@linux.ibm.com,
	anil.s.keshavamurthy@intel.com, linux-arch@vger.kernel.org,
	cameron@moodycamel.com, oleg@redhat.com, will@kernel.org,
	paulmck@kernel.org, mhiramat@kernel.org
Subject: [PATCH v4 22/23] freelist: Lock less freelist
Date: Fri, 28 Aug 2020 21:30:39 +0900	[thread overview]
Message-ID: <159861783901.992023.12163347531003713154.stgit@devnote2> (raw)
In-Reply-To: <159861759775.992023.12553306821235086809.stgit@devnote2>

From: Peter Zijlstra <peterz@infradead.org>

Cc: cameron@moodycamel.com
Cc: oleg@redhat.com
Cc: will@kernel.org
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 include/linux/freelist.h |  129 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)
 create mode 100644 include/linux/freelist.h

diff --git a/include/linux/freelist.h b/include/linux/freelist.h
new file mode 100644
index 000000000000..fc1842b96469
--- /dev/null
+++ b/include/linux/freelist.h
@@ -0,0 +1,129 @@
+/* SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause */
+#ifndef FREELIST_H
+#define FREELIST_H
+
+#include <linux/atomic.h>
+
+/*
+ * Copyright: cameron@moodycamel.com
+ *
+ * A simple CAS-based lock-free free list. Not the fastest thing in the world
+ * under heavy contention, but simple and correct (assuming nodes are never
+ * freed until after the free list is destroyed), and fairly speedy under low
+ * contention.
+ *
+ * Adapted from: https://moodycamel.com/blog/2014/solving-the-aba-problem-for-lock-free-free-lists
+ */
+
+struct freelist_node {
+	atomic_t		refs;
+	struct freelist_node	*next;
+};
+
+struct freelist_head {
+	struct freelist_node	*head;
+};
+
+#define REFS_ON_FREELIST 0x80000000
+#define REFS_MASK	 0x7FFFFFFF
+
+static inline void __freelist_add(struct freelist_node *node, struct freelist_head *list)
+{
+	/*
+	 * Since the refcount is zero, and nobody can increase it once it's
+	 * zero (except us, and we run only one copy of this method per node at
+	 * a time, i.e. the single thread case), then we know we can safely
+	 * change the next pointer of the node; however, once the refcount is
+	 * back above zero, then other threads could increase it (happens under
+	 * heavy contention, when the refcount goes to zero in between a load
+	 * and a refcount increment of a node in try_get, then back up to
+	 * something non-zero, then the refcount increment is done by the other
+	 * thread) -- so if the CAS to add the node to the actual list fails,
+	 * decrese the refcount and leave the add operation to the next thread
+	 * who puts the refcount back to zero (which could be us, hence the
+	 * loop).
+	 */
+	struct freelist_node *head = READ_ONCE(list->head);
+
+	for (;;) {
+		WRITE_ONCE(node->next, head);
+		atomic_set_release(&node->refs, 1);
+
+		if (!try_cmpxchg_release(&list->head, &head, node)) {
+			/*
+			 * Hmm, the add failed, but we can only try again when
+			 * the refcount goes back to zero.
+			 */
+			if (atomic_fetch_add_release(REFS_ON_FREELIST - 1, &node->refs) == 1)
+				continue;
+		}
+		return;
+	}
+}
+
+static inline void freelist_add(struct freelist_node *node, struct freelist_head *list)
+{
+	/*
+	 * We know that the should-be-on-freelist bit is 0 at this point, so
+	 * it's safe to set it using a fetch_add.
+	 */
+	if (!atomic_fetch_add_release(REFS_ON_FREELIST, &node->refs)) {
+		/*
+		 * Oh look! We were the last ones referencing this node, and we
+		 * know we want to add it to the free list, so let's do it!
+		 */
+		__freelist_add(node, list);
+	}
+}
+
+static inline struct freelist_node *freelist_try_get(struct freelist_head *list)
+{
+	struct freelist_node *prev, *next, *head = smp_load_acquire(&list->head);
+	unsigned int refs;
+
+	while (head) {
+		prev = head;
+		refs = atomic_read(&head->refs);
+		if ((refs & REFS_MASK) == 0 ||
+		    !atomic_try_cmpxchg_acquire(&head->refs, &refs, refs+1)) {
+			head = smp_load_acquire(&list->head);
+			continue;
+		}
+
+		/*
+		 * Good, reference count has been incremented (it wasn't at
+		 * zero), which means we can read the next and not worry about
+		 * it changing between now and the time we do the CAS.
+		 */
+		next = READ_ONCE(head->next);
+		if (try_cmpxchg_acquire(&list->head, &head, next)) {
+			/*
+			 * Yay, got the node. This means it was on the list,
+			 * which means should-be-on-freelist must be false no
+			 * matter the refcount (because nobody else knows it's
+			 * been taken off yet, it can't have been put back on).
+			 */
+			WARN_ON_ONCE(atomic_read(&head->refs) & REFS_ON_FREELIST);
+
+			/*
+			 * Decrease refcount twice, once for our ref, and once
+			 * for the list's ref.
+			 */
+			atomic_fetch_add(-2, &head->refs);
+
+			return head;
+		}
+
+		/*
+		 * OK, the head must have changed on us, but we still need to decrement
+		 * the refcount we increased.
+		 */
+		refs = atomic_fetch_add(-1, &prev->refs);
+		if (refs == REFS_ON_FREELIST + 1)
+			__freelist_add(prev, list);
+	}
+
+	return NULL;
+}
+
+#endif /* FREELIST_H */


  parent reply	other threads:[~2020-08-28 12:34 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-28 12:26 [PATCH v4 00/23] kprobes: Unify kretprobe trampoline handlers and make kretprobe lockless Masami Hiramatsu
2020-08-28 12:26 ` [PATCH v4 01/23] kprobes: Add generic kretprobe trampoline handler Masami Hiramatsu
2020-08-28 12:27 ` [PATCH v4 02/23] x86/kprobes: Use " Masami Hiramatsu
2020-08-28 12:27 ` [PATCH v4 03/23] arm: kprobes: " Masami Hiramatsu
2020-08-28 12:27 ` [PATCH v4 04/23] arm64: " Masami Hiramatsu
2020-08-28 13:31   ` Mark Rutland
2020-08-28 13:37     ` peterz
2020-08-28 13:48       ` Mark Rutland
2020-08-28 13:58       ` Masami Hiramatsu
2020-08-28 12:27 ` [PATCH v4 05/23] arc: " Masami Hiramatsu
2020-08-28 12:27 ` [PATCH v4 06/23] csky: " Masami Hiramatsu
2020-08-28 12:27 ` [PATCH v4 07/23] ia64: " Masami Hiramatsu
2020-08-28 12:28 ` [PATCH v4 08/23] mips: " Masami Hiramatsu
2020-08-28 12:28 ` [PATCH v4 09/23] parisc: " Masami Hiramatsu
2020-08-28 12:28 ` [PATCH v4 10/23] powerpc: " Masami Hiramatsu
2020-08-28 12:28 ` [PATCH v4 11/23] s390: " Masami Hiramatsu
2020-08-28 12:28 ` [PATCH v4 12/23] sh: " Masami Hiramatsu
2020-08-28 12:29 ` [PATCH v4 13/23] sparc: " Masami Hiramatsu
2020-08-28 12:29 ` [PATCH v4 14/23] kprobes: Remove NMI context check Masami Hiramatsu
2020-08-28 12:29 ` [PATCH v4 15/23] kprobes: Free kretprobe_instance with rcu callback Masami Hiramatsu
2020-08-28 12:29 ` [PATCH v4 16/23] kprobes: Make local used functions static Masami Hiramatsu
2020-08-28 12:29 ` [PATCH v4 17/23] llist: Add nonatomic __llist_add() Masami Hiramatsu
2020-08-28 12:29 ` [PATCH v4 18/23] sched: Fix try_invoke_on_locked_down_task() semantics Masami Hiramatsu
2020-08-29  2:01   ` Masami Hiramatsu
2020-08-29  7:30     ` peterz
2020-08-29 17:31       ` Paul E. McKenney
2020-08-28 12:30 ` [PATCH v4 19/23] kprobes: Remove kretprobe hash Masami Hiramatsu
2020-08-28 18:37   ` Masami Hiramatsu
2020-08-28 19:02     ` peterz
2020-08-28 19:32   ` Eddy_Wu
2020-08-28 20:29     ` Peter Zijlstra
2020-08-29  1:23       ` Masami Hiramatsu
2020-08-28 12:30 ` [PATCH v4 20/23] [RFC] kprobes: Remove task scan for updating kretprobe_instance Masami Hiramatsu
2020-08-28 12:52   ` peterz
2020-08-28 15:10     ` Masami Hiramatsu
2020-08-28 15:18       ` peterz
2020-08-28 16:01         ` Masami Hiramatsu
2020-08-28 12:30 ` [PATCH v4 21/23] asm-generic/atomic: Add try_cmpxchg() fallbacks Masami Hiramatsu
2020-08-28 12:30 ` Masami Hiramatsu [this message]
2020-08-28 12:30 ` [PATCH v4 23/23] kprobes: Replace rp->free_instance with freelist Masami Hiramatsu
2020-08-28 12:37 ` [PATCH v4 00/23] kprobes: Unify kretprobe trampoline handlers and make kretprobe lockless Masami Hiramatsu

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=159861783901.992023.12163347531003713154.stgit@devnote2 \
    --to=mhiramat@kernel.org \
    --cc=Eddy_Wu@trendmicro.com \
    --cc=anil.s.keshavamurthy@intel.com \
    --cc=cameron@moodycamel.com \
    --cc=davem@davemloft.net \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=naveen.n.rao@linux.ibm.com \
    --cc=oleg@redhat.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=will@kernel.org \
    --cc=x86@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;
as well as URLs for NNTP newsgroup(s).