Skip to content

Commit

Permalink
XArray: Fix xas_next() with a single entry at 0
Browse files Browse the repository at this point in the history
If there is only a single entry at 0, the first time we call xas_next(),
we return the entry.  Unfortunately, all subsequent times we call
xas_next(), we also return the entry at 0 instead of noticing that the
xa_index is now greater than zero.  This broke find_get_pages_contig().

Fixes: 64d3e9a ("xarray: Step through an XArray")
Reported-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
  • Loading branch information
Matthew Wilcox (Oracle) committed Jul 1, 2019
1 parent 6fbc727 commit 91abab8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/test_xarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,28 @@ static noinline void check_find_entry(struct xarray *xa)
XA_BUG_ON(xa, !xa_empty(xa));
}

static noinline void check_move_tiny(struct xarray *xa)
{
XA_STATE(xas, xa, 0);

XA_BUG_ON(xa, !xa_empty(xa));
rcu_read_lock();
XA_BUG_ON(xa, xas_next(&xas) != NULL);
XA_BUG_ON(xa, xas_next(&xas) != NULL);
rcu_read_unlock();
xa_store_index(xa, 0, GFP_KERNEL);
rcu_read_lock();
xas_set(&xas, 0);
XA_BUG_ON(xa, xas_next(&xas) != xa_mk_index(0));
XA_BUG_ON(xa, xas_next(&xas) != NULL);
xas_set(&xas, 0);
XA_BUG_ON(xa, xas_prev(&xas) != xa_mk_index(0));
XA_BUG_ON(xa, xas_prev(&xas) != NULL);
rcu_read_unlock();
xa_erase_index(xa, 0);
XA_BUG_ON(xa, !xa_empty(xa));
}

static noinline void check_move_small(struct xarray *xa, unsigned long idx)
{
XA_STATE(xas, xa, 0);
Expand Down Expand Up @@ -1217,6 +1239,8 @@ static noinline void check_move(struct xarray *xa)

xa_destroy(xa);

check_move_tiny(xa);

for (i = 0; i < 16; i++)
check_move_small(xa, 1UL << i);

Expand Down
4 changes: 4 additions & 0 deletions lib/xarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,8 @@ void *__xas_prev(struct xa_state *xas)

if (!xas_frozen(xas->xa_node))
xas->xa_index--;
if (!xas->xa_node)
return set_bounds(xas);
if (xas_not_node(xas->xa_node))
return xas_load(xas);

Expand Down Expand Up @@ -1031,6 +1033,8 @@ void *__xas_next(struct xa_state *xas)

if (!xas_frozen(xas->xa_node))
xas->xa_index++;
if (!xas->xa_node)
return set_bounds(xas);
if (xas_not_node(xas->xa_node))
return xas_load(xas);

Expand Down

0 comments on commit 91abab8

Please sign in to comment.