Currently, when vhost asks for the notify area of a queue, we return
its offset within the VFIO device fd, which is the offset of the BAR
region within the fd plus the doorbell offset within the BAR. We then
also write to the doorbell ourselves, but we compute its address by
adding that same fd offset to the address the BAR is mapped at. This is
only correct when the region happens to be at offset zero within the fd:
otherwise, the address ends up far outside of the BAR mapping.

Fix it by adding only the doorbell offset within the BAR, and check that
the doorbell is within the BAR before writing to it.

Fixes: 2eb13ddea384 ("vdpa/sfc: fix sync between QEMU and vhost-user")
Cc: [email protected]
Cc: [email protected]

Signed-off-by: Anatoly Burakov <[email protected]>
---
 drivers/vdpa/sfc/sfc_vdpa_ops.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/vdpa/sfc/sfc_vdpa_ops.c b/drivers/vdpa/sfc/sfc_vdpa_ops.c
index 84ba141b348..778cde115f8 100644
--- a/drivers/vdpa/sfc/sfc_vdpa_ops.c
+++ b/drivers/vdpa/sfc/sfc_vdpa_ops.c
@@ -815,6 +815,7 @@ sfc_vdpa_get_notify_area(int vid, int qid, uint64_t 
*offset, uint64_t *size)
        struct vfio_region_info reg = { .argsz = sizeof(reg) };
        const efx_nic_cfg_t *encp;
        int max_vring_cnt;
+       uint64_t bar_offset;
        int64_t len;
        void *dev;
 
@@ -847,7 +848,8 @@ sfc_vdpa_get_notify_area(int vid, int qid, uint64_t 
*offset, uint64_t *size)
        }
 
        /* Use bar_offset that was cached during sfc_vdpa_virtq_start() */
-       *offset = reg.offset + (uint64_t)ops_data->vq_cxt[qid].doorbell;
+       bar_offset = (uintptr_t)ops_data->vq_cxt[qid].doorbell;
+       *offset = reg.offset + bar_offset;
 
        len = (1U << encp->enc_vi_window_shift) / 2;
        if (len >= sysconf(_SC_PAGESIZE)) {
@@ -861,7 +863,11 @@ sfc_vdpa_get_notify_area(int vid, int qid, uint64_t 
*offset, uint64_t *size)
                      *offset);
 
        pci_dev = sfc_vdpa_adapter_by_dev_handle(dev)->pdev;
-       doorbell = (uint8_t *)pci_dev->mem_resource[reg.index].addr + *offset;
+       if (bar_offset + sizeof(uint16_t) > 
pci_dev->mem_resource[reg.index].len) {
+               sfc_vdpa_err(dev, "invalid doorbell offset : 0x%" PRIx64, 
bar_offset);
+               return -1;
+       }
+       doorbell = (uint8_t *)pci_dev->mem_resource[reg.index].addr + 
bar_offset;
 
        /*
         * virtio-net driver in VM sends queue notifications before
-- 
2.52.0

Reply via email to