On Wed, Jul 15, 2020 at 11:25 AM Bartosz Golaszewski <brgl@xxxxxxxx> wrote: > > From: Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx> > > 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@xxxxxxxxxxxx> > --- [snip!] > +void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp) > +{ > + struct devres *old_dr, *new_dr; > + struct list_head old_head; > + unsigned long flags; > + size_t total_size; > + void *ret = NULL; > + > + if (unlikely(!new_size)) { > + devm_kfree(dev, ptr); > + return ZERO_SIZE_PTR; > + } > + > + if (unlikely(ZERO_OR_NULL_PTR(ptr))) > + return devm_kmalloc(dev, new_size, gfp); > + > + if (WARN_ON(is_kernel_rodata((unsigned long)ptr))) > + /* > + * We cannot reliably realloc a const string returned by > + * devm_kstrdup_const(). > + */ > + return NULL; > + > + if (!check_dr_size(new_size, &total_size)) > + return NULL; > + > + spin_lock_irqsave(&dev->devres_lock, flags); > + > + old_dr = find_dr(dev, devm_kmalloc_release, devm_kmalloc_match, ptr); > + if (!old_dr) { > + spin_unlock_irqrestore(&dev->devres_lock, flags); > + WARN(1, "Memory chunk not managed or managed by a different device."); > + return NULL; > + } > + > + old_head = old_dr->node.entry; > + > + new_dr = krealloc(old_dr, total_size, gfp); Ugh, I wanted to check up on this patch and, after looking at it now, realized it's wrong. If the user calls devm_krealloc() with GFP_KERNEL we may end up sleeping with spinlock taken. Let me prepare another version. Bartosz