diff options
author | Tejun Heo <tj@kernel.org> | 2013-02-27 20:03:35 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-02-27 22:10:13 -0500 |
commit | 9bb26bc1ffa32ec983860a5a66b6f291a875e39d (patch) | |
tree | e821dc2bc21d6a61ae086d4eb45890f9c9c37920 /lib | |
parent | 6cdae7416a1c45c2ce105a78187d9b7e8feb9e24 (diff) |
idr: make idr_destroy() imply idr_remove_all()
idr is silly in quite a few ways, one of which is how it's supposed to
be destroyed - idr_destroy() doesn't release IDs and doesn't even whine
if the idr isn't empty. If the caller forgets idr_remove_all(), it
simply leaks memory.
Even ida gets this wrong and leaks memory on destruction. There is
absoltely no reason not to call idr_remove_all() from idr_destroy().
Nobody is abusing idr_destroy() for shrinking free layer buffer and
continues to use idr after idr_destroy(), so it's safe to do remove_all
from destroy.
In the whole kernel, there is only one place where idr_remove_all() is
legitimiately used without following idr_destroy() while there are quite
a few places where the caller forgets either idr_remove_all() or
idr_destroy() leaking memory.
This patch makes idr_destroy() call idr_destroy_all() and updates the
function description accordingly.
Signed-off-by: Tejun Heo <tj@kernel.org>
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 | 20 |
1 files changed, 11 insertions, 9 deletions
@@ -436,15 +436,6 @@ EXPORT_SYMBOL(idr_remove); | |||
436 | /** | 436 | /** |
437 | * idr_remove_all - remove all ids from the given idr tree | 437 | * idr_remove_all - remove all ids from the given idr tree |
438 | * @idp: idr handle | 438 | * @idp: idr handle |
439 | * | ||
440 | * idr_destroy() only frees up unused, cached idp_layers, but this | ||
441 | * function will remove all id mappings and leave all idp_layers | ||
442 | * unused. | ||
443 | * | ||
444 | * A typical clean-up sequence for objects stored in an idr tree will | ||
445 | * use idr_for_each() to free all objects, if necessay, then | ||
446 | * idr_remove_all() to remove all ids, and idr_destroy() to free | ||
447 | * up the cached idr_layers. | ||
448 | */ | 439 | */ |
449 | void idr_remove_all(struct idr *idp) | 440 | void idr_remove_all(struct idr *idp) |
450 | { | 441 | { |
@@ -484,9 +475,20 @@ EXPORT_SYMBOL(idr_remove_all); | |||
484 | /** | 475 | /** |
485 | * idr_destroy - release all cached layers within an idr tree | 476 | * idr_destroy - release all cached layers within an idr tree |
486 | * @idp: idr handle | 477 | * @idp: idr handle |
478 | * | ||
479 | * Free all id mappings and all idp_layers. After this function, @idp is | ||
480 | * completely unused and can be freed / recycled. The caller is | ||
481 | * responsible for ensuring that no one else accesses @idp during or after | ||
482 | * idr_destroy(). | ||
483 | * | ||
484 | * A typical clean-up sequence for objects stored in an idr tree will use | ||
485 | * idr_for_each() to free all objects, if necessay, then idr_destroy() to | ||
486 | * free up the id mappings and cached idr_layers. | ||
487 | */ | 487 | */ |
488 | void idr_destroy(struct idr *idp) | 488 | void idr_destroy(struct idr *idp) |
489 | { | 489 | { |
490 | idr_remove_all(idp); | ||
491 | |||
490 | while (idp->id_free_cnt) { | 492 | while (idp->id_free_cnt) { |
491 | struct idr_layer *p = get_from_free_list(idp); | 493 | struct idr_layer *p = get_from_free_list(idp); |
492 | kmem_cache_free(idr_layer_cache, p); | 494 | kmem_cache_free(idr_layer_cache, p); |