Skip to contentSkip to conversation

mscan: zero accidentally copied register content

PATCH In review Oliver Hartkopp 5 participants 14 messages Oct 5, 2011 – Oct 10, 2011

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

PATCH 1/1 mscan: zero accidentally copied register content

Due to the 16 bit access to mscan registers there's too much data copied to the zero initialized CAN frame when having an odd number of bytes to copy. This patch clears the data byte read from the invalid register entry.

---

Hello Wolf[gang|ram],

from an error report from Andre Naujoks i tracked down the problem of uninitialized data in (normally) initialized CAN frames to the mscan driver.

Regards,
Oliver

drivers/net/can/mscan/mscan.c +3 -0
index 92feac6..1b60fbe 100644
@@ -327,20 +327,23 @@ static void mscan_get_rx_frame(struct net_device *dev, struct can_frame *frame)
 	frame->can_dlc = get_can_dlc(in_8(&regs->rx.dlr) & 0xf);
 
 	if (!(frame->can_id & CAN_RTR_FLAG)) {
 		void __iomem *data = &regs->rx.dsr1_0;
 		u16 *payload = (u16 *)frame->data;
 
 		for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
 			*payload++ = in_be16(data);
 			data += 2 + _MSCAN_RESERVED_DSR_SIZE;
 		}
+		/* zero accidentally copied register content at odd DLCs */
+		if (frame->can_dlc & 1)
+			frame->data[frame->can_dlc] = 0;
 	}
 
 	out_8(&regs->canrflg, MSCAN_RXF);
 }
 
 static void mscan_get_err_frame(struct net_device *dev, struct can_frame *frame,
 				u8 canrflg)
 {
 	struct mscan_priv *priv = netdev_priv(dev);
 	struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
#1 lore raw
Quoted 29 lines · Oliver Hartkopp

Due to the 16 bit access to mscan registers there's too much data copied to the zero initialized CAN frame when having an odd number of bytes to copy. This patch clears the data byte read from the invalid register entry.

---

Hello Wolf[gang|ram],

from an error report from Andre Naujoks i tracked down the problem of uninitialized data in (normally) initialized CAN frames to the mscan driver.

Regards,
Oliver

drivers/net/can/mscan/mscan.c +0 -0
index 92feac6..1b60fbe 100644
@@ -327,20 +327,23 @@ static void mscan_get_rx_frame(struct net_device *dev, struct can_frame *frame)
 	frame->can_dlc = get_can_dlc(in_8(&regs->rx.dlr) & 0xf);
 
 	if (!(frame->can_id & CAN_RTR_FLAG)) {
 		void __iomem *data = &regs->rx.dsr1_0;
 		u16 *payload = (u16 *)frame->data;
 

drivers/net/can/mscan/mscan.c static void mscan_get_rx_frame(struct net_device *dev, struct can_frame *frame) · line 342

Quoted 10 lines
drivers/net/can/mscan/mscan.c +3 -0
 		for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
 			*payload++ = in_be16(data);
 			data += 2 + _MSCAN_RESERVED_DSR_SIZE;
 		}
+		/* zero accidentally copied register content at odd DLCs */
+		if (frame->can_dlc & 1)
+			frame->data[frame->can_dlc] = 0;
 	}
 
 	out_8(&regs->canrflg, MSCAN_RXF);

Nice catch, but wouldn't it be more elegant to never have an invalid byte in the first place?

if (can_dlc & 1)

	*payload = in_be16() & mask;

Regards,

Wolfram

Signature · 2 lines
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#2 lore raw
Quoted 39 lines · Wolfram Sang
Quoted 38 lines · Oliver Hartkopp

Due to the 16 bit access to mscan registers there's too much data copied to the zero initialized CAN frame when having an odd number of bytes to copy. This patch clears the data byte read from the invalid register entry.

---

Hello Wolf[gang|ram],

from an error report from Andre Naujoks i tracked down the problem of uninitialized data in (normally) initialized CAN frames to the mscan driver.

Regards,
Oliver

drivers/net/can/mscan/mscan.c +3 -0
index 92feac6..1b60fbe 100644
@@ -327,20 +327,23 @@ static void mscan_get_rx_frame(struct net_device *dev, struct can_frame *frame)
 	frame->can_dlc = get_can_dlc(in_8(&regs->rx.dlr) & 0xf);
 
 	if (!(frame->can_id & CAN_RTR_FLAG)) {
 		void __iomem *data = &regs->rx.dsr1_0;
 		u16 *payload = (u16 *)frame->data;
 
 		for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
 			*payload++ = in_be16(data);
 			data += 2 + _MSCAN_RESERVED_DSR_SIZE;
 		}
+		/* zero accidentally copied register content at odd DLCs */
+		if (frame->can_dlc & 1)
+			frame->data[frame->can_dlc] = 0;
 	}
 
