Matthew Wilcox | 4f3755d | 2016-05-20 17:02:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * multiorder.c: Multi-order radix tree entry testing |
| 3 | * Copyright (c) 2016 Intel Corporation |
| 4 | * Author: Ross Zwisler <ross.zwisler@linux.intel.com> |
| 5 | * Author: Matthew Wilcox <matthew.r.wilcox@intel.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms and conditions of the GNU General Public License, |
| 9 | * version 2, as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | */ |
| 16 | #include <linux/radix-tree.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/errno.h> |
Ross Zwisler | fd8f58c | 2018-05-18 16:09:01 -0700 | [diff] [blame] | 19 | #include <pthread.h> |
Matthew Wilcox | 4f3755d | 2016-05-20 17:02:14 -0700 | [diff] [blame] | 20 | |
| 21 | #include "test.h" |
| 22 | |
Matthew Wilcox | 4bb53bd | 2018-09-12 23:29:32 -0400 | [diff] [blame] | 23 | static int item_insert_order(struct xarray *xa, unsigned long index, |
| 24 | unsigned order) |
| 25 | { |
| 26 | XA_STATE_ORDER(xas, xa, index, order); |
| 27 | struct item *item = item_create(index, order); |
| 28 | |
| 29 | do { |
| 30 | xas_lock(&xas); |
| 31 | xas_store(&xas, item); |
| 32 | xas_unlock(&xas); |
| 33 | } while (xas_nomem(&xas, GFP_KERNEL)); |
| 34 | |
| 35 | if (!xas_error(&xas)) |
| 36 | return 0; |
| 37 | |
| 38 | free(item); |
| 39 | return xas_error(&xas); |
| 40 | } |
| 41 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 42 | void multiorder_iteration(struct xarray *xa) |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 43 | { |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 44 | XA_STATE(xas, xa, 0); |
| 45 | struct item *item; |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 46 | int i, j, err; |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 47 | |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 48 | #define NUM_ENTRIES 11 |
| 49 | int index[NUM_ENTRIES] = {0, 2, 4, 8, 16, 32, 34, 36, 64, 72, 128}; |
| 50 | int order[NUM_ENTRIES] = {1, 1, 2, 3, 4, 1, 0, 1, 3, 0, 7}; |
| 51 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 52 | printv(1, "Multiorder iteration test\n"); |
| 53 | |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 54 | for (i = 0; i < NUM_ENTRIES; i++) { |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 55 | err = item_insert_order(xa, index[i], order[i]); |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 56 | assert(!err); |
| 57 | } |
| 58 | |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 59 | for (j = 0; j < 256; j++) { |
| 60 | for (i = 0; i < NUM_ENTRIES; i++) |
| 61 | if (j <= (index[i] | ((1 << order[i]) - 1))) |
| 62 | break; |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 63 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 64 | xas_set(&xas, j); |
| 65 | xas_for_each(&xas, item, ULONG_MAX) { |
| 66 | int height = order[i] / XA_CHUNK_SHIFT; |
| 67 | int shift = height * XA_CHUNK_SHIFT; |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 68 | unsigned long mask = (1UL << order[i]) - 1; |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 69 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 70 | assert((xas.xa_index | mask) == (index[i] | mask)); |
| 71 | assert(xas.xa_node->shift == shift); |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 72 | assert(!radix_tree_is_internal_node(item)); |
| 73 | assert((item->index | mask) == (index[i] | mask)); |
| 74 | assert(item->order == order[i]); |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 75 | i++; |
| 76 | } |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 79 | item_kill_tree(xa); |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 82 | void multiorder_tagged_iteration(struct xarray *xa) |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 83 | { |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 84 | XA_STATE(xas, xa, 0); |
| 85 | struct item *item; |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 86 | int i, j; |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 87 | |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 88 | #define MT_NUM_ENTRIES 9 |
| 89 | int index[MT_NUM_ENTRIES] = {0, 2, 4, 16, 32, 40, 64, 72, 128}; |
| 90 | int order[MT_NUM_ENTRIES] = {1, 0, 2, 4, 3, 1, 3, 0, 7}; |
| 91 | |
| 92 | #define TAG_ENTRIES 7 |
| 93 | int tag_index[TAG_ENTRIES] = {0, 4, 16, 40, 64, 72, 128}; |
| 94 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 95 | printv(1, "Multiorder tagged iteration test\n"); |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 96 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 97 | for (i = 0; i < MT_NUM_ENTRIES; i++) |
| 98 | assert(!item_insert_order(xa, index[i], order[i])); |
| 99 | |
| 100 | assert(!xa_marked(xa, XA_MARK_1)); |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 101 | |
| 102 | for (i = 0; i < TAG_ENTRIES; i++) |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 103 | xa_set_mark(xa, tag_index[i], XA_MARK_1); |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 104 | |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 105 | for (j = 0; j < 256; j++) { |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 106 | int k; |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 107 | |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 108 | for (i = 0; i < TAG_ENTRIES; i++) { |
| 109 | for (k = i; index[k] < tag_index[i]; k++) |
| 110 | ; |
| 111 | if (j <= (index[k] | ((1 << order[k]) - 1))) |
| 112 | break; |
| 113 | } |
| 114 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 115 | xas_set(&xas, j); |
| 116 | xas_for_each_marked(&xas, item, ULONG_MAX, XA_MARK_1) { |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 117 | unsigned long mask; |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 118 | for (k = i; index[k] < tag_index[i]; k++) |
| 119 | ; |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 120 | mask = (1UL << order[k]) - 1; |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 121 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 122 | assert((xas.xa_index | mask) == (tag_index[i] | mask)); |
| 123 | assert(!xa_is_internal(item)); |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 124 | assert((item->index | mask) == (tag_index[i] | mask)); |
| 125 | assert(item->order == order[k]); |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 126 | i++; |
| 127 | } |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 130 | assert(tag_tagged_items(xa, 0, ULONG_MAX, TAG_ENTRIES, XA_MARK_1, |
Matthew Wilcox | 372266b | 2018-08-18 07:09:22 -0400 | [diff] [blame] | 131 | XA_MARK_2) == TAG_ENTRIES); |
Matthew Wilcox | 070c5ac | 2016-05-20 17:02:52 -0700 | [diff] [blame] | 132 | |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 133 | for (j = 0; j < 256; j++) { |
| 134 | int mask, k; |
| 135 | |
| 136 | for (i = 0; i < TAG_ENTRIES; i++) { |
| 137 | for (k = i; index[k] < tag_index[i]; k++) |
| 138 | ; |
| 139 | if (j <= (index[k] | ((1 << order[k]) - 1))) |
| 140 | break; |
| 141 | } |
| 142 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 143 | xas_set(&xas, j); |
| 144 | xas_for_each_marked(&xas, item, ULONG_MAX, XA_MARK_2) { |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 145 | for (k = i; index[k] < tag_index[i]; k++) |
| 146 | ; |
| 147 | mask = (1 << order[k]) - 1; |
| 148 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 149 | assert((xas.xa_index | mask) == (tag_index[i] | mask)); |
| 150 | assert(!xa_is_internal(item)); |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 151 | assert((item->index | mask) == (tag_index[i] | mask)); |
| 152 | assert(item->order == order[k]); |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 153 | i++; |
| 154 | } |
Matthew Wilcox | 070c5ac | 2016-05-20 17:02:52 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 157 | assert(tag_tagged_items(xa, 1, ULONG_MAX, MT_NUM_ENTRIES * 2, XA_MARK_1, |
Matthew Wilcox | 372266b | 2018-08-18 07:09:22 -0400 | [diff] [blame] | 158 | XA_MARK_0) == TAG_ENTRIES); |
Matthew Wilcox | 070c5ac | 2016-05-20 17:02:52 -0700 | [diff] [blame] | 159 | i = 0; |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 160 | xas_set(&xas, 0); |
| 161 | xas_for_each_marked(&xas, item, ULONG_MAX, XA_MARK_0) { |
| 162 | assert(xas.xa_index == tag_index[i]); |
Matthew Wilcox | 070c5ac | 2016-05-20 17:02:52 -0700 | [diff] [blame] | 163 | i++; |
| 164 | } |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 165 | assert(i == TAG_ENTRIES); |
Matthew Wilcox | 070c5ac | 2016-05-20 17:02:52 -0700 | [diff] [blame] | 166 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 167 | item_kill_tree(xa); |
Ross Zwisler | 643b57d | 2016-05-20 17:02:29 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Ross Zwisler | fd8f58c | 2018-05-18 16:09:01 -0700 | [diff] [blame] | 170 | bool stop_iteration = false; |
| 171 | |
| 172 | static void *creator_func(void *ptr) |
| 173 | { |
| 174 | /* 'order' is set up to ensure we have sibling entries */ |
| 175 | unsigned int order = RADIX_TREE_MAP_SHIFT - 1; |
| 176 | struct radix_tree_root *tree = ptr; |
| 177 | int i; |
| 178 | |
| 179 | for (i = 0; i < 10000; i++) { |
| 180 | item_insert_order(tree, 0, order); |
| 181 | item_delete_rcu(tree, 0); |
| 182 | } |
| 183 | |
| 184 | stop_iteration = true; |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | static void *iterator_func(void *ptr) |
| 189 | { |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 190 | XA_STATE(xas, ptr, 0); |
Ross Zwisler | fd8f58c | 2018-05-18 16:09:01 -0700 | [diff] [blame] | 191 | struct item *item; |
Ross Zwisler | fd8f58c | 2018-05-18 16:09:01 -0700 | [diff] [blame] | 192 | |
| 193 | while (!stop_iteration) { |
| 194 | rcu_read_lock(); |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 195 | xas_for_each(&xas, item, ULONG_MAX) { |
| 196 | if (xas_retry(&xas, item)) |
Ross Zwisler | fd8f58c | 2018-05-18 16:09:01 -0700 | [diff] [blame] | 197 | continue; |
Ross Zwisler | fd8f58c | 2018-05-18 16:09:01 -0700 | [diff] [blame] | 198 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 199 | item_sanity(item, xas.xa_index); |
Ross Zwisler | fd8f58c | 2018-05-18 16:09:01 -0700 | [diff] [blame] | 200 | } |
| 201 | rcu_read_unlock(); |
| 202 | } |
| 203 | return NULL; |
| 204 | } |
| 205 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 206 | static void multiorder_iteration_race(struct xarray *xa) |
Ross Zwisler | fd8f58c | 2018-05-18 16:09:01 -0700 | [diff] [blame] | 207 | { |
| 208 | const int num_threads = sysconf(_SC_NPROCESSORS_ONLN); |
| 209 | pthread_t worker_thread[num_threads]; |
Ross Zwisler | fd8f58c | 2018-05-18 16:09:01 -0700 | [diff] [blame] | 210 | int i; |
| 211 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 212 | pthread_create(&worker_thread[0], NULL, &creator_func, xa); |
Ross Zwisler | fd8f58c | 2018-05-18 16:09:01 -0700 | [diff] [blame] | 213 | for (i = 1; i < num_threads; i++) |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 214 | pthread_create(&worker_thread[i], NULL, &iterator_func, xa); |
Ross Zwisler | fd8f58c | 2018-05-18 16:09:01 -0700 | [diff] [blame] | 215 | |
| 216 | for (i = 0; i < num_threads; i++) |
| 217 | pthread_join(worker_thread[i], NULL); |
| 218 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 219 | item_kill_tree(xa); |
Ross Zwisler | fd8f58c | 2018-05-18 16:09:01 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 222 | static DEFINE_XARRAY(array); |
| 223 | |
Matthew Wilcox | 4f3755d | 2016-05-20 17:02:14 -0700 | [diff] [blame] | 224 | void multiorder_checks(void) |
| 225 | { |
Matthew Wilcox | 542980a | 2018-09-22 16:12:41 -0400 | [diff] [blame] | 226 | multiorder_iteration(&array); |
| 227 | multiorder_tagged_iteration(&array); |
| 228 | multiorder_iteration_race(&array); |
Matthew Wilcox | 2791653 | 2016-12-14 15:09:04 -0800 | [diff] [blame] | 229 | |
| 230 | radix_tree_cpu_dead(0); |
Matthew Wilcox | 4f3755d | 2016-05-20 17:02:14 -0700 | [diff] [blame] | 231 | } |
Matthew Wilcox | 8ac0486 | 2016-12-18 22:56:05 -0500 | [diff] [blame] | 232 | |
| 233 | int __weak main(void) |
| 234 | { |
| 235 | radix_tree_init(); |
| 236 | multiorder_checks(); |
| 237 | return 0; |
| 238 | } |