Skip to content

Commit

Permalink
drivers/virt/fsl_hypervisor: Fix error handling path
Browse files Browse the repository at this point in the history
[ Upstream commit 7f360bec37857bfd5a48cef21d86f58a09a3df63 ]

First, when memory allocation for sg_list_unaligned failed, there
is a bug of calling put_pages() as we haven't pinned any pages.

Second, if get_user_pages_fast() failed we should unpin num_pinned
pages.

This will address both.

As part of these changes, minor update in documentation.

Fixes: 6db7199 ("drivers/virt: introduce Freescale hypervisor management driver")
Signed-off-by: Souptick Joarder <[email protected]>
Reviewed-by: Dan Carpenter <[email protected]>
Reviewed-by: John Hubbard <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
  • Loading branch information
Souptick Joarder authored and gregkh committed Oct 29, 2020
1 parent 3070d0f commit 4a9f8c3
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions drivers/virt/fsl_hypervisor.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)

unsigned int i;
long ret = 0;
int num_pinned; /* return value from get_user_pages() */
int num_pinned = 0; /* return value from get_user_pages_fast() */
phys_addr_t remote_paddr; /* The next address in the remote buffer */
uint32_t count; /* The number of bytes left to copy */

Expand All @@ -174,7 +174,7 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)
return -EINVAL;

/*
* The array of pages returned by get_user_pages() covers only
* The array of pages returned by get_user_pages_fast() covers only
* page-aligned memory. Since the user buffer is probably not
* page-aligned, we need to handle the discrepancy.
*
Expand Down Expand Up @@ -224,7 +224,7 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)

/*
* 'pages' is an array of struct page pointers that's initialized by
* get_user_pages().
* get_user_pages_fast().
*/
pages = kzalloc(num_pages * sizeof(struct page *), GFP_KERNEL);
if (!pages) {
Expand All @@ -241,7 +241,7 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)
if (!sg_list_unaligned) {
pr_debug("fsl-hv: could not allocate S/G list\n");
ret = -ENOMEM;
goto exit;
goto free_pages;
}
sg_list = PTR_ALIGN(sg_list_unaligned, sizeof(struct fh_sg_list));

Expand All @@ -253,7 +253,6 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)
up_read(&current->mm->mmap_sem);

if (num_pinned != num_pages) {
/* get_user_pages() failed */
pr_debug("fsl-hv: could not lock source buffer\n");
ret = (num_pinned < 0) ? num_pinned : -EFAULT;
goto exit;
Expand Down Expand Up @@ -295,13 +294,13 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)
virt_to_phys(sg_list), num_pages);

exit:
if (pages) {
for (i = 0; i < num_pages; i++)
if (pages[i])
put_page(pages[i]);
if (pages && (num_pinned > 0)) {
for (i = 0; i < num_pinned; i++)
put_page(pages[i]);
}

kfree(sg_list_unaligned);
free_pages:
kfree(pages);

if (!ret)
Expand Down

0 comments on commit 4a9f8c3

Please sign in to comment.