Skip to content

Commit 3095dd9

Browse files
committed
Merge tag 'xarray-6.6' of git://git.infradead.org/users/willy/xarray
Pull xarray fixes from Matthew Wilcox: - Fix a bug encountered by people using bittorrent where they'd get NULL pointer dereferences on page cache lookups when using XFS - Two documentation fixes * tag 'xarray-6.6' of git://git.infradead.org/users/willy/xarray: idr: fix param name in idr_alloc_cyclic() doc xarray: Document necessary flag in alloc functions XArray: Do not return sibling entries from xa_load()
2 parents 7402e63 + 2a15de8 commit 3095dd9

File tree

4 files changed

+92
-4
lines changed

4 files changed

+92
-4
lines changed

include/linux/xarray.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,9 @@ static inline int __must_check xa_insert_irq(struct xarray *xa,
856856
* stores the index into the @id pointer, then stores the entry at
857857
* that index. A concurrent lookup will not see an uninitialised @id.
858858
*
859+
* Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
860+
* in xa_init_flags().
861+
*
859862
* Context: Any context. Takes and releases the xa_lock. May sleep if
860863
* the @gfp flags permit.
861864
* Return: 0 on success, -ENOMEM if memory could not be allocated or
@@ -886,6 +889,9 @@ static inline __must_check int xa_alloc(struct xarray *xa, u32 *id,
886889
* stores the index into the @id pointer, then stores the entry at
887890
* that index. A concurrent lookup will not see an uninitialised @id.
888891
*
892+
* Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
893+
* in xa_init_flags().
894+
*
889895
* Context: Any context. Takes and releases the xa_lock while
890896
* disabling softirqs. May sleep if the @gfp flags permit.
891897
* Return: 0 on success, -ENOMEM if memory could not be allocated or
@@ -916,6 +922,9 @@ static inline int __must_check xa_alloc_bh(struct xarray *xa, u32 *id,
916922
* stores the index into the @id pointer, then stores the entry at
917923
* that index. A concurrent lookup will not see an uninitialised @id.
918924
*
925+
* Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
926+
* in xa_init_flags().
927+
*
919928
* Context: Process context. Takes and releases the xa_lock while
920929
* disabling interrupts. May sleep if the @gfp flags permit.
921930
* Return: 0 on success, -ENOMEM if memory could not be allocated or
@@ -949,6 +958,9 @@ static inline int __must_check xa_alloc_irq(struct xarray *xa, u32 *id,
949958
* The search for an empty entry will start at @next and will wrap
950959
* around if necessary.
951960
*
961+
* Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
962+
* in xa_init_flags().
963+
*
952964
* Context: Any context. Takes and releases the xa_lock. May sleep if
953965
* the @gfp flags permit.
954966
* Return: 0 if the allocation succeeded without wrapping. 1 if the
@@ -983,6 +995,9 @@ static inline int xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry,
983995
* The search for an empty entry will start at @next and will wrap
984996
* around if necessary.
985997
*
998+
* Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
999+
* in xa_init_flags().
1000+
*
9861001
* Context: Any context. Takes and releases the xa_lock while
9871002
* disabling softirqs. May sleep if the @gfp flags permit.
9881003
* Return: 0 if the allocation succeeded without wrapping. 1 if the
@@ -1017,6 +1032,9 @@ static inline int xa_alloc_cyclic_bh(struct xarray *xa, u32 *id, void *entry,
10171032
* The search for an empty entry will start at @next and will wrap
10181033
* around if necessary.
10191034
*
1035+
* Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
1036+
* in xa_init_flags().
1037+
*
10201038
* Context: Process context. Takes and releases the xa_lock while
10211039
* disabling interrupts. May sleep if the @gfp flags permit.
10221040
* Return: 0 if the allocation succeeded without wrapping. 1 if the

lib/idr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ EXPORT_SYMBOL_GPL(idr_alloc);
100100
* @end: The maximum ID (exclusive).
101101
* @gfp: Memory allocation flags.
102102
*
103-
* Allocates an unused ID in the range specified by @nextid and @end. If
103+
* Allocates an unused ID in the range specified by @start and @end. If
104104
* @end is <= 0, it is treated as one larger than %INT_MAX. This allows
105105
* callers to use @start + N as @end as long as N is within integer range.
106106
* The search for an unused ID will start at the last ID allocated and will

lib/xarray.c

Lines changed: 7 additions & 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))
@@ -1802,6 +1802,9 @@ EXPORT_SYMBOL(xa_get_order);
18021802
* stores the index into the @id pointer, then stores the entry at
18031803
* that index. A concurrent lookup will not see an uninitialised @id.
18041804
*
1805+
* Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
1806+
* in xa_init_flags().
1807+
*
18051808
* Context: Any context. Expects xa_lock to be held on entry. May
18061809
* release and reacquire xa_lock if @gfp flags permit.
18071810
* Return: 0 on success, -ENOMEM if memory could not be allocated or
@@ -1850,6 +1853,9 @@ EXPORT_SYMBOL(__xa_alloc);
18501853
* The search for an empty entry will start at @next and will wrap
18511854
* around if necessary.
18521855
*
1856+
* Must only be operated on an xarray initialized with flag XA_FLAGS_ALLOC set
1857+
* in xa_init_flags().
1858+
*
18531859
* Context: Any context. Expects xa_lock to be held on entry. May
18541860
* release and reacquire xa_lock if @gfp flags permit.
18551861
* Return: 0 if the allocation succeeded without wrapping. 1 if the

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)