scsi: target: tcmu: Replace IDR by XArray
authorBodo Stroesser <bostroesser@gmail.com>
Wed, 24 Feb 2021 18:53:34 +0000 (19:53 +0100)
committerMartin K. Petersen <martin.petersen@oracle.com>
Wed, 10 Mar 2021 04:47:17 +0000 (23:47 -0500)
An attempt from Matthew Wilcox to replace IDR usage by XArray in tcmu more
than 1 year ago unfortunately got lost.

I rebased that work on latest tcmu and tested it.

Link: https://lore.kernel.org/r/20210224185335.13844-2-bostroesser@gmail.com
Reviewed-by: Mike Christie <michael.christie@oracle.com>
Signed-off-by: Bodo Stroesser <bostroesser@gmail.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
drivers/target/target_core_user.c

index 48ccdf7..a109592 100644 (file)
@@ -8,12 +8,12 @@
 
 #include <linux/spinlock.h>
 #include <linux/module.h>
-#include <linux/idr.h>
 #include <linux/kernel.h>
 #include <linux/timer.h>
 #include <linux/parser.h>
 #include <linux/vmalloc.h>
 #include <linux/uio_driver.h>
+#include <linux/xarray.h>
 #include <linux/radix-tree.h>
 #include <linux/stringify.h>
 #include <linux/bitops.h>
@@ -147,7 +147,7 @@ struct tcmu_dev {
        unsigned long *data_bitmap;
        struct radix_tree_root data_blocks;
 
-       struct idr commands;
+       struct xarray commands;
 
        struct timer_list cmd_timer;
        unsigned int cmd_time_out;
@@ -998,8 +998,8 @@ static int queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, sense_reason_t *scsi_err)
        struct tcmu_mailbox *mb = udev->mb_addr;
        struct tcmu_cmd_entry *entry;
        struct iovec *iov;
-       int iov_cnt, iov_bidi_cnt, cmd_id;
-       uint32_t cmd_head;
+       int iov_cnt, iov_bidi_cnt;
+       uint32_t cmd_id, cmd_head;
        uint64_t cdb_off;
        /* size of data buffer needed */
        size_t data_length = (size_t)tcmu_cmd->dbi_cnt * DATA_BLOCK_SIZE;
@@ -1052,8 +1052,8 @@ static int queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, sense_reason_t *scsi_err)
                 */
                goto free_and_queue;
 
-       cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 1, USHRT_MAX, GFP_NOWAIT);
-       if (cmd_id < 0) {
+       if (xa_alloc(&udev->commands, &cmd_id, tcmu_cmd, XA_LIMIT(1, 0xffff),
+                    GFP_NOWAIT) < 0) {
                pr_err("tcmu: Could not allocate cmd id.\n");
 
                tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
@@ -1436,7 +1436,7 @@ static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
                }
                WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
 
-               cmd = idr_remove(&udev->commands, entry->hdr.cmd_id);
+               cmd = xa_erase(&udev->commands, entry->hdr.cmd_id);
                if (!cmd) {
                        pr_err("cmd_id %u not found, ring is broken\n",
                               entry->hdr.cmd_id);
@@ -1454,7 +1454,7 @@ static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
                free_space = tcmu_run_tmr_queue(udev);
 
        if (atomic_read(&global_db_count) > tcmu_global_max_blocks &&
-           idr_is_empty(&udev->commands) && list_empty(&udev->qfull_queue)) {
+           xa_empty(&udev->commands) && list_empty(&udev->qfull_queue)) {
                /*
                 * Allocated blocks exceeded global block limit, currently no
                 * more pending or waiting commands so try to reclaim blocks.
@@ -1577,7 +1577,7 @@ static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
        INIT_LIST_HEAD(&udev->qfull_queue);
        INIT_LIST_HEAD(&udev->tmr_queue);
        INIT_LIST_HEAD(&udev->inflight_queue);
-       idr_init(&udev->commands);
+       xa_init_flags(&udev->commands, XA_FLAGS_ALLOC1);
 
        timer_setup(&udev->qfull_timer, tcmu_qfull_timedout, 0);
        timer_setup(&udev->cmd_timer, tcmu_cmd_timedout, 0);
@@ -1637,7 +1637,7 @@ static void tcmu_dev_kref_release(struct kref *kref)
        struct se_device *dev = &udev->se_dev;
        struct tcmu_cmd *cmd;
        bool all_expired = true;
-       int i;
+       unsigned long i;
 
        vfree(udev->mb_addr);
        udev->mb_addr = NULL;
@@ -1649,7 +1649,7 @@ static void tcmu_dev_kref_release(struct kref *kref)
 
        /* Upper layer should drain all requests before calling this */
        mutex_lock(&udev->cmdr_lock);
-       idr_for_each_entry(&udev->commands, cmd, i) {
+       xa_for_each(&udev->commands, i, cmd) {
                if (tcmu_check_and_free_pending_cmd(cmd) != 0)
                        all_expired = false;
        }
@@ -1657,7 +1657,7 @@ static void tcmu_dev_kref_release(struct kref *kref)
        tcmu_remove_all_queued_tmr(udev);
        if (!list_empty(&udev->qfull_queue))
                all_expired = false;
-       idr_destroy(&udev->commands);
+       xa_destroy(&udev->commands);
        WARN_ON(!all_expired);
 
        tcmu_blocks_release(&udev->data_blocks, 0, udev->dbi_max + 1);
@@ -2247,16 +2247,16 @@ static void tcmu_reset_ring(struct tcmu_dev *udev, u8 err_level)
 {
        struct tcmu_mailbox *mb;
        struct tcmu_cmd *cmd;
-       int i;
+       unsigned long i;
 
        mutex_lock(&udev->cmdr_lock);
 
-       idr_for_each_entry(&udev->commands, cmd, i) {
+       xa_for_each(&udev->commands, i, cmd) {
                pr_debug("removing cmd %u on dev %s from ring (is expired %d)\n",
                          cmd->cmd_id, udev->name,
                          test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags));
 
-               idr_remove(&udev->commands, i);
+               xa_erase(&udev->commands, i);
                if (!test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
                        WARN_ON(!cmd->se_cmd);
                        list_del_init(&cmd->queue_entry);