]> www.infradead.org Git - users/willy/xarray.git/commitdiff
scsi: tcmu: Convert to XArray
authorMatthew Wilcox <willy@infradead.org>
Wed, 24 Oct 2018 15:03:37 +0000 (11:03 -0400)
committerMatthew Wilcox (Oracle) <willy@infradead.org>
Fri, 9 Aug 2019 01:38:11 +0000 (21:38 -0400)
I used xa_for_each() instead of a counting loop which will speed up
deleting sparsely-populated devices.

Signed-off-by: Matthew Wilcox <willy@infradead.org>
drivers/target/target_core_user.c

index 04eda111920ec4cae3d41184cdfac9afde63922c..e384cf6e6159330692f87adf0cd1ba697c1ec015 100644 (file)
@@ -14,7 +14,7 @@
 #include <linux/parser.h>
 #include <linux/vmalloc.h>
 #include <linux/uio_driver.h>
-#include <linux/radix-tree.h>
+#include <linux/xarray.h>
 #include <linux/stringify.h>
 #include <linux/bitops.h>
 #include <linux/highmem.h>
@@ -141,7 +141,7 @@ struct tcmu_dev {
        uint32_t dbi_max;
        uint32_t dbi_thresh;
        unsigned long *data_bitmap;
-       struct radix_tree_root data_blocks;
+       struct xarray data_blocks;
 
        struct idr commands;
 
@@ -486,13 +486,13 @@ static inline bool tcmu_get_empty_block(struct tcmu_dev *udev,
                                        struct tcmu_cmd *tcmu_cmd)
 {
        struct page *page;
-       int ret, dbi;
+       int dbi;
 
        dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh);
        if (dbi == udev->dbi_thresh)
                return false;
 
-       page = radix_tree_lookup(&udev->data_blocks, dbi);
+       page = xa_load(&udev->data_blocks, dbi);
        if (!page) {
                if (atomic_add_return(1, &global_db_count) >
                                      tcmu_global_max_blocks)
@@ -503,8 +503,7 @@ static inline bool tcmu_get_empty_block(struct tcmu_dev *udev,
                if (!page)
                        goto err_alloc;
 
-               ret = radix_tree_insert(&udev->data_blocks, dbi, page);
-               if (ret)
+               if (xa_store(&udev->data_blocks, dbi, page, GFP_KERNEL))
                        goto err_insert;
        }
 
@@ -537,7 +536,7 @@ static bool tcmu_get_empty_blocks(struct tcmu_dev *udev,
 static inline struct page *
 tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
 {
-       return radix_tree_lookup(&udev->data_blocks, dbi);
+       return xa_load(&udev->data_blocks, dbi);
 }
 
 static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd)
@@ -1398,7 +1397,7 @@ static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
        timer_setup(&udev->qfull_timer, tcmu_qfull_timedout, 0);
        timer_setup(&udev->cmd_timer, tcmu_cmd_timedout, 0);
 
-       INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL);
+       xa_init(&udev->data_blocks);
 
        return &udev->se_dev;
 }
@@ -1611,19 +1610,19 @@ static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
        return -EINVAL;
 }
 
-static void tcmu_blocks_release(struct radix_tree_root *blocks,
-                               int start, int end)
+static void tcmu_blocks_release(struct xarray *blocks, unsigned long first,
+               unsigned long last)
 {
-       int i;
+       XA_STATE(xas, blocks, first);
        struct page *page;
 
-       for (i = start; i < end; i++) {
-               page = radix_tree_delete(blocks, i);
-               if (page) {
-                       __free_page(page);
-                       atomic_dec(&global_db_count);
-               }
+       xas_lock(&xas);
+       xas_for_each(&xas, page, last) {
+               xas_store(&xas, NULL);
+               __free_page(page);
+               atomic_dec(&global_db_count);
        }
+       xas_unlock(&xas);
 }
 
 static void tcmu_dev_kref_release(struct kref *kref)
@@ -1651,7 +1650,7 @@ static void tcmu_dev_kref_release(struct kref *kref)
        idr_destroy(&udev->commands);
        WARN_ON(!all_expired);
 
-       tcmu_blocks_release(&udev->data_blocks, 0, udev->dbi_max + 1);
+       tcmu_blocks_release(&udev->data_blocks, 0, udev->dbi_max);
        bitmap_free(udev->data_bitmap);
        mutex_unlock(&udev->cmdr_lock);
 
@@ -2656,7 +2655,7 @@ static void find_free_blocks(void)
                unmap_mapping_range(udev->inode->i_mapping, off, 0, 1);
 
                /* Release the block pages */
-               tcmu_blocks_release(&udev->data_blocks, start, end);
+               tcmu_blocks_release(&udev->data_blocks, start, end - 1);
                mutex_unlock(&udev->cmdr_lock);
 
                total_freed += end - start;