diff mbox series

net-core: remove unnecessary ETHTOOL_GCHANNELS initialization

Message ID 20200122223326.187954-1-lrizzo@google.com
State Rejected
Delegated to: David Miller
Headers show
Series net-core: remove unnecessary ETHTOOL_GCHANNELS initialization | expand

Commit Message

Luigi Rizzo Jan. 22, 2020, 10:33 p.m. UTC
struct ethtool_channels does not need .cmd to be set when calling the
driver's ethtool methods. Just zero-initialize it.

Tested: run ethtool -l and ethtool -L
Signed-off-by: Luigi Rizzo <lrizzo@google.com>
---
 net/ethtool/ioctl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Andrew Lunn Jan. 22, 2020, 11:47 p.m. UTC | #1
On Wed, Jan 22, 2020 at 02:33:26PM -0800, Luigi Rizzo wrote:
> struct ethtool_channels does not need .cmd to be set when calling the
> driver's ethtool methods. Just zero-initialize it.
> 
> Tested: run ethtool -l and ethtool -L

Hi Luigi

This seems pretty risky. You are assuming ethtool is the only user of
this API. What is actually wrong with putting a sane cmd value, rather
than the undefined value 0.

     Andrew
Luigi Rizzo Jan. 23, 2020, 12:18 a.m. UTC | #2
On Wed, Jan 22, 2020 at 3:47 PM Andrew Lunn <andrew@lunn.ch> wrote:
>
> On Wed, Jan 22, 2020 at 02:33:26PM -0800, Luigi Rizzo wrote:
> > struct ethtool_channels does not need .cmd to be set when calling the
> > driver's ethtool methods. Just zero-initialize it.
> >
> > Tested: run ethtool -l and ethtool -L
>
> Hi Luigi
>
> This seems pretty risky. You are assuming ethtool is the only user of
> this API. What is actually wrong with putting a sane cmd value, rather
> than the undefined value 0.

Hi Andrew, if I understand correctly your suggestion is that even if
the values are
unused, it is better to stay compliant with the header file
include/uapi/linux/ethtool.h,
which does suggest a value for .cmd for the various structs, and only
replace the value
in ethtool_set_channels() with the correct one ETHTOOL_SCHANNELS ?

cheers
luigi

>
>      Andrew
Michal Kubecek Jan. 23, 2020, 8:28 a.m. UTC | #3
On Wed, Jan 22, 2020 at 04:18:56PM -0800, Luigi Rizzo wrote:
> On Wed, Jan 22, 2020 at 3:47 PM Andrew Lunn <andrew@lunn.ch> wrote:
> >
> > On Wed, Jan 22, 2020 at 02:33:26PM -0800, Luigi Rizzo wrote:
> > > struct ethtool_channels does not need .cmd to be set when calling the
> > > driver's ethtool methods. Just zero-initialize it.
> > >
> > > Tested: run ethtool -l and ethtool -L
> >
> > Hi Luigi
> >
> > This seems pretty risky. You are assuming ethtool is the only user of
> > this API. What is actually wrong with putting a sane cmd value, rather
> > than the undefined value 0.
> 
> Hi Andrew, if I understand correctly your suggestion is that even if
> the values are unused, it is better to stay compliant with the header
> file include/uapi/linux/ethtool.h, which does suggest a value for .cmd
> for the various structs

The point is that unless you check ethtool_ops::get_channels() of all in
tree drivers, you cannot be sure there isn't one which depends on .cmd
being set to expected value. And even then there could be some out of
tree drivers; we usually don't care too much about those but it's always
question of what you gain by the cleanup. AFAICS, in this case it might
be few CPU cycles and even that isn't really sure.

> and only replace the value in ethtool_set_channels() with the correct
> one ETHTOOL_SCHANNELS ?

That would be incorrect. In ethtool_set_channels(), the structure
initialized is curr which is used with ->get_channels() to get current
values (for comparison of new values against driver maximum) so that
ETHTOOL_GCHANNELS is the right command.

Michal
Michal Kubecek Jan. 23, 2020, 8:40 a.m. UTC | #4
On Wed, Jan 22, 2020 at 04:18:56PM -0800, Luigi Rizzo wrote:
> On Wed, Jan 22, 2020 at 3:47 PM Andrew Lunn <andrew@lunn.ch> wrote:
> >
> > On Wed, Jan 22, 2020 at 02:33:26PM -0800, Luigi Rizzo wrote:
> > > struct ethtool_channels does not need .cmd to be set when calling the
> > > driver's ethtool methods. Just zero-initialize it.
> > >
> > > Tested: run ethtool -l and ethtool -L
> >
> > Hi Luigi
> >
> > This seems pretty risky. You are assuming ethtool is the only user of
> > this API. What is actually wrong with putting a sane cmd value, rather
> > than the undefined value 0.
> 
> Hi Andrew, if I understand correctly your suggestion is that even if
> the values are unused, it is better to stay compliant with the header
> file include/uapi/linux/ethtool.h, which does suggest a value for .cmd
> for the various structs