Quoted 8 lines

drivers/net/can/mscan/mscan.c static void mscan_get_rx_frame(struct net_device *dev, struct can_frame *frame) · line 342

Quoted 1 line
drivers/net/can/mscan/mscan.c +0 -0
 	out_8(&regs->canrflg, MSCAN_RXF);

Nice catch, but wouldn't it be more elegant to never have an invalid byte in the first place?

if (can_dlc & 1)

	*payload = in_be16() & mask;

Hm, then i would rather think about changing the for() statement and to read byte-by-byte instead of the current in_be16() usage with the 16bit access drawbacks ...

Regards,
Oliver

#3 lore raw
Quoted 12 lines · Oliver Hartkopp
Quoted 10 lines · Wolfram Sang

drivers/net/can/mscan/mscan.c static void mscan_get_rx_frame(struct net_device *dev, struct can_frame *frame) · line 342

Quoted 6 lines
drivers/net/can/mscan/mscan.c +3 -0
+		/* zero accidentally copied register content at odd DLCs */
+		if (frame->can_dlc & 1)
+			frame->data[frame->can_dlc] = 0;
 	}
 
 	out_8(&regs->canrflg, MSCAN_RXF);

Nice catch, but wouldn't it be more elegant to never have an invalid byte in the first place?

Quoted 9 lines
Quoted 3 lines

if (can_dlc & 1)

	*payload = in_be16() & mask;

Hm, then i would rather think about changing the for() statement and to read byte-by-byte instead of the current in_be16() usage with the 16bit access drawbacks ...

I think if one would like to rework the 16bit register access (which is used in the rx path /and/ in the tx path also) this should go via net-next after some discussion and testing.

IMHO this fix is small and clear and especially not risky. I wonder if reworking the 16 bit register access is worth the effort?

Regards,
Oliver

#4 lore raw

Hi Oliver,

Quoted 25 lines · Oliver Hartkopp
Quoted 12 lines · Oliver Hartkopp
Quoted 10 lines · Wolfram Sang

drivers/net/can/mscan/mscan.c static void mscan_get_rx_frame(struct net_device *dev, struct can_frame *frame) · line 342

Quoted 6 lines
drivers/net/can/mscan/mscan.c +3 -0
+		/* zero accidentally copied register content at odd DLCs */
+		if (frame->can_dlc & 1)
+			frame->data[frame->can_dlc] = 0;
 	}
 
 	out_8(&regs->canrflg, MSCAN_RXF);

Nice catch, but wouldn't it be more elegant to never have an invalid byte in the first place?

Quoted 9 lines
Quoted 3 lines

if (can_dlc & 1)

	*payload = in_be16() & mask;

Hm, then i would rather think about changing the for() statement and to read byte-by-byte instead of the current in_be16() usage with the 16bit access drawbacks ...

Quoted 3 lines

I think if one would like to rework the 16bit register access (which is used in the rx path /and/ in the tx path also) this should go via net-next after some discussion and testing.

Why do you want to change 16-bit accesses in general? They are faster than two 8 bit accesses.

Quoted 2 lines

IMHO this fix is small and clear and especially not risky. I wonder if reworking the 16 bit register access is worth the effort?

I would prefer:

if (!(frame->can_id & CAN_RTR_FLAG)) {
void __iomem *data = &regs->rx.dsr1_0; u16 *payload = (u16 *)frame->data;

for (i = 0; i < frame->can_dlc / 2; i++) {
*payload++ = in_be16(data); data += 2 + _MSCAN_RESERVED_DSR_SIZE;
}

		/* copy remaining byte */
		if (frame->can_dlc & 1)
			frame->data[frame->can_dlc - 1] = in_8(data);

}

Wolfgang.

#5 lore raw
Quoted 3 lines

Why do you want to change 16-bit accesses in general? They are faster than two 8 bit accesses.

Yup, was thinking the same.

Quoted 14 lines
Quoted 3 lines

IMHO this fix is small and clear and especially not risky. I wonder if reworking the 16 bit register access is worth the effort?

I would prefer:

drivers/net/can/mscan/mscan.c +0 -0
 	if (!(frame->can_id & CAN_RTR_FLAG)) {
 		void __iomem *data = &regs->rx.dsr1_0;
 		u16 *payload = (u16 *)frame->data;
 
 		for (i = 0; i < frame->can_dlc / 2; i++) {
 			*payload++ = in_be16(data);
 			data += 2 + _MSCAN_RESERVED_DSR_SIZE;
 		}
Quoted 1 line
		/* copy remaining byte */

if any

Quoted 2 lines
		if (frame->can_dlc & 1)
			frame->data[frame->can_dlc - 1] = in_8(data);

Ack.

Regards,

Wolfram

Signature · 2 lines
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#6 lore raw
Quoted 5 lines · Wolfram Sang
Quoted 3 lines

Why do you want to change 16-bit accesses in general? They are faster than two 8 bit accesses.

Yup, was thinking the same.

Ah, i did not get this from your code example

	if (can_dlc & 1)
		*payload = in_be16() & mask;

which probably does the same as Wolfgangs more obvious suggestion

	if (frame->can_dlc & 1)
		frame->data[frame->can_dlc - 1] = in_8(data);

:-)

