From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 20F41A0542; Thu, 13 Feb 2020 12:09:47 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 3A8671BE80; Thu, 13 Feb 2020 12:09:45 +0100 (CET) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by dpdk.org (Postfix) with ESMTP id 2245F1BC25 for ; Thu, 13 Feb 2020 12:09:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581592182; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=zPhU68Dzqfm9uj10A9FYRJDLd/ym0XgvDsXfs4zjFmg=; b=RznDcKZQWkHOzs9ykuvoqWBcwvGXVY6fSa4RfQNsehstnwTUrmSfQ3zNmfmjJvhQh+FxEm 75FkrVSuf+vyIKCQzOGF9/HQvPvnecNHt1vu6XodWdW0WVrvAJoDDGyLdloApwAmrfP01e i4G9I0sS+sVCHGcmzlThRIccp5HYhYM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-107-Yh0nrH4ZNG-y6hS5-rg7pg-1; Thu, 13 Feb 2020 06:09:41 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C7AF5477; Thu, 13 Feb 2020 11:09:39 +0000 (UTC) Received: from [10.33.36.13] (unknown [10.33.36.13]) by smtp.corp.redhat.com (Postfix) with ESMTP id EDDF626FCF; Thu, 13 Feb 2020 11:09:33 +0000 (UTC) To: Adrian Moreno , dev@dpdk.org Cc: maxime.coquelin@redhat.com, Tiwei Bie , Zhihong Wang , stable@dpdk.org References: <20200213100458.483410-1-amorenoz@redhat.com> From: Kevin Traynor Autocrypt: addr=ktraynor@redhat.com; keydata= mQINBF2J2awBEADUEPNhgNI+nJNgiTAUcw4YIgVXEoHlsNPyyzG1BEXkWXALy0Y3fNTiw6+r ltWDkF9jzL9kfkecgQ67itGfk1OaBXgSGKuw1PUpxAwX2Bi76LAR6M5OsyGM9TSVVQwARalz hMwRBIZPzPc7or6Pw7jAOJ8SQGJ1Zlp1YJCjrvpe87V1tH/LY8Wnxn/EuoseFmWILAQZAtYS tGjcrAgYn3SPMLR1B0BP5bTBY06vWQjiufH8drenfDnMJAzuBdG1mqjnTqCjULZ3Hunv4xqZ aMnkvL/K5Tj1c12Oe4930EE53LrXIBUltRg5mBudSWHnC7twjH0082HH9f963Z/2UI63SFIT iUvRvAzJYytgy7XnWLQ0+goZBADKYfolOuC0H8VgCaux8u8KFF28Dy+N6TV2KI58jTlyg1Zu l7QwykZpnOkJFiy37Gfbu3YEOzO72cP/S7/A+zvuqkxi63jyEkd+FY99vLt/HN2MUZwRmKDw UPbLkmrs8WU01/POVsqDcfvz7vu2St8hqqTiSIdQGS2zyTKB2/DvPSM3jws3udkIYSuhn+X4 QBiV6lkVZ7DSE6a065gnAauAql+b32Eymy+xnG5jCt1tR+0Cp2VZYCR9OU2gmomUKBDoX/He pSgED01CqYPNjN+TddirwmQX7ep4DtXc8FWvv2g/pq9WZFQk2QARAQABtCNLZXZpbiBUcmF5 bm9yIDxrdHJheW5vckByZWRoYXQuY29tPokCTgQTAQgAOBYhBAoiOaH51tHF7VYtEI9CINER a+yJBQJdidmsAhsDBQsJCAcCBhUKCQgLAgQWAgMBAh4BAheAAAoJEI9CINERa+yJoxIP/3VF 2TIgW4ckxhRFCvFu/606bnvCPie88ake4uWVWMAWwcMc4fK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wtVuhaDXhF3GfW+5W/x wAg7Qg3w00ASsb/XTHBIhMnenKDfS7ihtQA8SacwX8ySdxb+15XPyiplM979qBQ0mhnilulm MIJzEf/JxoYR5huuj4f1PFqqrsP06Dl+YGB7dQZp3IKggS5c3/TAynARRg9N89UsDXNtp7X0 tgIPFF5k6fnHE0J5O64GYHeTqN/1aE6dAEOV9WrGzQAJxU9ipikb8jKAWXzLewRIKGmoPcRZ WdB0NmIjmQARAQABiQI2BBgBCAAgFiEECiI5ofnW0cXtVi0Qj0Ig0RFr7IkFAl2J2awCGwwA CgkQj0Ig0RFr7IkkORAAl/NbX93WK5MEoRw7/DaPTo/Lo6Pj1XMeSqGyACigHK/452UDvlEH NjNJMzYYrNIjMtEmN9VVCfjT38CSca7mpGQVwchc0mC7QSPAETLCS+UacVf/Kwxz5FfkEUUw UT7A+uyVOIgW3d9ldlRzkHA2czonSSgTQU+i2g6DM4ha+BuQb4byAXH6HQHt/Zh1J64z0ohH v6iGsCzCY/sMWF8+LEGSnzMGRCLiiwSF0vJBHbzWK68fANaF4gBV0Z/+6tQRFN7YMhj/INmk qgvHj1ZzHFNtirjMGPRxoZs51YoLQM/aBPxKrnmXThx1ufH+0L6sGmFTugiDt0XSEkC5reH7 a+VhQ1VTFFQrClA8NmDSPzFeuhru4ryaaDHO+uEB16cNHxHrQtlP/2hts2JM5lwkZRWJ5A57 h8eDEIK5be47T85NVHfuTaboNRmgg1HygVejhGUtt69u/0MVRg/roUTa0FyEbNsvz4qAecyW yWzMcVrcGJDQLC9JLKEpoyUF6gdTKaiDL2Vao4+XRIA3Y57b6MO35a3HuzAv7+i5Z0mnDEJO XxXqTOmKYpMIGexzM/PtuA0712sT1abG9tAJ17ao/B7cqMW5IkKkalemFbWfI2unns4Papvo tk9igVqyp6EJDU98z5TJioCVojwK2laDaoIjTJk9YYv3iwCsqPd5feU= Message-ID: Date: Thu, 13 Feb 2020 11:09:30 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: <20200213100458.483410-1-amorenoz@redhat.com> Content-Language: en-US X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-MC-Unique: Yh0nrH4ZNG-y6hS5-rg7pg-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [dpdk-dev] [PATCH] vhost: protect log addr translation in iotlb updates 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: , Errors-To: dev-bounces@dpdk.org Sender: "dev" On 13/02/2020 10:04, Adrian Moreno wrote: > Currently, the log address translation only happens in the vhost-user's > translate_ring_addresses(). However, the IOTLB update handler is not > checking if it was mapped to re-trigger that translation. >=20 > Since the log address mapping could fail, check it on iotlb updates. > Also, check it on vring_translate() so we do not dirty pages if the > logging address is not yet ready. >=20 > Additionally, properly protect the accesses to the iotlb structures. >=20 > Fixes: 657414f3788a ("vhost: protect log addr translation in iotlb update= s") This fixes looks incorrect, it's suspiciously the same name and I couldn't find the commit-id in upstream. > Cc: stable@dpdk.org >=20 > Signed-off-by: Adrian Moreno > --- > lib/librte_vhost/vhost.c | 55 +++++++++++++++++++++++++++++++++ > lib/librte_vhost/vhost.h | 15 +++++++-- > lib/librte_vhost/vhost_user.c | 57 +++++++++++++++-------------------- > 3 files changed, 92 insertions(+), 35 deletions(-) >=20 > diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c > index c819a8477..442d896ef 100644 > --- a/lib/librte_vhost/vhost.c > +++ b/lib/librte_vhost/vhost.c > @@ -353,6 +353,56 @@ free_device(struct virtio_net *dev) > =09rte_free(dev); > } > =20 > +static __rte_always_inline int > +log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq) > +{ > +=09if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG)))) > +=09=09return 0; > + > +=09vq->log_guest_addr =3D translate_log_addr(dev, vq, > +=09=09=09=09=09=09vq->ring_addrs.log_guest_addr); > +=09if (vq->log_guest_addr =3D=3D 0) > +=09=09return -1; > + > +=09return 0; > +} > + > +/* > + * Converts vring log address to GPA > + * If IOMMU is enabled, the log address is IOVA > + * If IOMMU not enabled, the log address is already GPA > + * > + * Caller should have iotlb_lock read-locked > + */ > +uint64_t > +translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq, > +=09=09uint64_t log_addr) > +{ > +=09if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) { > +=09=09const uint64_t exp_size =3D sizeof(uint64_t); > +=09=09uint64_t hva, gpa; > +=09=09uint64_t size =3D exp_size; > + > +=09=09hva =3D vhost_iova_to_vva(dev, vq, log_addr, > +=09=09=09=09=09&size, VHOST_ACCESS_RW); > + > +=09=09if (size !=3D exp_size) > +=09=09=09return 0; > + > +=09=09gpa =3D hva_to_gpa(dev, hva, exp_size); > +=09=09if (!gpa) { > +=09=09=09VHOST_LOG_CONFIG(ERR, > +=09=09=09=09"VQ: Failed to find GPA for log_addr: 0x%" PRIx64 " hva: 0x%= " PRIx64 "\n", > +=09=09=09=09log_addr, hva); > +=09=09=09return 0; > +=09=09} > +=09=09return gpa; > + > +=09} else > +=09=09return log_addr; > +} > + > +/* Caller should have iotlb_lock read-locked */ > static int > vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq= ) > { > @@ -391,6 +441,7 @@ vring_translate_split(struct virtio_net *dev, struct = vhost_virtqueue *vq) > =09return 0; > } > =20 > +/* Caller should have iotlb_lock read-locked */ > static int > vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *v= q) > { > @@ -437,6 +488,10 @@ vring_translate(struct virtio_net *dev, struct vhost= _virtqueue *vq) > =09=09if (vring_translate_split(dev, vq) < 0) > =09=09=09return -1; > =09} > + > +=09if (log_translate(dev, vq) < 0) > +=09=09return -1; > + > =09vq->access_ok =3D 1; > =20 > =09return 0; > diff --git a/lib/librte_vhost/vhost.h b/lib/librte_vhost/vhost.h > index 686ce42a2..2087d1400 100644 > --- a/lib/librte_vhost/vhost.h > +++ b/lib/librte_vhost/vhost.h > @@ -462,14 +462,23 @@ static __rte_always_inline void > vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueu= e *vq, > =09=09=09uint64_t offset, uint64_t len) > { > -=09vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len); > +=09if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) { > +=09=09if (unlikely(vq->log_guest_addr =3D=3D 0)) > +=09=09=09return; > +=09=09__vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, > +=09=09=09=09=09len); > +=09} > } > =20 > static __rte_always_inline void > vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq, > =09=09 uint64_t offset, uint64_t len) > { > -=09vhost_log_write(dev, vq->log_guest_addr + offset, len); > +=09if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) { > +=09=09if (unlikely(vq->log_guest_addr =3D=3D 0)) > +=09=09=09return; > +=09=09__vhost_log_write(dev, vq->log_guest_addr + offset, len); > +=09} > } > =20 > static __rte_always_inline void > @@ -626,6 +635,8 @@ void *vhost_alloc_copy_ind_table(struct virtio_net *d= ev, > =09=09=09struct vhost_virtqueue *vq, > =09=09=09uint64_t desc_addr, uint64_t desc_len); > int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq); > +uint64_t translate_log_addr(struct virtio_net *dev, struct vhost_virtque= ue *vq, > +=09=09uint64_t log_addr); > void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq= ); > =20 > static __rte_always_inline uint64_t > diff --git a/lib/librte_vhost/vhost_user.c b/lib/librte_vhost/vhost_user.= c > index 69b84a882..5db9f148f 100644 > --- a/lib/librte_vhost/vhost_user.c > +++ b/lib/librte_vhost/vhost_user.c > @@ -656,13 +656,11 @@ ring_addr_to_vva(struct virtio_net *dev, struct vho= st_virtqueue *vq, > { > =09if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) { > =09=09uint64_t vva; > -=09=09uint64_t req_size =3D *size; > =20 > -=09=09vva =3D vhost_user_iotlb_cache_find(vq, ra, > +=09=09vhost_user_iotlb_rd_lock(vq); > +=09=09vva =3D vhost_iova_to_vva(dev, vq, ra, > =09=09=09=09=09size, VHOST_ACCESS_RW); > -=09=09if (req_size !=3D *size) > -=09=09=09vhost_user_iotlb_miss(dev, (ra + *size), > -=09=09=09=09=09 VHOST_ACCESS_RW); > +=09=09vhost_user_iotlb_rd_unlock(vq); > =20 > =09=09return vva; > =09} > @@ -670,37 +668,16 @@ ring_addr_to_vva(struct virtio_net *dev, struct vho= st_virtqueue *vq, > =09return qva_to_vva(dev, ra, size); > } > =20 > -/* > - * Converts vring log address to GPA > - * If IOMMU is enabled, the log address is IOVA > - * If IOMMU not enabled, the log address is already GPA > - */ > static uint64_t > -translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq, > -=09=09uint64_t log_addr) > +log_addr_to_gpa(struct virtio_net *dev, struct vhost_virtqueue *vq) > { > -=09if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) { > -=09=09const uint64_t exp_size =3D sizeof(struct vring_used) + > -=09=09=09sizeof(struct vring_used_elem) * vq->size; > -=09=09uint64_t hva, gpa; > -=09=09uint64_t size =3D exp_size; > - > -=09=09hva =3D vhost_iova_to_vva(dev, vq, log_addr, > -=09=09=09=09=09&size, VHOST_ACCESS_RW); > -=09=09if (size !=3D exp_size) > -=09=09=09return 0; > +=09uint64_t log_gpa; > =20 > -=09=09gpa =3D hva_to_gpa(dev, hva, exp_size); > -=09=09if (!gpa) { > -=09=09=09VHOST_LOG_CONFIG(ERR, > -=09=09=09=09"VQ: Failed to find GPA for log_addr: 0x%" PRIx64 " hva: 0x%= " PRIx64 "\n", > -=09=09=09=09log_addr, hva); > -=09=09=09return 0; > -=09=09} > -=09=09return gpa; > +=09vhost_user_iotlb_rd_lock(vq); > +=09log_gpa =3D translate_log_addr(dev, vq, vq->ring_addrs.log_guest_addr= ); > +=09vhost_user_iotlb_rd_unlock(vq); > =20 > -=09} else > -=09=09return log_addr; > +=09return log_gpa; > } > =20 > static struct virtio_net * > @@ -712,7 +689,7 @@ translate_ring_addresses(struct virtio_net *dev, int = vq_index) > =20 > =09if (addr->flags & (1 << VHOST_VRING_F_LOG)) { > =09=09vq->log_guest_addr =3D > -=09=09=09translate_log_addr(dev, vq, addr->log_guest_addr); > +=09=09=09log_addr_to_gpa(dev, vq); > =09=09if (vq->log_guest_addr =3D=3D 0) { > =09=09=09VHOST_LOG_CONFIG(DEBUG, > =09=09=09=09"(%d) failed to map log_guest_addr.\n", > @@ -2229,6 +2206,13 @@ is_vring_iotlb_split(struct vhost_virtqueue *vq, s= truct vhost_iotlb_msg *imsg) > =09if (ra->used_user_addr < end && (ra->used_user_addr + len) > start) > =09=09return 1; > =20 > +=09if (ra->flags & (1 << VHOST_VRING_F_LOG)) { > +=09=09len =3D sizeof(uint64_t); > +=09=09if (ra->log_guest_addr < end && > +=09=09 (ra->log_guest_addr + len) > start) > +=09=09=09return 1; > +=09} > + > =09return 0; > } > =20 > @@ -2254,6 +2238,13 @@ is_vring_iotlb_packed(struct vhost_virtqueue *vq, = struct vhost_iotlb_msg *imsg) > =09if (ra->used_user_addr < end && (ra->used_user_addr + len) > start) > =09=09return 1; > =20 > +=09if (ra->flags & (1 << VHOST_VRING_F_LOG)) { > +=09=09len =3D sizeof(uint64_t); > +=09=09if (ra->log_guest_addr < end && > +=09=09 (ra->log_guest_addr + len) > start) > +=09=09=09return 1; > +=09} > + > =09return 0; > } > =20 >=20