Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp1941730ybi; Thu, 18 Jul 2019 00:45:02 -0700 (PDT) X-Google-Smtp-Source: APXvYqyaBL5AjWDMDeVYx5Th/8rZwOuAhBFTwI101VH+4W9nK9UXLylJBNhauH7WpnH0dacN87YK X-Received: by 2002:a17:90a:de02:: with SMTP id m2mr49360037pjv.18.1563435902200; Thu, 18 Jul 2019 00:45:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563435902; cv=none; d=google.com; s=arc-20160816; b=btTZHdbF05zcJ7Tkb+bHRx2Oy/8YFFWyGfEIv2b6i4hnigl4Q4Mv4/mGQqIiuoPYVP lvzIxcvxkJikan3LTJFVx5o4T2Ht0SrgN7S737nRaW6QJ29ATVnXwvEZgJ/AhQcIKpTL 5fvETxTJFuSNFD4emwsyS8ES51JFftioNRCNhBpqX0g2Qs2cf0kuBMrxmOw0T18TP53y PqvacQfyc93qZwt0tZ4sJwZ3lshD0iEoiqbuasBjRxZcGKXyUnGoNlURJCFOaoahgyVR YV0LsJJ5/e6UNY34tfnMmicIGo9zvZhhouRaZmCtpvpS4ggR3b9uSve6/lhFoFDHE+Lx tf7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=+5h5a3O3K59BqV9oPAQLtP560NcGiWgROtgONhhQBcA=; b=ETiIsn1IBIyHwhd9tmBI+QV0wWi9l8BAMwJquczbcvgd5GFM4PLtjLjyugHhCLkyEy Iq0Dj6kx7lrCPhEyod8+Hz69L/q/cDsw6qJQ2qk9wqNAczvXQnYb1eApcRZb2mEcNhZh qZwetrRc+56BtAH/V78tj7vQShGglCY58RXM0+/mV6U9HRlz/9dwybUol15QUvr+gAn5 CQiNGE87ohKWOAE5tIoDs469Bkb+UCB8Ow390QZL5Gd6IcB4YMuaA+XnZmgZu2vcbk7w mX2yRTjIVupOvKeVRQj5U7fiGcijS0j3gfg0+SNRA1mREzJHk8tUU9h4TT9mxihpE098 UkDg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h21si1251060pgv.266.2019.07.18.00.44.45; Thu, 18 Jul 2019 00:45:02 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389282AbfGRHnK (ORCPT + 99 others); Thu, 18 Jul 2019 03:43:10 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:38024 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726472AbfGRHnJ (ORCPT ); Thu, 18 Jul 2019 03:43:09 -0400 Received: by mail-wm1-f65.google.com with SMTP id s15so3182085wmj.3 for ; Thu, 18 Jul 2019 00:43:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=+5h5a3O3K59BqV9oPAQLtP560NcGiWgROtgONhhQBcA=; b=V/Lbl3MS0QTR2eWLfgooAM/DNDuqF2N2EG4kJp3SiIh6ZtNh3MOHgNDLTR6pbk0Mxg Zcj4sdwfusFdTJBfewOFNvU3xS0oxL+77I7lX3JTCHXc9amahucRCnhp7NDGejZL3gcq FfB9xCcanNKhF2yewC0uf5iILakpBEMzXKV1b2Xd6AmGNEuyiN+UImGFDGFPHM/DdZnh 6RFUQ1D1qutjJdiDuB4wkjmt3zzXHWsC5fRrjyneO7dY1ta5eEBNWlUlGUhJ6cxtcnJW zX4FdQHpd4F4BaVyz5K7Q8w0gixjgNdaCv7+0WUyDNuP9IQ9o/l0+eKA6dX3iBl/bnIi hEGA== X-Gm-Message-State: APjAAAV+Ko+gbZwpg33F3z/ioI71YWyAq8qfjmmhu2qzwtScshEwpOgw S2JAS1c6nb6DYJ7z6c0Uh8RY+GU1kFA= X-Received: by 2002:a1c:3:: with SMTP id 3mr41105085wma.6.1563435787761; Thu, 18 Jul 2019 00:43:07 -0700 (PDT) Received: from steredhat ([5.170.38.133]) by smtp.gmail.com with ESMTPSA id g25sm18763167wmk.39.2019.07.18.00.43.06 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Thu, 18 Jul 2019 00:43:07 -0700 (PDT) Date: Thu, 18 Jul 2019 09:43:04 +0200 From: Stefano Garzarella To: "Michael S. Tsirkin" Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Stefan Hajnoczi , "David S. Miller" , virtualization@lists.linux-foundation.org, Jason Wang , kvm@vger.kernel.org Subject: Re: [PATCH v4 3/5] vsock/virtio: fix locking in virtio_transport_inc_tx_pkt() Message-ID: References: <20190717113030.163499-1-sgarzare@redhat.com> <20190717113030.163499-4-sgarzare@redhat.com> <20190717105056-mutt-send-email-mst@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190717105056-mutt-send-email-mst@kernel.org> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jul 17, 2019 at 4:51 PM Michael S. Tsirkin wrote: > > On Wed, Jul 17, 2019 at 01:30:28PM +0200, Stefano Garzarella wrote: > > fwd_cnt and last_fwd_cnt are protected by rx_lock, so we should use > > the same spinlock also if we are in the TX path. > > > > Move also buf_alloc under the same lock. > > > > Signed-off-by: Stefano Garzarella > > Wait a second is this a bugfix? > If it's used under the wrong lock won't values get corrupted? > Won't traffic then stall or more data get to sent than > credits? Before this series, we only read vvs->fwd_cnt and vvs->buf_alloc in this function, but using a different lock than the one used to write them. I'm not sure if a corruption can happen, but if we want to avoid the lock, we should use an atomic operation or memory barriers. Since now we also need to update vvs->last_fwd_cnt, in order to limit the credit message, I decided to take the same lock used to protect vvs->fwd_cnt and vvs->last_fwd_cnt. Thanks, Stefano > > > --- > > include/linux/virtio_vsock.h | 2 +- > > net/vmw_vsock/virtio_transport_common.c | 4 ++-- > > 2 files changed, 3 insertions(+), 3 deletions(-) > > > > diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h > > index 49fc9d20bc43..4c7781f4b29b 100644 > > --- a/include/linux/virtio_vsock.h > > +++ b/include/linux/virtio_vsock.h > > @@ -35,7 +35,6 @@ struct virtio_vsock_sock { > > > > /* Protected by tx_lock */ > > u32 tx_cnt; > > - u32 buf_alloc; > > u32 peer_fwd_cnt; > > u32 peer_buf_alloc; > > > > @@ -43,6 +42,7 @@ struct virtio_vsock_sock { > > u32 fwd_cnt; > > u32 last_fwd_cnt; > > u32 rx_bytes; > > + u32 buf_alloc; > > struct list_head rx_queue; > > }; > > > > diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c > > index a85559d4d974..34a2b42313b7 100644 > > --- a/net/vmw_vsock/virtio_transport_common.c > > +++ b/net/vmw_vsock/virtio_transport_common.c > > @@ -210,11 +210,11 @@ static void virtio_transport_dec_rx_pkt(struct virtio_vsock_sock *vvs, > > > > void virtio_transport_inc_tx_pkt(struct virtio_vsock_sock *vvs, struct virtio_vsock_pkt *pkt) > > { > > - spin_lock_bh(&vvs->tx_lock); > > + spin_lock_bh(&vvs->rx_lock); > > vvs->last_fwd_cnt = vvs->fwd_cnt; > > pkt->hdr.fwd_cnt = cpu_to_le32(vvs->fwd_cnt); > > pkt->hdr.buf_alloc = cpu_to_le32(vvs->buf_alloc); > > - spin_unlock_bh(&vvs->tx_lock); > > + spin_unlock_bh(&vvs->rx_lock); > > } > > EXPORT_SYMBOL_GPL(virtio_transport_inc_tx_pkt); > > > > -- > > 2.20.1