As my patch could be done without real testing, as i did not change the register access and only fixed the result ...

	if (frame->can_dlc & 1)
		frame->data[frame->can_dlc] = 0;

... it would be nice if e.g. Wolfgang could send his patch after some testing, as i currently don't have access to my MPC5200 hardware here.

Tnx & best regards,
Oliver

#7 lore raw
Quoted 5 lines

Ah, i did not get this from your code example

	if (can_dlc & 1)
		*payload = in_be16() & mask;

Sorry, I thought it was obvious that I was using pseudo_code here :)

Quoted 2 lines

... it would be nice if e.g. Wolfgang could send his patch after some testing, as i currently don't have access to my MPC5200 hardware here.

What about Andre?

Regards,

Wolfram

Signature · 2 lines
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#8 lore raw

Hi.

2011/10/6 Wolfram Sang <[email protected]>

Quoted 6 lines
Quoted 5 lines

Ah, i did not get this from your code example

      if (can_dlc & 1)
              *payload = in_be16() & mask;

Quoted 6 lines

Sorry, I thought it was obvious that I was using pseudo_code here :)

Quoted 2 lines

... it would be nice if e.g. Wolfgang could send his patch after some testing, as i currently don't have access to my MPC5200 hardware here.

What about Andre?

I am currently trying to test the first version Oliver sent. I am currently having some problems
with my hardware and I don't know what it is yet.

I will give Wolfgangs suggestion a try after my problems are sorted out here.

Regards
Andre

Quoted 17 lines

Regards,

  Wolfram

--
Pengutronix e.K.                           | Wolfram Sang                | Industrial Linux Solutions                 | http://www.pengutronix.de/  |

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iEYEARECAAYFAk6NtoAACgkQD27XaX1/VRudMQCeMX3yMc+Au65BwKDocJNXtG/d RxgAoJnY/p0csrHa0o/7SpnQdtEhEOQn
=Sxlb
-----END PGP SIGNATURE-----

#9 lore raw
In reply to Wolfgang Grandegger
Quoted 25 lines · Wolfgang Grandegger
Quoted 4 lines · Oliver Hartkopp

I think if one would like to rework the 16bit register access (which is used in the rx path /and/ in the tx path also) this should go via net-next after some discussion and testing.

Why do you want to change 16-bit accesses in general? They are faster than two 8 bit accesses.

Quoted 2 lines

IMHO this fix is small and clear and especially not risky. I wonder if reworking the 16 bit register access is worth the effort?

I would prefer:

drivers/net/can/mscan/mscan.c +0 -0
 	if (!(frame->can_id & CAN_RTR_FLAG)) {
 		void __iomem *data = &regs->rx.dsr1_0;
 		u16 *payload = (u16 *)frame->data;
 
 		for (i = 0; i < frame->can_dlc / 2; i++) {
 			*payload++ = in_be16(data);
 			data += 2 + _MSCAN_RESERVED_DSR_SIZE;
 		}
		/* copy remaining byte */
		if (frame->can_dlc & 1)
			frame->data[frame->can_dlc - 1] = in_8(data);
Quoted 1 line

}

Besides the fact that Andre is going to test this idea from Wolfgang now, are you really sure that it must be

	in_8(data)

and not

	in_8(data+1)

???

And that data definitely points to the right place?

I would prefer to be really cautious with these big endian 16 bit registers!

Therefore my fix with

+ /* zero accidentally copied register content at odd DLCs */
+ if (frame->can_dlc & 1)
+ frame->data[frame->can_dlc] = 0;

only repairing the result looks much more defensive to me.

Regards,
Oliver

#10 lore raw

2011/10/6 Oliver Hartkopp <[email protected]>:

Quoted 45 lines · Wolfgang Grandegger
Quoted 19 lines · Oliver Hartkopp
Quoted 4 lines

I think if one would like to rework the 16bit register access (which is used in the rx path /and/ in the tx path also) this should go via net-next after some discussion and testing.

Why do you want to change 16-bit accesses in general? They are faster than two 8 bit accesses.

Quoted 2 lines

IMHO this fix is small and clear and especially not risky. I wonder if reworking the 16 bit register access is worth the effort?

