2022-10-18 16:43:13

by Zhao Liu

[permalink] [raw]
Subject: [PATCH] x86/hyperv: Remove BUG_ON() for kmap_local_page()

From: Zhao Liu <[email protected]>

The commit 154fb14df7a3c ("x86/hyperv: Replace kmap() with
kmap_local_page()") keeps the BUG_ON() to check if kmap_local_page()
fails.

But in fact, kmap_local_page() always returns a valid kernel address
and won't return NULL here. It will BUG on its own if it fails. [1]

So directly use memcpy_to_page() which creates local mapping to copy.

[1]: https://lore.kernel.org/lkml/YztFEyUA48et0yTt@iweiny-mobl/

Fixes: 154fb14df7a3 ("x86/hyperv: Replace kmap() with kmap_local_page()")
Suggested-by: Fabio M. De Francesco <[email protected]>
Suggested-by: Ira Weiny <[email protected]>
Signed-off-by: Zhao Liu <[email protected]>
---
arch/x86/hyperv/hv_init.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c
index 29774126e931..f66c5709324f 100644
--- a/arch/x86/hyperv/hv_init.c
+++ b/arch/x86/hyperv/hv_init.c
@@ -459,13 +459,11 @@ void __init hyperv_init(void)
wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);

pg = vmalloc_to_page(hv_hypercall_pg);
- dst = kmap_local_page(pg);
src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
MEMREMAP_WB);
- BUG_ON(!(src && dst));
- memcpy(dst, src, HV_HYP_PAGE_SIZE);
+ BUG_ON(!src);
+ memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE);
memunmap(src);
- kunmap_local(dst);
} else {
hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
--
2.34.1


2022-10-19 03:07:43

by Ira Weiny

[permalink] [raw]
Subject: Re: [PATCH] x86/hyperv: Remove BUG_ON() for kmap_local_page()

On Wed, Oct 19, 2022 at 12:21:17AM +0800, Zhao Liu wrote:
> From: Zhao Liu <[email protected]>
>
> The commit 154fb14df7a3c ("x86/hyperv: Replace kmap() with
> kmap_local_page()") keeps the BUG_ON() to check if kmap_local_page()
> fails.
>
> But in fact, kmap_local_page() always returns a valid kernel address
> and won't return NULL here. It will BUG on its own if it fails. [1]
>
> So directly use memcpy_to_page() which creates local mapping to copy.
>
> [1]: https://lore.kernel.org/lkml/YztFEyUA48et0yTt@iweiny-mobl/
>
> Fixes: 154fb14df7a3 ("x86/hyperv: Replace kmap() with kmap_local_page()")

I don't know that a fixes is required here. We are not looking to backport any
of this and the other patch was correct. This is just a follow on cleanup.

> Suggested-by: Fabio M. De Francesco <[email protected]>
> Suggested-by: Ira Weiny <[email protected]>

Code looks good. Without the fixes.

Reviewed-by: Ira Weiny <[email protected]>

> Signed-off-by: Zhao Liu <[email protected]>
> ---
> arch/x86/hyperv/hv_init.c | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c
> index 29774126e931..f66c5709324f 100644
> --- a/arch/x86/hyperv/hv_init.c
> +++ b/arch/x86/hyperv/hv_init.c
> @@ -459,13 +459,11 @@ void __init hyperv_init(void)
> wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
>
> pg = vmalloc_to_page(hv_hypercall_pg);
> - dst = kmap_local_page(pg);
> src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
> MEMREMAP_WB);
> - BUG_ON(!(src && dst));
> - memcpy(dst, src, HV_HYP_PAGE_SIZE);
> + BUG_ON(!src);
> + memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE);
> memunmap(src);
> - kunmap_local(dst);
> } else {
> hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
> wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
> --
> 2.34.1
>

2022-10-20 08:35:48

by Zhao Liu

[permalink] [raw]
Subject: Re: [PATCH] x86/hyperv: Remove BUG_ON() for kmap_local_page()

On Tue, Oct 18, 2022 at 07:58:49PM -0700, Ira Weiny wrote:
> Date: Tue, 18 Oct 2022 19:58:49 -0700
> From: Ira Weiny <[email protected]>
> Subject: Re: [PATCH] x86/hyperv: Remove BUG_ON() for kmap_local_page()
>
> On Wed, Oct 19, 2022 at 12:21:17AM +0800, Zhao Liu wrote:
> > From: Zhao Liu <[email protected]>
> >
> > The commit 154fb14df7a3c ("x86/hyperv: Replace kmap() with
> > kmap_local_page()") keeps the BUG_ON() to check if kmap_local_page()
> > fails.
> >
> > But in fact, kmap_local_page() always returns a valid kernel address
> > and won't return NULL here. It will BUG on its own if it fails. [1]
> >
> > So directly use memcpy_to_page() which creates local mapping to copy.
> >
> > [1]: https://lore.kernel.org/lkml/YztFEyUA48et0yTt@iweiny-mobl/
> >
> > Fixes: 154fb14df7a3 ("x86/hyperv: Replace kmap() with kmap_local_page()")
>
> I don't know that a fixes is required here. We are not looking to backport any
> of this and the other patch was correct. This is just a follow on cleanup.
>
> > Suggested-by: Fabio M. De Francesco <[email protected]>
> > Suggested-by: Ira Weiny <[email protected]>
>
> Code looks good. Without the fixes.
>
> Reviewed-by: Ira Weiny <[email protected]>

Thanks! Let me quickly refresh a v2 patch without "Fixes" tag.

Zhao

>
> > Signed-off-by: Zhao Liu <[email protected]>
> > ---
> > arch/x86/hyperv/hv_init.c | 6 ++----
> > 1 file changed, 2 insertions(+), 4 deletions(-)
> >
> > diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c
> > index 29774126e931..f66c5709324f 100644
> > --- a/arch/x86/hyperv/hv_init.c
> > +++ b/arch/x86/hyperv/hv_init.c
> > @@ -459,13 +459,11 @@ void __init hyperv_init(void)
> > wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
> >
> > pg = vmalloc_to_page(hv_hypercall_pg);
> > - dst = kmap_local_page(pg);
> > src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
> > MEMREMAP_WB);
> > - BUG_ON(!(src && dst));
> > - memcpy(dst, src, HV_HYP_PAGE_SIZE);
> > + BUG_ON(!src);
> > + memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE);
> > memunmap(src);
> > - kunmap_local(dst);
> > } else {
> > hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
> > wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
> > --
> > 2.34.1
> >