Re: [PATCHSET] idr: implement idr_alloc() and convert existing users

From: J. Bruce Fields
Date: Tue Mar 26 2013 - 12:30:29 EST


On Tue, Mar 26, 2013 at 08:26:53AM -0700, Tejun Heo wrote:
> Hello, Jeff.
>
> On Tue, Mar 26, 2013 at 11:19:36AM -0400, Jeff Layton wrote:
> > +/**
> > + * idr_alloc_cyclic - allocate new idr entry in a cyclical fashion
> > + * @idr: the (initialized) idr
> > + * @ptr: pointer to be associated with the new id
> > + * @start: the minimum id (inclusive)
> > + * @end: the maximum id (exclusive, <= 0 for max)
> > + * @cur: ptr to current position in the range (typically, last allocated + 1)
> > + * @gfp_mask: memory allocation flags
> > + *
> > + * Essentially the same as idr_alloc, but prefers to allocate progressively
> > + * higher ids if it can. If the "cur" counter wraps, then it will start again
> > + * at the "start" end of the range and allocate one that has already been used.
> > + */
> > +int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, int *cur,
> > + gfp_t gfp_mask)
> > +{
> > + int id;
> > +
> > + id = idr_alloc(idr, ptr, *cur, end, gfp_mask);
> > + if (id == -ENOSPC)
> > + id = idr_alloc(idr, ptr, start, end, gfp_mask);
> > +
> > + if (likely(id >= 0))
> > + *cur = id + 1;
> > + return id;
> > +}
>
> Wouldn't it be better if idr itself could remember the last position?
> Also, @start/@end should always be honored even if, say, @start moves
> above the last position. Other than that, yeah.

The old nfsd stateid code just generated stateid's from a counter,
something like:

static u32 counter;
return counter++;

and then stuck them in a hash table. Which had the obvious bug with
(very unlikely, absent malice) collisions on wraparound.

I probably should have ignored idr and just made the obvious fix:

static u32 counter;

while (!id_exists_in_hash(counter))
counter++;
return counter;

So maybe we should just go back to that. And possibly choose some
better data structure.

The only requirements are that at a given moment in time id's should be
unique, and that we should make some effort to avoid reusing them
immediately.

I don't know what other "cyclic" idr users need.

--b.
--
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/