Unless you check all in tree drivers, you cannot be sure there isn't one
which would in fact rely on .cmd being set to expected value. And even
then there could be some out of tree driver; we usually don't care too
much about them but it's always matter of what you gain by the cleanup.
AFAICS it might be just few CPU cycles in this case - if we are lucky.

> and only replace the value in ethtool_set_channels() with the correct
> one ETHTOOL_SCHANNELS ?

That would be incorrect. The initialization in ethtool_set_channels() is
for curr variable which is passed to ->get_channels() (to get current
values for checking new values against maximum). Therefore the correct
command really is ETHTOOL_GCHANNELS.

Michal
Luigi Rizzo Jan. 23, 2020, 5:47 p.m. UTC | #5
On Thu, Jan 23, 2020 at 12:40 AM Michal Kubecek <mkubecek@suse.cz> wrote:
>
> On Wed, Jan 22, 2020 at 04:18:56PM -0800, Luigi Rizzo wrote:
> > On Wed, Jan 22, 2020 at 3:47 PM Andrew Lunn <andrew@lunn.ch> wrote:
> > >
> > > On Wed, Jan 22, 2020 at 02:33:26PM -0800, Luigi Rizzo wrote:
> > > > struct ethtool_channels does not need .cmd to be set when calling the
> > > > driver's ethtool methods. Just zero-initialize it.
> > > >
> > > > Tested: run ethtool -l and ethtool -L
> > >
> > > Hi Luigi
> > >
> > > This seems pretty risky. You are assuming ethtool is the only user of
> > > this API. What is actually wrong with putting a sane cmd value, rather
> > > than the undefined value 0.
> >
> > Hi Andrew, if I understand correctly your suggestion is that even if
> > the values are unused, it is better to stay compliant with the header
> > file include/uapi/linux/ethtool.h, which does suggest a value for .cmd
> > for the various structs
>
> Unless you check all in tree drivers, you cannot be sure there isn't one
> which would in fact rely on .cmd being set to expected value. And even
> then there could be some out of tree driver; we usually don't care too
> much about them but it's always matter of what you gain by the cleanup.
> AFAICS it might be just few CPU cycles in this case - if we are lucky.

The change was not about CPU savings, but trying to remove what I thought
were misleading or incorrect values.

Now I stand corrected, thank you for the feedback:
the header mentions that cmd should have a value
so let it be in, and as you say later, the first operation in
ethtool_set_channels()
is a get_channels so ETHTOOL_GCHANNELS is the correct value.

For the same reason though (comply with the header) we might perhaps
want to replace with cmd with ETHTOOL_SCHANNELS before actually
calling dev->ethtool_ops->set_channels()

(I realize this is not particularly important)

cheers
luigi
>
> > and only replace the value in ethtool_set_channels() with the correct
> > one ETHTOOL_SCHANNELS ?
>
> That would be incorrect. The initialization in ethtool_set_channels() is
> for curr variable which is passed to ->get_channels() (to get current
> values for checking new values against maximum). Therefore the correct
> command really is ETHTOOL_GCHANNELS.
>
> Michal
Michal Kubecek Jan. 23, 2020, 8:01 p.m. UTC | #6
On Thu, Jan 23, 2020 at 09:47:56AM -0800, Luigi Rizzo wrote:
> 
> For the same reason though (comply with the header) we might perhaps
> want to replace with cmd with ETHTOOL_SCHANNELS before actually
> calling dev->ethtool_ops->set_channels()
> 
> (I realize this is not particularly important)

That structure is filled by copy_from_user() and the structure passed by
userspace already has .cmd = ETHTOOL_SCHANNELS - otherwise we wouldn't
end up in ethtool_set_channels().

Michal
diff mbox series

Patch

diff --git a/net/ethtool/ioctl.c b/net/ethtool/ioctl.c
index 182bffbffa78..92442507a57e 100644
--- a/net/ethtool/ioctl.c
+++ b/net/ethtool/ioctl.c
@@ -1555,7 +1555,7 @@  static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
 						   void __user *useraddr)
 {
-	struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
+	struct ethtool_channels channels = {};
 
 	if (!dev->ethtool_ops->get_channels)
 		return -EOPNOTSUPP;
@@ -1570,7 +1570,7 @@  static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
 						   void __user *useraddr)
 {
-	struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
+	struct ethtool_channels channels, curr = {};
 	u16 from_channel, to_channel;
 	u32 max_rx_in_use = 0;
 	unsigned int i;