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

From: Bartosz Golaszewski
Date: Tue Jul 14 2020 - 10:48:43 EST


On Mon, Jul 13, 2020 at 9:29 PM Andy Shevchenko
<andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:
>
> On Mon, Jul 13, 2020 at 04:59:32PM +0200, Bartosz Golaszewski 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>
> > ---
> > .../driver-api/driver-model/devres.rst | 1 +
> > drivers/base/devres.c | 67 +++++++++++++++++++
> > include/linux/device.h | 2 +
> > 3 files changed, 70 insertions(+)
> >
> > diff --git a/Documentation/driver-api/driver-model/devres.rst b/Documentation/driver-api/driver-model/devres.rst
> > index eaaaafc21134f..f318a5c0033c1 100644
> > --- a/Documentation/driver-api/driver-model/devres.rst
> > +++ b/Documentation/driver-api/driver-model/devres.rst
> > @@ -354,6 +354,7 @@ MEM
> > devm_kmalloc()
> > devm_kmalloc_array()
> > devm_kmemdup()
> > + devm_krealloc()
> > devm_kstrdup()
> > devm_kvasprintf()
> > devm_kzalloc()
> > diff --git a/drivers/base/devres.c b/drivers/base/devres.c
> > index ed615d3b9cf15..1775d35462300 100644
> > --- a/drivers/base/devres.c
> > +++ b/drivers/base/devres.c
> > @@ -837,6 +837,73 @@ void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
> > }
> > EXPORT_SYMBOL_GPL(devm_kmalloc);
> >
> > +/**
> > + * devm_krealloc - Resource-managed krealloc()
> > + * @dev: Device to re-allocate memory for
> > + * @ptr: Pointer to the memory chunk to re-allocate
> > + * @new_size: New allocation size
> > + * @gfp: Allocation gfp flags
> > + *
> > + * Managed krealloc(). Resizes the memory chunk allocated with devm_kmalloc().
> > + * Behaves similarly to regular krealloc(): if @ptr is NULL or ZERO_SIZE_PTR,
> > + * it's the equivalent of devm_kmalloc(). If new_size is zero, it returns
>
> 'it frees the previously allocated memory and returns'
>
> > + * ZERO_SIZE_PTR. This function doesn't change the order in which the release
> > + * callback for the re-alloc'ed devres will be called (except when falling back
> > + * to devm_kmalloc()
>
> 'or when freeing resources when new_size is zero'
>

This is nit-picking but ok. :)

[snip!]

> > +++ b/include/linux/device.h
> > @@ -206,6 +206,8 @@ int devres_release_group(struct device *dev, void *id);
> >
> > /* managed devm_k.alloc/kfree for device drivers */
> > void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) __malloc;
>
> > +void *devm_krealloc(struct device *dev, void *ptr, size_t size,
> > + gfp_t gfp) __must_check;
>
> Strange indentation, also you can move __must_check to the beginning of the
> declaration.
>

There's nothing wrong with this indentation, what do you mean?
__must_check is usually put at the end of the line.

[snip!]

Bart