Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Weimin Xiong" <xiongwm2026@163.com>
Cc: kvm@vger.kernel.org
Subject: Re: (no subject)
Date: Sat, 18 Jul 2026 02:13:36 +0000	[thread overview]
Message-ID: <20260718021337.2156C1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260717021305.327441-1-xiongwm2026@163.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Pages are unpinned before the hardware IOMMU is updated, creating a Use-After-Free race condition where the device can DMA into reallocated physical memory.
- [High] Hardware unmap failures are silently ignored, leaving stale active mappings in the hardware while the OS considers the memory freed.
- [High] Pinned physical pages are permanently leaked when `vhost_vdpa_map` or `pin_user_pages` fails after a successfully mapped discontinuity in `vhost_vdpa_pa_map`.
--

--- Patch [1]: (no subject) ---
commit 9d5471c1144ba5ee59ae8bddf13fc8fca006ee54
Author: xiongweimin <xiongweimin@kylinos.cn>

vhost-vdpa: propagate set_map error to caller

The return value of ops->set_map() is currently ignored when handling
VHOST_IOTLB_BATCH_END. If the backend fails to program the IOTLB,
the VMM incorrectly believes the operation succeeded and may continue
with stale or incorrect mappings. Propagate the error from ops->set_map()
to the caller.

> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index ac55275fa0d0a..89af89af26ca9 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -1293,7 +1293,7 @@ static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
>  		break;
>  	case VHOST_IOTLB_BATCH_END:
>  		if (v->in_batch && ops->set_map)
> -			ops->set_map(vdpa, asid, iotlb);
> +			r = ops->set_map(vdpa, asid, iotlb);

[Severity: Critical]
This isn't a bug introduced by this patch, but does the unmap sequence
create a race condition where the device can DMA into reallocated memory?

Looking at vhost_vdpa_pa_unmap(), pages are unpinned before the mapping is
removed from the hardware IOMMU:

drivers/vhost/vdpa.c:vhost_vdpa_pa_unmap() {
    ...
            unpin_user_page(page);
        }
        atomic64_sub(PFN_DOWN(map->size), &dev->mm->pinned_vm);
        vhost_vdpa_general_unmap(v, map, asid);
}

For devices using set_map, the hardware is updated much later in
vhost_vdpa_unmap():

drivers/vhost/vdpa.c:vhost_vdpa_unmap() {
    ...
    vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1, asid);

    if (ops->set_map) {
        if (!v->in_batch)
            ops->set_map(vdpa, asid, iotlb);
    }
}

Could an unprivileged device execute asynchronous DMA into these newly freed
pages during the window between unpin_user_page() and the hardware update?

[Severity: High]
This is a pre-existing issue, but are we silently masking hardware unmap
failures elsewhere?

In vhost_vdpa_unmap(), the return value of ops->set_map() is discarded:

drivers/vhost/vdpa.c:vhost_vdpa_unmap() {
    ...
    if (ops->set_map) {
        if (!v->in_batch)
            ops->set_map(vdpa, asid, iotlb);
    }
}

Similarly, vhost_vdpa_general_unmap() ignores the return values of
ops->dma_unmap() and iommu_unmap():

drivers/vhost/vdpa.c:vhost_vdpa_general_unmap() {
    ...
    if (ops->dma_map) {
        ops->dma_unmap(vdpa, asid, map->start, map->size);
    } else if (ops->set_map == NULL) {
        iommu_unmap(v->domain, map->start, map->size);
    }
}

If the hardware backend encounters an error and fails to remove the mapping,
could this leave stale active mappings in the hardware while the OS
considers the memory freed?

[Severity: High]
This is also a pre-existing issue, but does the map function leak pinned
physical pages if an error occurs?

In vhost_vdpa_pa_map(), when a chunk is mapped, nchunks is reset to 0:

drivers/vhost/vdpa.c:vhost_vdpa_pa_map() {
    ...
            map_pfn = this_pfn;
            iova += csize;
            nchunks = 0;
        }
}

