netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: John Fastabend <john.fastabend@gmail.com>
To: Jesper Dangaard Brouer <brouer@redhat.com>,
	Jason Wang <jasowang@redhat.com>
Cc: "Michael S. Tsirkin" <mst@redhat.com>,
	netdev@vger.kernel.org,
	Alexei Starovoitov <alexei.starovoitov@gmail.com>,
	Saeed Mahameed <saeedm@mellanox.com>,
	Daniel Borkmann <borkmann@iogearbox.net>,
	"David S. Miller" <davem@davemloft.net>,
	Tariq Toukan <tariqt@mellanox.com>
Subject: Re: [net PATCH 4/4] virtio_net: fix ndo_xdp_xmit crash towards dev not ready for XDP
Date: Tue, 20 Feb 2018 09:01:56 -0800	[thread overview]
Message-ID: <d427b64f-062b-809b-cb74-a1005fe4029b@gmail.com> (raw)
In-Reply-To: <151913354012.28247.1462800242440559823.stgit@firesoul>

On 02/20/2018 05:32 AM, Jesper Dangaard Brouer wrote:
> When a driver implements the ndo_xdp_xmit() function, there is
> (currently) no generic way to determine whether it is safe to call.
> 
> It is e.g. unsafe to call the drivers ndo_xdp_xmit, if it have not
> allocated the needed XDP TX queues yet.  This is the case for
> virtio_net, which first allocates the XDP TX queues once an XDP/bpf
> prog is attached (in virtnet_xdp_set()).
> 
> Thus, a crash will occur for virtio_net when redirecting to another
> virtio_net device's ndo_xdp_xmit, which have not attached a XDP prog.
> The sample xdp_redirect_map tries to attach a dummy XDP prog to take
> this into account, but it can also easily fail if the virtio_net (or
> actually underlying vhost driver) have not allocated enough extra
> queues for the device.
> 
> Allocating more queue this is currently a manual config.
> Hint for libvirt XML add:
> 
>   <driver name='vhost' queues='16'>
>     <host mrg_rxbuf='off'/>
>     <guest tso4='off' tso6='off' ecn='off' ufo='off'/>
>   </driver>
> 
> The solution in this patch is to check that the device have loaded an
> XDP/bpf prog before proceeding.  This is similar to the check
> performed in driver ixgbe.
> 
> Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
> ---

Yep, nice catch. I caught this in ixgbe but didn't manage to
pull it into virtio.

Acked-by: John Fastabend <john.fastabend@gmail.com>

>  drivers/net/virtio_net.c |   12 +++++++++++-
>  1 file changed, 11 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 1e0e0fce3ab2..9bb9e562b893 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -452,8 +452,18 @@ static bool __virtnet_xdp_xmit(struct virtnet_info *vi,
>  static int virtnet_xdp_xmit(struct net_device *dev, struct xdp_buff *xdp)
>  {
>  	struct virtnet_info *vi = netdev_priv(dev);
> -	bool sent = __virtnet_xdp_xmit(vi, xdp);
> +	struct receive_queue *rq = vi->rq;
> +	struct bpf_prog *xdp_prog;
> +	bool sent;
> +
> +	/* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
> +	 * indicate XDP resources have been successfully allocated.
> +	 */
> +	xdp_prog = rcu_dereference(rq->xdp_prog);
> +	if (!xdp_prog)
> +		return -ENXIO;
>  
> +	sent = __virtnet_xdp_xmit(vi, xdp);
>  	if (!sent)
>  		return -ENOSPC;
>  	return 0;
> 

  reply	other threads:[~2018-02-20 17:02 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-20 13:31 [net PATCH 0/4] virtio_net: several bugs in XDP code for driver virtio_net Jesper Dangaard Brouer
2018-02-20 13:32 ` [net PATCH 1/4] virtio_net: disable XDP_REDIRECT in receive_mergeable() case Jesper Dangaard Brouer
2018-02-20 16:56   ` John Fastabend
2018-02-27  0:40   ` Michael S. Tsirkin
2018-02-27  2:25     ` Jason Wang
2018-02-27  2:28       ` Jason Wang
2018-02-20 13:32 ` [net PATCH 2/4] virtio_net: fix XDP code path in receive_small() Jesper Dangaard Brouer
2018-02-20 16:57   ` John Fastabend
2018-02-20 13:32 ` [net PATCH 3/4] virtio_net: fix memory leak in XDP_REDIRECT Jesper Dangaard Brouer
2018-02-20 16:59   ` John Fastabend
2018-02-20 13:32 ` [net PATCH 4/4] virtio_net: fix ndo_xdp_xmit crash towards dev not ready for XDP Jesper Dangaard Brouer
2018-02-20 17:01   ` John Fastabend [this message]
2018-02-21 20:09 ` [net PATCH 0/4] virtio_net: several bugs in XDP code for driver virtio_net David Miller

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=d427b64f-062b-809b-cb74-a1005fe4029b@gmail.com \
    --to=john.fastabend@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=borkmann@iogearbox.net \
    --cc=brouer@redhat.com \
    --cc=davem@davemloft.net \
    --cc=jasowang@redhat.com \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=saeedm@mellanox.com \
    --cc=tariqt@mellanox.com \
    /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).