2023-03-06 15:27:52

by James Clark

[permalink] [raw]
Subject: [PATCH 0/4] devres: Provide krealloc_array

Hi,

I had a use for a devm realloc_array in a separate change, so I've
added one and updated all the obvious existing uses of it that I could
find. This is basically a copy paste of the one in slab.h

Applies to v6.3-rc1

Thanks
James

James Clark (4):
devres: Provide krealloc_array
hwmon: pmbus: Use devm_krealloc_array
iio: adc: Use devm_krealloc_array
serial: qcom_geni: Use devm_krealloc_array

.../driver-api/driver-model/devres.rst | 1 +
drivers/hwmon/pmbus/pmbus_core.c | 6 +++---
drivers/iio/adc/xilinx-ams.c | 9 +++------
drivers/iio/adc/xilinx-xadc-core.c | 17 +++++++----------
drivers/tty/serial/qcom_geni_serial.c | 6 +++---
include/linux/device.h | 10 ++++++++++
6 files changed, 27 insertions(+), 22 deletions(-)

--
2.34.1



2023-03-06 15:27:59

by James Clark

[permalink] [raw]
Subject: [PATCH 1/4] devres: Provide krealloc_array

There is no krealloc_array equivalent in devres. Users would have to
do their own multiplication overflow check so provide one.

Signed-off-by: James Clark <[email protected]>
---
Documentation/driver-api/driver-model/devres.rst | 1 +
include/linux/device.h | 10 ++++++++++
2 files changed, 11 insertions(+)

diff --git a/Documentation/driver-api/driver-model/devres.rst b/Documentation/driver-api/driver-model/devres.rst
index 4249eb4239e0..8be086b3f829 100644
--- a/Documentation/driver-api/driver-model/devres.rst
+++ b/Documentation/driver-api/driver-model/devres.rst
@@ -364,6 +364,7 @@ MEM
devm_kmalloc_array()
devm_kmemdup()
devm_krealloc()
+ devm_krealloc_array()
devm_kstrdup()
devm_kstrdup_const()
devm_kvasprintf()
diff --git a/include/linux/device.h b/include/linux/device.h
index 1508e637bb26..0dd5956c8516 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -223,6 +223,16 @@ static inline void *devm_kcalloc(struct device *dev,
{
return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
}
+static inline __realloc_size(3, 4) void * __must_check
+devm_krealloc_array(struct device *dev, void *p, size_t new_n, size_t new_size, gfp_t flags)
+{
+ size_t bytes;
+
+ if (unlikely(check_mul_overflow(new_n, new_size, &bytes)))
+ return NULL;
+ return devm_krealloc(dev, p, bytes, flags);
+}
+
void devm_kfree(struct device *dev, const void *p);
char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;
const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp);
--
2.34.1


2023-03-06 15:28:07

by James Clark

[permalink] [raw]
Subject: [PATCH 2/4] hwmon: pmbus: Use devm_krealloc_array

Now that it exists, use it instead of doing the multiplication manually.

Signed-off-by: James Clark <[email protected]>
---
drivers/hwmon/pmbus/pmbus_core.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c
index 95e95783972a..e7bee25a3706 100644
--- a/drivers/hwmon/pmbus/pmbus_core.c
+++ b/drivers/hwmon/pmbus/pmbus_core.c
@@ -1190,9 +1190,9 @@ static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr)
{
if (data->num_attributes >= data->max_attributes - 1) {
int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE;
- void *new_attrs = devm_krealloc(data->dev, data->group.attrs,
- new_max_attrs * sizeof(void *),
- GFP_KERNEL);
+ void *new_attrs = devm_krealloc_array(data->dev, data->group.attrs,
+ new_max_attrs, sizeof(void *),
+ GFP_KERNEL);
if (!new_attrs)
return -ENOMEM;
data->group.attrs = new_attrs;
--
2.34.1


2023-03-06 15:28:12

by James Clark

[permalink] [raw]
Subject: [PATCH 3/4] iio: adc: Use devm_krealloc_array

Now that it exists, use it instead of doing the multiplication and
checking for overflow manually.

Signed-off-by: James Clark <[email protected]>
---
drivers/iio/adc/xilinx-ams.c | 9 +++------
drivers/iio/adc/xilinx-xadc-core.c | 17 +++++++----------
2 files changed, 10 insertions(+), 16 deletions(-)

diff --git a/drivers/iio/adc/xilinx-ams.c b/drivers/iio/adc/xilinx-ams.c
index 34cf336b3490..f0b71a1220e0 100644
--- a/drivers/iio/adc/xilinx-ams.c
+++ b/drivers/iio/adc/xilinx-ams.c
@@ -1263,7 +1263,7 @@ static int ams_parse_firmware(struct iio_dev *indio_dev)
struct device *dev = indio_dev->dev.parent;
struct fwnode_handle *child = NULL;
struct fwnode_handle *fwnode = dev_fwnode(dev);
- size_t ams_size, dev_size;
+ size_t ams_size;
int ret, ch_cnt = 0, i, rising_off, falling_off;
unsigned int num_channels = 0;

@@ -1320,11 +1320,8 @@ static int ams_parse_firmware(struct iio_dev *indio_dev)
}
}

