]> www.infradead.org Git - users/willy/xarray.git/commitdiff
st: Convert to XArray
authorMatthew Wilcox <willy@infradead.org>
Fri, 26 Oct 2018 13:18:32 +0000 (09:18 -0400)
committerMatthew Wilcox (Oracle) <willy@infradead.org>
Thu, 8 Aug 2019 03:39:47 +0000 (23:39 -0400)
Replace the st_index_idr with an XArray.

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

index e3266a64a4770a940a93f2a0b66dfbb446db81cd..068dcb5bb3a2c970a0c2ddc014aa958a111c71d9 100644 (file)
@@ -38,7 +38,7 @@ static const char *verstr = "20160209";
 #include <linux/blkdev.h>
 #include <linux/moduleparam.h>
 #include <linux/cdev.h>
-#include <linux/idr.h>
+#include <linux/xarray.h>
 #include <linux/delay.h>
 #include <linux/mutex.h>
 
@@ -222,9 +222,8 @@ static void scsi_tape_release(struct kref *);
 #define to_scsi_tape(obj) container_of(obj, struct scsi_tape, kref)
 
 static DEFINE_MUTEX(st_ref_mutex);
-static DEFINE_SPINLOCK(st_index_lock);
 static DEFINE_SPINLOCK(st_use_lock);
-static DEFINE_IDR(st_index_idr);
+static DEFINE_XARRAY_ALLOC(st_index);
 
 
 \f
@@ -239,13 +238,14 @@ static DEFINE_IDR(st_index_idr);
 
 static struct scsi_tape *scsi_tape_get(int dev)
 {
-       struct scsi_tape *STp = NULL;
+       struct scsi_tape *STp;
 
        mutex_lock(&st_ref_mutex);
-       spin_lock(&st_index_lock);
+       xa_lock(&st_index);
 
-       STp = idr_find(&st_index_idr, dev);
-       if (!STp) goto out;
+       STp = xa_load(&st_index, dev);
+       if (!STp)
+               goto out;
 
        kref_get(&STp->kref);
 
@@ -261,7 +261,7 @@ out_put:
        kref_put(&STp->kref, scsi_tape_release);
        STp = NULL;
 out:
-       spin_unlock(&st_index_lock);
+       xa_unlock(&st_index);
        mutex_unlock(&st_ref_mutex);
        return STp;
 }
@@ -4372,22 +4372,18 @@ static int st_probe(struct device *dev)
            tpnt->blksize_changed = 0;
        mutex_init(&tpnt->lock);
 
-       idr_preload(GFP_KERNEL);
-       spin_lock(&st_index_lock);
-       error = idr_alloc(&st_index_idr, tpnt, 0, ST_MAX_TAPES + 1, GFP_NOWAIT);
-       spin_unlock(&st_index_lock);
-       idr_preload_end();
+       error = xa_alloc(&st_index, &tpnt->index, tpnt,
+                       XA_LIMIT(0, ST_MAX_TAPES), GFP_KERNEL);
        if (error < 0) {
-               pr_warn("st: idr allocation failed: %d\n", error);
+               pr_warn("st: ID allocation failed: %d\n", error);
                goto out_put_queue;
        }
-       tpnt->index = error;
        sprintf(disk->disk_name, "st%d", tpnt->index);
        tpnt->stats = kzalloc(sizeof(struct scsi_tape_stats), GFP_KERNEL);
        if (tpnt->stats == NULL) {
                sdev_printk(KERN_ERR, SDp,
                            "st: Can't allocate statistics.\n");
-               goto out_idr_remove;
+               goto out_xa_erase;
        }
 
        dev_set_drvdata(dev, tpnt);
@@ -4409,10 +4405,8 @@ static int st_probe(struct device *dev)
 out_remove_devs:
        remove_cdevs(tpnt);
        kfree(tpnt->stats);
-out_idr_remove:
-       spin_lock(&st_index_lock);
-       idr_remove(&st_index_idr, tpnt->index);
-       spin_unlock(&st_index_lock);
+out_xa_erase:
+       xa_erase(&st_index, tpnt->index);
 out_put_queue:
        blk_put_queue(disk->queue);
 out_put_disk:
@@ -4434,12 +4428,10 @@ static int st_remove(struct device *dev)
        scsi_autopm_get_device(to_scsi_device(dev));
        remove_cdevs(tpnt);
 
+       xa_erase(&st_index, index);
        mutex_lock(&st_ref_mutex);
        kref_put(&tpnt->kref, scsi_tape_release);
        mutex_unlock(&st_ref_mutex);
-       spin_lock(&st_index_lock);
-       idr_remove(&st_index_idr, index);
-       spin_unlock(&st_index_lock);
        return 0;
 }
 
@@ -4526,7 +4518,6 @@ static void __exit exit_st(void)
        unregister_chrdev_region(MKDEV(SCSI_TAPE_MAJOR, 0),
                                 ST_MAX_TAPE_ENTRIES);
        class_unregister(&st_sysfs_class);
-       idr_destroy(&st_index_idr);
        printk(KERN_INFO "st: Unloaded.\n");
 }