Skip to content

Commit cbc0285

Browse files
author
Matthew Wilcox (Oracle)
committed
XArray: Do not return sibling entries from xa_load()
It is possible for xa_load() to observe a sibling entry pointing to another sibling entry. An example: Thread A: Thread B: xa_store_range(xa, entry, 188, 191, gfp); xa_load(xa, 191); entry = xa_entry(xa, node, 63); [entry is a sibling of 188] xa_store_range(xa, entry, 184, 191, gfp); if (xa_is_sibling(entry)) offset = xa_to_sibling(entry); entry = xa_entry(xas->xa, node, offset); [entry is now a sibling of 184] It is sufficient to go around this loop until we hit a non-sibling entry. Sibling entries always point earlier in the node, so we are guaranteed to terminate this search. Signed-off-by: Matthew Wilcox (Oracle) <[email protected]> Fixes: 6b24ca4 ("mm: Use multi-index entries in the page cache") Cc: [email protected]
1 parent f837f0a commit cbc0285

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

lib/xarray.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static void *xas_descend(struct xa_state *xas, struct xa_node *node)
206206
void *entry = xa_entry(xas->xa, node, offset);
207207

208208
xas->xa_node = node;
209-
if (xa_is_sibling(entry)) {
209+
while (xa_is_sibling(entry)) {
210210
offset = xa_to_sibling(entry);
211211
entry = xa_entry(xas->xa, node, offset);
212212
if (node->shift && xa_is_node(entry))

tools/testing/radix-tree/multiorder.c

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void multiorder_tagged_iteration(struct xarray *xa)
159159
item_kill_tree(xa);
160160
}
161161

162-
bool stop_iteration = false;
162+
bool stop_iteration;
163163

164164
static void *creator_func(void *ptr)
165165
{
@@ -201,6 +201,7 @@ static void multiorder_iteration_race(struct xarray *xa)
201201
pthread_t worker_thread[num_threads];
202202
int i;
203203

204+
stop_iteration = false;
204205
pthread_create(&worker_thread[0], NULL, &creator_func, xa);
205206
for (i = 1; i < num_threads; i++)
206207
pthread_create(&worker_thread[i], NULL, &iterator_func, xa);
@@ -211,19 +212,82 @@ static void multiorder_iteration_race(struct xarray *xa)
211212
item_kill_tree(xa);
212213
}
213214

215+
static void *load_creator(void *ptr)
216+
{
217+
/* 'order' is set up to ensure we have sibling entries */
218+
unsigned int order;
219+
struct radix_tree_root *tree = ptr;
220+
int i;
221+
222+
rcu_register_thread();
223+
item_insert_order(tree, 3 << RADIX_TREE_MAP_SHIFT, 0);
224+
item_insert_order(tree, 2 << RADIX_TREE_MAP_SHIFT, 0);
225+
for (i = 0; i < 10000; i++) {
226+
for (order = 1; order < RADIX_TREE_MAP_SHIFT; order++) {
227+
unsigned long index = (3 << RADIX_TREE_MAP_SHIFT) -
228+
(1 << order);
229+
item_insert_order(tree, index, order);
230+
item_delete_rcu(tree, index);
231+
}
232+
}
233+
rcu_unregister_thread();
234+
235+
stop_iteration = true;
236+
return NULL;
237+
}
238+
239+
static void *load_worker(void *ptr)
240+
{
241+
unsigned long index = (3 << RADIX_TREE_MAP_SHIFT) - 1;
242+
243+
rcu_register_thread();
244+
while (!stop_iteration) {
245+
struct item *item = xa_load(ptr, index);
246+
assert(!xa_is_internal(item));
247+
}
248+
rcu_unregister_thread();
249+
250+
return NULL;
251+
}
252+
253+
static void load_race(struct xarray *xa)
254+
{
255+
const int num_threads = sysconf(_SC_NPROCESSORS_ONLN) * 4;
256+
pthread_t worker_thread[num_threads];
257+
int i;
258+
259+
stop_iteration = false;
260+
pthread_create(&worker_thread[0], NULL, &load_creator, xa);
261+
for (i = 1; i < num_threads; i++)
262+
pthread_create(&worker_thread[i], NULL, &load_worker, xa);
263+
264+
for (i = 0; i < num_threads; i++)
265+
pthread_join(worker_thread[i], NULL);
266+
267+
item_kill_tree(xa);
268+
}
269+
214270
static DEFINE_XARRAY(array);
215271

216272
void multiorder_checks(void)
217273
{
218274
multiorder_iteration(&array);
219275
multiorder_tagged_iteration(&array);
220276
multiorder_iteration_race(&array);
277+
load_race(&array);
221278

222279
radix_tree_cpu_dead(0);
223280
}
224281

225-
int __weak main(void)
282+
int __weak main(int argc, char **argv)
226283
{
284+
int opt;
285+
286+
while ((opt = getopt(argc, argv, "ls:v")) != -1) {
287+
if (opt == 'v')
288+
test_verbose++;
289+
}
290+
227291
rcu_register_thread();
228292
radix_tree_init();
229293
multiorder_checks();

0 commit comments

Comments
 (0)