diff options
author | Jeff Layton <jlayton@redhat.com> | 2013-04-29 19:21:16 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-04-29 21:28:41 -0400 |
commit | 3e6628c4b347a558965041290c5a92791dd4c741 (patch) | |
tree | 7f437ed24761f467b61c0a401fc8d6a6ca7fee3a /lib | |
parent | 27cf10e133c393f39399137b028c7b24d64ca06e (diff) |
idr: introduce idr_alloc_cyclic()
As Tejun points out, there are several users of the IDR facility that
attempt to use it in a cyclic fashion. These users are likely to see
-ENOSPC errors after the counter wraps one or more times however.
This patchset adds a new idr_alloc_cyclic routine and converts several
of these users to it. Many of these users are in obscure parts of the
kernel, and I don't have a good way to test some of them. The change is
pretty straightforward though, so hopefully it won't be an issue.
There is one other cyclic user of idr_alloc that I didn't touch in
ipc/util.c. That one is doing some strange stuff that I didn't quite
understand, but it looks like it should probably be converted later
somehow.
This patch:
Thus spake Tejun Heo:
Ooh, BTW, the cyclic allocation is broken. It's prone to -ENOSPC
after the first wraparound. There are several cyclic users in the
kernel and I think it probably would be best to implement cyclic
support in idr.
This patch does that by adding new idr_alloc_cyclic function that such
users in the kernel can use. With this, there's no need for a caller to
keep track of the last value used as that's now tracked internally. This
should prevent the ENOSPC problems that can hit when the "last allocated"
counter exceeds INT_MAX.
Later patches will convert existing cyclic users to the new interface.
Signed-off-by: Jeff Layton <jlayton@redhat.com>
Reviewed-by: Tejun Heo <tj@kernel.org>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: "J. Bruce Fields" <bfields@fieldses.org>
Cc: Eric Paris <eparis@parisplace.org>
Cc: Jack Morgenstein <jackm@dev.mellanox.co.il>
Cc: John McCutchan <john@johnmccutchan.com>
Cc: Neil Horman <nhorman@tuxdriver.com>
Cc: Or Gerlitz <ogerlitz@mellanox.com>
Cc: Robert Love <rlove@rlove.org>
Cc: Roland Dreier <roland@purestorage.com>
Cc: Sridhar Samudrala <sri@us.ibm.com>
Cc: Steve Wise <swise@opengridcomputing.com>
Cc: Tom Tucker <tom@opengridcomputing.com>
Cc: Vlad Yasevich <vyasevich@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/idr.c | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -495,6 +495,33 @@ int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask) | |||
495 | } | 495 | } |
496 | EXPORT_SYMBOL_GPL(idr_alloc); | 496 | EXPORT_SYMBOL_GPL(idr_alloc); |
497 | 497 | ||
498 | /** | ||
499 | * idr_alloc_cyclic - allocate new idr entry in a cyclical fashion | ||
500 | * @idr: the (initialized) idr | ||
501 | * @ptr: pointer to be associated with the new id | ||
502 | * @start: the minimum id (inclusive) | ||
503 | * @end: the maximum id (exclusive, <= 0 for max) | ||
504 | * @gfp_mask: memory allocation flags | ||
505 | * | ||
506 | * Essentially the same as idr_alloc, but prefers to allocate progressively | ||
507 | * higher ids if it can. If the "cur" counter wraps, then it will start again | ||
508 | * at the "start" end of the range and allocate one that has already been used. | ||
509 | */ | ||
510 | int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, | ||
511 | gfp_t gfp_mask) | ||
512 | { | ||
513 | int id; | ||
514 | |||
515 | id = idr_alloc(idr, ptr, max(start, idr->cur), end, gfp_mask); | ||
516 | if (id == -ENOSPC) | ||
517 | id = idr_alloc(idr, ptr, start, end, gfp_mask); | ||
518 | |||
519 | if (likely(id >= 0)) | ||
520 | idr->cur = id + 1; | ||
521 | return id; | ||
522 | } | ||
523 | EXPORT_SYMBOL(idr_alloc_cyclic); | ||
524 | |||
498 | static void idr_remove_warning(int id) | 525 | static void idr_remove_warning(int id) |
499 | { | 526 | { |
500 | printk(KERN_WARNING | 527 | printk(KERN_WARNING |