[SCSI] sd: update index allocation and use ida instead of idr

Update index allocation as follows.

* sd_index_idr is used only for ID allocation and mapping
functionality is not used. Use more memory efficient ida instead.

* idr and ida have their own locks inside them and don't need them for
operation. Drop it.

* index wasn't freed if probing failed after index allocation. fix
it.

* ida allocation should be repeated if it fails with -EAGAIN.

Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>

authored by

Tejun Heo and committed by
James Bottomley
f27bac27 ecefe8a9

+14 -13
+14 -13
drivers/scsi/sd.c
··· 99 99 static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *); 100 100 static void sd_print_result(struct scsi_disk *, int); 101 101 102 - static DEFINE_IDR(sd_index_idr); 103 - static DEFINE_SPINLOCK(sd_index_lock); 102 + static DEFINE_IDA(sd_index_ida); 104 103 105 104 /* This semaphore is used to mediate the 0->1 reference get in the 106 105 * face of object destruction (i.e. we can't allow a get on an ··· 1642 1643 if (!gd) 1643 1644 goto out_free; 1644 1645 1645 - if (!idr_pre_get(&sd_index_idr, GFP_KERNEL)) 1646 - goto out_put; 1646 + do { 1647 + if (!ida_pre_get(&sd_index_ida, GFP_KERNEL)) 1648 + goto out_put; 1647 1649 1648 - spin_lock(&sd_index_lock); 1649 - error = idr_get_new(&sd_index_idr, NULL, &index); 1650 - spin_unlock(&sd_index_lock); 1650 + error = ida_get_new(&sd_index_ida, &index); 1651 + } while (error == -EAGAIN); 1651 1652 1652 - if (index >= SD_MAX_DISKS) 1653 - error = -EBUSY; 1654 1653 if (error) 1655 1654 goto out_put; 1655 + 1656 + error = -EBUSY; 1657 + if (index >= SD_MAX_DISKS) 1658 + goto out_free_index; 1656 1659 1657 1660 sdkp->device = sdp; 1658 1661 sdkp->driver = &sd_template; ··· 1676 1675 strncpy(sdkp->dev.bus_id, sdp->sdev_gendev.bus_id, BUS_ID_SIZE); 1677 1676 1678 1677 if (device_add(&sdkp->dev)) 1679 - goto out_put; 1678 + goto out_free_index; 1680 1679 1681 1680 get_device(&sdp->sdev_gendev); 1682 1681 ··· 1718 1717 1719 1718 return 0; 1720 1719 1720 + out_free_index: 1721 + ida_remove(&sd_index_ida, index); 1721 1722 out_put: 1722 1723 put_disk(gd); 1723 1724 out_free: ··· 1769 1766 struct scsi_disk *sdkp = to_scsi_disk(dev); 1770 1767 struct gendisk *disk = sdkp->disk; 1771 1768 1772 - spin_lock(&sd_index_lock); 1773 - idr_remove(&sd_index_idr, sdkp->index); 1774 - spin_unlock(&sd_index_lock); 1769 + ida_remove(&sd_index_ida, sdkp->index); 1775 1770 1776 1771 disk->private_data = NULL; 1777 1772 put_disk(disk);