Skip to contentSkip to conversation

iio: temperature: maxim_thermocouple: Fix sparse endianness warnings

PATCH In review sayli karnik 4 participants 6 messages Sep 26, 2016 – Sep 27, 2016 IIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iio: temperature: maxim_thermocouple: Fix sparse endianness warnings

Fix following endianness warnings using sparse:
warning: cast to restricted __be16
warning: cast to restricted __be32
Use __be16 type for incoming read data when storage_bytes equals 2 and __be32 when storage_bytes equals 4. Since the variable allocated already has the correct storage size the patch is classified as cosmetic.

---
v2:
Updated the subject line and changelog
Moved the spi_read function call inside respective case statements so that data is read only once.
Moved the return statement after the switch case

 drivers/iio/temperature/maxim_thermocouple.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/iio/temperature/maxim_thermocouple.c +9 -7
index 39dd202..2683b0f 100644
@@ -123,22 +123,24 @@ static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
 {
 	unsigned int storage_bytes = data->chip->read_size;
 	unsigned int shift = chan->scan_type.shift + (chan->address * 8);
-	unsigned int buf;
+	__be16 buf1;
+	__be32 buf2;
 	int ret;
 
-	ret = spi_read(data->spi, (void *) &buf, storage_bytes);
-	if (ret)
-		return ret;
-
 	switch (storage_bytes) {
 	case 2:
-		*val = be16_to_cpu(buf);
+		ret = spi_read(data->spi, (void *) &buf1, storage_bytes);
+		*val = be16_to_cpu(buf1);
 		break;
 	case 4:
-		*val = be32_to_cpu(buf);
+		ret = spi_read(data->spi, (void *) &buf2, storage_bytes);
+		*val = be32_to_cpu(buf2);
 		break;
 	}
 
+	if (ret)
+		return ret;
+
 	/* check to be sure this is a valid reading */
 	if (*val & data->chip->status_bit)
 		return -EINVAL;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 8 lines · sayli karnik

Fix following endianness warnings using sparse:
warning: cast to restricted __be16
warning: cast to restricted __be32
Use __be16 type for incoming read data when storage_bytes equals 2 and __be32 when storage_bytes equals 4. Since the variable allocated already has the correct storage size the patch is classified as cosmetic.

Hi Sayli -

v2 code looks like it handles both cases.

I think you've made a bug fix!

When the storage_bytes == 2, it would work on LE systems but give invalid data on BE systems. That unsigned int buf points to a location where only 2 bytes of data are placed during spi_read. Those 2 bytes go in the upper bytes. Then when the be16_to_cpu() happens that buf gets cast to 16bits. We get the upper bytes on LE systems (good bits) but the lower bytes on BE systems (bad bits)

Lars - Please check my logic and the code too. If this is a bug let us know if Sayli should include a fix tag.

Pending Lars' review, you can update the changelog.

Take a further look at storage_bytes and you'll find when it is set to 2 and when it is set to 4. That would be good information to include in the changelog. You'll find that this is only a bug in one of the two chips this driver supports.

Check your precommit checkpatch hooks. There are still CHECKs in the spi_read lines of code.

And, back to the less exciting. It would be good for that commit message to say what you did: replace unsigned int with __be16/32 I know you're short of space on that line. It's ok to drop the subdir (temperature) and just state the driver as in:
iio: maxim_thermocouple: commit message.

Please 'CC linux-iio on v3 and the module author, from bottom of source file.

Thanks Sayli,
alisons

Quoted 55 lines

---
v2:
Updated the subject line and changelog
Moved the spi_read function call inside respective case statements so that data is read only once.
Moved the return statement after the switch case

 drivers/iio/temperature/maxim_thermocouple.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/iio/temperature/maxim_thermocouple.c +9 -7
index 39dd202..2683b0f 100644
@@ -123,22 +123,24 @@ static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
 {
 	unsigned int storage_bytes = data->chip->read_size;
 	unsigned int shift = chan->scan_type.shift + (chan->address * 8);
-	unsigned int buf;
+	__be16 buf1;
+	__be32 buf2;
 	int ret;
 
-	ret = spi_read(data->spi, (void *) &buf, storage_bytes);
-	if (ret)
-		return ret;
-
 	switch (storage_bytes) {
 	case 2:
-		*val = be16_to_cpu(buf);
+		ret = spi_read(data->spi, (void *) &buf1, storage_bytes);
+		*val = be16_to_cpu(buf1);
 		break;
 	case 4:
-		*val = be32_to_cpu(buf);
+		ret = spi_read(data->spi, (void *) &buf2, storage_bytes);
+		*val = be32_to_cpu(buf2);
 		break;
 	}
 
+	if (ret)
+		return ret;
+
 	/* check to be sure this is a valid reading */
 	if (*val & data->chip->status_bit)
 		return -EINVAL;
Signature · 8 lines
2.7.4

-- 
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20160926171404.GA11773%40sayli-HP-15-Notebook-PC.
For more options, visit https://groups.google.com/d/optout.
#2 lore raw
Quoted 23 lines · Alison Schofield
Quoted 8 lines · sayli karnik

Fix following endianness warnings using sparse:
warning: cast to restricted __be16
warning: cast to restricted __be32
Use __be16 type for incoming read data when storage_bytes equals 2 and __be32 when storage_bytes equals 4. Since the variable allocated already has the correct storage size the patch is classified as cosmetic.

Hi Sayli -

v2 code looks like it handles both cases.

I think you've made a bug fix!

When the storage_bytes == 2, it would work on LE systems but give invalid data on BE systems. That unsigned int buf points to a location where only 2 bytes of data are placed during spi_read. Those 2 bytes go in the upper bytes. Then when the be16_to_cpu() happens that buf gets cast to 16bits. We get the upper bytes on LE systems (good bits) but the lower bytes on BE systems (bad bits)

Quoted 2 lines

Lars - Please check my logic and the code too. If this is a bug let us know if Sayli should include a fix tag.

Yes, your analysis is correct.

#3 lore raw
In reply to Alison Schofield
Quoted 14 lines · Alison Schofield
Quoted 8 lines · sayli karnik

Fix following endianness warnings using sparse:
warning: cast to restricted __be16
warning: cast to restricted __be32
Use __be16 type for incoming read data when storage_bytes equals 2 and __be32 when storage_bytes equals 4. Since the variable allocated already has the correct storage size the patch is classified as cosmetic.

Hi Sayli -

v2 code looks like it handles both cases.

Quoted 9 lines

I think you've made a bug fix!

When the storage_bytes == 2, it would work on LE systems but give invalid data on BE systems. That unsigned int buf points to a location where only 2 bytes of data are placed during spi_read. Those 2 bytes go in the upper bytes. Then when the be16_to_cpu() happens that buf gets cast to 16bits. We get the upper bytes on LE systems (good bits) but the lower bytes on BE systems (bad bits)

Yes I agree..so it's a bug fix for storage_bytes=2 and cosmetic for storage_bytes=4.

Quoted 5 lines

Lars - Please check my logic and the code too. If this is a bug let us know if Sayli should include a fix tag.

Pending Lars' review, you can update the changelog.

Quoted 8 lines

Take a further look at storage_bytes and you'll find when it is set to 2 and when it is set to 4. That would be good information to include in the changelog. You'll find that this is only a bug in one of the two chips this driver supports.

Check your precommit checkpatch hooks. There are still CHECKs in the spi_read lines of code.

I did run checkpatch, but no CHECKs were caught.
This is strange, only errors and warnings were caught when I ran:
perl scripts/checkpatch.pl -f drivers/iio/temperature/* | less I have run checkpatch many times on staging drivers until now. I can only predict that the CHECK in my code must be that there shouldn't be a space after the type cast.

Quoted 9 lines

And, back to the less exciting. It would be good for that commit message to say what you did: replace unsigned int with __be16/32 I know you're short of space on that line. It's ok to drop the subdir (temperature) and just state the driver as in:
iio: maxim_thermocouple: commit message.

Please 'CC linux-iio on v3 and the module author, from bottom of source file.

Sure!

Quoted 58 lines

Thanks Sayli,
alisons

Quoted 55 lines

---
v2:
Updated the subject line and changelog
Moved the spi_read function call inside respective case statements so that data is read only once.
Moved the return statement after the switch case

 drivers/iio/temperature/maxim_thermocouple.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/iio/temperature/maxim_thermocouple.c +9 -8
index 39dd202..2683b0f 100644
@@ -123,22 +123,24 @@ static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
 {
      unsigned int storage_bytes = data->chip->read_size;
      unsigned int shift = chan->scan_type.shift + (chan->address * 8);
-     unsigned int buf;
+     __be16 buf1;
+     __be32 buf2;
      int ret;
 
-     ret = spi_read(data->spi, (void *) &buf, storage_bytes);
-     if (ret)
-             return ret;
-
      switch (storage_bytes) {
      case 2:
-             *val = be16_to_cpu(buf);
+             ret = spi_read(data->spi, (void *) &buf1, storage_bytes);
+             *val = be16_to_cpu(buf1);
              break;
      case 4:
-             *val = be32_to_cpu(buf);
+             ret = spi_read(data->spi, (void *) &buf2, storage_bytes);
+             *val = be32_to_cpu(buf2);
              break;
      }
 
+     if (ret)
+             return ret;
+
      /* check to be sure this is a valid reading */
      if (*val & data->chip->status_bit)
              return -EINVAL;
--

2.7.4

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20160926171404.GA11773%40sayli-HP-15-Notebook-PC.
For more options, visit https://groups.google.com/d/optout.

#4 lore raw
Quoted 40 lines · sayli karnik
Quoted 14 lines · Alison Schofield
Quoted 8 lines · sayli karnik

Fix following endianness warnings using sparse:
warning: cast to restricted __be16
warning: cast to restricted __be32
Use __be16 type for incoming read data when storage_bytes equals 2 and __be32 when storage_bytes equals 4. Since the variable allocated already has the correct storage size the patch is classified as cosmetic.

Hi Sayli -

v2 code looks like it handles both cases.

Quoted 9 lines

I think you've made a bug fix!

When the storage_bytes == 2, it would work on LE systems but give invalid data on BE systems. That unsigned int buf points to a location where only 2 bytes of data are placed during spi_read. Those 2 bytes go in the upper bytes. Then when the be16_to_cpu() happens that buf gets cast to 16bits. We get the upper bytes on LE systems (good bits) but the lower bytes on BE systems (bad bits)

Yes I agree..so it's a bug fix for storage_bytes=2 and cosmetic for storage_bytes=4.

Quoted 5 lines

Lars - Please check my logic and the code too. If this is a bug let us know if Sayli should include a fix tag.

Pending Lars' review, you can update the changelog.

Quoted 8 lines

Take a further look at storage_bytes and you'll find when it is set to 2 and when it is set to 4. That would be good information to include in the changelog. You'll find that this is only a bug in one of the two chips this driver supports.

Check your precommit checkpatch hooks. There are still CHECKs in the spi_read lines of code.

Quoted 6 lines

I did run checkpatch, but no CHECKs were caught.
This is strange, only errors and warnings were caught when I ran:
perl scripts/checkpatch.pl -f drivers/iio/temperature/* | less I have run checkpatch many times on staging drivers until now. I can only predict that the CHECK in my code must be that there shouldn't be a space after the type cast.

checkpatch may be less verbose on non-staging files. Try running checkpatch on your patch, rather than on the file.

julia

Quoted 78 lines
Quoted 10 lines

And, back to the less exciting. It would be good for that commit message to say what you did: replace unsigned int with __be16/32 I know you're short of space on that line. It's ok to drop the subdir (temperature) and just state the driver as in:
iio: maxim_thermocouple: commit message.

Please 'CC linux-iio on v3 and the module author, from bottom of source file.

Sure!

Quoted 52 lines

Thanks Sayli,
alisons

Quoted 49 lines

---
v2:
Updated the subject line and changelog
Moved the spi_read function call inside respective case statements so that data is read only once.
Moved the return statement after the switch case

 drivers/iio/temperature/maxim_thermocouple.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/iio/temperature/maxim_thermocouple.c +9 -8
index 39dd202..2683b0f 100644
@@ -123,22 +123,24 @@ static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
 {
      unsigned int storage_bytes = data->chip->read_size;
      unsigned int shift = chan->scan_type.shift + (chan->address * 8);
-     unsigned int buf;
+     __be16 buf1;
+     __be32 buf2;
      int ret;
 
-     ret = spi_read(data->spi, (void *) &buf, storage_bytes);
-     if (ret)
-             return ret;
-
      switch (storage_bytes) {
      case 2:
-             *val = be16_to_cpu(buf);
+             ret = spi_read(data->spi, (void *) &buf1, storage_bytes);
+             *val = be16_to_cpu(buf1);
              break;
      case 4:
-             *val = be32_to_cpu(buf);
+             ret = spi_read(data->spi, (void *) &buf2, storage_bytes);
+             *val = be32_to_cpu(buf2);
              break;
      }
 
+     if (ret)
+             return ret;
+
      /* check to be sure this is a valid reading */
      if (*val & data->chip->status_bit)
              return -EINVAL;
--

2.7.4

Quoted 6 lines
Quoted 6 lines

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20160926171404.GA11773%40sayli-HP-15-Notebook-PC.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/CAKG5xWhp5iZ8Ae6j90m7KVjeAz4KEUcarqJ_R6pu8pL2WaZ5Kg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

#5 lore raw
In reply to sayli karnik
Quoted 24 lines · sayli karnik
Quoted 14 lines · Alison Schofield
Quoted 8 lines · sayli karnik

Fix following endianness warnings using sparse:
warning: cast to restricted __be16
warning: cast to restricted __be32
Use __be16 type for incoming read data when storage_bytes equals 2 and __be32 when storage_bytes equals 4. Since the variable allocated already has the correct storage size the patch is classified as cosmetic.

Hi Sayli -

v2 code looks like it handles both cases.

Quoted 9 lines

I think you've made a bug fix!

When the storage_bytes == 2, it would work on LE systems but give invalid data on BE systems. That unsigned int buf points to a location where only 2 bytes of data are placed during spi_read. Those 2 bytes go in the upper bytes. Then when the be16_to_cpu() happens that buf gets cast to 16bits. We get the upper bytes on LE systems (good bits) but the lower bytes on BE systems (bad bits)

Quoted 2 lines

Yes I agree..so it's a bug fix for storage_bytes=2 and cosmetic for storage_bytes=4.

You got it. See more inline below...

Quoted 10 lines
Quoted 10 lines

Lars - Please check my logic and the code too. If this is a bug let us know if Sayli should include a fix tag.

Pending Lars' review, you can update the changelog.

Take a further look at storage_bytes and you'll find when it is set to 2 and when it is set to 4. That would be good information to include in the changelog. You'll find that this is only a bug in one of the two chips this driver supports.

You'll state the impact of the issue in your changelog. It's only an issue on Big Endian systems with one of the supported chips (look that up)

In regard to the Fix tag:
I probably didn't need to ask if we should put it a fix tag. We should just put one in, explain the impact of of the bug, and then Jonathan can make the decision from there. To make a fix tag, we go back in the git log to find when this issue was introduced. This one's easy...short history. Here's the tag. You will put it after your Signed off line, but above the ---. You can see how it's done in some other logs on in linus-iio email.

Fixes 1f25ca11d84a ("iio: temperature: add support for Maxim thermocouple chips
Quoted 10 lines
Quoted 4 lines

Check your precommit checkpatch hooks. There are still CHECKs in the spi_read lines of code.

I did run checkpatch, but no CHECKs were caught.
This is strange, only errors and warnings were caught when I ran:
perl scripts/checkpatch.pl -f drivers/iio/temperature/* | less I have run checkpatch many times on staging drivers until now. I can only predict that the CHECK in my code must be that there shouldn't be a space after the type cast.

Your prediction is right. Found with checkpatch --strict option.
CHECK: No space is necessary after a cast
#132: FILE: maxim_thermocouple.c:132:
+ ret = spi_read(data->spi, (void *) &buf1, storage_bytes);

CHECK: No space is necessary after a cast
#136: FILE: maxim_thermocouple.c:136:
+ ret = spi_read(data->spi, (void *) &buf2, storage_bytes);

I think you're set. Create a new version and let's get it reviewed by linux-iio further.

You dropped the 'cc list on this thread. Adding them back in.

Thanks Sayli,
alisons

Quoted 70 lines
Quoted 10 lines

And, back to the less exciting. It would be good for that commit message to say what you did: replace unsigned int with __be16/32 I know you're short of space on that line. It's ok to drop the subdir (temperature) and just state the driver as in:
iio: maxim_thermocouple: commit message.

Please 'CC linux-iio on v3 and the module author, from bottom of source file.

Sure!

Quoted 58 lines

Thanks Sayli,
alisons

Quoted 55 lines

---
v2:
Updated the subject line and changelog
Moved the spi_read function call inside respective case statements so that data is read only once.
Moved the return statement after the switch case

 drivers/iio/temperature/maxim_thermocouple.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/iio/temperature/maxim_thermocouple.c +9 -8
index 39dd202..2683b0f 100644
@@ -123,22 +123,24 @@ static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
 {
      unsigned int storage_bytes = data->chip->read_size;
      unsigned int shift = chan->scan_type.shift + (chan->address * 8);
-     unsigned int buf;
+     __be16 buf1;
+     __be32 buf2;
      int ret;
 
-     ret = spi_read(data->spi, (void *) &buf, storage_bytes);
-     if (ret)
-             return ret;
-
      switch (storage_bytes) {
      case 2:
-             *val = be16_to_cpu(buf);
+             ret = spi_read(data->spi, (void *) &buf1, storage_bytes);
+             *val = be16_to_cpu(buf1);
              break;
      case 4:
-             *val = be32_to_cpu(buf);
+             ret = spi_read(data->spi, (void *) &buf2, storage_bytes);
+             *val = be32_to_cpu(buf2);
              break;
      }
 
+     if (ret)
+             return ret;
+
      /* check to be sure this is a valid reading */
      if (*val & data->chip->status_bit)
              return -EINVAL;
--

2.7.4

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20160926171404.GA11773%40sayli-HP-15-Notebook-PC.
For more options, visit https://groups.google.com/d/optout.