Skip to contentSkip to conversation

devres: Provide krealloc_array

PATCH In review James Clark 3 participants 15 messages May 9, 2023 – Jun 5, 2023

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

v4 cover devres: Provide krealloc_array

Changes since v3:

* Rebase onto v6.4-rc1

Changes since v2:

* Remove change in qcom_geni_serial.c in the last commmit and replace it with a comment instead
* Whitespace fix

Changes since v1:

* Style fix

-----------------------

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.4-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: Comment use of devm_krealloc rather than
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           |  5 +++++
 include/linux/device.h                          | 11 +++++++++++
 6 files changed, 30 insertions(+), 19 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 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.

---
 Documentation/driver-api/driver-model/devres.rst |  1 +
 include/linux/device.h                           | 11 +++++++++++
 2 files changed, 12 insertions(+)
Documentation/driver-api/driver-model/devres.rst +1 -0
index 4249eb4239e0..8be086b3f829 100644
@@ -364,6 +364,7 @@ MEM
   devm_kmalloc_array()
   devm_kmemdup()
   devm_krealloc()
+  devm_krealloc_array()
   devm_kstrdup()
   devm_kstrdup_const()
   devm_kvasprintf()
include/linux/device.h +11 -0
index 472dd24d4823..58f4f5948edb 100644
@@ -223,6 +223,17 @@ 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);
Signature · 1 line
2.34.1
maintainer
#2 lore raw
Quoted 27 lines · James Clark

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

---
 Documentation/driver-api/driver-model/devres.rst |  1 +
 include/linux/device.h                           | 11 +++++++++++
 2 files changed, 12 insertions(+)
Documentation/driver-api/driver-model/devres.rst +1 -0
index 4249eb4239e0..8be086b3f829 100644
@@ -364,6 +364,7 @@ MEM
   devm_kmalloc_array()
   devm_kmemdup()
   devm_krealloc()
+  devm_krealloc_array()
   devm_kstrdup()
   devm_kstrdup_const()
   devm_kvasprintf()
include/linux/device.h +0 -0
index 472dd24d4823..58f4f5948edb 100644

include/linux/device.h static inline void *devm_kcalloc(struct device *dev, · line 226

Quoted 4 lines
include/linux/device.h +1 -0
@@ -223,6 +223,17 @@ 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

Shouldn't you have a blank line before this one?

include/linux/device.h static inline void *devm_kcalloc(struct device *dev, · line 235

Quoted 9 lines
include/linux/device.h +9 -0
+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);
+}

I dislike how we have to keep copying the "real" functions (i.e. krealloc_array) into something like this, but I can't think of a better way to do it.

thanks,

greg k-h

#3 lore raw
Quoted 26 lines · Greg KH
Quoted 25 lines · James Clark

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

---
 Documentation/driver-api/driver-model/devres.rst |  1 +
 include/linux/device.h                           | 11 +++++++++++
 2 files changed, 12 insertions(+)
Documentation/driver-api/driver-model/devres.rst +1 -0
index 4249eb4239e0..8be086b3f829 100644
@@ -364,6 +364,7 @@ MEM
   devm_kmalloc_array()
   devm_kmemdup()
   devm_krealloc()
+  devm_krealloc_array()
   devm_kstrdup()
   devm_kstrdup_const()
   devm_kvasprintf()
include/linux/device.h +0 -1
index 472dd24d4823..58f4f5948edb 100644
--- a/include/linux/device.h
Quoted 8 lines

include/linux/device.h static inline void *devm_kcalloc(struct device *dev, · line 226

Quoted 6 lines

+++ b/include/linux/device.h

include/linux/device.h +1 -0
@@ -223,6 +223,17 @@ 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

Shouldn't you have a blank line before this one?

I was going for consistency with the rest of this section which doesn't have newlines between the functions for some reason. I can add one and resubmit but it might look a bit out of place?

Quoted 15 lines

include/linux/device.h static inline void *devm_kcalloc(struct device *dev, · line 235

Quoted 10 lines
include/linux/device.h +9 -0
+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);
+}

