]> git.itanic.dy.fi Git - linux-stable/commitdiff
XArray: Fix xas_find returning too many entries
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Sat, 18 Jan 2020 03:13:21 +0000 (22:13 -0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 29 Jan 2020 15:45:27 +0000 (16:45 +0100)
commit c44aa5e8ab58b5f4cf473970ec784c3333496a2e upstream.

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: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: stable@vger.kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
lib/test_xarray.c
lib/xarray.c

index 030034d5305ad0eb05f7d56080f84c5106ab8d30..03c3f42966cecad8fcabd712c12a75c9d0487da0 100644 (file)
@@ -2,6 +2,7 @@
 /*
  * test_xarray.c: Test the XArray API
  * Copyright (c) 2017-2018 Microsoft Corporation
+ * Copyright (c) 2019-2020 Oracle
  * Author: Matthew Wilcox <willy@infradead.org>
  */
 
@@ -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_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;
        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)
        XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT) !=
                        xa_mk_value(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, next);
+       xa_erase_index(xa, next + 1);
        XA_BUG_ON(xa, !xa_empty(xa));
 #endif
 }
index 21b7e551bd4fc72c0b7d1e62914e88db388a952e..47e17d46e5f85f922e78e4e54add193d6be79898 100644 (file)
@@ -1,7 +1,8 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
  * XArray implementation
- * Copyright (c) 2017 Microsoft Corporation
+ * Copyright (c) 2017-2018 Microsoft Corporation
+ * Copyright (c) 2018-2020 Oracle
  * Author: Matthew Wilcox <willy@infradead.org>
  */
 
@@ -1081,6 +1082,8 @@ void *xas_find(struct xa_state *xas, unsigned long max)
 
        if (xas_error(xas))
                return NULL;
+       if (xas->xa_index > max)
+               return set_bounds(xas);
 
        if (!xas->xa_node) {
                xas->xa_index = 1;
@@ -1150,6 +1153,8 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
 
        if (xas_error(xas))
                return NULL;
+       if (xas->xa_index > max)
+               goto max;
 
        if (!xas->xa_node) {
                xas->xa_index = 1;
@@ -1867,7 +1872,8 @@ void *xa_find_after(struct xarray *xa, unsigned long *indexp,
                        entry = xas_find_marked(&xas, max, filter);
                else
                        entry = xas_find(&xas, max);
-               if (xas.xa_node == XAS_BOUNDS)
+
+               if (xas_invalid(&xas))
                        break;
                if (xas_sibling(&xas))
                        continue;