aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMatthew Wilcox <willy@infradead.org>2017-11-17 08:16:34 -0500
committerMatthew Wilcox <willy@infradead.org>2018-10-21 10:45:59 -0400
commit687149fca1f37c447e5d161e0a4a04cb2c880cb6 (patch)
tree5134bc243622997b10644a67a55ee51e6278930a /lib
parent80a0a1a9a3cde9b23851e8eb7160e2786549306a (diff)
xarray: Destroy an XArray
This function frees all the internal memory allocated to the xarray and reinitialises it to be empty. Signed-off-by: Matthew Wilcox <willy@infradead.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/test_xarray.c34
-rw-r--r--lib/xarray.c28
2 files changed, 62 insertions, 0 deletions
diff --git a/lib/test_xarray.c b/lib/test_xarray.c
index e3c2d4d00b15..a96f67caa1c2 100644
--- a/lib/test_xarray.c
+++ b/lib/test_xarray.c
@@ -465,6 +465,39 @@ static noinline void check_find(struct xarray *xa)
465 check_multi_find_2(xa); 465 check_multi_find_2(xa);
466} 466}
467 467
468static noinline void check_destroy(struct xarray *xa)
469{
470 unsigned long index;
471
472 XA_BUG_ON(xa, !xa_empty(xa));
473
474 /* Destroying an empty array is a no-op */
475 xa_destroy(xa);
476 XA_BUG_ON(xa, !xa_empty(xa));
477
478 /* Destroying an array with a single entry */
479 for (index = 0; index < 1000; index++) {
480 xa_store_index(xa, index, GFP_KERNEL);
481 XA_BUG_ON(xa, xa_empty(xa));
482 xa_destroy(xa);
483 XA_BUG_ON(xa, !xa_empty(xa));
484 }
485
486 /* Destroying an array with a single entry at ULONG_MAX */
487 xa_store(xa, ULONG_MAX, xa, GFP_KERNEL);
488 XA_BUG_ON(xa, xa_empty(xa));
489 xa_destroy(xa);
490 XA_BUG_ON(xa, !xa_empty(xa));
491
492#ifdef CONFIG_XARRAY_MULTI
493 /* Destroying an array with a multi-index entry */
494 xa_store_order(xa, 1 << 11, 11, xa, GFP_KERNEL);
495 XA_BUG_ON(xa, xa_empty(xa));
496 xa_destroy(xa);
497 XA_BUG_ON(xa, !xa_empty(xa));
498#endif
499}
500
468static DEFINE_XARRAY(array); 501static DEFINE_XARRAY(array);
469 502
470static int xarray_checks(void) 503static int xarray_checks(void)
@@ -478,6 +511,7 @@ static int xarray_checks(void)
478 check_cmpxchg(&array); 511 check_cmpxchg(&array);
479 check_multi_store(&array); 512 check_multi_store(&array);
480 check_find(&array); 513 check_find(&array);
514 check_destroy(&array);
481 515
482 printk("XArray: %u of %u tests passed\n", tests_passed, tests_run); 516 printk("XArray: %u of %u tests passed\n", tests_passed, tests_run);
483 return (tests_run == tests_passed) ? 0 : -EINVAL; 517 return (tests_run == tests_passed) ? 0 : -EINVAL;
diff --git a/lib/xarray.c b/lib/xarray.c
index 70e04810c8c2..057dbe0d3bf6 100644
--- a/lib/xarray.c
+++ b/lib/xarray.c
@@ -1524,6 +1524,34 @@ unsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start,
1524} 1524}
1525EXPORT_SYMBOL(xa_extract); 1525EXPORT_SYMBOL(xa_extract);
1526 1526
1527/**
1528 * xa_destroy() - Free all internal data structures.
1529 * @xa: XArray.
1530 *
1531 * After calling this function, the XArray is empty and has freed all memory
1532 * allocated for its internal data structures. You are responsible for
1533 * freeing the objects referenced by the XArray.
1534 *
1535 * Context: Any context. Takes and releases the xa_lock, interrupt-safe.
1536 */
1537void xa_destroy(struct xarray *xa)
1538{
1539 XA_STATE(xas, xa, 0);
1540 unsigned long flags;
1541 void *entry;
1542
1543 xas.xa_node = NULL;
1544 xas_lock_irqsave(&xas, flags);
1545 entry = xa_head_locked(xa);
1546 RCU_INIT_POINTER(xa->xa_head, NULL);
1547 xas_init_marks(&xas);
1548 /* lockdep checks we're still holding the lock in xas_free_nodes() */
1549 if (xa_is_node(entry))
1550 xas_free_nodes(&xas, xa_to_node(entry));
1551 xas_unlock_irqrestore(&xas, flags);
1552}
1553EXPORT_SYMBOL(xa_destroy);
1554
1527#ifdef XA_DEBUG 1555#ifdef XA_DEBUG
1528void xa_dump_node(const struct xa_node *node) 1556void xa_dump_node(const struct xa_node *node)
1529{ 1557{