If a subsequent discontinuity causes a failure, the error cleanup path
skips unpinning the currently pending chunk because nchunks evaluates
to false:

drivers/vhost/vdpa.c:vhost_vdpa_pa_map() {
    ...
out:
    if (ret) {
        if (nchunks) {
            unsigned long pfn;

            WARN_ON(!last_pfn);
            for (pfn = map_pfn; pfn <= last_pfn; pfn++)
                unpin_user_page(pfn_to_page(pfn));
        }
    }
}

Will this permanently leak the pinned memory if a failure happens after a
successfully mapped discontinuity?

>  		v->in_batch = false;
>  		break;
>  	default:

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260717021305.327441-1-xiongwm2026@163.com?part=1

  reply	other threads:[~2026-07-18  2:13 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-16  3:02 [PATCH] vhost-vdpa: propagate set_map error to caller Weimin Xiong
2026-07-16 10:01 ` Eugenio Perez Martin
2026-07-17  2:13   ` Weimin Xiong
2026-07-18  2:13     ` sashiko-bot [this message]
2026-07-17  2:17   ` Re: [PATCH] vhost-vdpa: propagate set_map error to caller Weimin Xiong
2026-07-17  3:03 ` sashiko-bot
  -- strict thread matches above, loose matches on Subject: below --
2014-02-04 14:18 (no subject) Валентин Сайков
2010-05-12 14:05 carlopmart
2009-10-03  5:02 debmail_5f63g
2009-09-07 16:43 the uknl
2008-05-15  4:15 林先生
2008-05-03  4:01 林先生
2008-04-19 21:53 钟文辉
2008-04-14  9:01 钟文辉
2008-04-14  8:55 钟文辉
2008-04-14  8:49 钟文辉
2008-04-13 14:24 钟文辉
2008-04-13 14:13 钟文辉
2008-04-13  6:20 钟文辉
2008-04-13  5:42 钟文辉
2008-04-09 22:40 钟文辉
2008-04-09 22:32 钟文辉
2008-03-31  9:59 钟文辉
2008-03-23 21:10 钟文辉
2008-03-23 19:49 钟文辉
2007-12-18  4:09 Yu Fei
2007-12-04 17:29 余上
2007-10-09  6:59 Donna Lackey
2007-10-05 16:24 Aline Edmonds
2007-09-13  3:08 Dong, Eddie
2007-08-21 19:54 Melgar
2007-08-21 19:43 Joeann Moyini
2007-08-21  9:51 shirlee Taruc
2007-08-21  0:01 holley
2007-08-20 19:08 Gmagm sssssssssssa
2007-08-18 10:00 Guzman
2007-08-18  9:54 aanidi-CYedFwHYa5iBl8o7nT1L3dBPR1lH4CV8
2007-08-12 19:59 Izik Eidus
     [not found] ` <64F9B87B6B770947A9F8391472E032160CBECF40-yEcIvxbTEBqsx+V+t5oei8rau4O3wl8o3fe8/T/H7NteoWH0uzbU5w@public.gmane.org>
2007-08-12 21:37   ` Anthony Liguori
2007-08-13  8:10     ` Avi Kivity
2007-08-14 10:38   ` Carsten Otte
2007-08-09 17:27 Harlan Kellis
2007-08-07  4:23 Jean
2007-07-27  4:03 Frederick
     [not found] <FC1D1B23302A22499C60C967336B2AE00186B15C@pdsmsx411.ccr.corp.intel.com>
     [not found] ` <FC1D1B23302A22499C60C967336B2AE00186B15C-wq7ZOvIWXbNpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2007-07-24 13:40   ` Shaohua Li
2007-07-21  5:45 Sandy Holloway
2007-07-20 19:48 Cohen Micky
2007-07-20  8:13 Dong, Eddie
2007-07-20  8:11 Dong, Eddie
2007-07-16 13:20 Marshall
2007-07-16  0:06 Hull N.Flossie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260718021337.2156C1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=xiongwm2026@163.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox