@@ -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
130138void * __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}
838846EXPORT_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.
0 commit comments