Re: [BK PATCH] SCSI host num allocation improvement

Andrew Morton
2004-02-27T01:36:42Z

Andrew Morton <akpm@osdl.org> wrote:
>
> The lib/idr.c code is a bit clumsy but it does do the job relatively
> efficiently.

hmm, not too bad actually. It compiles, but I didn't test it.

--- 25/drivers/scsi/hosts.c~scsi-host-allocation-fix Thu Feb 26 17:30:40 2004
+++ 25-akpm/drivers/scsi/hosts.c Thu Feb 26 17:36:47 2004
@@ -30,6 +30,7 @@
#include <linux/list.h>
#include <linux/completion.h>
#include <linux/unistd.h>
+#include <linux/idr.h>

#include <scsi/scsi_host.h>
#include "scsi.h"
@@ -38,9 +39,6 @@
#include "scsi_logging.h"

-static int scsi_host_next_hn; /* host_no for next new host */
-
-
static void scsi_host_cls_release(struct class_device *class_dev)
{
put_device(&class_to_shost(class_dev)->shost_gendev);
@@ -166,6 +164,30 @@ static void scsi_host_dev_release(struct
kfree(shost);
}

+static DECLARE_MUTEX(host_num_lock);
+static struct idr allocated_hosts;
+
+/**
+ * scsi_host_num_alloc - allocate a unique host number for a scsi host.
+ *
+ * Note:
+ * Must hold host_num_lock when calling this, and continue holding it
+ * till after the host is added to the shost_class.
+ *
+ * Return value:
+ * A unique host number.
+ **/
+static int scsi_host_num_alloc(void)
+{
+ int ret;
+
+ down(&host_num_lock);
+ idr_pre_get(&allocated_hosts, GFP_KERNEL);
+ ret = idr_get_new(&allocated_hosts, NULL);
+ up(&host_num_lock);
+ return ret;
+}
+
/**
* scsi_host_alloc - register a scsi host adapter instance.
* @sht: pointer to scsi host template
@@ -214,7 +236,6 @@ struct Scsi_Host *scsi_host_alloc(struct

init_MUTEX(&shost->scan_mutex);

- shost->host_no = scsi_host_next_hn++; /* XXX(hch): still racy */
shost->dma_channel = 0xff;

/* These three are default values which can be overridden */
@@ -263,6 +284,8 @@ struct Scsi_Host *scsi_host_alloc(struct
if (rval)
goto fail_kfree;

+ shost->host_no = scsi_host_num_alloc();
+
device_initialize(&shost->shost_gendev);
snprintf(shost->shost_gendev.bus_id, BUS_ID_SIZE, "host%d",
shost->host_no);
@@ -308,6 +331,9 @@ struct Scsi_Host *scsi_register(struct s
void scsi_unregister(struct Scsi_Host *shost)
{
list_del(&shost->sht_legacy_list);
+ down(&host_num_lock);
+ idr_remove(&allocated_hosts, shost->host_no);
+ up(&host_num_lock);
scsi_host_put(shost);
}

@@ -361,6 +387,7 @@ void scsi_host_put(struct Scsi_Host *sho

int scsi_init_hosts(void)
{
+ idr_init(&allocated_hosts);
return class_register(&shost_class);
}

_