xref: /linux/drivers/md/dm-vdo/thread-registry.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
189f9b701SMatthew Sakai // SPDX-License-Identifier: GPL-2.0-only
289f9b701SMatthew Sakai /*
389f9b701SMatthew Sakai  * Copyright 2023 Red Hat
489f9b701SMatthew Sakai  */
589f9b701SMatthew Sakai 
689f9b701SMatthew Sakai #include "thread-registry.h"
789f9b701SMatthew Sakai 
820be466cSMike Snitzer #include <asm/current.h>
989f9b701SMatthew Sakai #include <linux/rculist.h>
1089f9b701SMatthew Sakai 
1189f9b701SMatthew Sakai #include "permassert.h"
1289f9b701SMatthew Sakai 
1389f9b701SMatthew Sakai /*
1489f9b701SMatthew Sakai  * We need to be careful when using other facilities that may use thread registry functions in
1589f9b701SMatthew Sakai  * their normal operation. For example, we do not want to invoke the logger while holding a lock.
1689f9b701SMatthew Sakai  */
1789f9b701SMatthew Sakai 
vdo_initialize_thread_registry(struct thread_registry * registry)1882b354ffSMike Snitzer void vdo_initialize_thread_registry(struct thread_registry *registry)
1989f9b701SMatthew Sakai {
2089f9b701SMatthew Sakai 	INIT_LIST_HEAD(&registry->links);
2189f9b701SMatthew Sakai 	spin_lock_init(&registry->lock);
2289f9b701SMatthew Sakai }
2389f9b701SMatthew Sakai 
2489f9b701SMatthew Sakai /* Register the current thread and associate it with a data pointer. */
vdo_register_thread(struct thread_registry * registry,struct registered_thread * new_thread,const void * pointer)2582b354ffSMike Snitzer void vdo_register_thread(struct thread_registry *registry,
2689f9b701SMatthew Sakai 			 struct registered_thread *new_thread, const void *pointer)
2789f9b701SMatthew Sakai {
2889f9b701SMatthew Sakai 	struct registered_thread *thread;
2989f9b701SMatthew Sakai 	bool found_it = false;
3089f9b701SMatthew Sakai 
3189f9b701SMatthew Sakai 	INIT_LIST_HEAD(&new_thread->links);
3289f9b701SMatthew Sakai 	new_thread->pointer = pointer;
3389f9b701SMatthew Sakai 	new_thread->task = current;
3489f9b701SMatthew Sakai 
3589f9b701SMatthew Sakai 	spin_lock(&registry->lock);
3689f9b701SMatthew Sakai 	list_for_each_entry(thread, &registry->links, links) {
3789f9b701SMatthew Sakai 		if (thread->task == current) {
3889f9b701SMatthew Sakai 			/* There should be no existing entry. */
3989f9b701SMatthew Sakai 			list_del_rcu(&thread->links);
4089f9b701SMatthew Sakai 			found_it = true;
4189f9b701SMatthew Sakai 			break;
4289f9b701SMatthew Sakai 		}
4389f9b701SMatthew Sakai 	}
4489f9b701SMatthew Sakai 	list_add_tail_rcu(&new_thread->links, &registry->links);
4589f9b701SMatthew Sakai 	spin_unlock(&registry->lock);
4689f9b701SMatthew Sakai 
47*6a79248bSMike Snitzer 	VDO_ASSERT_LOG_ONLY(!found_it, "new thread not already in registry");
4889f9b701SMatthew Sakai 	if (found_it) {
4989f9b701SMatthew Sakai 		/* Ensure no RCU iterators see it before re-initializing. */
5089f9b701SMatthew Sakai 		synchronize_rcu();
5189f9b701SMatthew Sakai 		INIT_LIST_HEAD(&thread->links);
5289f9b701SMatthew Sakai 	}
5389f9b701SMatthew Sakai }
5489f9b701SMatthew Sakai 
vdo_unregister_thread(struct thread_registry * registry)5582b354ffSMike Snitzer void vdo_unregister_thread(struct thread_registry *registry)
5689f9b701SMatthew Sakai {
5789f9b701SMatthew Sakai 	struct registered_thread *thread;
5889f9b701SMatthew Sakai 	bool found_it = false;
5989f9b701SMatthew Sakai 
6089f9b701SMatthew Sakai 	spin_lock(&registry->lock);
6189f9b701SMatthew Sakai 	list_for_each_entry(thread, &registry->links, links) {
6289f9b701SMatthew Sakai 		if (thread->task == current) {
6389f9b701SMatthew Sakai 			list_del_rcu(&thread->links);
6489f9b701SMatthew Sakai 			found_it = true;
6589f9b701SMatthew Sakai 			break;
6689f9b701SMatthew Sakai 		}
6789f9b701SMatthew Sakai 	}
6889f9b701SMatthew Sakai 	spin_unlock(&registry->lock);
6989f9b701SMatthew Sakai 
70*6a79248bSMike Snitzer 	VDO_ASSERT_LOG_ONLY(found_it, "thread found in registry");
7189f9b701SMatthew Sakai 	if (found_it) {
7289f9b701SMatthew Sakai 		/* Ensure no RCU iterators see it before re-initializing. */
7389f9b701SMatthew Sakai 		synchronize_rcu();
7489f9b701SMatthew Sakai 		INIT_LIST_HEAD(&thread->links);
7589f9b701SMatthew Sakai 	}
7689f9b701SMatthew Sakai }
7789f9b701SMatthew Sakai 
vdo_lookup_thread(struct thread_registry * registry)7882b354ffSMike Snitzer const void *vdo_lookup_thread(struct thread_registry *registry)
7989f9b701SMatthew Sakai {
8089f9b701SMatthew Sakai 	struct registered_thread *thread;
8189f9b701SMatthew Sakai 	const void *result = NULL;
8289f9b701SMatthew Sakai 
8389f9b701SMatthew Sakai 	rcu_read_lock();
8489f9b701SMatthew Sakai 	list_for_each_entry_rcu(thread, &registry->links, links) {
8589f9b701SMatthew Sakai 		if (thread->task == current) {
8689f9b701SMatthew Sakai 			result = thread->pointer;
8789f9b701SMatthew Sakai 			break;
8889f9b701SMatthew Sakai 		}
8989f9b701SMatthew Sakai 	}
9089f9b701SMatthew Sakai 	rcu_read_unlock();
9189f9b701SMatthew Sakai 
9289f9b701SMatthew Sakai 	return result;
9389f9b701SMatthew Sakai }
94