I dislike how we have to keep copying the "real" functions (i.e. krealloc_array) into something like this, but I can't think of a better way to do it.

Maybe something could be done with some macro magic, but it would probably end up being worse than just copying them and would affect the real ones as well. So yeah I can't think of any easy gains either.

Thanks
James

Quoted 3 lines

thanks,

greg k-h

maintainer
#4 lore raw
Quoted 38 lines · James Clark
Quoted 26 lines · Greg KH
Quoted 25 lines · James Clark

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

---
 Documentation/driver-api/driver-model/devres.rst |  1 +
 include/linux/device.h                           | 11 +++++++++++
 2 files changed, 12 insertions(+)
Documentation/driver-api/driver-model/devres.rst +1 -0
index 4249eb4239e0..8be086b3f829 100644
@@ -364,6 +364,7 @@ MEM
   devm_kmalloc_array()
   devm_kmemdup()
   devm_krealloc()
+  devm_krealloc_array()
   devm_kstrdup()
   devm_kstrdup_const()
   devm_kvasprintf()
include/linux/device.h +0 -1
index 472dd24d4823..58f4f5948edb 100644
--- a/include/linux/device.h
Quoted 8 lines

include/linux/device.h static inline void *devm_kcalloc(struct device *dev, · line 226

Quoted 6 lines

+++ b/include/linux/device.h

include/linux/device.h +1 -0
@@ -223,6 +223,17 @@ 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

Shouldn't you have a blank line before this one?

Quoted 3 lines

I was going for consistency with the rest of this section which doesn't have newlines between the functions for some reason. I can add one and resubmit but it might look a bit out of place?

Ah, wasn't aware of that, given the lack of context. So nevermind, it's fine for now.

Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
include/linux/device.h +5 -0
+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)))
Quoted 9 lines

include/linux/device.h static inline void *devm_kcalloc(struct device *dev, · line 235

Quoted 4 lines
include/linux/device.h +4 -0
+		return NULL;
+
+	return devm_krealloc(dev, p, bytes, flags);
+}

I dislike how we have to keep copying the "real" functions (i.e. krealloc_array) into something like this, but I can't think of a better way to do it.

Quoted 3 lines

Maybe something could be done with some macro magic, but it would probably end up being worse than just copying them and would affect the real ones as well. So yeah I can't think of any easy gains either.

Ok, that's good. Given a lack of objections from others, I'll just take this through my driver core tree in a few days.

thanks,

greg k-h

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#5 lore raw

(Removed irrelevant recipients), +Cc: coresight ml

Hi Greg,

Quoted 14 lines · Greg KH
Quoted 13 lines · James Clark
Quoted 10 lines · Greg KH
Quoted 9 lines · James Clark

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

---
  Documentation/driver-api/driver-model/devres.rst |  1 +
  include/linux/device.h                           | 11 +++++++++++
  2 files changed, 12 insertions(+)

...

Quoted 6 lines
Quoted 3 lines

Maybe something could be done with some macro magic, but it would probably end up being worse than just copying them and would affect the real ones as well. So yeah I can't think of any easy gains either.

Ok, that's good. Given a lack of objections from others, I'll just take this through my driver core tree in a few days.

Apologies for hijacking the thread. We have a series for CoreSight[1] that depends on this series, which I see that, is queued in your driver-core-next.

