Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drivers/virt/fsl_hypervisor: Fix error handling path

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: 6db7199407ca ("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>

authored by

Souptick Joarder and committed by
Greg Kroah-Hartman
7f360bec 9f30eb29

+8 -9
+8 -9
drivers/virt/fsl_hypervisor.c
··· 157 157 158 158 unsigned int i; 159 159 long ret = 0; 160 - int num_pinned; /* return value from get_user_pages() */ 160 + int num_pinned = 0; /* return value from get_user_pages_fast() */ 161 161 phys_addr_t remote_paddr; /* The next address in the remote buffer */ 162 162 uint32_t count; /* The number of bytes left to copy */ 163 163 ··· 174 174 return -EINVAL; 175 175 176 176 /* 177 - * The array of pages returned by get_user_pages() covers only 177 + * The array of pages returned by get_user_pages_fast() covers only 178 178 * page-aligned memory. Since the user buffer is probably not 179 179 * page-aligned, we need to handle the discrepancy. 180 180 * ··· 224 224 225 225 /* 226 226 * 'pages' is an array of struct page pointers that's initialized by 227 - * get_user_pages(). 227 + * get_user_pages_fast(). 228 228 */ 229 229 pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL); 230 230 if (!pages) { ··· 241 241 if (!sg_list_unaligned) { 242 242 pr_debug("fsl-hv: could not allocate S/G list\n"); 243 243 ret = -ENOMEM; 244 - goto exit; 244 + goto free_pages; 245 245 } 246 246 sg_list = PTR_ALIGN(sg_list_unaligned, sizeof(struct fh_sg_list)); 247 247 ··· 250 250 num_pages, param.source != -1 ? FOLL_WRITE : 0, pages); 251 251 252 252 if (num_pinned != num_pages) { 253 - /* get_user_pages() failed */ 254 253 pr_debug("fsl-hv: could not lock source buffer\n"); 255 254 ret = (num_pinned < 0) ? num_pinned : -EFAULT; 256 255 goto exit; ··· 291 292 virt_to_phys(sg_list), num_pages); 292 293 293 294 exit: 294 - if (pages) { 295 - for (i = 0; i < num_pages; i++) 296 - if (pages[i]) 297 - put_page(pages[i]); 295 + if (pages && (num_pinned > 0)) { 296 + for (i = 0; i < num_pinned; i++) 297 + put_page(pages[i]); 298 298 } 299 299 300 300 kfree(sg_list_unaligned); 301 + free_pages: 301 302 kfree(pages); 302 303 303 304 if (!ret)