- dev_size = array_size(sizeof(*dev_channels), num_channels);
- if (dev_size == SIZE_MAX)
- return -ENOMEM;
-
- dev_channels = devm_krealloc(dev, ams_channels, dev_size, GFP_KERNEL);
+ dev_channels = devm_krealloc_array(dev, ams_channels, num_channels,
+ sizeof(*dev_channels), GFP_KERNEL);
if (!dev_channels)
return -ENOMEM;

diff --git a/drivers/iio/adc/xilinx-xadc-core.c b/drivers/iio/adc/xilinx-xadc-core.c
index 292f2892d223..287df3bb951e 100644
--- a/drivers/iio/adc/xilinx-xadc-core.c
+++ b/drivers/iio/adc/xilinx-xadc-core.c
@@ -613,20 +613,17 @@ static int xadc_update_scan_mode(struct iio_dev *indio_dev,
const unsigned long *mask)
{
struct xadc *xadc = iio_priv(indio_dev);
- size_t new_size, n;
+ size_t n;
void *data;

n = bitmap_weight(mask, indio_dev->masklength);

- if (check_mul_overflow(n, sizeof(*xadc->data), &new_size))
- return -ENOMEM;
-
- data = devm_krealloc(indio_dev->dev.parent, xadc->data,
- new_size, GFP_KERNEL);
+ data = devm_krealloc_array(indio_dev->dev.parent, xadc->data,
+ n, sizeof(*xadc->data), GFP_KERNEL);
if (!data)
return -ENOMEM;

- memset(data, 0, new_size);
+ memset(data, 0, n*sizeof(*xadc->data));
xadc->data = data;

return 0;
@@ -1281,9 +1278,9 @@ static int xadc_parse_dt(struct iio_dev *indio_dev, unsigned int *conf, int irq)
}

indio_dev->num_channels = num_channels;
- indio_dev->channels = devm_krealloc(dev, channels,
- sizeof(*channels) * num_channels,
- GFP_KERNEL);
+ indio_dev->channels = devm_krealloc_array(dev, channels,
+ num_channels, sizeof(*channels),
+ GFP_KERNEL);
/* If we can't resize the channels array, just use the original */
if (!indio_dev->channels)
indio_dev->channels = channels;
--
2.34.1


2023-03-06 15:28:15

by James Clark

[permalink] [raw]
Subject: [PATCH 4/4] serial: qcom_geni: Use devm_krealloc_array

Now that it exists, use it instead of doing the multiplication manually.