I would like to queue [1] for the next version (as there are other
work that depend on this, e.g., [2]). Do you have any recommendations/comments on the proposal ? Are you able to share a stable branch which can be merged to coresight/next and queue the
series ontop ? (PS: I haven't queued anything for coresight/next yet).

Kind regards
Suzuki

#6 lore raw

Hi Greg,

Links updated to the series.

Quoted 29 lines · Suzuki K Poulose

(Removed irrelevant recipients), +Cc: coresight ml

Hi Greg,

Quoted 14 lines · Greg KH
Quoted 13 lines · James Clark
Quoted 10 lines · Greg KH
Quoted 9 lines · James Clark

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

---
  Documentation/driver-api/driver-model/devres.rst |  1 + include/linux/device.h                           | 11 +++++++++++

  2 files changed, 12 insertions(+)

...

Quoted 6 lines
Quoted 3 lines

Maybe something could be done with some macro magic, but it would probably end up being worse than just copying them and would affect the real ones as well. So yeah I can't think of any easy gains either.

Ok, that's good.  Given a lack of objections from others, I'll just take this through my driver core tree in a few days.

Quoted 8 lines

Apologies for hijacking the thread. We have a series for CoreSight[1] that depends on this series, which I see that, is queued in your driver-core-next.

I would like to queue [1] for the next version (as there are other
work that depend on this, e.g., [2]). Do you have any recommendations/comments on the proposal ? Are you able to share a stable branch which can be merged to coresight/next and queue the
series ontop ? (PS: I haven't queued anything for coresight/next yet).

[1] https://lkml.kernel.org/r/[email protected]
[2] https://lkml.kernel.org/r/[email protected]

Suzuki

maintainer
#7 lore raw
In reply to Suzuki K Poulose
Quoted 33 lines · Suzuki K Poulose

(Removed irrelevant recipients), +Cc: coresight ml

Hi Greg,

Quoted 14 lines · Greg KH
Quoted 13 lines · James Clark
Quoted 10 lines · Greg KH
Quoted 9 lines · James Clark

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

---
  Documentation/driver-api/driver-model/devres.rst |  1 +
  include/linux/device.h                           | 11 +++++++++++
  2 files changed, 12 insertions(+)

...

Quoted 6 lines
Quoted 3 lines

Maybe something could be done with some macro magic, but it would probably end up being worse than just copying them and would affect the real ones as well. So yeah I can't think of any easy gains either.

Ok, that's good. Given a lack of objections from others, I'll just take this through my driver core tree in a few days.

Apologies for hijacking the thread. We have a series for CoreSight[1] that depends on this series, which I see that, is queued in your driver-core-next.

Quoted 5 lines

I would like to queue [1] for the next version (as there are other work that depend on this, e.g., [2]). Do you have any recommendations/comments on the proposal ? Are you able to share a stable branch which can be merged to coresight/next and queue the series ontop ? (PS: I haven't queued anything for coresight/next yet).

You can pull from my driver-core-next branch just fine and assume it will be stable. So just pull in that one commit and all should be good in the future.

thanks,

greg k-h

#8 lore raw
Quoted 40 lines · Greg KH
Quoted 33 lines · Suzuki K Poulose

(Removed irrelevant recipients), +Cc: coresight ml

Hi Greg,

Quoted 14 lines · Greg KH
Quoted 13 lines · James Clark
Quoted 10 lines · Greg KH
Quoted 9 lines · James Clark

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

---
   Documentation/driver-api/driver-model/devres.rst |  1 +
   include/linux/device.h                           | 11 +++++++++++
   2 files changed, 12 insertions(+)

...

Quoted 6 lines
Quoted 3 lines

Maybe something could be done with some macro magic, but it would probably end up being worse than just copying them and would affect the real ones as well. So yeah I can't think of any easy gains either.

Ok, that's good. Given a lack of objections from others, I'll just take this through my driver core tree in a few days.

Apologies for hijacking the thread. We have a series for CoreSight[1] that depends on this series, which I see that, is queued in your driver-core-next.

Quoted 5 lines

I would like to queue [1] for the next version (as there are other work that depend on this, e.g., [2]). Do you have any recommendations/comments on the proposal ? Are you able to share a stable branch which can be merged to coresight/next and queue the series ontop ? (PS: I haven't queued anything for coresight/next yet).

Quoted 3 lines

You can pull from my driver-core-next branch just fine and assume it will be stable. So just pull in that one commit and all should be good in the future.

Thanks Greg, I will give it a go

Suzuki

Quoted 4 lines

thanks,

greg k-h

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#9 lore raw

Hi Greg

Quoted 3 lines · Suzuki K Poulose
Quoted 2 lines · Greg KH
Quoted 1 line · Suzuki K Poulose

(Removed irrelevant recipients), +Cc: coresight ml

...

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

Apologies for hijacking the thread. We have a series for CoreSight[1] that depends on this series, which I see that, is queued in your driver-core-next.

I would like to queue [1] for the next version (as there are other work that depend on this, e.g., [2]). Do you have any recommendations/comments on the proposal ? Are you able to share a stable branch which can be merged to coresight/next and queue the series ontop ? (PS: I haven't queued anything for coresight/next yet).

Quoted 5 lines
Quoted 3 lines

You can pull from my driver-core-next branch just fine and assume it will be stable.  So just pull in that one commit and all should be good in the future.

Thanks Greg, I will give it a go

Does this look fine with you ?

https://git.kernel.org/pub/scm/linux/kernel/git/coresight/linux.git/log/?h=tmp/devm_krealloc_array

Suzuki

maintainer
#10 lore raw
Quoted 23 lines · Suzuki K Poulose

Hi Greg

Quoted 3 lines · Suzuki K Poulose
Quoted 2 lines · Greg KH
Quoted 1 line · Suzuki K Poulose

(Removed irrelevant recipients), +Cc: coresight ml

...

Quoted 14 lines
Quoted 13 lines
Quoted 9 lines

Apologies for hijacking the thread. We have a series for CoreSight[1] that depends on this series, which I see that, is queued in your driver-core-next.

I would like to queue [1] for the next version (as there are other work that depend on this, e.g., [2]). Do you have any recommendations/comments on the proposal ? Are you able to share a stable branch which can be merged to coresight/next and queue the series ontop ? (PS: I haven't queued anything for coresight/next yet).

You can pull from my driver-core-next branch just fine and assume it will be stable.  So just pull in that one commit and all should be good in the future.

Quoted 5 lines
Quoted 1 line

Thanks Greg, I will give it a go

Does this look fine with you ?

https://git.kernel.org/pub/scm/linux/kernel/git/coresight/linux.git/log/?h=tmp/devm_krealloc_array

Looks good to me!

#12 lore raw

PATCH v4 2/4 hwmon: pmbus: Use devm_krealloc_array

In reply to James Clark

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

---
 drivers/hwmon/pmbus/pmbus_core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/hwmon/pmbus/pmbus_core.c +3 -3
index 9d14954da94f..fa06325f5a7c 100644
@@ -1191,9 +1191,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;
Signature · 1 line
2.34.1
#13 lore raw

PATCH v4 3/4 iio: adc: Use devm_krealloc_array

In reply to James Clark

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

---
 drivers/iio/adc/xilinx-ams.c       |  9 +++------
 drivers/iio/adc/xilinx-xadc-core.c | 17 +++++++----------
 2 files changed, 10 insertions(+), 16 deletions(-)
drivers/iio/adc/xilinx-ams.c +3 -6
index 34cf336b3490..f0b71a1220e0 100644
@@ -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;
 
drivers/iio/adc/xilinx-xadc-core.c +7 -10
index 292f2892d223..dba73300f894 100644
@@ -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;
Signature · 1 line
2.34.1
#14 lore raw

PATCH v4 4/4 serial: qcom_geni: Comment use of devm_krealloc rather than devm_krealloc_array

In reply to James Clark

Now that devm_krealloc_array is available, add a comment justifying not changing this occurrence to avoid any future auto fixups.

---
 drivers/tty/serial/qcom_geni_serial.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/tty/serial/qcom_geni_serial.c +5 -0
index 08dc3e2a729c..3a6cf762449f 100644
@@ -1053,6 +1053,11 @@ 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) {
+		/*
+		 * Use krealloc rather than krealloc_array because rx_buf is
+		 * accessed as 1 byte entries as well as 4 byte entries so it's
+		 * not necessarily an array.
+		 */
 		port->rx_buf = devm_krealloc(uport->dev, port->rx_buf,
 					     port->rx_fifo_depth * sizeof(u32),
 					     GFP_KERNEL);
Signature · 1 line
2.34.1