summaryrefslogtreecommitdiffstats
path: root/drivers/md/dm-vdo/thread-registry.c
blob: d4a077d58c60c46809c0a80e724b96ad9c5298eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright 2023 Red Hat
 */

#include "thread-registry.h"

#include <asm/current.h>
#include <linux/rculist.h>

#include "permassert.h"

/*
 * We need to be careful when using other facilities that may use thread registry functions in
 * their normal operation. For example, we do not want to invoke the logger while holding a lock.
 */

void vdo_initialize_thread_registry(struct thread_registry *registry)
{
	INIT_LIST_HEAD(&registry->links);
	spin_lock_init(&registry->lock);
}

/* Register the current thread and associate it with a data pointer. */
void vdo_register_thread(struct thread_registry *registry,
			 struct registered_thread *new_thread, const void *pointer)
{
	struct registered_thread *thread;
	bool found_it = false;

	INIT_LIST_HEAD(&new_thread->links);
	new_thread->pointer = pointer;
	new_thread->task = current;

	spin_lock(&registry->lock);
	list_for_each_entry(thread, &registry->links, links) {
		if (thread->task == current) {
			/* There should be no existing entry. */
			list_del_rcu(&thread->links);
			found_it = true;
			break;
		}
	}
	list_add_tail_rcu(&new_thread->links, &registry->links);
	spin_unlock(&registry->lock);

	VDO_ASSERT_LOG_ONLY(!found_it, "new thread not already in registry");
	if (found_it) {
		/* Ensure no RCU iterators see it before re-initializing. */
		synchronize_rcu();
		INIT_LIST_HEAD(&thread->links);
	}
}

void vdo_unregister_thread(struct thread_registry *registry)
{
	struct registered_thread *thread;
	bool found_it = false;

	spin_lock(&registry->lock);
	list_for_each_entry(thread, &registry->links, links) {
		if (thread->task == current) {
			list_del_rcu(&thread->links);
			found_it = true;
			break;
		}
	}
	spin_unlock(&registry->lock);

	VDO_ASSERT_LOG_ONLY(found_it, "thread found in registry");
	if (found_it) {
		/* Ensure no RCU iterators see it before re-initializing. */
		synchronize_rcu();
		INIT_LIST_HEAD(&thread->links);
	}
}

const void *vdo_lookup_thread(struct thread_registry *registry)
{
	struct registered_thread *thread;
	const void *result = NULL;

	rcu_read_lock();
	list_for_each_entry_rcu(thread, &registry->links, links) {
		if (thread->task == current) {
			result = thread->pointer;
			break;
		}
	}
	rcu_read_unlock();

	return result;
}