XArray: Fix xas_find returning too many entries

If you call xas_find() with the initial index > max, it should have
returned NULL but was returning the entry at index.
Signed-off-by: default avatarMatthew Wilcox (Oracle) <willy@infradead.org>
Cc: stable@vger.kernel.org
parent 19c30f4d
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
/* /*
* test_xarray.c: Test the XArray API * test_xarray.c: Test the XArray API
* Copyright (c) 2017-2018 Microsoft Corporation * Copyright (c) 2017-2018 Microsoft Corporation
* Copyright (c) 2019-2020 Oracle
* Author: Matthew Wilcox <willy@infradead.org> * Author: Matthew Wilcox <willy@infradead.org>
*/ */
...@@ -911,6 +912,7 @@ static noinline void check_multi_find_1(struct xarray *xa, unsigned order) ...@@ -911,6 +912,7 @@ static noinline void check_multi_find_1(struct xarray *xa, unsigned order)
xa_store_order(xa, multi, order, xa_mk_value(multi), GFP_KERNEL); xa_store_order(xa, multi, order, xa_mk_value(multi), GFP_KERNEL);
XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL); XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
XA_BUG_ON(xa, xa_store_index(xa, next + 1, GFP_KERNEL) != NULL);
index = 0; index = 0;
XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) != XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) !=
...@@ -923,9 +925,12 @@ static noinline void check_multi_find_1(struct xarray *xa, unsigned order) ...@@ -923,9 +925,12 @@ static noinline void check_multi_find_1(struct xarray *xa, unsigned order)
XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT) != XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT) !=
xa_mk_value(next)); xa_mk_value(next));
XA_BUG_ON(xa, index != next); XA_BUG_ON(xa, index != next);
XA_BUG_ON(xa, xa_find_after(xa, &index, next, XA_PRESENT) != NULL);
XA_BUG_ON(xa, index != next);
xa_erase_index(xa, multi); xa_erase_index(xa, multi);
xa_erase_index(xa, next); xa_erase_index(xa, next);
xa_erase_index(xa, next + 1);
XA_BUG_ON(xa, !xa_empty(xa)); XA_BUG_ON(xa, !xa_empty(xa));
#endif #endif
} }
......
// SPDX-License-Identifier: GPL-2.0+ // SPDX-License-Identifier: GPL-2.0+
/* /*
* XArray implementation * XArray implementation
* Copyright (c) 2017 Microsoft Corporation * Copyright (c) 2017-2018 Microsoft Corporation
* Copyright (c) 2018-2020 Oracle
* Author: Matthew Wilcox <willy@infradead.org> * Author: Matthew Wilcox <willy@infradead.org>
*/ */
...@@ -1083,6 +1084,8 @@ void *xas_find(struct xa_state *xas, unsigned long max) ...@@ -1083,6 +1084,8 @@ void *xas_find(struct xa_state *xas, unsigned long max)
if (xas_error(xas) || xas->xa_node == XAS_BOUNDS) if (xas_error(xas) || xas->xa_node == XAS_BOUNDS)
return NULL; return NULL;
if (xas->xa_index > max)
return set_bounds(xas);
if (!xas->xa_node) { if (!xas->xa_node) {
xas->xa_index = 1; xas->xa_index = 1;
...@@ -1152,6 +1155,8 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark) ...@@ -1152,6 +1155,8 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
if (xas_error(xas)) if (xas_error(xas))
return NULL; return NULL;
if (xas->xa_index > max)
goto max;
if (!xas->xa_node) { if (!xas->xa_node) {
xas->xa_index = 1; xas->xa_index = 1;
...@@ -1869,7 +1874,8 @@ void *xa_find_after(struct xarray *xa, unsigned long *indexp, ...@@ -1869,7 +1874,8 @@ void *xa_find_after(struct xarray *xa, unsigned long *indexp,
entry = xas_find_marked(&xas, max, filter); entry = xas_find_marked(&xas, max, filter);
else else
entry = xas_find(&xas, max); entry = xas_find(&xas, max);
if (xas.xa_node == XAS_BOUNDS)
if (xas_invalid(&xas))
break; break;
if (xas_sibling(&xas)) if (xas_sibling(&xas))
continue; continue;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment