1 // SPDX-License-Identifier: GPL-2.0
5 * Salman Qazi describes the following radix-tree bug:
7 * In the following case, we get can get a deadlock:
9 * 0. The radix tree contains two items, one has the index 0.
10 * 1. The reader (in this case find_get_pages) takes the rcu_read_lock.
11 * 2. The reader acquires slot(s) for item(s) including the index 0 item.
12 * 3. The non-zero index item is deleted, and as a consequence the other item
13 * is moved to the root of the tree. The place where it used to be is queued
14 * for deletion after the readers finish.
15 * 3b. The zero item is deleted, removing it from the direct slot, it remains in
16 * the rcu-delayed indirect node.
17 * 4. The reader looks at the index 0 slot, and finds that the page has 0 ref
19 * 5. The reader looks at it again, hoping that the item will either be freed
20 * or the ref count will increase. This never happens, as the slot it is
21 * looking at will never be updated. Also, this slot can never be reclaimed
22 * because the reader is holding rcu_read_lock and is in an infinite loop.
24 * The fix is to re-use the same "indirect" pointer case that requires a slot
25 * lookup retry into a general "retry the lookup" bit.
28 * This test should run to completion in a few seconds. The above bug would
29 * cause it to hang indefinitely.
34 #include <linux/kernel.h>
35 #include <linux/gfp.h>
36 #include <linux/slab.h>
37 #include <linux/radix-tree.h>
38 #include <linux/rcupdate.h>
44 #include "regression.h"
46 static RADIX_TREE(mt_tree
, GFP_KERNEL
);
55 static struct page
*page_alloc(int index
)
58 p
= malloc(sizeof(struct page
));
61 pthread_mutex_init(&p
->lock
, NULL
);
66 static void page_rcu_free(struct rcu_head
*rcu
)
68 struct page
*p
= container_of(rcu
, struct page
, rcu
);
70 pthread_mutex_destroy(&p
->lock
);
74 static void page_free(struct page
*p
)
76 call_rcu(&p
->rcu
, page_rcu_free
);
79 static unsigned find_get_pages(unsigned long start
,
80 unsigned int nr_pages
, struct page
**pages
)
82 XA_STATE(xas
, &mt_tree
, start
);
87 xas_for_each(&xas
, page
, ULONG_MAX
) {
88 if (xas_retry(&xas
, page
))
91 pthread_mutex_lock(&page
->lock
);
95 /* don't actually update page refcount */
96 pthread_mutex_unlock(&page
->lock
);
98 /* Has the page moved? */
99 if (unlikely(page
!= xas_reload(&xas
)))
106 pthread_mutex_unlock(&page
->lock
);
114 static pthread_barrier_t worker_barrier
;
116 static void *regression1_fn(void *arg
)
118 rcu_register_thread();
120 if (pthread_barrier_wait(&worker_barrier
) ==
121 PTHREAD_BARRIER_SERIAL_THREAD
) {
124 for (j
= 0; j
< 1000000; j
++) {
129 radix_tree_insert(&mt_tree
, 0, p
);
134 radix_tree_insert(&mt_tree
, 1, p
);
138 p
= radix_tree_delete(&mt_tree
, 1);
139 pthread_mutex_lock(&p
->lock
);
141 pthread_mutex_unlock(&p
->lock
);
146 p
= radix_tree_delete(&mt_tree
, 0);
147 pthread_mutex_lock(&p
->lock
);
149 pthread_mutex_unlock(&p
->lock
);
156 for (j
= 0; j
< 100000000; j
++) {
157 struct page
*pages
[10];
159 find_get_pages(0, 10, pages
);
163 rcu_unregister_thread();
168 static pthread_t
*threads
;
169 void regression1_test(void)
176 printv(1, "running regression test 1, should finish in under a minute\n");
178 pthread_barrier_init(&worker_barrier
, NULL
, nr_threads
);
180 threads
= malloc(nr_threads
* sizeof(pthread_t
*));
182 for (i
= 0; i
< nr_threads
; i
++) {
184 if (pthread_create(&threads
[i
], NULL
, regression1_fn
, (void *)arg
)) {
185 perror("pthread_create");
190 for (i
= 0; i
< nr_threads
; i
++) {
191 if (pthread_join(threads
[i
], NULL
)) {
192 perror("pthread_join");
199 printv(1, "regression test 1, done\n");