All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nicolas Pitre <nico@fluxnic.net>
To: David Laight <David.Laight@ACULAB.COM>
Cc: 'Jakub Kicinski' <kuba@kernel.org>, Andrew Lunn <andrew@lunn.ch>,
	netdev <netdev@vger.kernel.org>, Lee Jones <lee.jones@linaro.org>
Subject: RE: [PATCH net-next v2 6/7] drivers: net: smc911x: Fix cast from pointer to integer of different size
Date: Fri, 6 Nov 2020 10:05:59 -0500 (EST)	[thread overview]
Message-ID: <nycvar.YSQ.7.78.906.2011060942360.2184@knanqh.ubzr> (raw)
In-Reply-To: <babda61688af4f42b4a9e0fb41808272@AcuMS.aculab.com>

On Fri, 6 Nov 2020, David Laight wrote:

> From: Jakub Kicinski
> > Sent: 05 November 2020 22:47
> > 
> > On Wed,  4 Nov 2020 16:48:57 +0100 Andrew Lunn wrote:
> > > -	buf = (char*)((u32)skb->data & ~0x3);
> > > -	len = (skb->len + 3 + ((u32)skb->data & 3)) & ~0x3;
> > > -	cmdA = (((u32)skb->data & 0x3) << 16) |
> > > +	offset = (unsigned long)skb->data & 3;
> > > +	buf = skb->data - offset;
> > > +	len = skb->len + offset;
> > > +	cmdA = (offset << 16) |
> > 
> > The len calculation is wrong, you trusted people on the mailing list
> > too much ;)
> 
> I misread what the comment-free convoluted code was doing....
> 
> Clearly it is rounding the base address down to a multiple of 4
> and passing the offset in cmdA.
> This is fine - but quite why the (I assume) hardware doesn't do
> this itself and just document that it does a 32bit read is
> another matter - the logic will be much the same and I doubt
> anything modern is that pushed for space.
> 
> However rounding the length up to a multiple of 4 is buggy.
> If this is an ethernet chipset it must (honest) be able to
> send frames that don't end on a 4 byte boundary.
> So rounding up the length is very dubious.

I probably wrote that code. Probably something like 20 years ago at this 
point. I no longer have access to the actual hardware either.

But my recollection is that this ethernet chip had the ability to do 1, 
2 or 4 byte wide data transfers.

To be able to efficiently use I/O helpers like readsl()/writesl() on 
ARM, the host memory pointer had to be aligned to a 32-bit boundary 
because misaligned accesses were not supported by the hardware and 
therefore were very costly to perform in software with a bytewise 
approach. Remember that back then, the CPU clock was very close to the 
actual ethernet throughput and PIO was the only option.

This was made even worse by the fact that, on some boards, the hw 
designers didn't consider connecting the byte select signals as a 
worthwhile thing to do. That means only 32-bit wide access to the chip 
were possible.

So to work around this, the skb buffer address was rounded down, the 
length was rounded up, and 
the on-chip pointer was adjusted to refer to the actual data 
payload accordingly with the original length. Therefore the proposed 
patch is indeed wrong.

Just to say that, although the code might look suspicious, there was a 
reason for that and it did work correctly for a long long time at this 
point. Obviously those were only 32- bit systems (I really doubt those 
ethernet chips were ever used on 64-bit systems).


Nicolas

  reply	other threads:[~2020-11-06 15:06 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-04 15:48 [PATCH net-next v2 0/7] smsc W=1 warning fixes Andrew Lunn
2020-11-04 15:48 ` [PATCH net-next v2 1/7] drivers: net: smc91x: Fix set but unused W=1 warning Andrew Lunn
2020-11-05 22:37   ` Jakub Kicinski
2020-11-06  8:48     ` David Laight
2020-11-06 17:42       ` Jakub Kicinski
2020-11-04 15:48 ` [PATCH net-next v2 2/7] drivers: net: smc91x: Fix missing kerneldoc reported by W=1 Andrew Lunn
2020-11-04 15:48 ` [PATCH net-next v2 3/7] drivers: net: smc911x: Work around set but unused status Andrew Lunn
2020-11-04 15:48 ` [PATCH net-next v2 4/7] drivers: net: smc911x: Fix set but unused status because of DBG macro Andrew Lunn
2020-11-04 15:48 ` [PATCH net-next v2 5/7] drivers: net: smc911x: Fix passing wrong number of parameters to DBG() macro Andrew Lunn
2020-11-04 15:48 ` [PATCH net-next v2 6/7] drivers: net: smc911x: Fix cast from pointer to integer of different size Andrew Lunn
2020-11-05 22:47   ` Jakub Kicinski
2020-11-06  8:44     ` David Laight
2020-11-06 15:05       ` Nicolas Pitre [this message]
2020-11-06 15:29         ` David Laight
2020-11-06 15:47           ` Nicolas Pitre
2020-11-04 15:48 ` [PATCH net-next v2 7/7] drivers: net: smsc: Add COMPILE_TEST support Andrew Lunn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=nycvar.YSQ.7.78.906.2011060942360.2184@knanqh.ubzr \
    --to=nico@fluxnic.net \
    --cc=David.Laight@ACULAB.COM \
    --cc=andrew@lunn.ch \
    --cc=kuba@kernel.org \
    --cc=lee.jones@linaro.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.