Subject: Re: [RFC net-next 1/4] net: Hold netdev instance lock during ndo_open/ndo_stop

From: Stanislav Fomichev <[email-masked]>
Date: Wed, 5 Feb 2025 13:31:40 -0800

On 02/05, Joe Damato wrote:
> On Tue, Feb 04, 2025 at 03:00:54PM -0800, Stanislav Fomichev wrote:
> > For the drivers that use shaper API, switch to the mode where
> > core stack holds the netdev lock. This affects two drivers:
> > 
> > * iavf - already grabs netdev lock in ndo_open/ndo_stop, so mostly
> >          remove these
> > * netdevsim - switch to _locked APIs to avoid deadlock
> > 
> > Cc: Saeed Mahameed <[email-masked]>
> > Signed-off-by: Stanislav Fomichev <[email-masked]>
> > ---
> >  Documentation/networking/netdevices.rst     |  6 ++++--
> >  drivers/net/ethernet/intel/iavf/iavf_main.c | 14 ++++++-------
> >  drivers/net/netdevsim/netdev.c              | 14 ++++++++-----
> >  include/linux/netdevice.h                   | 23 +++++++++++++++++++++
> >  net/core/dev.c                              | 12 +++++++++++
> >  net/core/dev.h                              |  6 ++++--
> >  6 files changed, 58 insertions(+), 17 deletions(-)
> 
> [...]
> 
> > @@ -4474,12 +4471,12 @@ static int iavf_close(struct net_device *netdev)
> >  	u64 aq_to_restore;
> >  	int status;
> >  
> > -	netdev_lock(netdev);
> > +	netdev_assert_locked(netdev);
> > +
> >  	mutex_lock(&adapter->crit_lock);
> >  
> >  	if (adapter->state <= __IAVF_DOWN_PENDING) {
> >  		mutex_unlock(&adapter->crit_lock);
> > -		netdev_unlock(netdev);
> >  		return 0;
> >  	}
> >  
> > @@ -4532,6 +4529,7 @@ static int iavf_close(struct net_device *netdev)
> >  	if (!status)
> >  		netdev_warn(netdev, "Device resources not yet released\n");
> >  
> > +	netdev_lock(netdev);
> 
> I'm probably just misreading the rest of the patch, but I was just
> wondering: is this netdev_lock call here intentional? I am asking
> because I thought the lock was taken in ndo_stop before this is
> called?

Yes, this part is a bit confusing. Existing iavf_close looks like
this:

iavf_close() {
  netdev_lock()
  .. 
  netdev_unlock()
  wait_event_timeout(down_waitqueue)
}

I change it to the following:

netdev_lock()
iavf_close() {
  .. 
  netdev_unlock()
  wait_event_timeout(down_waitqueue)
  netdev_lock()
}
netdev_unlock()

And the diff is confusing because I reuse existing netdev_lock call,
so it looks like I only add netdev_unlock...

I don't think I can hold instance lock during wait_event_timeout because
the wake_up(down_waitqueue) callers grab netdev instance lock as well.