Skip to content

Commit f824857

Browse files
brglgregkh
authored andcommitted
devres: provide devm_krealloc()
Implement the managed variant of krealloc(). This function works with all memory allocated by devm_kmalloc() (or devres functions using it implicitly like devm_kmemdup(), devm_kstrdup() etc.). Managed realloc'ed chunks can be manually released with devm_kfree(). Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Link: https://lore.kernel.org/r/20200824173859.4910-2-brgl@bgdev.pl Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 81b1422 commit f824857

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

‎Documentation/driver-api/driver-model/devres.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ MEM
354354
devm_kmalloc()
355355
devm_kmalloc_array()
356356
devm_kmemdup()
357+
devm_krealloc()
357358
devm_kstrdup()
358359
devm_kvasprintf()
359360
devm_kzalloc()

‎drivers/base/devres.c‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ static void add_dr(struct device *dev, struct devres_node *node)
126126
list_add_tail(&node->entry, &dev->devres_head);
127127
}
128128

129+
static void replace_dr(struct device *dev,
130+
struct devres_node *old, struct devres_node *new)
131+
{
132+
devres_log(dev, old, "REPLACE");
133+
BUG_ON(!list_empty(&new->entry));
134+
list_replace(&old->entry, &new->entry);
135+
}
136+
129137
#ifdef CONFIG_DEBUG_DEVRES
130138
void * __devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid,
131139
const char *name)
@@ -837,6 +845,103 @@ void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
837845
}
838846
EXPORT_SYMBOL_GPL(devm_kmalloc);
839847

848+
/**
849+
* devm_krealloc - Resource-managed krealloc()
850+
* @dev: Device to re-allocate memory for
851+
* @ptr: Pointer to the memory chunk to re-allocate
852+
* @new_size: New allocation size
853+
* @gfp: Allocation gfp flags
854+
*
855+
* Managed krealloc(). Resizes the memory chunk allocated with devm_kmalloc().
856+
* Behaves similarly to regular krealloc(): if @ptr is NULL or ZERO_SIZE_PTR,
857+
* it's the equivalent of devm_kmalloc(). If new_size is zero, it frees the
858+
* previously allocated memory and returns ZERO_SIZE_PTR. This function doesn't
859+
* change the order in which the release callback for the re-alloc'ed devres
860+
* will be called (except when falling back to devm_kmalloc() or when freeing
861+
* resources when new_size is zero). The contents of the memory are preserved
862+
* up to the lesser of new and old sizes.
863+
*/
864+
void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp)
865+
{
866+
size_t total_new_size, total_old_size;
867+
struct devres *old_dr, *new_dr;
868+
unsigned long flags;
869+
870+
if (unlikely(!new_size)) {
871+
devm_kfree(dev, ptr);
872+
return ZERO_SIZE_PTR;
873+
}
874+
875+
if (unlikely(ZERO_OR_NULL_PTR(ptr)))
876+
return devm_kmalloc(dev, new_size, gfp);
877+
878+
if (WARN_ON(is_kernel_rodata((unsigned long)ptr)))
879+
/*
880+
* We cannot reliably realloc a const string returned by
881+
* devm_kstrdup_const().
882+
*/
883+
return NULL;
884+
885+
if (!check_dr_size(new_size, &total_new_size))
886+
return NULL;
887+
888+
total_old_size = ksize(container_of(ptr, struct devres, data));
889+
if (total_old_size == 0) {
890+
WARN(1, "Pointer doesn't point to dynamically allocated memory.");
891+
return NULL;
892+
}
893+
894+
/*
895+
* If new size is smaller or equal to the actual number of bytes
896+
* allocated previously - just return the same pointer.
897+
*/
898+
if (total_new_size <= total_old_size)
899+
return ptr;
900+
901+
/*
902+
* Otherwise: allocate new, larger chunk. We need to allocate before
903+
* taking the lock as most probably the caller uses GFP_KERNEL.
904+
*/
905+
new_dr = alloc_dr(devm_kmalloc_release,
906+
total_new_size, gfp, dev_to_node(dev));
907+
if (!new_dr)
908+
return NULL;
909+
910+
/*
911+
* The spinlock protects the linked list against concurrent
912+
* modifications but not the resource itself.
913+
*/
914+
spin_lock_irqsave(&dev->devres_lock, flags);
915+
916+
old_dr = find_dr(dev, devm_kmalloc_release, devm_kmalloc_match, ptr);
917+
if (!old_dr) {
918+
spin_unlock_irqrestore(&dev->devres_lock, flags);
919+
kfree(new_dr);
920+
WARN(1, "Memory chunk not managed or managed by a different device.");
921+
return NULL;
922+
}
923+
924+
replace_dr(dev, &old_dr->node, &new_dr->node);
925+
926+
spin_unlock_irqrestore(&dev->devres_lock, flags);
927+
928+
/*
929+
* We can copy the memory contents after releasing the lock as we're
930+
* no longer modyfing the list links.
931+
*/
932+
memcpy(new_dr->data, old_dr->data,
933+
total_old_size - offsetof(struct devres, data));
934+
/*
935+
* Same for releasing the old devres - it's now been removed from the
936+
* list. This is also the reason why we must not use devm_kfree() - the
937+
* links are no longer valid.
938+
*/
939+
kfree(old_dr);
940+
941+
return new_dr->data;
942+
}
943+
EXPORT_SYMBOL_GPL(devm_krealloc);
944+
840945
/**
841946
* devm_kstrdup - Allocate resource managed space and
842947
* copy an existing string into that.

‎include/linux/device.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ int devres_release_group(struct device *dev, void *id);
206206

207207
/* managed devm_k.alloc/kfree for device drivers */
208208
void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __malloc;
209+
void *devm_krealloc(struct device *dev, void *ptr, size_t size,
210+
gfp_t gfp) __must_check;
209211
__printf(3, 0) char *devm_kvasprintf(struct device *dev, gfp_t gfp,
210212
const char *fmt, va_list ap) __malloc;
211213
__printf(3, 4) char *devm_kasprintf(struct device *dev, gfp_t gfp,

0 commit comments

Comments
 (0)