Skip to contentSkip to conversation

staging: iio replaced kmalloc with local variables.

PATCH In review anish 7 participants 14 messages Jun 6, 2011 – Jun 7, 2011 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

anish

author
#0 lore raw

PATCH 1/2 staging: iio replaced kmalloc with local variables.

From: anish kumar <[email protected]>

Replace kmalloc with local variables as it was un-necessary and also removed the redudant code after this change.

---
 drivers/staging/iio/accel/kxsd9.c      |   19 +++----------------
 drivers/staging/iio/adc/max1363_core.c |    3 +--
 2 files changed, 4 insertions(+), 18 deletions(-)
drivers/staging/iio/accel/kxsd9.c +3 -16
index 431aa0f..7f6e6e5 100644
@@ -255,7 +255,10 @@ static const struct attribute_group kxsd9_attribute_group = {
 
 static int __devinit kxsd9_power_up(struct spi_device *spi)
 {
+	struct spi_message msg;
 	int ret;
+	u8 tx[2], tx2[2];
+
 	struct spi_transfer xfers[2] = {
 		{
 			.bits_per_word = 8,
@@ -267,19 +270,7 @@ static int __devinit kxsd9_power_up(struct spi_device *spi)
 			.cs_change = 1,
 		},
 	};
-	struct spi_message msg;
-	u8 *tx2;
-	u8 *tx = kmalloc(2, GFP_KERNEL);
 
-	if (tx == NULL) {
-		ret = -ENOMEM;
-		goto error_ret;
-	}
-	tx2 = kmalloc(2, GFP_KERNEL);
-	if (tx2 == NULL) {
-		ret = -ENOMEM;
-		goto error_free_tx;
-	}
 	tx[0] = 0x0d;
 	tx[1] = 0x40;
 
@@ -293,10 +284,6 @@ static int __devinit kxsd9_power_up(struct spi_device *spi)
 	spi_message_add_tail(&xfers[1], &msg);
 	ret = spi_sync(spi, &msg);
 
-	kfree(tx2);
-error_free_tx:
-	kfree(tx);
-error_ret:
 	return ret;
 
 };
drivers/staging/iio/adc/max1363_core.c +1 -2
index 1037087..0026242 100644
@@ -207,7 +207,7 @@ static int max1363_write_basic_config(struct i2c_client *client,
 				      unsigned char d2)
 {
 	int ret;
-	u8 *tx_buf = kmalloc(2, GFP_KERNEL);
+	u8 tx_buf[2];
 
 	if (!tx_buf)
 		return -ENOMEM;
@@ -215,7 +215,6 @@ static int max1363_write_basic_config(struct i2c_client *client,
 	tx_buf[1] = d2;
 
 	ret = i2c_master_send(client, tx_buf, 2);
-	kfree(tx_buf);
 
 	return (ret > 0) ? 0 : ret;
 }
Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 3 lines · anish

From: anish kumar <[email protected]>

Replace kmalloc with local variables as it was un-necessary and

Is it really unnecessary?
Or is this hardware that cannot transfer buffers on the stack? IIRC there have been similar problems with SCSI command buffers on the stack.

Quoted 76 lines

also removed the redudant code after this change.

---
 drivers/staging/iio/accel/kxsd9.c      |   19 +++---------------- drivers/staging/iio/adc/max1363_core.c |    3 +--

 2 files changed, 4 insertions(+), 18 deletions(-)
drivers/staging/iio/accel/kxsd9.c +3 -16
index 431aa0f..7f6e6e5 100644
@@ -255,7 +255,10 @@ static const struct attribute_group kxsd9_attribute_group = {
 
  static int __devinit kxsd9_power_up(struct spi_device *spi)
  {
+       struct spi_message msg;
        int ret;
+       u8 tx[2], tx2[2];
+
        struct spi_transfer xfers[2] = {
                {
                        .bits_per_word = 8,
@@ -267,19 +270,7 @@ static int __devinit kxsd9_power_up(struct spi_device *spi)
                        .cs_change = 1,
                },
        };
-       struct spi_message msg;
-       u8 *tx2;
-       u8 *tx = kmalloc(2, GFP_KERNEL);
 
-       if (tx == NULL) {
-               ret = -ENOMEM;
-               goto error_ret;
-       }
-       tx2 = kmalloc(2, GFP_KERNEL);
-       if (tx2 == NULL) {
-               ret = -ENOMEM;
-               goto error_free_tx;
-       }
        tx[0] = 0x0d;
        tx[1] = 0x40;
 
@@ -293,10 +284,6 @@ static int __devinit kxsd9_power_up(struct spi_device *spi)
        spi_message_add_tail(&xfers[1], &msg);
        ret = spi_sync(spi, &msg);
 
-       kfree(tx2);
-error_free_tx:
-       kfree(tx);
-error_ret:
        return ret;
 
  };
drivers/staging/iio/adc/max1363_core.c +1 -3
index 1037087..0026242 100644
@@ -207,7 +207,7 @@ static int max1363_write_basic_config(struct i2c_client *client,
                                      unsigned char d2)
  {
        int ret;
-       u8 *tx_buf = kmalloc(2, GFP_KERNEL);
+       u8 tx_buf[2];
 
        if (!tx_buf)
                return -ENOMEM;
@@ -215,7 +215,6 @@ static int max1363_write_basic_config(struct i2c_client *client,
        tx_buf[1] = d2;
 
        ret = i2c_master_send(client, tx_buf, 2);
-       kfree(tx_buf);
 
        return (ret > 0) ? 0 : ret;
  }
--
Quoted 1 line

1.7.0.4

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds

#2 lore raw
Quoted 8 lines · Geert Uytterhoeven
Quoted 3 lines · anish

From: anish kumar <[email protected]>

Replace kmalloc with local variables as it was un-necessary and

Is it really unnecessary?
Or is this hardware that cannot transfer buffers on the stack? IIRC there have been similar problems with SCSI command buffers on the stack.

Yes. You're right. These buffers are not allowed to be stack memory. The documentation is the section "What memory is DMA'able?" in Documentation/DMA-API-HOWTO.txt.

regards,
dan carpenter

#3 lore raw
In reply to anish

Am Montag 06 Juni 2011, 21:07:37 schrieb anish:

drivers/staging/iio/adc/max1363_core.c static int max1363_write_basic_config(struct i2c_client · line 213

Quoted 10 lines

+++ b/drivers/staging/iio/adc/max1363_core.c

drivers/staging/iio/adc/max1363_core.c +1 -1
@@ -207,7 +207,7 @@ static int max1363_write_basic_config(struct i2c_client
 *client, unsigned char d2)
 {
 	int ret;
-	u8 *tx_buf = kmalloc(2, GFP_KERNEL);
+	u8 tx_buf[2];
 
 	if (!tx_buf)
 		return -ENOMEM;

These last two lines can also be removed ;)

Thanks,
Peter

#4 lore raw
In reply to anish
Quoted 4 lines · anish

From: anish kumar <[email protected]>

Replace kmalloc with local variables as it was un-necessary and also removed the redudant code after this change.

No, it was necessary, you just broke this driver on ARM-based systems, which isn't nice at all :(

SPI data, like USB data, has to come from kmalloced data, not from the stack, or bad things can, and will, happen.

So I can't accept this patch, sorry.

greg k-h

#5 lore raw
Quoted 6 lines · Greg KH
Quoted 3 lines · anish

From: anish kumar <[email protected]> Replace kmalloc with local variables as it was un-necessary and also removed the redudant code after this change.

SPI data, like USB data, has to come from kmalloced data, not from the stack, or bad things can, and will, happen.

Perhaps just add a comment like:

+ u8 *tx = kmalloc(2, GFP_KERNEL); /* can't be on stack */

It might be better to do a single kmalloc(4)
than 2 separate kmalloc(2)'s.

#6 lore raw
Quoted 11 lines · Joe Perches
Quoted 6 lines · Greg KH
Quoted 3 lines · anish

From: anish kumar <[email protected]> Replace kmalloc with local variables as it was un-necessary and also removed the redudant code after this change.

SPI data, like USB data, has to come from kmalloced data, not from the stack, or bad things can, and will, happen.

Perhaps just add a comment like:

+ u8 *tx = kmalloc(2, GFP_KERNEL); /* can't be on stack */

You really want to do to that for _EVERY_ SPI and USB driver? I don't think so. It's a known thing that this is a requirement for SPI and USB drivers.

Quoted 2 lines

It might be better to do a single kmalloc(4)
than 2 separate kmalloc(2)'s.

No, don't do that, that might cause problems as well with some controllers. We see some ARM controllers having problems with alignment issues. Now ideally we are fixing them in those controllers, and not having to fix them in the individual drivers, but if at all possible, a new allocation is the way to go.

thanks,

greg k-h

#7 lore raw
Quoted 6 lines · Greg KH
Quoted 5 lines · Joe Perches
Quoted 4 lines · Greg KH
Quoted 3 lines · anish

From: anish kumar <[email protected]> Replace kmalloc with local variables as it was un-necessary and also removed the redudant code after this change.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

SPI data, like USB data, has to come from kmalloced data, not from the stack, or bad things can, and will, happen.

Perhaps just add a comment like:
+ u8 *tx = kmalloc(2, GFP_KERNEL); /* can't be on stack */

You really want to do to that for _EVERY_ SPI and USB driver? I don't think so.

Nope, only the ones that look especially odd because
kmalloc(sizeof(struct foo), ...)
or

	kmalloc(sizeof("type), ...)

is not used.

It might be better to just declare a 2 byte struct.

cheers, Joe

#8 lore raw
Quoted 14 lines · Joe Perches
Quoted 6 lines · Greg KH
Quoted 5 lines · Joe Perches
Quoted 4 lines · Greg KH
Quoted 3 lines · anish

From: anish kumar <[email protected]> Replace kmalloc with local variables as it was un-necessary and also removed the redudant code after this change.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

SPI data, like USB data, has to come from kmalloced data, not from the stack, or bad things can, and will, happen.

Perhaps just add a comment like:
+ u8 *tx = kmalloc(2, GFP_KERNEL); /* can't be on stack */

You really want to do to that for _EVERY_ SPI and USB driver? I don't think so.

Quoted 7 lines

Nope, only the ones that look especially odd because
kmalloc(sizeof(struct foo), ...)
or

	kmalloc(sizeof("type), ...)

is not used.

It might be better to just declare a 2 byte struct.

No, this is a very common thing for all USB and SPI drivers. It's so obvious that once I saw the Subject: line, I knew this patch was going to be wrong.

This is something that the USB and SPI developers know all about, it's the way things work, and this driver works, so why are people trying to "clean" it up in ways that will break it, or cause extra work with structures where they are not needed at all?

odd.

greg k-h

#9 lore raw
Quoted 20 lines · Greg KH
Quoted 11 lines · Joe Perches
Quoted 10 lines · Greg KH
Quoted 9 lines · Joe Perches
Quoted 6 lines · Greg KH
Quoted 3 lines · anish

From: anish kumar <[email protected]> Replace kmalloc with local variables as it was un-necessary and also removed the redudant code after this change.

SPI data, like USB data, has to come from kmalloced data, not from the stack, or bad things can, and will, happen.

Perhaps just add a comment like:
+ u8 *tx = kmalloc(2, GFP_KERNEL); /* can't be on stack */

Quoted 8 lines
Quoted 2 lines

You really want to do to that for _EVERY_ SPI and USB driver? I don't think so.

Nope, only the ones that look especially odd because
kmalloc(sizeof(struct foo), ...)
or

	kmalloc(sizeof("type), ...)

is not used.
It might be better to just declare a 2 byte struct.

Quoted 3 lines

No, this is a very common thing for all USB and SPI drivers. It's so obvious that once I saw the Subject: line, I knew this patch was going to be wrong.

As did I.
I seek to find a way to avoid seeing them in the future too.

Quoted 5 lines

This is something that the USB and SPI developers know all about, it's the way things work, and this driver works, so why are people trying to "clean" it up in ways that will break it, or cause extra work with structures where they are not needed at all?
odd.

Because people perform pattern recognition as a means to avoid the work required for complete understanding.

Comments akin to the one in drivers/usb/serial/io_ti.c:

lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
as not all platforms can do DMA from stack */

help people avoid applying patterns to inappropriate uses.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#11 lore raw
Quoted 31 lines · anish singh

On Tue, Jun 7, 2011 at 4:11 AM, Greg KH <[email protected] <mailto:[email protected]>> wrote:

On Mon, Jun 06, 2011 at 03:28:29PM -0700, Joe Perches wrote:
> On Mon, 2011-06-06 at 15:21 -0700, Greg KH wrote:
> > On Mon, Jun 06, 2011 at 03:10:57PM -0700, Joe Perches wrote:
> > > On Mon, 2011-06-06 at 14:55 -0700, Greg KH wrote:
> > > > On Tue, Jun 07, 2011 at 12:37:37AM +0530, anish wrote:
> > > > > From: anish kumar <[email protected] <mailto:[email protected]>> > > > > > Replace kmalloc with local variables as it was un-necessary and
> > > > > also removed the redudant code after this change.
> > > > SPI data, like USB data, has to come from kmalloced data, not from the > > > > stack, or bad things can, and will, happen.
> > > Perhaps just add a comment like:
> > > + u8 *tx = kmalloc(2, GFP_KERNEL); /* can't be on stack */
> > You really want to do to that for _EVERY_ SPI and USB driver? I don't
> > think so.
>
> Nope, only the ones that look especially odd because
> kmalloc(sizeof(struct foo), ...)
> or
> kmalloc(sizeof("type), ...)
> is not used.
>
> It might be better to just declare a 2 byte struct.

No, this is a very common thing for all USB and SPI drivers. It's so obvious that once I saw the Subject: line, I knew this patch was going to be wrong.

Quoted 8 lines

This is something that the USB and SPI developers know all about, it's the way things work, and this driver works, so why are people trying to "clean" it up in ways that will break it, or cause extra work with structures where they are not needed at all?

Sorry for noise as i didn't the SPI requirements,thought it is similar to I2C and in cleaning up below part i wrongly cleaned SPI part also.Below was also part of patch.

Not to worry, you are far from the first person to fall into this issue! Also, you have highlighted a weird corner in that driver, that could do with tidying up (just not quite the fix you had in mind!).

Quoted 14 lines

static int max1363_write_basic_config(struct i2c_client *client,
unsigned char d2)

 {
       int ret;
(quoted diff) +1 -2
-       u8 *tx_buf = kmalloc(2, GFP_KERNEL);
+       u8 tx_buf[2];
       if (!tx_buf)
               return -ENOMEM;
@@ -215,7 +215,6 @@ static int max1363_write_basic_config(struct i2c_client *client,
       tx_buf[1] = d2;
       ret = i2c_master_send(client, tx_buf, 2);
-       kfree(tx_buf);
       return (ret > 0) ? 0 : ret;
 }
Quoted 1 line

I think above patch is ok as it is I2C and I2C doesn't have that requirement.

Yes. I2C bus drivers that do dma do the copy into dma safe memory internally. Makes for more bouncing around of data, but i2c is slow anyway so it doesn't matter. Also, based on a quick look this morning, the dma buffers tend to require various headers to be in place etc which isn't typically the case for spi (a much more 'raw' bus).

Can you cc [email protected] on that patch when you send it out please.

Jonathan

#12 lore raw
Quoted 57 lines · Jonathan Cameron
Quoted 37 lines · anish singh

On Tue, Jun 7, 2011 at 4:11 AM, Greg KH <[email protected] <mailto:[email protected]>> wrote:

On Mon, Jun 06, 2011 at 03:28:29PM -0700, Joe Perches wrote:
> On Mon, 2011-06-06 at 15:21 -0700, Greg KH wrote:
> > On Mon, Jun 06, 2011 at 03:10:57PM -0700, Joe Perches wrote:
> > > On Mon, 2011-06-06 at 14:55 -0700, Greg KH wrote:
> > > > On Tue, Jun 07, 2011 at 12:37:37AM +0530, anish wrote:
> > > > > From: anish kumar <[email protected] <mailto:[email protected]>> > > > > > Replace kmalloc with local variables as it was un-necessary and > > > > > also removed the redudant code after this change. > > > > SPI data, like USB data, has to come from kmalloced data, not from the > > > > stack, or bad things can, and will, happen. > > > Perhaps just add a comment like:
> > > + u8 *tx = kmalloc(2, GFP_KERNEL); /* can't be on stack */
> > You really want to do to that for _EVERY_ SPI and USB driver? I don't > > think so.
>
> Nope, only the ones that look especially odd because
> kmalloc(sizeof(struct foo), ...)
> or
> kmalloc(sizeof("type), ...)
> is not used.
>
> It might be better to just declare a 2 byte struct.

No, this is a very common thing for all USB and SPI drivers. It's so obvious that once I saw the Subject: line, I knew this patch was going to be wrong.

This is something that the USB and SPI developers know all about, it's the way things work, and this driver works, so why are people trying to "clean" it up in ways that will break it, or cause extra work with structures where they are not needed at all?

Quoted 4 lines

Sorry for noise as i didn't the SPI requirements,thought it is similar to I2C and
in cleaning up below part i wrongly cleaned SPI part also.Below was also part of patch.

Not to worry, you are far from the first person to fall into this issue! Also, you have highlighted a weird corner in that driver, that could do with tidying up (just not quite the fix you had in mind!).

Quoted 12 lines

static int max1363_write_basic_config(struct i2c_client *client,
unsigned char d2)

 {
       int ret;
(quoted diff) +1 -2
-       u8 *tx_buf = kmalloc(2, GFP_KERNEL);
+       u8 tx_buf[2];
       if (!tx_buf)
               return -ENOMEM;
@@ -215,7 +215,6 @@ static int max1363_write_basic_config(struct i2c_client
       *client, tx_buf[1] = d2;
       ret = i2c_master_send(client, tx_buf, 2);
-       kfree(tx_buf);
Quoted 8 lines
Quoted 3 lines
(quoted diff) +0 -0
       return (ret > 0) ? 0 : ret;
 }

I think above patch is ok as it is I2C and I2C doesn't have that requirement.

Yes. I2C bus drivers that do dma do the copy into dma safe memory internally. Makes for more bouncing around of data, but i2c is slow anyway so it doesn't matter. Also, based on a quick look this morning, the dma buffers tend to require various headers to be in place etc which isn't typically the case for spi (a much more 'raw' bus).

I couldn't understand this comment.Specifically "various headers"? Will appreciate it if you kindly explain.

Quoted 3 lines

Can you cc [email protected] on that patch when you send it out please.

Sure.Sorry for not sending it there.

Quoted 2 lines

Jonathan

#13 lore raw
Quoted 66 lines · anish kumar
Quoted 57 lines · Jonathan Cameron
Quoted 37 lines · anish singh

On Tue, Jun 7, 2011 at 4:11 AM, Greg KH <[email protected] <mailto:[email protected]>> wrote:

On Mon, Jun 06, 2011 at 03:28:29PM -0700, Joe Perches wrote:
> On Mon, 2011-06-06 at 15:21 -0700, Greg KH wrote:
> > On Mon, Jun 06, 2011 at 03:10:57PM -0700, Joe Perches wrote:
> > > On Mon, 2011-06-06 at 14:55 -0700, Greg KH wrote:
> > > > On Tue, Jun 07, 2011 at 12:37:37AM +0530, anish wrote:
> > > > > From: anish kumar <[email protected] <mailto:[email protected]>> > > > > > Replace kmalloc with local variables as it was un-necessary and > > > > > also removed the redudant code after this change. > > > > SPI data, like USB data, has to come from kmalloced data, not from the > > > > stack, or bad things can, and will, happen. > > > Perhaps just add a comment like:
> > > + u8 *tx = kmalloc(2, GFP_KERNEL); /* can't be on stack */
> > You really want to do to that for _EVERY_ SPI and USB driver? I don't > > think so.
>
> Nope, only the ones that look especially odd because
> kmalloc(sizeof(struct foo), ...)
> or
> kmalloc(sizeof("type), ...)
> is not used.
>
> It might be better to just declare a 2 byte struct.

No, this is a very common thing for all USB and SPI drivers. It's so obvious that once I saw the Subject: line, I knew this patch was going to be wrong.

This is something that the USB and SPI developers know all about, it's the way things work, and this driver works, so why are people trying to "clean" it up in ways that will break it, or cause extra work with structures where they are not needed at all?

Quoted 4 lines

Sorry for noise as i didn't the SPI requirements,thought it is similar to I2C and
in cleaning up below part i wrongly cleaned SPI part also.Below was also part of patch.

Not to worry, you are far from the first person to fall into this issue! Also, you have highlighted a weird corner in that driver, that could do with tidying up (just not quite the fix you had in mind!).

Quoted 12 lines

static int max1363_write_basic_config(struct i2c_client *client,
unsigned char d2)

 {
       int ret;
(quoted diff) +1 -2
-       u8 *tx_buf = kmalloc(2, GFP_KERNEL);
+       u8 tx_buf[2];
       if (!tx_buf)
               return -ENOMEM;
@@ -215,7 +215,6 @@ static int max1363_write_basic_config(struct i2c_client
       *client, tx_buf[1] = d2;
       ret = i2c_master_send(client, tx_buf, 2);
-       kfree(tx_buf);
Quoted 8 lines
Quoted 3 lines
(quoted diff) +0 -0
       return (ret > 0) ? 0 : ret;
 }

I think above patch is ok as it is I2C and I2C doesn't have that requirement.

Yes. I2C bus drivers that do dma do the copy into dma safe memory internally. Makes for more bouncing around of data, but i2c is slow anyway so it doesn't matter. Also, based on a quick look this morning, the dma buffers tend to require various headers to be in place etc which isn't typically the case for spi (a much more 'raw' bus).

Quoted 1 line

I couldn't understand this comment.Specifically "various headers"?

principally looking at some drivers, i2c has an address. This is sometimes at the start of the dma buffer sent to the controller.

Quoted 1 line

Will appreciate it if you kindly explain.

Take a look at say, i2c-cpm.c (first example grep gave me ;) cpm_i2c_parse_message
is the function that builds up the dma buffer contents.
The memcpy gives it away, as that is copying to +1 in the buffer that is dma'd off to the controller. The tb[0] contains the address.

Anyhow, this is probably needed for some spi controllers as well, but certainly not all of them. Digging down in pxa2xx_spi for example, we have a the original tx and rx pointers passed all the way through to the eventual dma transfer ( I think, that driver isn't all that easy to follow!).

Jonathan

#14 lore raw
In reply to Joe Perches
Quoted 8 lines · Joe Perches
Quoted 6 lines · Greg KH
Quoted 3 lines · anish

From: anish kumar <[email protected]> Replace kmalloc with local variables as it was un-necessary and also removed the redudant code after this change.

SPI data, like USB data, has to come from kmalloced data, not from the stack, or bad things can, and will, happen.

Quoted 6 lines

Perhaps just add a comment like:

+ u8 *tx = kmalloc(2, GFP_KERNEL); /* can't be on stack */

It might be better to do a single kmalloc(4)
than 2 separate kmalloc(2)'s.

Actually, this little corner of the driver is the only place it isn't using the buffers allocated with the chip state. After I send our latest clean up series in these are all explicitly marked ____cacheline_aligned anyway which should make it clear something a little unusual is going on.

I'll clean up this function and credit it to Anish (if Anish doesn't mind of course!)

Jonathan