Loading...
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | // SPDX-License-Identifier: GPL-2.0 /* * Regression1 * Description: * Salman Qazi describes the following radix-tree bug: * * In the following case, we get can get a deadlock: * * 0. The radix tree contains two items, one has the index 0. * 1. The reader (in this case find_get_pages) takes the rcu_read_lock. * 2. The reader acquires slot(s) for item(s) including the index 0 item. * 3. The non-zero index item is deleted, and as a consequence the other item * is moved to the root of the tree. The place where it used to be is queued * for deletion after the readers finish. * 3b. The zero item is deleted, removing it from the direct slot, it remains in * the rcu-delayed indirect node. * 4. The reader looks at the index 0 slot, and finds that the page has 0 ref * count * 5. The reader looks at it again, hoping that the item will either be freed * or the ref count will increase. This never happens, as the slot it is * looking at will never be updated. Also, this slot can never be reclaimed * because the reader is holding rcu_read_lock and is in an infinite loop. * * The fix is to re-use the same "indirect" pointer case that requires a slot * lookup retry into a general "retry the lookup" bit. * * Running: * This test should run to completion in a few seconds. The above bug would * cause it to hang indefinitely. * * Upstream commit: * Not yet */ #include <linux/kernel.h> #include <linux/gfp.h> #include <linux/slab.h> #include <linux/radix-tree.h> #include <linux/rcupdate.h> #include <stdlib.h> #include <pthread.h> #include <stdio.h> #include <assert.h> #include "regression.h" static RADIX_TREE(mt_tree, GFP_KERNEL); struct page { pthread_mutex_t lock; struct rcu_head rcu; int count; unsigned long index; }; static struct page *page_alloc(int index) { struct page *p; p = malloc(sizeof(struct page)); p->count = 1; p->index = index; pthread_mutex_init(&p->lock, NULL); return p; } static void page_rcu_free(struct rcu_head *rcu) { struct page *p = container_of(rcu, struct page, rcu); assert(!p->count); pthread_mutex_destroy(&p->lock); free(p); } static void page_free(struct page *p) { call_rcu(&p->rcu, page_rcu_free); } static unsigned find_get_pages(unsigned long start, unsigned int nr_pages, struct page **pages) { XA_STATE(xas, &mt_tree, start); struct page *page; unsigned int ret = 0; rcu_read_lock(); xas_for_each(&xas, page, ULONG_MAX) { if (xas_retry(&xas, page)) continue; pthread_mutex_lock(&page->lock); if (!page->count) goto unlock; /* don't actually update page refcount */ pthread_mutex_unlock(&page->lock); /* Has the page moved? */ if (unlikely(page != xas_reload(&xas))) goto put_page; pages[ret] = page; ret++; continue; unlock: pthread_mutex_unlock(&page->lock); put_page: xas_reset(&xas); } rcu_read_unlock(); return ret; } static pthread_barrier_t worker_barrier; static void *regression1_fn(void *arg) { rcu_register_thread(); if (pthread_barrier_wait(&worker_barrier) == PTHREAD_BARRIER_SERIAL_THREAD) { int j; for (j = 0; j < 1000000; j++) { struct page *p; p = page_alloc(0); xa_lock(&mt_tree); radix_tree_insert(&mt_tree, 0, p); xa_unlock(&mt_tree); p = page_alloc(1); xa_lock(&mt_tree); radix_tree_insert(&mt_tree, 1, p); xa_unlock(&mt_tree); xa_lock(&mt_tree); p = radix_tree_delete(&mt_tree, 1); pthread_mutex_lock(&p->lock); p->count--; pthread_mutex_unlock(&p->lock); xa_unlock(&mt_tree); page_free(p); xa_lock(&mt_tree); p = radix_tree_delete(&mt_tree, 0); pthread_mutex_lock(&p->lock); p->count--; pthread_mutex_unlock(&p->lock); xa_unlock(&mt_tree); page_free(p); } } else { int j; for (j = 0; j < 100000000; j++) { struct page *pages[10]; find_get_pages(0, 10, pages); } } rcu_unregister_thread(); return NULL; } static pthread_t *threads; void regression1_test(void) { int nr_threads; int i; long arg; /* Regression #1 */ printv(1, "running regression test 1, should finish in under a minute\n"); nr_threads = 2; pthread_barrier_init(&worker_barrier, NULL, nr_threads); threads = malloc(nr_threads * sizeof(*threads)); for (i = 0; i < nr_threads; i++) { arg = i; if (pthread_create(&threads[i], NULL, regression1_fn, (void *)arg)) { perror("pthread_create"); exit(1); } } for (i = 0; i < nr_threads; i++) { if (pthread_join(threads[i], NULL)) { perror("pthread_join"); exit(1); } } free(threads); printv(1, "regression test 1, done\n"); } |