Skip to content

Commit e398fb4

Browse files
Souptick Joarderjgross1
authored andcommitted
xen/privcmd: Corrected error handling path
Previously, if lock_pages() end up partially mapping pages, it used to return -ERRNO due to which unlock_pages() have to go through each pages[i] till *nr_pages* to validate them. This can be avoided by passing correct number of partially mapped pages & -ERRNO separately, while returning from lock_pages() due to error. With this fix unlock_pages() doesn't need to validate pages[i] till *nr_pages* for error scenario and few condition checks can be ignored. Signed-off-by: Souptick Joarder <jrdr.linux@gmail.com> Reviewed-by: Juergen Gross <jgross@suse.com> Reviewed-by: Paul Durrant <paul@xen.org> Cc: John Hubbard <jhubbard@nvidia.com> Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com> Cc: Paul Durrant <xadimgnik@gmail.com> Link: https://lore.kernel.org/r/1594525195-28345-2-git-send-email-jrdr.linux@gmail.com Signed-off-by: Juergen Gross <jgross@suse.com>
1 parent bcf8768 commit e398fb4

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

‎drivers/xen/privcmd.c‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -580,28 +580,29 @@ static long privcmd_ioctl_mmap_batch(
580580

581581
static int lock_pages(
582582
struct privcmd_dm_op_buf kbufs[], unsigned int num,
583-
struct page *pages[], unsigned int nr_pages)
583+
struct page *pages[], unsigned int nr_pages, unsigned int *pinned)
584584
{
585585
unsigned int i;
586586

587587
for (i = 0; i < num; i++) {
588588
unsigned int requested;
589-
int pinned;
589+
int page_count;
590590

591591
requested = DIV_ROUND_UP(
592592
offset_in_page(kbufs[i].uptr) + kbufs[i].size,
593593
PAGE_SIZE);
594594
if (requested > nr_pages)
595595
return -ENOSPC;
596596

597-
pinned = get_user_pages_fast(
597+
page_count = get_user_pages_fast(
598598
(unsigned long) kbufs[i].uptr,
599599
requested, FOLL_WRITE, pages);
600-
if (pinned < 0)
601-
return pinned;
600+
if (page_count < 0)
601+
return page_count;
602602

603-
nr_pages -= pinned;
604-
pages += pinned;
603+
*pinned += page_count;
604+
nr_pages -= page_count;
605+
pages += page_count;
605606
}
606607

607608
return 0;
@@ -611,13 +612,8 @@ static void unlock_pages(struct page *pages[], unsigned int nr_pages)
611612
{
612613
unsigned int i;
613614

614-
if (!pages)
615-
return;
616-
617-
for (i = 0; i < nr_pages; i++) {
618-
if (pages[i])
619-
put_page(pages[i]);
620-
}
615+
for (i = 0; i < nr_pages; i++)
616+
put_page(pages[i]);
621617
}
622618

623619
static long privcmd_ioctl_dm_op(struct file *file, void __user *udata)
@@ -630,6 +626,7 @@ static long privcmd_ioctl_dm_op(struct file *file, void __user *udata)
630626
struct xen_dm_op_buf *xbufs = NULL;
631627
unsigned int i;
632628
long rc;
629+
unsigned int pinned = 0;
633630

634631
if (copy_from_user(&kdata, udata, sizeof(kdata)))
635632
return -EFAULT;
@@ -683,9 +680,11 @@ static long privcmd_ioctl_dm_op(struct file *file, void __user *udata)
683680
goto out;
684681
}
685682

686-
rc = lock_pages(kbufs, kdata.num, pages, nr_pages);
687-
if (rc)
683+
rc = lock_pages(kbufs, kdata.num, pages, nr_pages, &pinned);
684+
if (rc < 0) {
685+
nr_pages = pinned;
688686
goto out;
687+
}
689688

690689
for (i = 0; i < kdata.num; i++) {
691690
set_xen_guest_handle(xbufs[i].h, kbufs[i].uptr);

0 commit comments

Comments
 (0)