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 B5AD0A0542; Thu, 13 Feb 2020 12:26:05 +0100 (CET) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 434DF1BF81; Thu, 13 Feb 2020 12:26:05 +0100 (CET) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by dpdk.org (Postfix) with ESMTP id 68E711BF7A for ; Thu, 13 Feb 2020 12:26:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581593162; 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=ml7Pk+hRE5ZGdTE4ipAkrkV16OqlgBoBBWuGSeMyutE=; b=Zosj2XctQQ4smi+N5Jasfs7CKI3vWP7IPQoZYSbNwpkSoBhNUkV+9kQHgGJVnRpYA3PCz2 0nhawyvEDWEr65i0ku4vDixg0dMTFdrAS/W1RGhtGJQalUKLkz7vSF8W7QKehabYx+2+Sp EFuD/IaGwz1GwJpJNH/nT3WCbAwJzzU= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-174-NaLhMRjwM1aPmH6hDMqL4A-1; Thu, 13 Feb 2020 06:25:58 -0500 Received: by mail-wr1-f72.google.com with SMTP id s13so2198524wrb.21 for ; Thu, 13 Feb 2020 03:25:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=ml7Pk+hRE5ZGdTE4ipAkrkV16OqlgBoBBWuGSeMyutE=; b=IUfAkaAvntdzYsyqL2vfBlXDNOLNJnXknwFDEFI0rkGzpkpp+qiKjqrl0Hqxp509wy FlYWIUI8WowDsBYuhUoF8yv668YMNcK5Fy7KPHY/WXUK/U6lQxD3zHewIY+p3dGzqhu6 afnWE6f5dNmwYei6bptzo84lrdOPlyfVKeBMuKe2s9ZAIDbgBpP5yYqRlhlAbxjGAprH oYYZotWLMUnKszBumbM5MWzlPm5uvF2X198JJyhq74/wVgHxLxGrzwIqLXUIPYRlok1f COPTi3ctpkEkGSrADOYoq65ytaaB+9HUgRnWh3vscO1MAE2k30o7KrGPvAij4VQCXv0p oUKQ== X-Gm-Message-State: APjAAAWgTRcaovjEw2Gt+2DT8l2/wph+5661AEEDOK3A1X3wcfI2z3im ehrl0cKJMHBrmvIuEB9s3j49ViH8HmcDJ9b4NcZpW+gcv6ez0aFtNcOHJi33xhtGUHX8XYLYLlS wF1M= X-Received: by 2002:a7b:c14e:: with SMTP id z14mr5283827wmi.58.1581593157287; Thu, 13 Feb 2020 03:25:57 -0800 (PST) X-Google-Smtp-Source: APXvYqwVf3Nm+WHKK2y2CJnCQaTGsXiCrejREiFNzfkMgV4WEBGracZ2y//nJe9y5eygMWiVgU67yA== X-Received: by 2002:a7b:c14e:: with SMTP id z14mr5283789wmi.58.1581593156816; Thu, 13 Feb 2020 03:25:56 -0800 (PST) Received: from amorenoz.users.ipa.redhat.com (static.169.4.0.81.ibercom.com. [81.0.4.169]) by smtp.gmail.com with ESMTPSA id b13sm2595450wrq.48.2020.02.13.03.25.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Feb 2020 03:25:56 -0800 (PST) To: Kevin Traynor , dev@dpdk.org Cc: maxime.coquelin@redhat.com, Tiwei Bie , Zhihong Wang , stable@dpdk.org References: <20200213100458.483410-1-amorenoz@redhat.com> From: Adrian Moreno Autocrypt: addr=amorenoz@redhat.com; prefer-encrypt=mutual; keydata= mQ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Ey1YN/U10 r6tRRH1kNyrCSbTG0d9MtcQO58h7DLXuzUhErB+BtG52A04t5cweIJTJC+koV5XPeilzlHnm RFoj0ncruGa9Odns21BNt3cy9wLfK+aUnWuAB1uc6bJGQPiAwjkilz7g7MBRUuIQ2Zt7HGLc SwARAQABiQE8BBgBCAAmFiEEogUDgihhmbOPHy26d5C5fbYeFsUFAl1syNUCGwwFCQHhM4AA CgkQd5C5fbYeFsUlSwf8CH+u/IXaE7WeWxwFkMaORfW8cM4q0xrL3M6yRGuQNW+kMjnrvK9U J9G+L1/5uTRbDQ/4LdoKqize8LjehA+iF6ba4t9Npikh8fLKWgaJfQ/hPhH4C3O5gWPOLTW6 ylGxiuER4CdFwQIoAMMslhFA7G+teeOKBq36E+1+zrybI6Xy1UBSlpDK9j4CtTnMQejjuSQb Qhle+l8VroaUHq869wjAhRHHhqmtJKggI+OvzgQpDIwfHIDypb1BuKydi2W6cVYEALUYyCLS dTBDhzj8zR5tPCsga8J7+TclQzkWOiI2C6ZtiWrMsL/Uym3uXk5nsoc7lSj7yLd/MrBRhYfP JQ== Message-ID: <400e2937-0d0d-3347-44f8-1a2956c39f63@redhat.com> Date: Thu, 13 Feb 2020 12:25:55 +0100 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: Content-Language: en-US X-MC-Unique: NaLhMRjwM1aPmH6hDMqL4A-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit 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 2/13/20 12:09 PM, Kevin Traynor wrote: > 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. >> >> 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. >> >> Additionally, properly protect the accesses to the iotlb structures. >> >> Fixes: 657414f3788a ("vhost: protect log addr translation in iotlb updates") > > This fixes looks incorrect, it's suspiciously the same name and I > couldn't find the commit-id in upstream. > Thanks for spotting that Kevin. You're right. The correct line should be: Fixes: fbda9f145927 ("vhost: translate incoming log address to GPA") I'll wait for more comments and repost with the correct Fixes line. Adrian. >> Cc: stable@dpdk.org >> >> 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(-) >> >> 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) >> rte_free(dev); >> } >> >> +static __rte_always_inline int >> +log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq) >> +{ >> + if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG)))) >> + return 0; >> + >> + vq->log_guest_addr = translate_log_addr(dev, vq, >> + vq->ring_addrs.log_guest_addr); >> + if (vq->log_guest_addr == 0) >> + return -1; >> + >> + return 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, >> + uint64_t log_addr) >> +{ >> + if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) { >> + const uint64_t exp_size = sizeof(uint64_t); >> + uint64_t hva, gpa; >> + uint64_t size = exp_size; >> + >> + hva = vhost_iova_to_vva(dev, vq, log_addr, >> + &size, VHOST_ACCESS_RW); >> + >> + if (size != exp_size) >> + return 0; >> + >> + gpa = hva_to_gpa(dev, hva, exp_size); >> + if (!gpa) { >> + VHOST_LOG_CONFIG(ERR, >> + "VQ: Failed to find GPA for log_addr: 0x%" PRIx64 " hva: 0x%" PRIx64 "\n", >> + log_addr, hva); >> + return 0; >> + } >> + return gpa; >> + >> + } else >> + return 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) >> return 0; >> } >> >> +/* Caller should have iotlb_lock read-locked */ >> static int >> vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq) >> { >> @@ -437,6 +488,10 @@ vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq) >> if (vring_translate_split(dev, vq) < 0) >> return -1; >> } >> + >> + if (log_translate(dev, vq) < 0) >> + return -1; >> + >> vq->access_ok = 1; >> >> return 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_virtqueue *vq, >> uint64_t offset, uint64_t len) >> { >> - vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len); >> + if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) { >> + if (unlikely(vq->log_guest_addr == 0)) >> + return; >> + __vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, >> + len); >> + } >> } >> >> static __rte_always_inline void >> vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq, >> uint64_t offset, uint64_t len) >> { >> - vhost_log_write(dev, vq->log_guest_addr + offset, len); >> + if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL))) { >> + if (unlikely(vq->log_guest_addr == 0)) >> + return; >> + __vhost_log_write(dev, vq->log_guest_addr + offset, len); >> + } >> } >> >> static __rte_always_inline void >> @@ -626,6 +635,8 @@ void *vhost_alloc_copy_ind_table(struct virtio_net *dev, >> struct vhost_virtqueue *vq, >> uint64_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_virtqueue *vq, >> + uint64_t log_addr); >> void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq); >> >> 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 vhost_virtqueue *vq, >> { >> if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) { >> uint64_t vva; >> - uint64_t req_size = *size; >> >> - vva = vhost_user_iotlb_cache_find(vq, ra, >> + vhost_user_iotlb_rd_lock(vq); >> + vva = vhost_iova_to_vva(dev, vq, ra, >> size, VHOST_ACCESS_RW); >> - if (req_size != *size) >> - vhost_user_iotlb_miss(dev, (ra + *size), >> - VHOST_ACCESS_RW); >> + vhost_user_iotlb_rd_unlock(vq); >> >> return vva; >> } >> @@ -670,37 +668,16 @@ ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq, >> return qva_to_vva(dev, ra, size); >> } >> >> -/* >> - * 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, >> - uint64_t log_addr) >> +log_addr_to_gpa(struct virtio_net *dev, struct vhost_virtqueue *vq) >> { >> - if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) { >> - const uint64_t exp_size = sizeof(struct vring_used) + >> - sizeof(struct vring_used_elem) * vq->size; >> - uint64_t hva, gpa; >> - uint64_t size = exp_size; >> - >> - hva = vhost_iova_to_vva(dev, vq, log_addr, >> - &size, VHOST_ACCESS_RW); >> - if (size != exp_size) >> - return 0; >> + uint64_t log_gpa; >> >> - gpa = hva_to_gpa(dev, hva, exp_size); >> - if (!gpa) { >> - VHOST_LOG_CONFIG(ERR, >> - "VQ: Failed to find GPA for log_addr: 0x%" PRIx64 " hva: 0x%" PRIx64 "\n", >> - log_addr, hva); >> - return 0; >> - } >> - return gpa; >> + vhost_user_iotlb_rd_lock(vq); >> + log_gpa = translate_log_addr(dev, vq, vq->ring_addrs.log_guest_addr); >> + vhost_user_iotlb_rd_unlock(vq); >> >> - } else >> - return log_addr; >> + return log_gpa; >> } >> >> static struct virtio_net * >> @@ -712,7 +689,7 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index) >> >> if (addr->flags & (1 << VHOST_VRING_F_LOG)) { >> vq->log_guest_addr = >> - translate_log_addr(dev, vq, addr->log_guest_addr); >> + log_addr_to_gpa(dev, vq); >> if (vq->log_guest_addr == 0) { >> VHOST_LOG_CONFIG(DEBUG, >> "(%d) failed to map log_guest_addr.\n", >> @@ -2229,6 +2206,13 @@ is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg) >> if (ra->used_user_addr < end && (ra->used_user_addr + len) > start) >> return 1; >> >> + if (ra->flags & (1 << VHOST_VRING_F_LOG)) { >> + len = sizeof(uint64_t); >> + if (ra->log_guest_addr < end && >> + (ra->log_guest_addr + len) > start) >> + return 1; >> + } >> + >> return 0; >> } >> >> @@ -2254,6 +2238,13 @@ is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg) >> if (ra->used_user_addr < end && (ra->used_user_addr + len) > start) >> return 1; >> >> + if (ra->flags & (1 << VHOST_VRING_F_LOG)) { >> + len = sizeof(uint64_t); >> + if (ra->log_guest_addr < end && >> + (ra->log_guest_addr + len) > start) >> + return 1; >> + } >> + >> return 0; >> } >> >> >