vhost-vdpa: fix page pinning leakage in error path (rework)
Subsystems (2)
PATCH v2 1/1 vhost-vdpa: fix page pinning leakage in error path (rework)
Pinned pages are not properly accounted particularly when mapping error occurs on IOTLB update. Clean up dangling pinned pages for the error path.
The memory usage for bookkeeping pinned pages is reverted to what it was before: only one single free page is needed. This helps reduce the host memory demand for VM with a large amount of memory, or in the situation where host is running short of free memory.
---
Changes in v2:
- Drop the reversion patch
- Fix unhandled page leak towards the end of page_list
drivers/vhost/vdpa.c | 79 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 18 deletions(-)
drivers/vhost/vdpa.c +61 -18
@@ -560,6 +560,8 @@ static int vhost_vdpa_map(struct vhost_vdpa *v,if (r)vhost_iotlb_del_range (dev->iotlb, iova, iova + size -1 ); +else +atomic64_add (size >>PAGE_SHIFT , &dev->mm->pinned_vm);return r; }
@@ -591,14 +593,16 @@ static int vhost_vdpa_process_iotlb_update(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 locked, lock_limit, pinned, i; +unsigned long lock_limit, sz2pin, nchunks, i;u64 iova = msg->iova; +long pinned;int ret =0 ;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 ;
@@ -607,52 +611,75 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, gup_flags |=FOLL_WRITE ; npages =PAGE_ALIGN (msg->size + (iova & ~PAGE_MASK )) >>PAGE_SHIFT ; -if (!npages) -return -EINVAL ; +if (!npages) { + ret = -EINVAL ; + goto free; + }mmap_read_lock (dev->mm); - locked =atomic64_add_return (npages, &dev->mm->pinned_vm); lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; - -if (locked > lock_limit) { +if (npages +atomic64_read (&dev->mm->pinned_vm) > lock_limit) { ret = -ENOMEM ; - goto out; + goto unlock; } cur_base = msg->uaddr &PAGE_MASK ; iova &=PAGE_MASK ; + nchunks =0 ;while (npages) { - pinned = min_t(unsigned long, npages, list_size); - ret =pin_user_pages (cur_base, pinned, - gup_flags, page_list,NULL ); -if (ret != pinned) + sz2pin = min_t(unsigned long, npages, list_size); + pinned =pin_user_pages (cur_base, sz2pin, + gup_flags, page_list,NULL ); +if (sz2pin != pinned) { +if (pinned <0 ) { + ret = pinned; + }else { +unpin_user_pages (page_list, pinned); + ret = -ENOMEM ; + } goto out; + } + nchunks++;if (!last_pfn) map_pfn =page_to_pfn (page_list[0 ]); -for (i =0 ; i < ret; i++) { +for (i =0 ; i < pinned; i++) {unsigned long this_pfn =page_to_pfn (page_list[i]);u64 csize;if (last_pfn && (this_pfn != last_pfn +1 )) {/* Pin a contiguous chunk of memory */ csize = (last_pfn - map_pfn +1 ) <<PAGE_SHIFT ; -if (vhost_vdpa_map (v, iova, csize, - map_pfn <<PAGE_SHIFT , - msg->perm)) + ret =vhost_vdpa_map (v, iova, csize, + map_pfn <<PAGE_SHIFT , + msg->perm); +if (ret) { +/* +* Unpin the pages that are left unmapped +* from this point on in the current +* page_list. The remaining outstanding +* ones which may stride across several +* chunks will be covered in the common +* error path subsequently. +*/ +unpin_user_pages (&page_list[i], + pinned - i); goto out; + } + map_pfn = this_pfn; iova += csize; + nchunks =0 ; } last_pfn = this_pfn; } - cur_base += ret <<PAGE_SHIFT ; - npages -= ret; + cur_base += pinned <<PAGE_SHIFT ; + npages -= pinned; }/* Pin the rest chunk */
@@ -660,10 +687,26 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, map_pfn << PAGE_SHIFT, msg->perm); out:if (ret) { +if (nchunks && last_pfn) { +unsigned long pfn; + +/* +* Unpin the outstanding pages which are yet to be +* mapped but haven't due to vdpa_map() or +* pin_user_pages() failure. +* +* Mapped pages are accounted in vdpa_map(), hence +* the corresponding unpinning will be handled by +* vdpa_unmap(). +*/ +for (pfn = map_pfn; pfn <= last_pfn; pfn++) +unpin_user_page (pfn_to_page (pfn)); + }vhost_vdpa_unmap (v, msg->iova, msg->size); -atomic64_sub (npages, &dev->mm->pinned_vm); } +unlock:mmap_read_unlock (dev->mm); +free:free_page ((unsigned long )page_list);return ret; }
Signature · 6 lines
1.8.3.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 115 lines · Si-Wei Liu
Pinned pages are not properly accounted particularly when mapping error occurs on IOTLB update. Clean up dangling pinned pages for the error path.
The memory usage for bookkeeping pinned pages is reverted to what it was before: only one single free page is needed. This helps reduce the host memory demand for VM with a large amount of memory, or in the situation where host is running short of free memory.
Fixes 4c8cf31885f6 ("vhost: introduce vDPA-based backend")Signed-off-by Si-Wei Liu <[email protected]>---
Changes in v2:
- Drop the reversion patch
- Fix unhandled page leak towards the end ofpage_listdrivers/vhost/vdpa.c | 79 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 18 deletions(-)drivers/vhost/vdpa.c +29
-15@@ -560,6 +560,8 @@ static int vhost_vdpa_map(struct vhost_vdpa *v,if (r)vhost_iotlb_del_range (dev->iotlb, iova, iova + size -1 ); +else +atomic64_add (size >>PAGE_SHIFT , &dev->mm->pinned_vm);return r; }@@ -591,14 +593,16 @@ static int vhost_vdpa_process_iotlb_update(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 locked, lock_limit, pinned, i; +unsigned long lock_limit, sz2pin, nchunks, i;u64 iova = msg->iova; +long pinned;int ret =0 ;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 ;@@ -607,52 +611,75 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, gup_flags |=FOLL_WRITE ; npages =PAGE_ALIGN (msg->size + (iova & ~PAGE_MASK )) >>PAGE_SHIFT ; -if (!npages) -return -EINVAL ; +if (!npages) { + ret = -EINVAL ; + goto free; + }mmap_read_lock (dev->mm); - locked =atomic64_add_return (npages, &dev->mm->pinned_vm); lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; - -if (locked > lock_limit) { +if (npages +atomic64_read (&dev->mm->pinned_vm) > lock_limit) { ret = -ENOMEM ; - goto out; + goto unlock; } cur_base = msg->uaddr &PAGE_MASK ; iova &=PAGE_MASK ; + nchunks =0 ;while (npages) { - pinned = min_t(unsigned long, npages, list_size); - ret =pin_user_pages (cur_base, pinned, - gup_flags, page_list,NULL ); -if (ret != pinned) + sz2pin = min_t(unsigned long, npages, list_size); + pinned =pin_user_pages (cur_base, sz2pin, + gup_flags, page_list,NULL ); +if (sz2pin != pinned) { +if (pinned <0 ) { + ret = pinned; + }else { +unpin_user_pages (page_list, pinned); + ret = -ENOMEM ; + } goto out; + } + nchunks++;if (!last_pfn) map_pfn =page_to_pfn (page_list[0 ]); -for (i =0 ; i < ret; i++) { +for (i =0 ; i < pinned; i++) {unsigned long this_pfn =page_to_pfn (page_list[i]);u64 csize;if (last_pfn && (this_pfn != last_pfn +1 )) {/* Pin a contiguous chunk of memory */ csize = (last_pfn - map_pfn +1 ) <<PAGE_SHIFT ; -if (vhost_vdpa_map (v, iova, csize, - map_pfn <<PAGE_SHIFT , - msg->perm)) + ret =vhost_vdpa_map (v, iova, csize, + map_pfn <<PAGE_SHIFT , + msg->perm); +if (ret) {
drivers/vhost/vdpa.c static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, · line 669
Quoted 10 lines
drivers/vhost/vdpa.c +10
-0+/* +* Unpin the pages that are left unmapped +* from this point on in the current +* page_list. The remaining outstanding +* ones which may stride across several +* chunks will be covered in the common +* error path subsequently. +*/ +unpin_user_pages (&page_list[i], + pinned - i);
Can we simply do last_pfn = this_pfn here?
Quoted 19 lines
drivers/vhost/vdpa.c +5
-2goto out; + } + map_pfn = this_pfn; iova += csize; + nchunks =0 ; } last_pfn = this_pfn; } - cur_base += ret <<PAGE_SHIFT ; - npages -= ret; + cur_base += pinned <<PAGE_SHIFT ; + npages -= pinned; }/* Pin the rest chunk */
drivers/vhost/vdpa.c static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, · line 690
Quoted 4 lines
drivers/vhost/vdpa.c +1
-0@@ -660,10 +687,26 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, map_pfn << PAGE_SHIFT, msg->perm); out:if (ret) { +if (nchunks && last_pfn) {
Any reason for checking last_pfn here?
Note that we did:
+ nchunks++;
if (!last_pfn)
map_pfn = page_to_pfn(page_list[0]);
Thanks
Quoted 23 lines
drivers/vhost/vdpa.c +16
-1+unsigned long pfn; + +/* +* Unpin the outstanding pages which are yet to be +* mapped but haven't due to vdpa_map() or +* pin_user_pages() failure. +* +* Mapped pages are accounted in vdpa_map(), hence +* the corresponding unpinning will be handled by +* vdpa_unmap(). +*/ +for (pfn = map_pfn; pfn <= last_pfn; pfn++) +unpin_user_page (pfn_to_page (pfn)); + }vhost_vdpa_unmap (v, msg->iova, msg->size); -atomic64_sub (npages, &dev->mm->pinned_vm); } +unlock:mmap_read_unlock (dev->mm); +free:free_page ((unsigned long )page_list);return ret; }
Quoted 114 lines · Jason Wang
Quoted 112 lines · Si-Wei Liu
Pinned pages are not properly accounted particularly when mapping error occurs on IOTLB update. Clean up dangling pinned pages for the error path.
The memory usage for bookkeeping pinned pages is reverted to what it was before: only one single free page is needed. This helps reduce the host memory demand for VM with a large amount of memory, or in the situation where host is running short of free memory.
Fixes 4c8cf31885f6 ("vhost: introduce vDPA-based backend")Signed-off-by Si-Wei Liu <[email protected]>---
Changes in v2:
- Drop the reversion patch
- Fix unhandled page leak towards the end ofpage_listdrivers/vhost/vdpa.c | 79 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 18 deletions(-)drivers/vhost/vdpa.c +33
-13@@ -560,6 +560,8 @@ static int vhost_vdpa_map(struct vhost_vdpa *v,if (r)vhost_iotlb_del_range (dev->iotlb, iova, iova + size -1 ); +else +atomic64_add (size >>PAGE_SHIFT , &dev->mm->pinned_vm);return r; }@@ -591,14 +593,16 @@ static int vhost_vdpa_process_iotlb_update(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 locked, lock_limit, pinned, i; +unsigned long lock_limit, sz2pin, nchunks, i;u64 iova = msg->iova; +long pinned;int ret =0 ;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 ;@@ -607,52 +611,75 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, gup_flags |= FOLL_WRITE; npages =PAGE_ALIGN (msg->size + (iova & ~PAGE_MASK )) >>PAGE_SHIFT ; -if (!npages) -return -EINVAL ; +if (!npages) { + ret = -EINVAL ; + goto free; + }mmap_read_lock (dev->mm); - locked =atomic64_add_return (npages, &dev->mm->pinned_vm); lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; - -if (locked > lock_limit) { +if (npages +atomic64_read (&dev->mm->pinned_vm) > lock_limit) { ret = -ENOMEM ; - goto out; + goto unlock; } cur_base = msg->uaddr &PAGE_MASK ; iova &=PAGE_MASK ; + nchunks =0 ;while (npages) { - pinned = min_t(unsigned long, npages, list_size); - ret =pin_user_pages (cur_base, pinned, - gup_flags, page_list,NULL ); -if (ret != pinned) + sz2pin = min_t(unsigned long, npages, list_size); + pinned =pin_user_pages (cur_base, sz2pin, + gup_flags, page_list,NULL ); +if (sz2pin != pinned) { +if (pinned <0 ) { + ret = pinned; + }else { +unpin_user_pages (page_list, pinned); + ret = -ENOMEM ; + } goto out; + } + nchunks++;if (!last_pfn) map_pfn =page_to_pfn (page_list[0 ]); - for (i = 0; i < ret; i++) { +for (i =0 ; i < pinned; i++) {unsigned long this_pfn =page_to_pfn (page_list[i]);u64 csize;if (last_pfn && (this_pfn != last_pfn +1 )) {/* Pin a contiguous chunk of memory */ csize = (last_pfn - map_pfn +1 ) <<PAGE_SHIFT ; -if (vhost_vdpa_map (v, iova, csize, - map_pfn <<PAGE_SHIFT , - msg->perm)) + ret =vhost_vdpa_map (v, iova, csize, + map_pfn <<PAGE_SHIFT , + msg->perm); +if (ret) { +/* +* Unpin the pages that are left unmapped +* from this point on in the current +* page_list. The remaining outstanding +* ones which may stride across several
Quoted 8 lines
drivers/vhost/vdpa.c static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, · line 669
Quoted 5 lines
drivers/vhost/vdpa.c +5
-0+* chunks will be covered in the common +* error path subsequently. +*/ +unpin_user_pages (&page_list[i], + pinned - i);Can we simply do
last_pfn=this_pfnhere?
Nope. They are not contiguous segments of memory. Noted the conditional (this_pfn != last_pfn + 1) being held here.
Quoted 27 lines
Quoted 18 lines
drivers/vhost/vdpa.c +5
-1goto out; + } + map_pfn = this_pfn; iova += csize; + nchunks =0 ; } last_pfn = this_pfn; } - cur_base += ret <<PAGE_SHIFT ; - npages -= ret; + cur_base += pinned <<PAGE_SHIFT ; + npages -= pinned; }/* Pin the rest chunk */ drivers/vhost/vdpa.c static int · line 691
Quoted 5 lines
drivers/vhost/vdpa.c +1
-0@@ -660,10 +687,26 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, map_pfn << PAGE_SHIFT, msg->perm); out:if (ret) { +if (nchunks && last_pfn) {Any reason for checking
last_pfnhere?
Quoted 6 lines
Note that we did:
+ nchunks++;
if (!last_pfn) map_pfn = page_to_pfn(page_list[0]);
It's for explicit coding to make sure this common error path can be reused no matter if last_pfn has a sane value assigned or not. I can change it to an implicit WARN_ON() if need be.
Thanks,
-Siwei
Quoted 29 lines
Thanks
Quoted 23 lines
drivers/vhost/vdpa.c +16
-1+unsigned long pfn; + +/* +* Unpin the outstanding pages which are yet to be +* mapped but haven't due to vdpa_map() or +* pin_user_pages() failure. +* +* Mapped pages are accounted in vdpa_map(), hence +* the corresponding unpinning will be handled by +* vdpa_unmap(). +*/ +for (pfn = map_pfn; pfn <= last_pfn; pfn++) +unpin_user_page (pfn_to_page (pfn)); + }vhost_vdpa_unmap (v, msg->iova, msg->size); -atomic64_sub (npages, &dev->mm->pinned_vm); } +unlock:mmap_read_unlock (dev->mm); +free:free_page ((unsigned long )page_list);return ret;}
Quoted 123 lines · si-wei liu
Quoted 113 lines · Jason Wang
Quoted 111 lines · Si-Wei Liu
Pinned pages are not properly accounted particularly when mapping error occurs on IOTLB update. Clean up dangling pinned pages for the error path.
The memory usage for bookkeeping pinned pages is reverted to what it was before: only one single free page is needed. This helps reduce the host memory demand for VM with a large amount of memory, or in the situation where host is running short of free memory.
Fixes 4c8cf31885f6 ("vhost: introduce vDPA-based backend")Signed-off-by Si-Wei Liu <[email protected]>---
Changes in v2:
- Drop the reversion patch
- Fix unhandled page leak towards the end ofpage_list
drivers/vhost/vdpa.c| 79
++++++++++++++++++++++++++++++++++++++++------------1 file changed, 61 insertions(+), 18 deletions(-)drivers/vhost/vdpa.c +4
-1@@ -560,6 +560,8 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, if (r) vhost_iotlb_del_range(dev->iotlb, iova, iova + size - 1); + else + atomic64_add(size >> PAGE_SHIFT, &dev->mm->pinned_vm); return r; }@@ -591,14 +593,16 @@ static int vhost_vdpa_process_iotlb_update(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 locked, lock_limit, pinned, i; + unsigned long lock_limit, sz2pin, nchunks, i; u64 iova = msg->iova; + long pinned; int ret = 0; 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;drivers/vhost/vdpa.c +29
-12@@ -607,52 +611,75 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, gup_flags |= FOLL_WRITE; npages = PAGE_ALIGN(msg->size + (iova & ~PAGE_MASK)) >> PAGE_SHIFT; - if (!npages) - return -EINVAL; + if (!npages) { + ret = -EINVAL; + goto free; + } mmap_read_lock(dev->mm); - locked = atomic64_add_return(npages, &dev->mm->pinned_vm); lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - - if (locked > lock_limit) { + if (npages + atomic64_read(&dev->mm->pinned_vm) > lock_limit) { ret = -ENOMEM; - goto out; + goto unlock; } cur_base = msg->uaddr & PAGE_MASK; iova &= PAGE_MASK; + nchunks = 0; while (npages) { - pinned = min_t(unsigned long, npages, list_size); - ret = pin_user_pages(cur_base, pinned, - gup_flags, page_list, NULL); - if (ret != pinned) + sz2pin = min_t(unsigned long, npages, list_size); + pinned = pin_user_pages(cur_base, sz2pin, + gup_flags, page_list, NULL); + if (sz2pin != pinned) { + if (pinned < 0) { + ret = pinned; + } else { + unpin_user_pages(page_list, pinned); + ret = -ENOMEM; + } goto out; + } + nchunks++; if (!last_pfn) map_pfn = page_to_pfn(page_list[0]); - for (i = 0; i < ret; i++) { + for (i = 0; i < pinned; i++) { unsigned long this_pfn = page_to_pfn(page_list[i]); u64 csize; if (last_pfn && (this_pfn != last_pfn + 1)) { /* Pin a contiguous chunk of memory */ csize = (last_pfn - map_pfn + 1) << PAGE_SHIFT; - if (vhost_vdpa_map(v, iova, csize, - map_pfn << PAGE_SHIFT, - msg->perm)) + ret = vhost_vdpa_map(v, iova, csize, + map_pfn << PAGE_SHIFT, + msg->perm); + if (ret) { + /* +* Unpin the pages that are left unmapped +* from this point on in the current +* page_list. The remaining outstanding +* ones which may stride across several Quoted 8 lines
drivers/vhost/vdpa.c static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, · line 669
Quoted 5 lines
drivers/vhost/vdpa.c +5
-0+* chunks will be covered in the common +* error path subsequently. +*/ + unpin_user_pages(&page_list[i], + pinned - i);Can we simply do
last_pfn=this_pfnhere?
Quoted 1 line
Nope. They are not contiguous segments of memory. Noted the conditional (
this_pfn!=last_pfn+ 1) being held here.
Right.
Quoted 28 lines
Quoted 28 lines
Quoted 21 lines
drivers/vhost/vdpa.c +5
-1goto out; + } + map_pfn = this_pfn; iova += csize; + nchunks = 0; } last_pfn = this_pfn; } - cur_base += ret << PAGE_SHIFT; - npages -= ret; + cur_base += pinned << PAGE_SHIFT; + npages -= pinned; } /* Pin the rest chunk */@@ -660,10 +687,26 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, map_pfn << PAGE_SHIFT, msg->perm);Quoted 3 lines
out:
if (ret) {
+ if (nchunks &&last_pfn) {Any reason for checking
last_pfnhere?
Quoted 7 lines
Quoted 6 lines
Note that we did:
+ nchunks++;
if (!last_pfn)
map_pfn=page_to_pfn(page_list[0]);It's for explicit coding to make sure this common error path can be reused no matter if
last_pfnhas a sane value assigned or not. I can change it to an implicitWARN_ON()if need be.
Just to make sure I understand. A question, when will we get nchunks != 0 but last_pfn == 0?
Thanks
Quoted 33 lines
Thanks,
-SiweiQuoted 29 lines
Thanks
Quoted 23 lines
+ unsigned long pfn;
+
+ /*
+ * Unpin the outstanding pages which are yet to be + * mapped but haven't due tovdpa_map()or + *pin_user_pages()failure.
+ *
+ * Mapped pages are accounted invdpa_map(), hence + * the corresponding unpinning will be handled by + *vdpa_unmap().
+ */
+ for (pfn =map_pfn; pfn <=last_pfn; pfn++) + unpin_user_page(pfn_to_page(pfn));
+ }
vhost_vdpa_unmap(v, msg->iova, msg->size); -atomic64_sub(npages, &dev->mm->pinned_vm);
}
+unlock:
mmap_read_unlock(dev->mm);
+free:
free_page((unsigned long)page_list);
return ret;
}
Quoted 162 lines · Jason Wang
Quoted 124 lines · si-wei liu
Quoted 114 lines · Jason Wang
Quoted 112 lines · Si-Wei Liu
Pinned pages are not properly accounted particularly when mapping error occurs on IOTLB update. Clean up dangling pinned pages for the error path.
The memory usage for bookkeeping pinned pages is reverted to what it was before: only one single free page is needed. This helps reduce the host memory demand for VM with a large amount of memory, or in the situation where host is running short of free memory.
Fixes 4c8cf31885f6 ("vhost: introduce vDPA-based backend")Signed-off-by Si-Wei Liu <[email protected]>---
Changes in v2:
- Drop the reversion patch
- Fix unhandled page leak towards the end ofpage_listdrivers/vhost/vdpa.c | 79 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 18 deletions(-)drivers/vhost/vdpa.c +33
-13@@ -560,6 +560,8 @@ static int vhost_vdpa_map(struct vhost_vdpa *v,if (r)vhost_iotlb_del_range (dev->iotlb, iova, iova + size -1 ); +else +atomic64_add (size >>PAGE_SHIFT , &dev->mm->pinned_vm);return r; }@@ -591,14 +593,16 @@ static int vhost_vdpa_process_iotlb_update(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 locked, lock_limit, pinned, i; +unsigned long lock_limit, sz2pin, nchunks, i;u64 iova = msg->iova; +long pinned;int ret =0 ;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 ;@@ -607,52 +611,75 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, gup_flags |= FOLL_WRITE; npages =PAGE_ALIGN (msg->size + (iova & ~PAGE_MASK )) >>PAGE_SHIFT ; -if (!npages) -return -EINVAL ; +if (!npages) { + ret = -EINVAL ; + goto free; + }mmap_read_lock (dev->mm); - locked =atomic64_add_return (npages, &dev->mm->pinned_vm); lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; - -if (locked > lock_limit) { +if (npages +atomic64_read (&dev->mm->pinned_vm) > lock_limit) { ret = -ENOMEM ; - goto out; + goto unlock; } cur_base = msg->uaddr &PAGE_MASK ; iova &=PAGE_MASK ; + nchunks =0 ;while (npages) { - pinned = min_t(unsigned long, npages, list_size); - ret =pin_user_pages (cur_base, pinned, - gup_flags, page_list,NULL ); -if (ret != pinned) + sz2pin = min_t(unsigned long, npages, list_size); + pinned =pin_user_pages (cur_base, sz2pin, + gup_flags, page_list,NULL ); +if (sz2pin != pinned) { +if (pinned <0 ) { + ret = pinned; + }else { +unpin_user_pages (page_list, pinned); + ret = -ENOMEM ; + } goto out; + } + nchunks++;if (!last_pfn) map_pfn =page_to_pfn (page_list[0 ]); - for (i = 0; i < ret; i++) { +for (i =0 ; i < pinned; i++) {unsigned long this_pfn =page_to_pfn (page_list[i]);u64 csize;if (last_pfn && (this_pfn != last_pfn +1 )) {/* Pin a contiguous chunk of memory */ csize = (last_pfn - map_pfn +1 ) <<PAGE_SHIFT ; -if (vhost_vdpa_map (v, iova, csize, - map_pfn <<PAGE_SHIFT , - msg->perm)) + ret =vhost_vdpa_map (v, iova, csize, + map_pfn <<PAGE_SHIFT , + msg->perm); +if (ret) { +/* +* Unpin the pages that are left unmapped +* from this point on in the current +* page_list. The remaining outstanding +* ones which may stride across several Quoted 8 lines
drivers/vhost/vdpa.c static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, · line 669
Quoted 5 lines
drivers/vhost/vdpa.c +5
-0+* chunks will be covered in the common +* error path subsequently. +*/ +unpin_user_pages (&page_list[i], + pinned - i);Can we simply do
last_pfn=this_pfnhere?Quoted 1 line
Nope. They are not contiguous segments of memory. Noted the conditional (
this_pfn!=last_pfn+ 1) being held here.Right.
Quoted 30 lines
Quoted 30 lines
Quoted 19 lines
drivers/vhost/vdpa.c +5
-1goto out; + } + map_pfn = this_pfn; iova += csize; + nchunks =0 ; } last_pfn = this_pfn; } - cur_base += ret <<PAGE_SHIFT ; - npages -= ret; + cur_base += pinned <<PAGE_SHIFT ; + npages -= pinned; }/* Pin the rest chunk */ drivers/vhost/vdpa.c static int · line 691
Quoted 5 lines
drivers/vhost/vdpa.c +1
-0@@ -660,10 +687,26 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, map_pfn << PAGE_SHIFT, msg->perm); out:if (ret) { +if (nchunks && last_pfn) {Any reason for checking
last_pfnhere?Note that we did:
Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
+ nchunks++;
if (!last_pfn) map_pfn = page_to_pfn(page_list[0]);It's for explicit coding to make sure this common error path can be reused no matter if
last_pfnhas a sane value assigned or not. I can change it to an implicitWARN_ON()if need be.Just to make sure I understand. A question, when will we get nchunks != 0 but
last_pfn== 0?
The current code has implicit assumption that nchunks != 0 infers last_pfn != 0. However, this assumption could break subject to code structure changes for eg. failure may occur after the increment of nchunks and before the for loop. I feel it'd be the best to capture this assumption with something explicit.
-Siwei
Quoted 38 lines
Thanks
Quoted 33 lines
Thanks,
-SiweiQuoted 29 lines
Thanks
Quoted 23 lines
drivers/vhost/vdpa.c +16
-1+unsigned long pfn; + +/* +* Unpin the outstanding pages which are yet to be +* mapped but haven't due to vdpa_map() or +* pin_user_pages() failure. +* +* Mapped pages are accounted in vdpa_map(), hence +* the corresponding unpinning will be handled by +* vdpa_unmap(). +*/ +for (pfn = map_pfn; pfn <= last_pfn; pfn++) +unpin_user_page (pfn_to_page (pfn)); + }vhost_vdpa_unmap (v, msg->iova, msg->size); -atomic64_sub (npages, &dev->mm->pinned_vm); } +unlock:mmap_read_unlock (dev->mm); +free:free_page ((unsigned long )page_list);return ret;}
Michael S. Tsirkin
maintainerQuoted 176 lines · si-wei liu
Quoted 167 lines · Jason Wang
Quoted 123 lines · si-wei liu
Quoted 121 lines · Jason Wang
Quoted 117 lines · Si-Wei Liu
Pinned pages are not properly accounted particularly when mapping error occurs on IOTLB update. Clean up dangling pinned pages for the error path.
The memory usage for bookkeeping pinned pages is reverted to what it was before: only one single free page is needed. This helps reduce the host memory demand for VM with a large amount of memory, or in the situation where host is running short of free memory.
Fixes 4c8cf31885f6 ("vhost: introduce vDPA-based backend")Signed-off-by Si-Wei Liu <[email protected]>---
Changes in v2:
- Drop the reversion patch
- Fix unhandled page leak towards the end ofpage_listdrivers/vhost/vdpa.c | 79 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 18 deletions(-)drivers/vhost/vdpa.c +38
-13@@ -560,6 +560,8 @@ static int vhost_vdpa_map(struct vhost_vdpa *v,if (r)vhost_iotlb_del_range (dev->iotlb, iova, iova + size -1 ); +else +atomic64_add (size >>PAGE_SHIFT , &dev->mm->pinned_vm);return r; }@@ -591,14 +593,16 @@ static int vhost_vdpa_process_iotlb_update(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 locked, lock_limit, pinned, i; +unsigned long lock_limit, sz2pin, nchunks, i;u64 iova = msg->iova; +long pinned;int ret =0 ;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 ;@@ -607,52 +611,75 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, gup_flags |= FOLL_WRITE; npages =PAGE_ALIGN (msg->size + (iova & ~PAGE_MASK )) >>PAGE_SHIFT ; -if (!npages) -return -EINVAL ; +if (!npages) { + ret = -EINVAL ; + goto free; + }mmap_read_lock (dev->mm); - locked =atomic64_add_return (npages, &dev->mm->pinned_vm); lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; - -if (locked > lock_limit) { +if (npages +atomic64_read (&dev->mm->pinned_vm) > lock_limit) { ret = -ENOMEM ; - goto out; + goto unlock; } cur_base = msg->uaddr &PAGE_MASK ; iova &=PAGE_MASK ; + nchunks =0 ;while (npages) { - pinned = min_t(unsigned long, npages, list_size); - ret =pin_user_pages (cur_base, pinned, - gup_flags, page_list,NULL ); -if (ret != pinned) + sz2pin = min_t(unsigned long, npages, list_size); + pinned =pin_user_pages (cur_base, sz2pin, + gup_flags, page_list,NULL ); +if (sz2pin != pinned) { +if (pinned <0 ) { + ret = pinned; + }else { +unpin_user_pages (page_list, pinned); + ret = -ENOMEM ; + } goto out; + } + nchunks++;if (!last_pfn) map_pfn =page_to_pfn (page_list[0 ]); - for (i = 0; i < ret; i++) { +for (i =0 ; i < pinned; i++) {unsigned long this_pfn =page_to_pfn (page_list[i]);u64 csize;if (last_pfn && (this_pfn != last_pfn +1 )) {/* Pin a contiguous chunk of memory */ csize = (last_pfn - map_pfn +1 ) <<PAGE_SHIFT ; -if (vhost_vdpa_map (v, iova, csize, - map_pfn <<PAGE_SHIFT , - msg->perm)) + ret =vhost_vdpa_map (v, iova, csize, + map_pfn <<PAGE_SHIFT , + msg->perm); +if (ret) { +/* +* Unpin the pages that are left unmapped +* from this point on in the current +* page_list. The remaining outstanding +* ones which may stride across several +* chunks will be covered in the common +* error path subsequently. +*/ +unpin_user_pages (&page_list[i], + pinned - i);Quoted 3 lines
Quoted 1 line
Can we simply do
last_pfn=this_pfnhere?Nope. They are not contiguous segments of memory. Noted the conditional (
this_pfn!=last_pfn+ 1) being held here.Right.
Quoted 34 lines
Quoted 34 lines
Quoted 19 lines
drivers/vhost/vdpa.c +5
-1goto out; + } + map_pfn = this_pfn; iova += csize; + nchunks =0 ; } last_pfn = this_pfn; } - cur_base += ret <<PAGE_SHIFT ; - npages -= ret; + cur_base += pinned <<PAGE_SHIFT ; + npages -= pinned; }/* Pin the rest chunk */ drivers/vhost/vdpa.c static int · line 691
Quoted 5 lines
drivers/vhost/vdpa.c +1
-0@@ -660,10 +687,26 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, map_pfn << PAGE_SHIFT, msg->perm); out:if (ret) { +if (nchunks && last_pfn) {Any reason for checking
last_pfnhere?Note that we did:
+ nchunks++;
if (!last_pfn) map_pfn = page_to_pfn(page_list[0]);Quoted 7 lines
Quoted 3 lines
It's for explicit coding to make sure this common error path can be reused no matter if
last_pfnhas a sane value assigned or not. I can change it to an implicitWARN_ON()if need be.Just to make sure I understand. A question, when will we get nchunks != 0 but
last_pfn== 0?
Quoted 7 lines
The current code has implicit assumption that nchunks != 0 infers
last_pfn!= 0. However, this assumption could break subject to code structure changes for eg. failure may occur after the increment of nchunks and before the for loop. I feel it'd be the best to capture this assumption with something explicit.-Siwei
if here isn't really an explicit way to document assumptions, it's a way to avoid assumptions :)
A way to document assumptions is probably BUG_ON.
Quoted 38 lines
Quoted 38 lines
Thanks
Quoted 33 lines
Thanks,
-SiweiQuoted 29 lines
Thanks
Quoted 23 lines
drivers/vhost/vdpa.c +16
-1+unsigned long pfn; + +/* +* Unpin the outstanding pages which are yet to be +* mapped but haven't due to vdpa_map() or +* pin_user_pages() failure. +* +* Mapped pages are accounted in vdpa_map(), hence +* the corresponding unpinning will be handled by +* vdpa_unmap(). +*/ +for (pfn = map_pfn; pfn <= last_pfn; pfn++) +unpin_user_page (pfn_to_page (pfn)); + }vhost_vdpa_unmap (v, msg->iova, msg->size); -atomic64_sub (npages, &dev->mm->pinned_vm); } +unlock:mmap_read_unlock (dev->mm); +free:free_page ((unsigned long )page_list);return ret;}
Quoted 173 lines · Michael S. Tsirkin
Quoted 172 lines · si-wei liu
Quoted 157 lines · Jason Wang
Quoted 120 lines · si-wei liu
Quoted 119 lines · Jason Wang
Quoted 117 lines · Si-Wei Liu
Pinned pages are not properly accounted particularly when mapping error occurs on IOTLB update. Clean up dangling pinned pages for the error path.
The memory usage for bookkeeping pinned pages is reverted to what it was before: only one single free page is needed. This helps reduce the host memory demand for VM with a large amount of memory, or in the situation where host is running short of free memory.
Fixes 4c8cf31885f6 ("vhost: introduce vDPA-based backend")Signed-off-by Si-Wei Liu <[email protected]>---
Changes in v2:
- Drop the reversion patch
- Fix unhandled page leak towards the end ofpage_listdrivers/vhost/vdpa.c | 79 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 18 deletions(-)drivers/vhost/vdpa.c +38
-13@@ -560,6 +560,8 @@ static int vhost_vdpa_map(struct vhost_vdpa *v,if (r)vhost_iotlb_del_range (dev->iotlb, iova, iova + size -1 ); +else +atomic64_add (size >>PAGE_SHIFT , &dev->mm->pinned_vm);return r; }@@ -591,14 +593,16 @@ static int vhost_vdpa_process_iotlb_update(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 locked, lock_limit, pinned, i; +unsigned long lock_limit, sz2pin, nchunks, i;u64 iova = msg->iova; +long pinned;int ret =0 ;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 ;@@ -607,52 +611,75 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, gup_flags |= FOLL_WRITE; npages =PAGE_ALIGN (msg->size + (iova & ~PAGE_MASK )) >>PAGE_SHIFT ; -if (!npages) -return -EINVAL ; +if (!npages) { + ret = -EINVAL ; + goto free; + }mmap_read_lock (dev->mm); - locked =atomic64_add_return (npages, &dev->mm->pinned_vm); lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; - -if (locked > lock_limit) { +if (npages +atomic64_read (&dev->mm->pinned_vm) > lock_limit) { ret = -ENOMEM ; - goto out; + goto unlock; } cur_base = msg->uaddr &PAGE_MASK ; iova &=PAGE_MASK ; + nchunks =0 ;while (npages) { - pinned = min_t(unsigned long, npages, list_size); - ret =pin_user_pages (cur_base, pinned, - gup_flags, page_list,NULL ); -if (ret != pinned) + sz2pin = min_t(unsigned long, npages, list_size); + pinned =pin_user_pages (cur_base, sz2pin, + gup_flags, page_list,NULL ); +if (sz2pin != pinned) { +if (pinned <0 ) { + ret = pinned; + }else { +unpin_user_pages (page_list, pinned); + ret = -ENOMEM ; + } goto out; + } + nchunks++;if (!last_pfn) map_pfn =page_to_pfn (page_list[0 ]); - for (i = 0; i < ret; i++) { +for (i =0 ; i < pinned; i++) {unsigned long this_pfn =page_to_pfn (page_list[i]);u64 csize;if (last_pfn && (this_pfn != last_pfn +1 )) {/* Pin a contiguous chunk of memory */ csize = (last_pfn - map_pfn +1 ) <<PAGE_SHIFT ; -if (vhost_vdpa_map (v, iova, csize, - map_pfn <<PAGE_SHIFT , - msg->perm)) + ret =vhost_vdpa_map (v, iova, csize, + map_pfn <<PAGE_SHIFT , + msg->perm); +if (ret) { +/* +* Unpin the pages that are left unmapped +* from this point on in the current +* page_list. The remaining outstanding +* ones which may stride across several +* chunks will be covered in the common +* error path subsequently. +*/ +unpin_user_pages (&page_list[i], + pinned - i);Quoted 3 lines
Quoted 1 line
Can we simply do
last_pfn=this_pfnhere?Nope. They are not contiguous segments of memory. Noted the conditional (
this_pfn!=last_pfn+ 1) being held here.Right.
Quoted 29 lines
Quoted 29 lines
Quoted 17 lines
drivers/vhost/vdpa.c +5
-1goto out; + } + map_pfn = this_pfn; iova += csize; + nchunks =0 ; } last_pfn = this_pfn; } - cur_base += ret <<PAGE_SHIFT ; - npages -= ret; + cur_base += pinned <<PAGE_SHIFT ; + npages -= pinned; }/* Pin the rest chunk */ drivers/vhost/vdpa.c static int · line 691
Quoted 5 lines
drivers/vhost/vdpa.c +1
-0@@ -660,10 +687,26 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, map_pfn << PAGE_SHIFT, msg->perm); out:if (ret) { +if (nchunks && last_pfn) {Any reason for checking
last_pfnhere?Note that we did:
+ nchunks++;
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
if (!last_pfn) map_pfn = page_to_pfn(page_list[0]);It's for explicit coding to make sure this common error path can be reused no matter if
last_pfnhas a sane value assigned or not. I can change it to an implicitWARN_ON()if need be.Just to make sure I understand. A question, when will we get nchunks != 0 but
last_pfn== 0?The current code has implicit assumption that nchunks != 0 infers
last_pfn!= 0. However, this assumption could break subject to code structure changes for eg. failure may occur after the increment of nchunks and before the for loop. I feel it'd be the best to capture this assumption with something explicit.
Quoted 3 lines
Quoted 1 line
-Siwei
if here isn't really an explicit way to document assumptions, it's a way to avoid assumptions :)
Agreed. I was referring to the v3 patch which had turned the defensive coding to a WARN_ON().
Quoted 1 line
A way to document assumptions is probably
BUG_ON.
If you're fine with below checkpatch warning I can definitely convert it to a BUG_ON:
WARNING: Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()
Let me know if I need to post a v4 for this nit.
Thanks
-Siwei
Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Thanks
Quoted 22 lines
Thanks,
-SiweiQuoted 19 lines
Thanks
Quoted 15 lines
drivers/vhost/vdpa.c +14
-0+unsigned long pfn; + +/* +* Unpin the outstanding pages which are yet to be +* mapped but haven't due to vdpa_map() or +* pin_user_pages() failure. +* +* Mapped pages are accounted in vdpa_map(), hence +* the corresponding unpinning will be handled by +* vdpa_unmap(). +*/ +for (pfn = map_pfn; pfn <= last_pfn; pfn++) +unpin_user_page (pfn_to_page (pfn)); + }vhost_vdpa_unmap (v, msg->iova, msg->size);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/vhost/vdpa.c +2
-1-atomic64_sub (npages, &dev->mm->pinned_vm); } +unlock:mmap_read_unlock (dev->mm); +free:free_page ((unsigned long )page_list);return ret;}
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 176 lines · si-wei liu
Quoted 174 lines · Michael S. Tsirkin
Quoted 173 lines · si-wei liu
Quoted 158 lines · Jason Wang
Quoted 121 lines · si-wei liu
Quoted 120 lines · Jason Wang
Quoted 118 lines · Si-Wei Liu
Pinned pages are not properly accounted particularly when mapping error occurs on IOTLB update. Clean up dangling pinned pages for the error path.
The memory usage for bookkeeping pinned pages is reverted to what it was before: only one single free page is needed. This helps reduce the host memory demand for VM with a large amount of memory, or in the situation where host is running short of free memory.
Fixes 4c8cf31885f6 ("vhost: introduce vDPA-based backend")Signed-off-by Si-Wei Liu <[email protected]>---
Changes in v2:
- Drop the reversion patch
- Fix unhandled page leak towards the end ofpage_list
drivers/vhost/vdpa.c| 79
++++++++++++++++++++++++++++++++++++++++------------1 file changed, 61 insertions(+), 18 deletions(-)drivers/vhost/vdpa.c +4
-1@@ -560,6 +560,8 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, if (r) vhost_iotlb_del_range(dev->iotlb, iova, iova + size - 1); + else + atomic64_add(size >> PAGE_SHIFT, &dev->mm->pinned_vm); return r; }@@ -591,14 +593,16 @@ static int vhost_vdpa_process_iotlb_update(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 locked, lock_limit, pinned, i; + unsigned long lock_limit, sz2pin, nchunks, i; u64 iova = msg->iova; + long pinned; int ret = 0; 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;drivers/vhost/vdpa.c +34
-12@@ -607,52 +611,75 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, gup_flags |= FOLL_WRITE; npages = PAGE_ALIGN(msg->size + (iova & ~PAGE_MASK)) >>PAGE_SHIFT ; - if (!npages) - return -EINVAL; + if (!npages) { + ret = -EINVAL; + goto free; + } mmap_read_lock(dev->mm); - locked = atomic64_add_return(npages, &dev->mm->pinned_vm); lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; - - if (locked > lock_limit) { + if (npages + atomic64_read(&dev->mm->pinned_vm) > lock_limit) { ret = -ENOMEM; - goto out; + goto unlock; } cur_base = msg->uaddr & PAGE_MASK; iova &= PAGE_MASK; + nchunks = 0; while (npages) { - pinned = min_t(unsigned long, npages, list_size); - ret = pin_user_pages(cur_base, pinned, - gup_flags, page_list, NULL); - if (ret != pinned) + sz2pin = min_t(unsigned long, npages, list_size); + pinned = pin_user_pages(cur_base, sz2pin, + gup_flags, page_list, NULL); + if (sz2pin != pinned) { + if (pinned < 0) { + ret = pinned; + } else { + unpin_user_pages(page_list, pinned); + ret = -ENOMEM; + } goto out; + } + nchunks++; if (!last_pfn) map_pfn = page_to_pfn(page_list[0]); - for (i = 0; i < ret; i++) { + for (i = 0; i < pinned; i++) { unsigned long this_pfn = page_to_pfn(page_list[i]); u64 csize; if (last_pfn && (this_pfn != last_pfn + 1)) { /* Pin a contiguous chunk of memory */ csize = (last_pfn - map_pfn + 1) << PAGE_SHIFT; - if (vhost_vdpa_map(v, iova, csize, - map_pfn << PAGE_SHIFT, - msg->perm)) + ret = vhost_vdpa_map(v, iova, csize, + map_pfn << PAGE_SHIFT, + msg->perm); + if (ret) { + /* +* Unpin the pages that are left unmapped +* from this point on in the current +* page_list. The remaining outstanding +* ones which may stride across several +* chunks will be covered in the common +* error path subsequently. +*/ +unpin_user_pages (&page_list[i], + pinned - i);Quoted 3 lines
Quoted 1 line
Can we simply do
last_pfn=this_pfnhere?Nope. They are not contiguous segments of memory. Noted the conditional (
this_pfn!=last_pfn+ 1) being held here.Right.
Quoted 29 lines
Quoted 29 lines
Quoted 19 lines
drivers/vhost/vdpa.c +5
-1goto out; + } + map_pfn = this_pfn; iova += csize; + nchunks = 0; } last_pfn = this_pfn; } - cur_base += ret << PAGE_SHIFT; - npages -= ret; + cur_base += pinned << PAGE_SHIFT; + npages -= pinned; } /* Pin the rest chunk */@@ -660,10 +687,26 @@ static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v, map_pfn << PAGE_SHIFT, msg->perm);Quoted 3 lines
out:
if (ret) {
+ if (nchunks &&last_pfn) {Any reason for checking
last_pfnhere?Note that we did:
+ nchunks++;
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
if (!last_pfn)
map_pfn=page_to_pfn(page_list[0]);It's for explicit coding to make sure this common error path can be reused no matter if
last_pfnhas a sane value assigned or not. I can change it to an implicitWARN_ON()if need be.Just to make sure I understand. A question, when will we get nchunks != 0 but
last_pfn== 0?The current code has implicit assumption that nchunks != 0 infers
last_pfn!= 0. However, this assumption could break subject to code structure changes for eg. failure may occur after the increment of nchunks and before the for loop. I feel it'd be the best to capture this assumption with something explicit.
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
-Siwei
if here isn't really an explicit way to document assumptions, it's a way to avoid assumptions :)
Agreed. I was referring to the v3 patch which had turned the defensive coding to a
WARN_ON().
I think I get you now.
Quoted 7 lines
Quoted 2 lines
A way to document assumptions is probably
BUG_ON.If you're fine with below checkpatch warning I can definitely convert it to a
BUG_ON:WARNING: Avoid crashing the kernel - try using
WARN_ON& recovery code rather thanBUG()orBUG_ON()Let me know if I need to post a v4 for this nit.
I will ack for V3.
Thanks
Quoted 41 lines
Thanks
-SiweiQuoted 34 lines
Quoted 34 lines
Quoted 34 lines
Thanks
Quoted 30 lines
Thanks,
-SiweiQuoted 27 lines
Thanks
Quoted 23 lines
+ unsigned long pfn;
+
+ /*
+ * Unpin the outstanding pages which are yet to be + * mapped but haven't due tovdpa_map()or + *pin_user_pages()failure.
+ *
+ * Mapped pages are accounted invdpa_map(), hence + * the corresponding unpinning will be handled by + *vdpa_unmap().
+ */
+ for (pfn =map_pfn; pfn <=last_pfn; pfn++) + unpin_user_page(pfn_to_page(pfn));
+ }
vhost_vdpa_unmap(v, msg->iova, msg->size); -atomic64_sub(npages, &dev->mm->pinned_vm);
}
+unlock:
mmap_read_unlock(dev->mm);
+free:
free_page((unsigned long)page_list);
return ret;
}