I would prefer:

      if (!(frame->can_id & CAN_RTR_FLAG)) { void __iomem *data = &regs->rx.dsr1_0; u16 *payload = (u16 *)frame->data;

Quoted 8 lines

              for (i = 0; i < frame->can_dlc / 2; i++) { *payload++ = in_be16(data);
                      data += 2 + _MSCAN_RESERVED_DSR_SIZE; }
              /* copy remaining byte */
              if (frame->can_dlc & 1) frame->data[frame->can_dlc - 1] = in_8(data); }

Besides the fact that Andre is going to test this idea from Wolfgang now, are you really sure that it must be

       in_8(data)

and not

       in_8(data+1)

???

And that data definitely points to the right place?

I would prefer to be really cautious with these big endian 16 bit registers!

Quoted 7 lines

Therefore my fix with

+               /* zero accidentally copied register content at odd DLCs */
+               if (frame->can_dlc & 1)
+                       frame->data[frame->can_dlc] = 0;

only repairing the result looks much more defensive to me.

First things first: Both ways seem to work correctly. At least on the MPC5200 I have here.

But I am with Oliver on this one. The solution looks much simpler and endianess errors are not possible. If the few CPU cycles are worth it on the other hand, then Wolfgangs version is probably preferable. I don't have access to this kind of hardware on a little endian machine to test it, though.

Greetings
Andre

Quoted 4 lines

Regards,
Oliver

#11 lore raw
Quoted 35 lines · Andre Naujoks

2011/10/6 Oliver Hartkopp <[email protected]>:

Quoted 34 lines · Wolfgang Grandegger
Quoted 15 lines · Oliver Hartkopp
Quoted 4 lines

I think if one would like to rework the 16bit register access (which is used in the rx path /and/ in the tx path also) this should go via net-next after some discussion and testing.

Why do you want to change 16-bit accesses in general? They are faster than two 8 bit accesses.

Quoted 2 lines

IMHO this fix is small and clear and especially not risky. I wonder if reworking the 16 bit register access is worth the effort?

I would prefer:

Quoted 12 lines
      if (!(frame->can_id & CAN_RTR_FLAG)) {
              void __iomem *data = &regs->rx.dsr1_0;
              u16 *payload = (u16 *)frame->data;

              for (i = 0; i < frame->can_dlc / 2; i++) {
                      *payload++ = in_be16(data);
                      data += 2 + _MSCAN_RESERVED_DSR_SIZE;
              }
              /* copy remaining byte */
              if (frame->can_dlc & 1)
                      frame->data[frame->can_dlc - 1] = in_8(data);
      }

Besides the fact that Andre is going to test this idea from Wolfgang now, are you really sure that it must be

       in_8(data)

That should be the right byte.

Quoted 19 lines
Quoted 17 lines

and not

       in_8(data+1)

???

And that data definitely points to the right place?

I would prefer to be really cautious with these big endian 16 bit registers!

Therefore my fix with

(quoted diff) +3 -0
+               /* zero accidentally copied register content at odd DLCs */
+               if (frame->can_dlc & 1)
+                       frame->data[frame->can_dlc] = 0;
Quoted 1 line

only repairing the result looks much more defensive to me.

Quoted 8 lines

First things first: Both ways seem to work correctly. At least on the MPC5200 I have here.

But I am with Oliver on this one. The solution looks much simpler and endianess errors are not possible. If the few CPU cycles are worth it on the other hand, then Wolfgangs version is probably preferable. I don't have access to this kind of hardware on a little endian machine to test it, though.

Well, copying just the relevant bytes seem much more straight-forward than removing accidentally copied bytes later-on. You do not need to care about little endian. The MSCAN is only available on PowerPC SOCs, which are big endian.

I'm going to test and post a patch tomorrow.

Wolfgang.

#13 lore raw
In reply to Oliver Hartkopp
Quoted 6 lines · Oliver Hartkopp

Due to the 16 bit access to mscan registers there's too much data copied to the zero initialized CAN frame when having an odd number of bytes to copy. This patch clears the data byte read from the invalid register entry.

This problem have some other drivers, too, e.g. the at91 and the flexcan driver both copy unconditionally all 8 bytes from the hardware. However, I don't know if the hardware sets the remaining bytes to zero.

cheers, Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
#12 lore raw
In reply to Wolfgang Grandegger
Quoted 6 lines · Wolfgang Grandegger

Well, copying just the relevant bytes seem much more straight-forward than removing accidentally copied bytes later-on. You do not need to care about little endian. The MSCAN is only available on PowerPC SOCs, which are big endian.

I'm going to test and post a patch tomorrow.

Thanks.

My patch is then superseded by this one

"mscan: too much data copied to CAN frame due to 16 bit accesses"

http://patchwork.ozlabs.org/patch/118364/

Tnx,
Oliver