Thread (7 messages) 7 messages, 3 authors, 2020-07-25

Re: [PATCH v5 1/3] devres: provide devm_krealloc()

From: Bartosz Golaszewski <hidden>
Date: 2020-07-25 19:20:59
Also in: linux-iio, lkml

On Wed, Jul 15, 2020 at 11:25 AM Bartosz Golaszewski [off-list ref] wrote:
From: Bartosz Golaszewski <redacted>

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 <redacted>
---
[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

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help