drivers/virt/fsl_hypervisor: Fix error handling path
[ Upstream commit5.4-rM2-2.2.x-imx-squashed7f360bec37
] 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:6db7199407
("drivers/virt: introduce Freescale hypervisor management driver") Signed-off-by: Souptick Joarder <jrdr.linux@gmail.com> Reviewed-by: Dan Carpenter <dan.carpenter@oracle.com> Reviewed-by: John Hubbard <jhubbard@nvidia.com> Link: https://lore.kernel.org/r/1598995271-6755-1-git-send-email-jrdr.linux@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
parent
bf12e769ff
commit
be700c52ae
|
@ -157,7 +157,7 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)
|
||||||
|
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
long ret = 0;
|
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 */
|
phys_addr_t remote_paddr; /* The next address in the remote buffer */
|
||||||
uint32_t count; /* The number of bytes left to copy */
|
uint32_t count; /* The number of bytes left to copy */
|
||||||
|
|
||||||
|
@ -174,7 +174,7 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)
|
||||||
return -EINVAL;
|
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 memory. Since the user buffer is probably not
|
||||||
* page-aligned, we need to handle the discrepancy.
|
* page-aligned, we need to handle the discrepancy.
|
||||||
*
|
*
|
||||||
|
@ -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
|
* 'pages' is an array of struct page pointers that's initialized by
|
||||||
* get_user_pages().
|
* get_user_pages_fast().
|
||||||
*/
|
*/
|
||||||
pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
|
pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL);
|
||||||
if (!pages) {
|
if (!pages) {
|
||||||
|
@ -241,7 +241,7 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)
|
||||||
if (!sg_list_unaligned) {
|
if (!sg_list_unaligned) {
|
||||||
pr_debug("fsl-hv: could not allocate S/G list\n");
|
pr_debug("fsl-hv: could not allocate S/G list\n");
|
||||||
ret = -ENOMEM;
|
ret = -ENOMEM;
|
||||||
goto exit;
|
goto free_pages;
|
||||||
}
|
}
|
||||||
sg_list = PTR_ALIGN(sg_list_unaligned, sizeof(struct fh_sg_list));
|
sg_list = PTR_ALIGN(sg_list_unaligned, sizeof(struct fh_sg_list));
|
||||||
|
|
||||||
|
@ -250,7 +250,6 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)
|
||||||
num_pages, param.source != -1 ? FOLL_WRITE : 0, pages);
|
num_pages, param.source != -1 ? FOLL_WRITE : 0, pages);
|
||||||
|
|
||||||
if (num_pinned != num_pages) {
|
if (num_pinned != num_pages) {
|
||||||
/* get_user_pages() failed */
|
|
||||||
pr_debug("fsl-hv: could not lock source buffer\n");
|
pr_debug("fsl-hv: could not lock source buffer\n");
|
||||||
ret = (num_pinned < 0) ? num_pinned : -EFAULT;
|
ret = (num_pinned < 0) ? num_pinned : -EFAULT;
|
||||||
goto exit;
|
goto exit;
|
||||||
|
@ -292,13 +291,13 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)
|
||||||
virt_to_phys(sg_list), num_pages);
|
virt_to_phys(sg_list), num_pages);
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
if (pages) {
|
if (pages && (num_pinned > 0)) {
|
||||||
for (i = 0; i < num_pages; i++)
|
for (i = 0; i < num_pinned; i++)
|
||||||
if (pages[i])
|
put_page(pages[i]);
|
||||||
put_page(pages[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
kfree(sg_list_unaligned);
|
kfree(sg_list_unaligned);
|
||||||
|
free_pages:
|
||||||
kfree(pages);
|
kfree(pages);
|
||||||
|
|
||||||
if (!ret)
|
if (!ret)
|
||||||
|
|
Loading…
Reference in New Issue