diff mbox series

[3/4] iio: adc: Use devm_krealloc_array

Message ID 20230306152723.3090195-4-james.clark@arm.com
State Superseded
Headers show
Series devres: Provide krealloc_array | expand

Commit Message

James Clark March 6, 2023, 3:27 p.m. UTC
Now that it exists, use it instead of doing the multiplication and
checking for overflow manually.

Signed-off-by: James Clark <james.clark@arm.com>
---
 drivers/iio/adc/xilinx-ams.c       |  9 +++------
 drivers/iio/adc/xilinx-xadc-core.c | 17 +++++++----------
 2 files changed, 10 insertions(+), 16 deletions(-)

Comments

Michal Simek March 8, 2023, 11:58 a.m. UTC | #1
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 <james.clark@arm.com>
> ---
>   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
James Clark March 9, 2023, 3:04 p.m. UTC | #2
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 <james.clark@arm.com>
>> ---
>>   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
diff mbox series

Patch

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;