From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by dpdk.org (Postfix) with ESMTP id 63A571B6A0 for ; Wed, 31 Jan 2018 07:51:26 +0100 (CET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Jan 2018 22:51:25 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,438,1511856000"; d="scan'208";a="14352394" Received: from fmsmsx108.amr.corp.intel.com ([10.18.124.206]) by orsmga008.jf.intel.com with ESMTP; 30 Jan 2018 22:51:25 -0800 Received: from fmsmsx101.amr.corp.intel.com (10.18.124.199) by FMSMSX108.amr.corp.intel.com (10.18.124.206) with Microsoft SMTP Server (TLS) id 14.3.319.2; Tue, 30 Jan 2018 22:51:24 -0800 Received: from shsmsx104.ccr.corp.intel.com (10.239.4.70) by fmsmsx101.amr.corp.intel.com (10.18.124.199) with Microsoft SMTP Server (TLS) id 14.3.319.2; Tue, 30 Jan 2018 22:51:24 -0800 Received: from shsmsx101.ccr.corp.intel.com ([169.254.1.192]) by SHSMSX104.ccr.corp.intel.com ([169.254.5.127]) with mapi id 14.03.0319.002; Wed, 31 Jan 2018 14:51:22 +0800 From: "Chen, Junjie J" To: Victor Kaplansky , Maxime Coquelin CC: "dev@dpdk.org" Thread-Topic: [dpdk-dev] [PATCH v5] vhost_user: protect active rings from async ring changes Thread-Index: AQHTj5oOM/+0QKgYckGyh4P0eNh47KONn+cw Date: Wed, 31 Jan 2018 06:51:22 +0000 Message-ID: References: <20180117154925-mutt-send-email-victork@redhat.com> In-Reply-To: <20180117154925-mutt-send-email-victork@redhat.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: dlp-product: dlpe-windows dlp-version: 11.0.0.116 dlp-reaction: no-action x-ctpclassification: CTP_NT x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiZDUwMDhmNTktYWEyZC00MzlkLTlhN2EtZGExNTAwNDdhM2VjIiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX05UIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE3LjIuNS4xOCIsIlRydXN0ZWRMYWJlbEhhc2giOiJVUzRaeXV2M01pZmF0S2JMU1p0VmNlXC96VVg4ZmdvMnVCcis2SUlZb3lkVndMcE5vUW1RM2FpSmFDZVNrRUFtSyJ9 x-originating-ip: [10.239.127.40] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: Re: [dpdk-dev] [PATCH v5] vhost_user: protect active rings from async ring changes X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 31 Jan 2018 06:51:27 -0000 Hi=20 May I know why not use trylock also in enqueue path? Cheers JJ >=20 > When performing live migration or memory hot-plugging, the changes to the > device and vrings made by message handler done independently from vring > usage by PMD threads. >=20 > This causes for example segfaults during live-migration with MQ enable, b= ut > in general virtually any request sent by qemu changing the state of devic= e > can cause problems. >=20 > These patches fixes all above issues by adding a spinlock to every vring = and > requiring message handler to start operation only after ensuring that all= PMD > threads related to the device are out of critical section accessing the v= ring > data. >=20 > Each vring has its own lock in order to not create contention between PMD > threads of different vrings and to prevent performance degradation by > scaling queue pair number. >=20 > See https://bugzilla.redhat.com/show_bug.cgi?id=3D1450680 >=20 > Signed-off-by: Victor Kaplansky > --- > v5: > o get rid of spinlock wrapping functions in vhost.h >=20 > v4: >=20 > o moved access_unlock before accessing enable flag and > access_unlock after iommu_unlock consistently. > o cosmetics: removed blank line. > o the access_lock variable moved to be in the same > cache line with enable and access_ok flags. > o dequeue path is now guarded with trylock and returning > zero if unsuccessful. > o GET_VRING_BASE operation is not guarded by access lock > to avoid deadlock with device_destroy. See the comment > in the code. > o Fixed error path exit from enqueue and dequeue carefully > unlocking access and iommu locks as appropriate. >=20 > v3: > o Added locking to enqueue flow. > o Enqueue path guarded as well as dequeue path. > o Changed name of active_lock. > o Added initialization of guarding spinlock. > o Reworked functions skimming over all virt-queues. > o Performance measurements done by Maxime Coquelin shows > no degradation in bandwidth and throughput. > o Spelling. > o Taking lock only on set operations. > o IOMMU messages are not guarded by access lock. >=20 > v2: > o Fixed checkpatch complains. > o Added Signed-off-by. > o Refined placement of guard to exclude IOMMU messages. > o TODO: performance degradation measurement. >=20 > lib/librte_vhost/vhost.h | 6 ++-- > lib/librte_vhost/vhost.c | 1 + > lib/librte_vhost/vhost_user.c | 70 > +++++++++++++++++++++++++++++++++++++++++++ > lib/librte_vhost/virtio_net.c | 28 ++++++++++++++--- > 4 files changed, 99 insertions(+), 6 deletions(-) >=20 > diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h index > 1cc81c17..c8f2a817 100644 > --- a/lib/librte_vhost/vhost.h > +++ b/lib/librte_vhost/vhost.h > @@ -108,12 +108,14 @@ struct vhost_virtqueue { >=20 > /* Backend value to determine if device should started/stopped */ > int backend; > + int enabled; > + int access_ok; > + rte_spinlock_t access_lock; > + > /* Used to notify the guest (trigger interrupt) */ > int callfd; > /* Currently unused as polling mode is enabled */ > int kickfd; > - int enabled; > - int access_ok; >=20 > /* Physical address of used ring, for logging */ > uint64_t log_guest_addr; > diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c index > 4f8b73a0..dcc42fc7 100644 > --- a/lib/librte_vhost/vhost.c > +++ b/lib/librte_vhost/vhost.c > @@ -259,6 +259,7 @@ alloc_vring_queue(struct virtio_net *dev, uint32_t > vring_idx) >=20 > dev->virtqueue[vring_idx] =3D vq; > init_vring_queue(dev, vring_idx); > + rte_spinlock_init(&vq->access_lock); >=20 > dev->nr_vring +=3D 1; >=20 > diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.= c > index f4c7ce46..0685d4e7 100644 > --- a/lib/librte_vhost/vhost_user.c > +++ b/lib/librte_vhost/vhost_user.c > @@ -1190,12 +1190,47 @@ > vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev, > VhostUserMsg *msg) > return alloc_vring_queue(dev, vring_idx); } >=20 > +static void > +vhost_user_lock_all_queue_pairs(struct virtio_net *dev) { > + unsigned int i =3D 0; > + unsigned int vq_num =3D 0; > + > + while (vq_num < dev->nr_vring) { > + struct vhost_virtqueue *vq =3D dev->virtqueue[i]; > + > + if (vq) { > + rte_spinlock_lock(&vq->access_lock); > + vq_num++; > + } > + i++; > + } > +} > + > +static void > +vhost_user_unlock_all_queue_pairs(struct virtio_net *dev) { > + unsigned int i =3D 0; > + unsigned int vq_num =3D 0; > + > + while (vq_num < dev->nr_vring) { > + struct vhost_virtqueue *vq =3D dev->virtqueue[i]; > + > + if (vq) { > + rte_spinlock_unlock(&vq->access_lock); > + vq_num++; > + } > + i++; > + } > +} > + > int > vhost_user_msg_handler(int vid, int fd) { > struct virtio_net *dev; > struct VhostUserMsg msg; > int ret; > + int unlock_required =3D 0; >=20 > dev =3D get_device(vid); > if (dev =3D=3D NULL) > @@ -1241,6 +1276,38 @@ vhost_user_msg_handler(int vid, int fd) > return -1; > } >=20 > + /* > + * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE, > + * since it is sent when virtio stops and device is destroyed. > + * destroy_device waits for queues to be inactive, so it is safe. > + * Otherwise taking the access_lock would cause a dead lock. > + */ > + switch (msg.request.master) { > + case VHOST_USER_SET_FEATURES: > + case VHOST_USER_SET_PROTOCOL_FEATURES: > + case VHOST_USER_SET_OWNER: > + case VHOST_USER_RESET_OWNER: > + case VHOST_USER_SET_MEM_TABLE: > + case VHOST_USER_SET_LOG_BASE: > + case VHOST_USER_SET_LOG_FD: > + case VHOST_USER_SET_VRING_NUM: > + case VHOST_USER_SET_VRING_ADDR: > + case VHOST_USER_SET_VRING_BASE: > + case VHOST_USER_SET_VRING_KICK: > + case VHOST_USER_SET_VRING_CALL: > + case VHOST_USER_SET_VRING_ERR: > + case VHOST_USER_SET_VRING_ENABLE: > + case VHOST_USER_SEND_RARP: > + case VHOST_USER_NET_SET_MTU: > + case VHOST_USER_SET_SLAVE_REQ_FD: > + vhost_user_lock_all_queue_pairs(dev); > + unlock_required =3D 1; > + break; > + default: > + break; > + > + } > + > switch (msg.request.master) { > case VHOST_USER_GET_FEATURES: > msg.payload.u64 =3D vhost_user_get_features(dev); @@ -1342,6 > +1409,9 @@ vhost_user_msg_handler(int vid, int fd) >=20 > } >=20 > + if (unlock_required) > + vhost_user_unlock_all_queue_pairs(dev); > + > if (msg.flags & VHOST_USER_NEED_REPLY) { > msg.payload.u64 =3D !!ret; > msg.size =3D sizeof(msg.payload.u64); > diff --git a/lib/librte_vhost/virtio_net.c b/lib/librte_vhost/virtio_net.= c index > 6fee16e5..e09a927d 100644 > --- a/lib/librte_vhost/virtio_net.c > +++ b/lib/librte_vhost/virtio_net.c > @@ -44,6 +44,7 @@ > #include > #include > #include > +#include >=20 > #include "iotlb.h" > #include "vhost.h" > @@ -326,8 +327,11 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t > queue_id, > } >=20 > vq =3D dev->virtqueue[queue_id]; > + > + rte_spinlock_lock(&vq->access_lock); > + > if (unlikely(vq->enabled =3D=3D 0)) > - return 0; > + goto out_access_unlock; >=20 > if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) > vhost_user_iotlb_rd_lock(vq); > @@ -419,6 +423,9 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t > queue_id, > if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) > vhost_user_iotlb_rd_unlock(vq); >=20 > +out_access_unlock: > + rte_spinlock_unlock(&vq->access_lock); > + > return count; > } >=20 > @@ -651,8 +658,11 @@ virtio_dev_merge_rx(struct virtio_net *dev, > uint16_t queue_id, > } >=20 > vq =3D dev->virtqueue[queue_id]; > + > + rte_spinlock_lock(&vq->access_lock); > + > if (unlikely(vq->enabled =3D=3D 0)) > - return 0; > + goto out_access_unlock; >=20 > if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) > vhost_user_iotlb_rd_lock(vq); > @@ -715,6 +725,9 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t > queue_id, > if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) > vhost_user_iotlb_rd_unlock(vq); >=20 > +out_access_unlock: > + rte_spinlock_unlock(&vq->access_lock); > + > return pkt_idx; > } >=20 > @@ -1180,9 +1193,13 @@ rte_vhost_dequeue_burst(int vid, uint16_t > queue_id, > } >=20 > vq =3D dev->virtqueue[queue_id]; > - if (unlikely(vq->enabled =3D=3D 0)) > + > + if (unlikely(rte_spinlock_trylock(&vq->access_lock) =3D=3D 0)) > return 0; >=20 > + if (unlikely(vq->enabled =3D=3D 0)) > + goto out_access_unlock; > + > vq->batch_copy_nb_elems =3D 0; >=20 > if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) @@ > -1240,7 +1257,7 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id, > if (rarp_mbuf =3D=3D NULL) { > RTE_LOG(ERR, VHOST_DATA, > "Failed to allocate memory for mbuf.\n"); > - return 0; > + goto out; > } >=20 > if (make_rarp_packet(rarp_mbuf, &dev->mac)) { @@ -1356,6 > +1373,9 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id, > if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) > vhost_user_iotlb_rd_unlock(vq); >=20 > +out_access_unlock: > + rte_spinlock_unlock(&vq->access_lock); > + > if (unlikely(rarp_mbuf !=3D NULL)) { > /* > * Inject it to the head of "pkts" array, so that switch's mac > -- > Victor