]> www.infradead.org Git - linux.git/commitdiff
devres: Fix devm_krealloc() wasting memory
authorZijun Hu <quic_zijuhu@quicinc.com>
Tue, 2 Jul 2024 14:51:50 +0000 (22:51 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 4 Jul 2024 10:34:38 +0000 (12:34 +0200)
Driver API devm_krealloc() calls alloc_dr() with wrong argument
@total_new_size, so causes more memory to be allocated than required
fix this memory waste by using @new_size as the argument for alloc_dr().

Fixes: f82485722e5d ("devres: provide devm_krealloc()")
Cc: stable@vger.kernel.org
Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
Link: https://lore.kernel.org/r/1719931914-19035-2-git-send-email-quic_zijuhu@quicinc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/base/devres.c

index 3df0025d12aa488dd772b1f87638f70a1aba6086..ff2247eec43cefccfab650198217cadb05d4f81f 100644 (file)
@@ -896,9 +896,12 @@ void *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp)
        /*
         * Otherwise: allocate new, larger chunk. We need to allocate before
         * taking the lock as most probably the caller uses GFP_KERNEL.
+        * alloc_dr() will call check_dr_size() to reserve extra memory
+        * for struct devres automatically, so size @new_size user request
+        * is delivered to it directly as devm_kmalloc() does.
         */
        new_dr = alloc_dr(devm_kmalloc_release,
-                         total_new_size, gfp, dev_to_node(dev));
+                         new_size, gfp, dev_to_node(dev));
        if (!new_dr)
                return NULL;