Signed-off-by: James Clark <[email protected]>
---
drivers/tty/serial/qcom_geni_serial.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/tty/serial/qcom_geni_serial.c b/drivers/tty/serial/qcom_geni_serial.c
index d69592e5e2ec..23fc33d182ac 100644
--- a/drivers/tty/serial/qcom_geni_serial.c
+++ b/drivers/tty/serial/qcom_geni_serial.c
@@ -1056,9 +1056,9 @@ static int setup_fifos(struct qcom_geni_serial_port *port)
(port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;

if (port->rx_buf && (old_rx_fifo_depth != port->rx_fifo_depth) && port->rx_fifo_depth) {
- port->rx_buf = devm_krealloc(uport->dev, port->rx_buf,
- port->rx_fifo_depth * sizeof(u32),
- GFP_KERNEL);
+ port->rx_buf = devm_krealloc_array(uport->dev, port->rx_buf,
+ port->rx_fifo_depth, sizeof(u32),
+ GFP_KERNEL);
if (!port->rx_buf)
return -ENOMEM;
}
--
2.34.1


2023-03-06 15:30:58

by Guenter Roeck

[permalink] [raw]
Subject: Re: [PATCH 2/4] hwmon: pmbus: Use devm_krealloc_array

On 3/6/23 07:27, James Clark wrote:
> Now that it exists, use it instead of doing the multiplication manually.
>
> Signed-off-by: James Clark <[email protected]>

Acked-by: Guenter Roeck <[email protected]>

> ---
> drivers/hwmon/pmbus/pmbus_core.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/hwmon/pmbus/pmbus_core.c b/drivers/hwmon/pmbus/pmbus_core.c
> index 95e95783972a..e7bee25a3706 100644
> --- a/drivers/hwmon/pmbus/pmbus_core.c
> +++ b/drivers/hwmon/pmbus/pmbus_core.c
> @@ -1190,9 +1190,9 @@ static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr)
> {
> if (data->num_attributes >= data->max_attributes - 1) {
> int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE;
> - void *new_attrs = devm_krealloc(data->dev, data->group.attrs,
> - new_max_attrs * sizeof(void *),
> - GFP_KERNEL);
> + void *new_attrs = devm_krealloc_array(data->dev, data->group.attrs,
> + new_max_attrs, sizeof(void *),
> + GFP_KERNEL);
> if (!new_attrs)
> return -ENOMEM;
> data->group.attrs = new_attrs;


2023-03-08 11:59:17

by Michal Simek

[permalink] [raw]
Subject: Re: [PATCH 3/4] iio: adc: Use devm_krealloc_array



On 3/6/23 16:27, James Clark wrote:
>
> Now that it exists, use it instead of doing the multiplication and
> checking for overflow manually.
>
> Signed-off-by: James Clark <[email protected]>
> ---
> drivers/iio/adc/xilinx-ams.c | 9 +++------
> drivers/iio/adc/xilinx-xadc-core.c | 17 +++++++----------
> 2 files changed, 10 insertions(+), 16 deletions(-)
>
> diff --git a/drivers/iio/adc/xilinx-ams.c b/drivers/iio/adc/xilinx-ams.c
> index 34cf336b3490..f0b71a1220e0 100644
> --- a/drivers/iio/adc/xilinx-ams.c
> +++ b/drivers/iio/adc/xilinx-ams.c
> @@ -1263,7 +1263,7 @@ static int ams_parse_firmware(struct iio_dev *indio_dev)
> struct device *dev = indio_dev->dev.parent;
> struct fwnode_handle *child = NULL;
> struct fwnode_handle *fwnode = dev_fwnode(dev);
> - size_t ams_size, dev_size;
> + size_t ams_size;
> int ret, ch_cnt = 0, i, rising_off, falling_off;
> unsigned int num_channels = 0;
>
> @@ -1320,11 +1320,8 @@ static int ams_parse_firmware(struct iio_dev *indio_dev)
> }
> }
>
> - dev_size = array_size(sizeof(*dev_channels), num_channels);
> - if (dev_size == SIZE_MAX)
> - return -ENOMEM;
> -
> - dev_channels = devm_krealloc(dev, ams_channels, dev_size, GFP_KERNEL);
> + dev_channels = devm_krealloc_array(dev, ams_channels, num_channels,
> + sizeof(*dev_channels), GFP_KERNEL);
> if (!dev_channels)
> return -ENOMEM;
>
> diff --git a/drivers/iio/adc/xilinx-xadc-core.c b/drivers/iio/adc/xilinx-xadc-core.c
> index 292f2892d223..287df3bb951e 100644
> --- a/drivers/iio/adc/xilinx-xadc-core.c
> +++ b/drivers/iio/adc/xilinx-xadc-core.c
> @@ -613,20 +613,17 @@ static int xadc_update_scan_mode(struct iio_dev *indio_dev,
> const unsigned long *mask)
> {
> struct xadc *xadc = iio_priv(indio_dev);
> - size_t new_size, n;
> + size_t n;
> void *data;
>
> n = bitmap_weight(mask, indio_dev->masklength);
>
> - if (check_mul_overflow(n, sizeof(*xadc->data), &new_size))
> - return -ENOMEM;
> -
> - data = devm_krealloc(indio_dev->dev.parent, xadc->data,
> - new_size, GFP_KERNEL);
> + data = devm_krealloc_array(indio_dev->dev.parent, xadc->data,
> + n, sizeof(*xadc->data), GFP_KERNEL);
> if (!data)
> return -ENOMEM;
>
> - memset(data, 0, new_size);
> + memset(data, 0, n*sizeof(*xadc->data));

this is not correct coding style.

M

2023-03-09 15:07:31

by James Clark

[permalink] [raw]
Subject: Re: [PATCH 3/4] iio: adc: Use devm_krealloc_array



On 08/03/2023 11:58, Michal Simek wrote:
>
>
> On 3/6/23 16:27, James Clark wrote:
>>
>> Now that it exists, use it instead of doing the multiplication and
>> checking for overflow manually.
>>
>> Signed-off-by: James Clark <[email protected]>
>> ---
>>   drivers/iio/adc/xilinx-ams.c       |  9 +++------
>>   drivers/iio/adc/xilinx-xadc-core.c | 17 +++++++----------
>>   2 files changed, 10 insertions(+), 16 deletions(-)
>>
>> diff --git a/drivers/iio/adc/xilinx-ams.c b/drivers/iio/adc/xilinx-ams.c
>> index 34cf336b3490..f0b71a1220e0 100644
>> --- a/drivers/iio/adc/xilinx-ams.c
>> +++ b/drivers/iio/adc/xilinx-ams.c
>> @@ -1263,7 +1263,7 @@ static int ams_parse_firmware(struct iio_dev
>> *indio_dev)
>>          struct device *dev = indio_dev->dev.parent;
>>          struct fwnode_handle *child = NULL;
>>          struct fwnode_handle *fwnode = dev_fwnode(dev);
>> -       size_t ams_size, dev_size;
>> +       size_t ams_size;
>>          int ret, ch_cnt = 0, i, rising_off, falling_off;
>>          unsigned int num_channels = 0;
>>
>> @@ -1320,11 +1320,8 @@ static int ams_parse_firmware(struct iio_dev
>> *indio_dev)
>>                  }
>>          }
>>
>> -       dev_size = array_size(sizeof(*dev_channels), num_channels);
>> -       if (dev_size == SIZE_MAX)
>> -               return -ENOMEM;
>> -
>> -       dev_channels = devm_krealloc(dev, ams_channels, dev_size,
>> GFP_KERNEL);
>> +       dev_channels = devm_krealloc_array(dev, ams_channels,
>> num_channels,
>> +                                          sizeof(*dev_channels),
>> GFP_KERNEL);
>>          if (!dev_channels)
>>                  return -ENOMEM;
>>
>> diff --git a/drivers/iio/adc/xilinx-xadc-core.c
>> b/drivers/iio/adc/xilinx-xadc-core.c
>> index 292f2892d223..287df3bb951e 100644
>> --- a/drivers/iio/adc/xilinx-xadc-core.c
>> +++ b/drivers/iio/adc/xilinx-xadc-core.c
>> @@ -613,20 +613,17 @@ static int xadc_update_scan_mode(struct iio_dev
>> *indio_dev,
>>          const unsigned long *mask)
>>   {
>>          struct xadc *xadc = iio_priv(indio_dev);
>> -       size_t new_size, n;
>> +       size_t n;
>>          void *data;
>>
>>          n = bitmap_weight(mask, indio_dev->masklength);
>>
>> -       if (check_mul_overflow(n, sizeof(*xadc->data), &new_size))
>> -               return -ENOMEM;
>> -
>> -       data = devm_krealloc(indio_dev->dev.parent, xadc->data,
>> -                            new_size, GFP_KERNEL);
>> +       data = devm_krealloc_array(indio_dev->dev.parent, xadc->data,
>> +                                  n, sizeof(*xadc->data), GFP_KERNEL);
>>          if (!data)
>>                  return -ENOMEM;
>>
>> -       memset(data, 0, new_size);
>> +       memset(data, 0, n*sizeof(*xadc->data));
>
> this is not correct coding style.

Oops, fixed in v2, thanks.

>
> M