Skip to contentSkip to conversation

drivers/virt/fsl_hypervisor: Correcting error handling path

PATCH Superseded Souptick Joarder 2 participants 4 messages May 13, 2020 – May 26, 2020

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers/virt/fsl_hypervisor: Correcting error handling path

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

Second, if get_user_pages_fast() failed we should unpinned num_pinned pages, no point of checking till num_pages.

This will address both.

---
 drivers/virt/fsl_hypervisor.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/virt/fsl_hypervisor.c +2 -2
index 1b0b11b..ea344d7 100644
@@ -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() */
 	phys_addr_t remote_paddr; /* The next address in the remote buffer */
 	uint32_t count; /* The number of bytes left to copy */
 
@@ -293,7 +293,7 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)
 
 exit:
 	if (pages) {
-		for (i = 0; i < num_pages; i++)
+		for (i = 0; i < num_pinned; i++)
 			if (pages[i])
 				put_page(pages[i]);
 	}
Signature · 1 line
1.9.1
#1 lore raw
Quoted 8 lines · Souptick Joarder

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

Second, if get_user_pages_fast() failed we should unpinned num_pinned pages, no point of checking till num_pages.

This will address both.

Any comment on this patch ?

Quoted 31 lines
---
 drivers/virt/fsl_hypervisor.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/virt/fsl_hypervisor.c +2 -3
index 1b0b11b..ea344d7 100644
@@ -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() */
        phys_addr_t remote_paddr; /* The next address in the remote buffer */
        uint32_t count; /* The number of bytes left to copy */
 
@@ -293,7 +293,7 @@ static long ioctl_memcpy(struct fsl_hv_ioctl_memcpy __user *p)
 
 exit:
        if (pages) {
-               for (i = 0; i < num_pages; i++)
+               for (i = 0; i < num_pinned; i++)
                        if (pages[i])
                                put_page(pages[i]);
        }
--

1.9.1

Reviewed-by on v1 1/1
#2 lore raw
In reply to Souptick Joarder
Quoted 9 lines · Souptick Joarder

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

Second, if get_user_pages_fast() failed we should unpinned num_pinned pages, no point of checking till num_pages.

This will address both.

If gup_flags were | FOLL_LONGTERM then this patch would fix a double free because of the put_page() in __gup_longterm_locked().

mm/gup.c
1786 if (check_dax_vmas(vmas_tmp, rc)) {
1787 for (i = 0; i < rc; i++)

  1788                                  put_page(pages[i]);
                                        ^^^^^^^^^^^^^^^^^^^

put_page() here and also in the caller.

  1789                          rc = -EOPNOTSUPP;
  1790                          goto out;
  1791                  }

But since this isn't FOLL_LONGTERM the patch is a nice cleanup which doesn't affect run time.

regards,
dan carpenter

#3 lore raw
Quoted 22 lines · Dan Carpenter
Quoted 9 lines · Souptick Joarder

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

Second, if get_user_pages_fast() failed we should unpinned num_pinned pages, no point of checking till num_pages.

This will address both.

If gup_flags were | FOLL_LONGTERM then this patch would fix a double free because of the put_page() in __gup_longterm_locked().

mm/gup.c
1786 if (check_dax_vmas(vmas_tmp, rc)) {
1787 for (i = 0; i < rc; i++)

  1788                                  put_page(pages[i]);
                                        ^^^^^^^^^^^^^^^^^^^

put_page() here and also in the caller.

Quoted 8 lines
  1789                          rc = -EOPNOTSUPP;
  1790                          goto out;
  1791                  }

But since this isn't FOLL_LONGTERM the patch is a nice cleanup which doesn't affect run time.

Hi Andrew,
Is it fine to take it through mm tree ?