diff options
author | Joe Perches <joe@perches.com> | 2013-10-11 16:11:38 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2013-10-16 21:29:07 -0400 |
commit | 64c862a839a8db2c02bbaa88b923d13e1208919d (patch) | |
tree | 0b3765aec1193c5040f8f840edd36876b7412dd5 /drivers/base/devres.c | |
parent | d723a92dd465d549bf79dd481c09d59f0be02936 (diff) |
devres: add kernel standard devm_k.alloc functions
Currently, devm_ managed memory only supports kzalloc.
Convert the devm_kzalloc implementation to devm_kmalloc and remove the
complete memset to 0 but still set the initial struct devres header and
whatever padding before data to 0.
Add the other normal alloc variants as static inlines with __GFP_ZERO
added to the gfp flag where appropriate:
devm_kzalloc
devm_kcalloc
devm_kmalloc_array
Add gfp.h to device.h for the newly added static inlines.
akpm: the current API forces us to replace kmalloc() with kzalloc() when
performing devm_ conversions. This adds a relatively minor overhead.
More significantly, it will defeat kmemcheck used-uninitialized checking,
and for a particular driver, losing used-uninitialised checking for their
core controlling data structures will significantly degrade kmemcheck
usefulness.
Signed-off-by: Joe Perches <joe@perches.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Sangjung Woo <sangjung.woo@samsung.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/base/devres.c')
-rw-r--r-- | drivers/base/devres.c | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/drivers/base/devres.c b/drivers/base/devres.c index 507379e7b763..37e67a26e388 100644 --- a/drivers/base/devres.c +++ b/drivers/base/devres.c | |||
@@ -91,7 +91,8 @@ static __always_inline struct devres * alloc_dr(dr_release_t release, | |||
91 | if (unlikely(!dr)) | 91 | if (unlikely(!dr)) |
92 | return NULL; | 92 | return NULL; |
93 | 93 | ||
94 | memset(dr, 0, tot_size); | 94 | memset(dr, 0, offsetof(struct devres, data)); |
95 | |||
95 | INIT_LIST_HEAD(&dr->node.entry); | 96 | INIT_LIST_HEAD(&dr->node.entry); |
96 | dr->node.release = release; | 97 | dr->node.release = release; |
97 | return dr; | 98 | return dr; |
@@ -745,58 +746,62 @@ void devm_remove_action(struct device *dev, void (*action)(void *), void *data) | |||
745 | EXPORT_SYMBOL_GPL(devm_remove_action); | 746 | EXPORT_SYMBOL_GPL(devm_remove_action); |
746 | 747 | ||
747 | /* | 748 | /* |
748 | * Managed kzalloc/kfree | 749 | * Managed kmalloc/kfree |
749 | */ | 750 | */ |
750 | static void devm_kzalloc_release(struct device *dev, void *res) | 751 | static void devm_kmalloc_release(struct device *dev, void *res) |
751 | { | 752 | { |
752 | /* noop */ | 753 | /* noop */ |
753 | } | 754 | } |
754 | 755 | ||
755 | static int devm_kzalloc_match(struct device *dev, void *res, void *data) | 756 | static int devm_kmalloc_match(struct device *dev, void *res, void *data) |
756 | { | 757 | { |
757 | return res == data; | 758 | return res == data; |
758 | } | 759 | } |
759 | 760 | ||
760 | /** | 761 | /** |
761 | * devm_kzalloc - Resource-managed kzalloc | 762 | * devm_kmalloc - Resource-managed kmalloc |
762 | * @dev: Device to allocate memory for | 763 | * @dev: Device to allocate memory for |
763 | * @size: Allocation size | 764 | * @size: Allocation size |
764 | * @gfp: Allocation gfp flags | 765 | * @gfp: Allocation gfp flags |
765 | * | 766 | * |
766 | * Managed kzalloc. Memory allocated with this function is | 767 | * Managed kmalloc. Memory allocated with this function is |
767 | * automatically freed on driver detach. Like all other devres | 768 | * automatically freed on driver detach. Like all other devres |
768 | * resources, guaranteed alignment is unsigned long long. | 769 | * resources, guaranteed alignment is unsigned long long. |
769 | * | 770 | * |
770 | * RETURNS: | 771 | * RETURNS: |
771 | * Pointer to allocated memory on success, NULL on failure. | 772 | * Pointer to allocated memory on success, NULL on failure. |
772 | */ | 773 | */ |
773 | void * devm_kzalloc(struct device *dev, size_t size, gfp_t gfp) | 774 | void * devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) |
774 | { | 775 | { |
775 | struct devres *dr; | 776 | struct devres *dr; |
776 | 777 | ||
777 | /* use raw alloc_dr for kmalloc caller tracing */ | 778 | /* use raw alloc_dr for kmalloc caller tracing */ |
778 | dr = alloc_dr(devm_kzalloc_release, size, gfp); | 779 | dr = alloc_dr(devm_kmalloc_release, size, gfp); |
779 | if (unlikely(!dr)) | 780 | if (unlikely(!dr)) |
780 | return NULL; | 781 | return NULL; |
781 | 782 | ||
783 | /* | ||
784 | * This is named devm_kzalloc_release for historical reasons | ||
785 | * The initial implementation did not support kmalloc, only kzalloc | ||
786 | */ | ||
782 | set_node_dbginfo(&dr->node, "devm_kzalloc_release", size); | 787 | set_node_dbginfo(&dr->node, "devm_kzalloc_release", size); |
783 | devres_add(dev, dr->data); | 788 | devres_add(dev, dr->data); |
784 | return dr->data; | 789 | return dr->data; |
785 | } | 790 | } |
786 | EXPORT_SYMBOL_GPL(devm_kzalloc); | 791 | EXPORT_SYMBOL_GPL(devm_kmalloc); |
787 | 792 | ||
788 | /** | 793 | /** |
789 | * devm_kfree - Resource-managed kfree | 794 | * devm_kfree - Resource-managed kfree |
790 | * @dev: Device this memory belongs to | 795 | * @dev: Device this memory belongs to |
791 | * @p: Memory to free | 796 | * @p: Memory to free |
792 | * | 797 | * |
793 | * Free memory allocated with devm_kzalloc(). | 798 | * Free memory allocated with devm_kmalloc(). |
794 | */ | 799 | */ |
795 | void devm_kfree(struct device *dev, void *p) | 800 | void devm_kfree(struct device *dev, void *p) |
796 | { | 801 | { |
797 | int rc; | 802 | int rc; |
798 | 803 | ||
799 | rc = devres_destroy(dev, devm_kzalloc_release, devm_kzalloc_match, p); | 804 | rc = devres_destroy(dev, devm_kmalloc_release, devm_kmalloc_match, p); |
800 | WARN_ON(rc); | 805 | WARN_ON(rc); |
801 | } | 806 | } |
802 | EXPORT_SYMBOL_GPL(devm_kfree); | 807 | EXPORT_SYMBOL_GPL(devm_kfree); |