Re: [PATCH] use idr_get_new to allocate a bus id indrivers/i2c/i2c-core.c

From: Andrew Morton
Date: Sat May 15 2004 - 19:00:15 EST


Faik Uygur <faikuygur@xxxxxxx> wrote:
>
> Hi,
>
> This patch uses idr_get_new to allocate a bus id while registering
> a new adapter.
>

The IDR interface is a bit cumbersome. Even though you called
idr_pre_get(), there's no guarantee that the memory which it preallocated
is still present when you call idr_get_new().

It's easily fixed though:


> int i2c_add_adapter(struct i2c_adapter *adap)
> {
> - static int nr = 0;
> + int id;
> struct list_head *item;
> struct i2c_driver *driver;
>
> + if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
> + return -ENOMEM;
> +
> down(&core_lists);
>
> - adap->nr = nr++;
> + id = idr_get_new(&i2c_adapter_idr, NULL);
> + adap->nr = id & MAX_ID_MASK;
> init_MUTEX(&adap->bus_lock);
> init_MUTEX(&adap->clist_lock);
> list_add_tail(&adap->list,&adapters);
> @@ -207,6 +213,9 @@
> /* wait for sysfs to drop all references */
> wait_for_completion(&adap->dev_released);
> wait_for_completion(&adap->class_dev_released);
> +
> + /* free dynamically allocated bus id */
> + idr_remove(&i2c_adapter_idr, adap->nr);
>
> dev_dbg(&adap->dev, "adapter unregistered\n");

Just move the idr_pre_get() to after the down(). That way you know nobody
else will steal the preallocation.

Is the kernel likely to ever have so many bus IDs that we actually need
this patch? Or do you specifically want first-fit-from-zero for some
reason?

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/