FAILED: patch "[PATCH] vhost-vdpa: fix pinned page leak in vhost_vdpa_pa_map() error path" failed to apply to 5.10-stable tree
Subsystems (2)
Sasha Levin
maintainerThe patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.10.y
git checkout FETCH_HEAD
git cherry-pick -x fd1462c96bafe2cb14e88c837b270f8990b717b5
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 5.10.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From fd1462c96bafe2cb14e88c837b270f8990b717b5 Mon Sep 17 00:00:00 2001
From: Tal Zussman <[email protected]>
Date: Sat, 31 Oct 2026 13:45:34 -0400
Subject: [PATCH] vhost-vdpa: fix pinned page leak in vhost_vdpa_pa_map() error
path
When pin_user_pages() or vhost_vdpa_map() fails, vhost_vdpa_pa_map() is supposed to unpin the pages pinned after the last successfully mapped chunk, i.e. the range [map_pfn, last_pfn]. The unpin is guarded by nchunks, but nchunks is reset to 0 every time a chunk is mapped from inside the current page list, so a failure after any such boundary leaves that pending range pinned forever. With physically scattered memory nearly every page list contains a boundary, so the leak happens on practically every failed IOTLB update.
The pending range is non-empty exactly when a page has been pinned, which last_pfn already tells us, so drop nchunks and test last_pfn.
Message-ID: <[email protected]>
--- drivers/vhost/vdpa.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/vhost/vdpa.c +2 -6
@@ -1170,7 +1170,7 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v,unsigned long list_size =PAGE_SIZE /sizeof (struct page *);unsigned int gup_flags =FOLL_LONGTERM ;unsigned long npages, cur_base, map_pfn, last_pfn =0 ; -unsigned long lock_limit, sz2pin, nchunks, i; +unsigned long lock_limit, sz2pin, i;unsigned long page_offset;u64 start = iova;long pinned;
@@ -1206,7 +1206,6 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v, cur_base = uaddr &PAGE_MASK ; iova &=PAGE_MASK ; - nchunks =0 ;while (npages) { sz2pin = min_t(unsigned long, npages, list_size);
@@ -1221,7 +1220,6 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v, } goto out; } - nchunks++;if (!last_pfn) map_pfn =page_to_pfn (page_list[0 ]);
@@ -1252,7 +1250,6 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v, map_pfn = this_pfn; iova += csize; - nchunks =0 ; } last_pfn = this_pfn;
@@ -1267,7 +1264,7 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v, PFN_PHYS(map_pfn), perm, NULL); out:if (ret) { -if (nchunks) { +if (last_pfn) {unsigned long pfn;/*
@@ -1279,7 +1276,6 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v,* the corresponding unpinning will be handled by * vdpa_unmap(). */ -WARN_ON (!last_pfn);for (pfn = map_pfn; pfn <= last_pfn; pfn++)unpin_user_page (pfn_to_page (pfn)); }
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 1/3 vdpa: factor out vhost_vdpa_pa_map() and vhost_vdpa_pa_unmap()
From: Xie Yongji <[email protected]>
[ Upstream commit 22af48cf91aae5f2fd32fe811d9be1c52d7a801b ]
The upcoming patch is going to support VA mapping/unmapping. So let's factor out the logic of PA mapping/unmapping firstly to make the code more readable.
Stable-dep-of: fd1462c96baf ("vhost-vdpa: fix pinned page leak in vhost_vdpa_pa_map() error path")
--- drivers/vhost/vdpa.c | 55 ++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 20 deletions(-)
drivers/vhost/vdpa.c +35 -20
@@ -517,7 +517,7 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,return r; } -static void vhost_vdpa_iotlb_unmap (struct vhost_vdpa *v,u64 start,u64 last) +static void vhost_vdpa_pa_unmap (struct vhost_vdpa *v,u64 start,u64 last) {struct vhost_dev *dev = &v->vdev;struct vhost_iotlb *iotlb = dev->iotlb;
@@ -539,6 +539,11 @@ static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v, u64 start, u64 last) } } +static void vhost_vdpa_iotlb_unmap (struct vhost_vdpa *v,u64 start,u64 last) +{ +return vhost_vdpa_pa_unmap (v, start, last); +} +static void vhost_vdpa_iotlb_free (struct vhost_vdpa *v) {struct vhost_dev *dev = &v->vdev;
@@ -619,38 +624,28 @@ static void vhost_vdpa_unmap(struct vhost_vdpa *v, u64 iova, u64 size) } } -static int vhost_vdpa_process_iotlb_update (struct vhost_vdpa *v, -struct vhost_iotlb_msg *msg) +static int vhost_vdpa_pa_map (struct vhost_vdpa *v, +u64 iova,u64 size,u64 uaddr,u32 perm) {struct vhost_dev *dev = &v->vdev; -struct vhost_iotlb *iotlb = dev->iotlb;struct page **page_list;unsigned long list_size =PAGE_SIZE /sizeof (struct page *);unsigned int gup_flags =FOLL_LONGTERM ;unsigned long npages, cur_base, map_pfn, last_pfn =0 ;unsigned long lock_limit, sz2pin, nchunks, i; -u64 iova = msg->iova; +u64 start = iova;long pinned;int ret =0 ; -if (msg->iova < v->range.first || !msg->size || - msg->iova >U64_MAX - msg->size +1 || - msg->iova + msg->size -1 > v->range.last) -return -EINVAL ; - -if (vhost_iotlb_itree_first (iotlb, msg->iova, - msg->iova + msg->size -1 )) -return -EEXIST ; -/* Limit the use of memory for bookkeeping */ page_list = (struct page **)__get_free_page (GFP_KERNEL );if (!page_list)return -ENOMEM ; -if (msg->perm &VHOST_ACCESS_WO ) +if (perm &VHOST_ACCESS_WO ) gup_flags |=FOLL_WRITE ; - npages =PFN_UP (msg->size + (iova & ~PAGE_MASK )); + npages =PFN_UP (size + (iova & ~PAGE_MASK ));if (!npages) { ret = -EINVAL ; goto free;
@@ -664,7 +659,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, goto unlock; } - cur_base = msg->uaddr &PAGE_MASK ; + cur_base = uaddr &PAGE_MASK ; iova &=PAGE_MASK ; nchunks =0 ;
@@ -695,7 +690,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, csize =PFN_PHYS (last_pfn - map_pfn +1 ); ret =vhost_vdpa_map (v, iova, csize,PFN_PHYS (map_pfn), - msg->perm); + perm);if (ret) {/* * Unpin the pages that are left unmapped
@@ -724,7 +719,7 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,/* Pin the rest chunk */ ret =vhost_vdpa_map (v, iova,PFN_PHYS (last_pfn - map_pfn +1 ), -PFN_PHYS (map_pfn), msg->perm); +PFN_PHYS (map_pfn), perm); out:if (ret) {if (nchunks) {
@@ -743,13 +738,33 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,for (pfn = map_pfn; pfn <= last_pfn; pfn++)unpin_user_page (pfn_to_page (pfn)); } -vhost_vdpa_unmap (v, msg->iova, msg->size); +vhost_vdpa_unmap (v, start, size); } unlock:mmap_read_unlock (dev->mm); free:free_page ((unsigned long )page_list);return ret; + +} + +static int vhost_vdpa_process_iotlb_update (struct vhost_vdpa *v, +struct vhost_iotlb_msg *msg) +{ +struct vhost_dev *dev = &v->vdev; +struct vhost_iotlb *iotlb = dev->iotlb; + +if (msg->iova < v->range.first || !msg->size || + msg->iova >U64_MAX - msg->size +1 || + msg->iova + msg->size -1 > v->range.last) +return -EINVAL ; + +if (vhost_iotlb_itree_first (iotlb, msg->iova, + msg->iova + msg->size -1 )) +return -EEXIST ; + +return vhost_vdpa_pa_map (v, msg->iova, msg->size, msg->uaddr, + msg->perm); } static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev,
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/3 vhost/vdpa: reject overflowing PA map page counts on 32-bit
From: Yousef Alhouseen <[email protected]>
[ Upstream commit 0619aaa34c0c2a2dcb07f0e9c8a34e7efb8c4cdf ]
vhost_vdpa_pa_map() adds the IOVA page offset to the user-controlled map size before computing the number of pages to pin. On 32-bit systems, where unsigned long is narrower than u64, that addition can overflow and the code can pin and map fewer pages than the requested IOTLB range.
Reject sizes that overflow the unsigned long page-count calculation.
Message-ID: <CAMuQ4bX-iDvcUOPPY+NLz95tkRJYwWqvzAr=U48uNaub_HZLGw@mail.gmail.com>
Stable-dep-of: fd1462c96baf ("vhost-vdpa: fix pinned page leak in vhost_vdpa_pa_map() error path")
--- drivers/vhost/vdpa.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/vhost/vdpa.c +8 -1
@@ -633,6 +633,7 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v,unsigned int gup_flags =FOLL_LONGTERM ;unsigned long npages, cur_base, map_pfn, last_pfn =0 ;unsigned long lock_limit, sz2pin, nchunks, i; +unsigned long page_offset;u64 start = iova;long pinned;int ret =0 ;
@@ -645,7 +646,13 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v,if (perm &VHOST_ACCESS_WO ) gup_flags |=FOLL_WRITE ; - npages =PFN_UP (size + (iova & ~PAGE_MASK )); + page_offset = iova & ~PAGE_MASK ; +if (size >ULONG_MAX - page_offset) { + ret = -EINVAL ; + goto free; + } + + npages =PFN_UP (size + page_offset);if (!npages) { ret = -EINVAL ; goto free;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/3 vhost-vdpa: fix pinned page leak in vhost_vdpa_pa_map() error path
In reply to Sasha LevinFrom: Tal Zussman <[email protected]>
[ Upstream commit fd1462c96bafe2cb14e88c837b270f8990b717b5 ]
When pin_user_pages() or vhost_vdpa_map() fails, vhost_vdpa_pa_map() is supposed to unpin the pages pinned after the last successfully mapped chunk, i.e. the range [map_pfn, last_pfn]. The unpin is guarded by nchunks, but nchunks is reset to 0 every time a chunk is mapped from inside the current page list, so a failure after any such boundary leaves that pending range pinned forever. With physically scattered memory nearly every page list contains a boundary, so the leak happens on practically every failed IOTLB update.
The pending range is non-empty exactly when a page has been pinned, which last_pfn already tells us, so drop nchunks and test last_pfn.
Message-ID: <[email protected]>
--- drivers/vhost/vdpa.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/vhost/vdpa.c +2 -6
@@ -632,7 +632,7 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v,unsigned long list_size =PAGE_SIZE /sizeof (struct page *);unsigned int gup_flags =FOLL_LONGTERM ;unsigned long npages, cur_base, map_pfn, last_pfn =0 ; -unsigned long lock_limit, sz2pin, nchunks, i; +unsigned long lock_limit, sz2pin, i;unsigned long page_offset;u64 start = iova;long pinned;
@@ -668,7 +668,6 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v, cur_base = uaddr &PAGE_MASK ; iova &=PAGE_MASK ; - nchunks =0 ;while (npages) { sz2pin = min_t(unsigned long, npages, list_size);
@@ -683,7 +682,6 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v, } goto out; } - nchunks++;if (!last_pfn) map_pfn =page_to_pfn (page_list[0 ]);
@@ -714,7 +712,6 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v, map_pfn = this_pfn; iova += csize; - nchunks =0 ; } last_pfn = this_pfn;
@@ -729,7 +726,7 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v, PFN_PHYS(map_pfn), perm); out:if (ret) { -if (nchunks) { +if (last_pfn) {unsigned long pfn;/*
@@ -741,7 +738,6 @@ static int vhost_vdpa_pa_map(struct vhost_vdpa *v,* the corresponding unpinning will be handled by * vdpa_unmap(). */ -WARN_ON (!last_pfn);for (pfn = map_pfn; pfn <= last_pfn; pfn++)unpin_user_page (pfn_to_page (pfn)); }
Signature · 1 line
2.53.0