17e934cf5SMatthew Wilcox (Oracle) // SPDX-License-Identifier: GPL-2.0-or-later
27e934cf5SMatthew Wilcox (Oracle) /*
37e934cf5SMatthew Wilcox (Oracle)  * iteration_check_2.c: Check that deleting a tagged entry doesn't cause
47e934cf5SMatthew Wilcox (Oracle)  * an RCU walker to finish early.
57e934cf5SMatthew Wilcox (Oracle)  * Copyright (c) 2020 Oracle
67e934cf5SMatthew Wilcox (Oracle)  * Author: Matthew Wilcox <willy@infradead.org>
77e934cf5SMatthew Wilcox (Oracle)  */
87e934cf5SMatthew Wilcox (Oracle) #include <pthread.h>
97e934cf5SMatthew Wilcox (Oracle) #include "test.h"
107e934cf5SMatthew Wilcox (Oracle) 
117e934cf5SMatthew Wilcox (Oracle) static volatile bool test_complete;
127e934cf5SMatthew Wilcox (Oracle) 
iterator(void * arg)137e934cf5SMatthew Wilcox (Oracle) static void *iterator(void *arg)
147e934cf5SMatthew Wilcox (Oracle) {
157e934cf5SMatthew Wilcox (Oracle) 	XA_STATE(xas, arg, 0);
167e934cf5SMatthew Wilcox (Oracle) 	void *entry;
177e934cf5SMatthew Wilcox (Oracle) 
187e934cf5SMatthew Wilcox (Oracle) 	rcu_register_thread();
197e934cf5SMatthew Wilcox (Oracle) 
207e934cf5SMatthew Wilcox (Oracle) 	while (!test_complete) {
217e934cf5SMatthew Wilcox (Oracle) 		xas_set(&xas, 0);
227e934cf5SMatthew Wilcox (Oracle) 		rcu_read_lock();
237e934cf5SMatthew Wilcox (Oracle) 		xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0)
247e934cf5SMatthew Wilcox (Oracle) 			;
257e934cf5SMatthew Wilcox (Oracle) 		rcu_read_unlock();
267e934cf5SMatthew Wilcox (Oracle) 		assert(xas.xa_index >= 100);
277e934cf5SMatthew Wilcox (Oracle) 	}
287e934cf5SMatthew Wilcox (Oracle) 
297e934cf5SMatthew Wilcox (Oracle) 	rcu_unregister_thread();
307e934cf5SMatthew Wilcox (Oracle) 	return NULL;
317e934cf5SMatthew Wilcox (Oracle) }
327e934cf5SMatthew Wilcox (Oracle) 
throbber(void * arg)337e934cf5SMatthew Wilcox (Oracle) static void *throbber(void *arg)
347e934cf5SMatthew Wilcox (Oracle) {
357e934cf5SMatthew Wilcox (Oracle) 	struct xarray *xa = arg;
367e934cf5SMatthew Wilcox (Oracle) 
377e934cf5SMatthew Wilcox (Oracle) 	rcu_register_thread();
387e934cf5SMatthew Wilcox (Oracle) 
397e934cf5SMatthew Wilcox (Oracle) 	while (!test_complete) {
407e934cf5SMatthew Wilcox (Oracle) 		int i;
417e934cf5SMatthew Wilcox (Oracle) 
427e934cf5SMatthew Wilcox (Oracle) 		for (i = 0; i < 100; i++) {
437e934cf5SMatthew Wilcox (Oracle) 			xa_store(xa, i, xa_mk_value(i), GFP_KERNEL);
447e934cf5SMatthew Wilcox (Oracle) 			xa_set_mark(xa, i, XA_MARK_0);
457e934cf5SMatthew Wilcox (Oracle) 		}
467e934cf5SMatthew Wilcox (Oracle) 		for (i = 0; i < 100; i++)
477e934cf5SMatthew Wilcox (Oracle) 			xa_erase(xa, i);
487e934cf5SMatthew Wilcox (Oracle) 	}
497e934cf5SMatthew Wilcox (Oracle) 
507e934cf5SMatthew Wilcox (Oracle) 	rcu_unregister_thread();
517e934cf5SMatthew Wilcox (Oracle) 	return NULL;
527e934cf5SMatthew Wilcox (Oracle) }
537e934cf5SMatthew Wilcox (Oracle) 
iteration_test2(unsigned test_duration)547e934cf5SMatthew Wilcox (Oracle) void iteration_test2(unsigned test_duration)
557e934cf5SMatthew Wilcox (Oracle) {
567e934cf5SMatthew Wilcox (Oracle) 	pthread_t threads[2];
577e934cf5SMatthew Wilcox (Oracle) 	DEFINE_XARRAY(array);
587e934cf5SMatthew Wilcox (Oracle) 	int i;
597e934cf5SMatthew Wilcox (Oracle) 
607e934cf5SMatthew Wilcox (Oracle) 	printv(1, "Running iteration test 2 for %d seconds\n", test_duration);
617e934cf5SMatthew Wilcox (Oracle) 
627e934cf5SMatthew Wilcox (Oracle) 	test_complete = false;
637e934cf5SMatthew Wilcox (Oracle) 
647e934cf5SMatthew Wilcox (Oracle) 	xa_store(&array, 100, xa_mk_value(100), GFP_KERNEL);
657e934cf5SMatthew Wilcox (Oracle) 	xa_set_mark(&array, 100, XA_MARK_0);
667e934cf5SMatthew Wilcox (Oracle) 
677e934cf5SMatthew Wilcox (Oracle) 	if (pthread_create(&threads[0], NULL, iterator, &array)) {
687e934cf5SMatthew Wilcox (Oracle) 		perror("create iterator thread");
697e934cf5SMatthew Wilcox (Oracle) 		exit(1);
707e934cf5SMatthew Wilcox (Oracle) 	}
717e934cf5SMatthew Wilcox (Oracle) 	if (pthread_create(&threads[1], NULL, throbber, &array)) {
727e934cf5SMatthew Wilcox (Oracle) 		perror("create throbber thread");
737e934cf5SMatthew Wilcox (Oracle) 		exit(1);
747e934cf5SMatthew Wilcox (Oracle) 	}
757e934cf5SMatthew Wilcox (Oracle) 
767e934cf5SMatthew Wilcox (Oracle) 	sleep(test_duration);
777e934cf5SMatthew Wilcox (Oracle) 	test_complete = true;
787e934cf5SMatthew Wilcox (Oracle) 
797e934cf5SMatthew Wilcox (Oracle) 	for (i = 0; i < 2; i++) {
807e934cf5SMatthew Wilcox (Oracle) 		if (pthread_join(threads[i], NULL)) {
817e934cf5SMatthew Wilcox (Oracle) 			perror("pthread_join");
827e934cf5SMatthew Wilcox (Oracle) 			exit(1);
837e934cf5SMatthew Wilcox (Oracle) 		}
847e934cf5SMatthew Wilcox (Oracle) 	}
857e934cf5SMatthew Wilcox (Oracle) 
867e934cf5SMatthew Wilcox (Oracle) 	xa_destroy(&array);
877e934cf5SMatthew Wilcox (Oracle) }
88