lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <YzRVaJA0EyfcVisW@liuwe-devbox-debian-v2>
Date:   Wed, 28 Sep 2022 14:08:40 +0000
From:   Wei Liu <wei.liu@...nel.org>
To:     Zhao Liu <zhao1.liu@...ux.intel.com>
Cc:     "K . Y . Srinivasan" <kys@...rosoft.com>,
        Haiyang Zhang <haiyangz@...rosoft.com>,
        Stephen Hemminger <sthemmin@...rosoft.com>,
        Wei Liu <wei.liu@...nel.org>, Dexuan Cui <decui@...rosoft.com>,
        Thomas Gleixner <tglx@...utronix.de>,
        Ingo Molnar <mingo@...hat.com>, Borislav Petkov <bp@...en8.de>,
        Dave Hansen <dave.hansen@...ux.intel.com>, x86@...nel.org,
        "H . Peter Anvin" <hpa@...or.com>, linux-hyperv@...r.kernel.org,
        linux-kernel@...r.kernel.org, Ira Weiny <ira.weiny@...el.com>,
        "Fabio M . De Francesco" <fmdefrancesco@...il.com>,
        Zhenyu Wang <zhenyu.z.wang@...el.com>,
        Zhao Liu <zhao1.liu@...el.com>,
        Dave Hansen <dave.hansen@...el.com>
Subject: Re: [PATCH] x86/hyperv: Replace kmap() with kmap_local_page()

On Wed, Sep 28, 2022 at 05:56:40PM +0800, Zhao Liu wrote:
> From: Zhao Liu <zhao1.liu@...el.com>
> 
> kmap() is being deprecated in favor of kmap_local_page()[1].
> 
> There are two main problems with kmap(): (1) It comes with an overhead as
> mapping space is restricted and protected by a global lock for
> synchronization and (2) it also requires global TLB invalidation when the
> kmap's pool wraps and it might block when the mapping space is fully
> utilized until a slot becomes available.
> 
> With kmap_local_page() the mappings are per thread, CPU local, can take
> page faults, and can be called from any context (including interrupts).
> It is faster than kmap() in kernels with HIGHMEM enabled. Furthermore,
> the tasks can be preempted and, when they are scheduled to run again, the
> kernel virtual addresses are restored and are still valid.
> 
> In the fuction hyperv_init() of hyperv/hv_init.c, the mapping is used in a
> single thread and is short live. So, in this case, it's safe to simply use
> kmap_local_page() to create mapping, and this avoids the wasted cost of
> kmap() for global synchronization.
> 

The kmap call in that function is not performance critical in any way,
and at this point in the initialization process I don't expect there to
be any contention, so the downside of kmap is not really a concern here.

That being said, kmap getting deprecated is a good enough reason to
switch to kmap_local_page. And I appreciate this well-written,
well-reasoned commit message.

I will apply it to hyperv-next later -- I doubt people will object to
this change, but just in case.

Thanks,
Wei.

> In addtion, the fuction hyperv_init() checks if kmap() fails by BUG_ON().
> From the original discussion[2], the BUG_ON() here is just used to
> explicitly panic NULL pointer. So still keep the BUG_ON() in place to check
> if kmap_local_page() fails. Based on this consideration, memcpy_to_page()
> is not selected here but only kmap_local_page() is used.
> 
> Therefore, replace kmap() with kmap_local_page() in hyperv/hv_init.c.
> 
> [1]: https://lore.kernel.org/all/20220813220034.806698-1-ira.weiny@intel.com
> [2]: https://lore.kernel.org/lkml/20200915103710.cqmdvzh5lys4wsqo@liuwe-devbox-debian-v2/
> 
> Suggested-by: Dave Hansen <dave.hansen@...el.com>
> Suggested-by: Ira Weiny <ira.weiny@...el.com>
> Suggested-by: Fabio M. De Francesco <fmdefrancesco@...il.com>
> Signed-off-by: Zhao Liu <zhao1.liu@...el.com>
> 
> ---
> Suggested by credits.
> 	Dave: Referred to his comments about whether kmap() can fail and the
> 	      suggestion to keep BUG_ON() in place.
> 	Ira: Referred to his task documentation and review comments about
> 	     keeping BUG_ON() for kmap_local_page().
> 	Fabio: Stole some of his boiler plate commit message.
> ---
>  arch/x86/hyperv/hv_init.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c
> index 3de6d8b53367..72fe46eb183f 100644
> --- a/arch/x86/hyperv/hv_init.c
> +++ b/arch/x86/hyperv/hv_init.c
> @@ -459,13 +459,13 @@ void __init hyperv_init(void)
>  		wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
>  
>  		pg = vmalloc_to_page(hv_hypercall_pg);
> -		dst = kmap(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);
>  		memunmap(src);
> -		kunmap(pg);
> +		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
> 

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