netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jason Wang <jasowang@redhat.com>
To: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	mst@redhat.com, jasowang@redhat.com
Cc: davem@davemloft.net, kuba@kernel.org, erwan.yvin@stericsson.com
Subject: [PATCH 2/3] caif_virtio: fix the race between virtio_device_ready() and ndo_open()
Date: Mon, 20 Jun 2022 13:11:14 +0800	[thread overview]
Message-ID: <20220620051115.3142-3-jasowang@redhat.com> (raw)
In-Reply-To: <20220620051115.3142-1-jasowang@redhat.com>

We used to depend on the virtio_device_ready() that is called after
probe() by virtio_dev_probe() after netdev registration. This
cause a race between ndo_open() and virtio_device_ready(): if
ndo_open() is called before virtio_device_ready(), the driver may
start to use the device (e.g TX) before DRIVER_OK which violates the
spec.

Fixing this by switching to use register_netdevice() and protect the
virtio_device_ready() with rtnl_lock() to make sure ndo_open() can
only be called after virtio_device_ready().

Fixes: 0d2e1a2926b18 ("caif_virtio: Introduce caif over virtio")
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 drivers/net/caif/caif_virtio.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/net/caif/caif_virtio.c b/drivers/net/caif/caif_virtio.c
index c677ded81133..66375bea2fcd 100644
--- a/drivers/net/caif/caif_virtio.c
+++ b/drivers/net/caif/caif_virtio.c
@@ -719,13 +719,21 @@ static int cfv_probe(struct virtio_device *vdev)
 	/* Carrier is off until netdevice is opened */
 	netif_carrier_off(netdev);
 
+	/* serialize netdev register + virtio_device_ready() with ndo_open() */
+	rtnl_lock();
+
 	/* register Netdev */
-	err = register_netdev(netdev);
+	err = register_netdevice(netdev);
 	if (err) {
+		rtnl_unlock();
 		dev_err(&vdev->dev, "Unable to register netdev (%d)\n", err);
 		goto err;
 	}
 
+	virtio_device_ready(vdev);
+
+	rtnl_unlock();
+
 	debugfs_init(cfv);
 
 	return 0;
-- 
2.25.1


  parent reply	other threads:[~2022-06-20  5:11 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-20  5:11 [PATCH 0/3] Fixing races in probe/remove Jason Wang
2022-06-20  5:11 ` [PATCH 1/3] caif_virtio: remove virtqueue_disable_cb() in probe Jason Wang
2022-06-20  9:02   ` Michael S. Tsirkin
2022-06-21  3:10     ` Jason Wang
2022-06-20  5:11 ` Jason Wang [this message]
2022-06-20  9:04   ` [PATCH 2/3] caif_virtio: fix the race between virtio_device_ready() and ndo_open() Michael S. Tsirkin
2022-06-20  5:11 ` [PATCH 3/3] caif_virtio: fix the race between reset and netdev unregister Jason Wang
2022-06-20  9:09   ` Michael S. Tsirkin
2022-06-20  9:18     ` Jason Wang
2022-06-20 10:18       ` Michael S. Tsirkin
2022-06-21  3:09         ` Jason Wang
2022-06-21  6:00           ` Michael S. Tsirkin
2022-06-21  6:25             ` Jason Wang

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=20220620051115.3142-3-jasowang@redhat.com \
    --to=jasowang@redhat.com \
    --cc=davem@davemloft.net \
    --cc=erwan.yvin@stericsson.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).