diff mbox series

[v3] hyperv: simplify and rename generate_guest_id

Message ID 20220923114259.2945-1-kunyu@nfschina.com (mailing list archive)
State Not Applicable
Headers show
Series [v3] hyperv: simplify and rename generate_guest_id | expand

Commit Message

Li kunyu Sept. 23, 2022, 11:42 a.m. UTC
The generate_guest_id function is more suitable for use after the
following modifications.
1. Modify the type of the guest_id variable to u64, which is compatible
with the caller.
2. Remove all parameters from the function, and write the parameter
(LINUX_VERSION_CODE) passed in by the actual call into the function
implementation.
3. Rename the function to make it clearly a Hyper-V related function,
and modify it to hv_generate_guest_id.

Signed-off-by: Li kunyu <kunyu@nfschina.com>

--------
 v2: Fix generate_guest_id to hv_generate_guest_id.
 v3: Fix [PATCH v2] asm-generic: Remove the ... to [PATCH v3] hyperv: simp
     lify ... and remove extra spaces 
---
 arch/arm64/hyperv/mshyperv.c   |  2 +-
 arch/x86/hyperv/hv_init.c      |  2 +-
 include/asm-generic/mshyperv.h | 12 +++++-------
 3 files changed, 7 insertions(+), 9 deletions(-)

Comments

Michael Kelley (LINUX) Sept. 23, 2022, 8:18 p.m. UTC | #1
From: Li kunyu <kunyu@nfschina.com> Sent: Friday, September 23, 2022 4:43 AM
> 
> The generate_guest_id function is more suitable for use after the
> following modifications.
> 1. Modify the type of the guest_id variable to u64, which is compatible
> with the caller.
> 2. Remove all parameters from the function, and write the parameter
> (LINUX_VERSION_CODE) passed in by the actual call into the function
> implementation.
> 3. Rename the function to make it clearly a Hyper-V related function,
> and modify it to hv_generate_guest_id.
> 
> Signed-off-by: Li kunyu <kunyu@nfschina.com>
> 
> --------
>  v2: Fix generate_guest_id to hv_generate_guest_id.
>  v3: Fix [PATCH v2] asm-generic: Remove the ... to [PATCH v3] hyperv: simp
>      lify ... and remove extra spaces
> ---
>  arch/arm64/hyperv/mshyperv.c   |  2 +-
>  arch/x86/hyperv/hv_init.c      |  2 +-
>  include/asm-generic/mshyperv.h | 12 +++++-------
>  3 files changed, 7 insertions(+), 9 deletions(-)
> 
> diff --git a/arch/arm64/hyperv/mshyperv.c b/arch/arm64/hyperv/mshyperv.c
> index bbbe351e9045..3863fd226e0e 100644
> --- a/arch/arm64/hyperv/mshyperv.c
> +++ b/arch/arm64/hyperv/mshyperv.c
> @@ -38,7 +38,7 @@ static int __init hyperv_init(void)
>  		return 0;
> 
>  	/* Setup the guest ID */
> -	guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
> +	guest_id = hv_generate_guest_id();
>  	hv_set_vpreg(HV_REGISTER_GUEST_OSID, guest_id);
> 
>  	/* Get the features and hints from Hyper-V */
> diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c
> index 3de6d8b53367..93770791b858 100644
> --- a/arch/x86/hyperv/hv_init.c
> +++ b/arch/x86/hyperv/hv_init.c
> @@ -426,7 +426,7 @@ void __init hyperv_init(void)
>  	 * 1. Register the guest ID
>  	 * 2. Enable the hypercall and register the hypercall page
>  	 */
> -	guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
> +	guest_id = hv_generate_guest_id();
>  	wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
> 
>  	/* Hyper-V requires to write guest os id via ghcb in SNP IVM. */
> diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h
> index c05d2ce9b6cd..7f4a23cee56f 100644
> --- a/include/asm-generic/mshyperv.h
> +++ b/include/asm-generic/mshyperv.h
> @@ -25,6 +25,7 @@
>  #include <linux/nmi.h>
>  #include <asm/ptrace.h>
>  #include <asm/hyperv-tlfs.h>
> +#include <linux/version.h>

Having added the #include of linux/version.h here, you should remove it
from arch/arm64/hyperv/mshyperv.c and arch/x86/hyperv/hv_init.c.
It shouldn't be needed any longer in those two files.

Michael

> 
>  struct ms_hyperv_info {
>  	u32 features;
> @@ -105,15 +106,12 @@ static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
>  }
> 
>  /* Generate the guest OS identifier as described in the Hyper-V TLFS */
> -static inline  __u64 generate_guest_id(__u64 d_info1, __u64 kernel_version,
> -				       __u64 d_info2)
> +static inline u64 hv_generate_guest_id(void)
>  {
> -	__u64 guest_id = 0;
> +	u64 guest_id;
> 
> -	guest_id = (((__u64)HV_LINUX_VENDOR_ID) << 48);
> -	guest_id |= (d_info1 << 48);
> -	guest_id |= (kernel_version << 16);
> -	guest_id |= d_info2;
> +	guest_id = (((u64)HV_LINUX_VENDOR_ID) << 48);
> +	guest_id |= (((u64)LINUX_VERSION_CODE) << 16);
> 
>  	return guest_id;
>  }
> --
> 2.18.2
Olaf Hering Sept. 23, 2022, 9:09 p.m. UTC | #2
Fri, 23 Sep 2022 19:42:59 +0800 Li kunyu <kunyu@nfschina.com>:

> +++ b/include/asm-generic/mshyperv.h
> +#include <linux/version.h>

A very long time ago I removed most usage of version.h AFAIR,
so that changing uname -r will dirty just a tiny amount of objects.

But, this header is always coming back, like bad weed.

Please reconsider the suggested approach.

Thanks,
Olaf
Michael Kelley (LINUX) Sept. 24, 2022, 5:31 a.m. UTC | #3
From: Olaf Hering <olaf@aepfle.de> Sent: Friday, September 23, 2022 2:09 PM
> 
> Fri, 23 Sep 2022 19:42:59 +0800 Li kunyu <kunyu@nfschina.com>:
> 
> > +++ b/include/asm-generic/mshyperv.h
> > +#include <linux/version.h>
> 
> A very long time ago I removed most usage of version.h AFAIR,
> so that changing uname -r will dirty just a tiny amount of objects.
> 
> But, this header is always coming back, like bad weed.
> 
> Please reconsider the suggested approach.

Could you elaborate?  This code is getting the Linux kernel version
to pass to the hypervisor for later diagnostic purposes.  In a large
cloud environment like Azure, it's helpful sometimes to be able to
correlate emerging fleet-wide issues to particular kernel versions.
The Hyper-V code in Linux has been using LINUX_VERSION_CODE
this way for the past 10 years, so this isn't new.

Is there a more preferred mechanism for kernel code to get its version? 

Michael
Olaf Hering Sept. 26, 2022, 7:56 a.m. UTC | #4
Sat, 24 Sep 2022 05:31:34 +0000 "Michael Kelley (LINUX)" <mikelley@microsoft.com>:

> From: Olaf Hering <olaf@aepfle.de> Sent: Friday, September 23, 2022 2:09 PM

> > A very long time ago I removed most usage of version.h AFAIR,
> Could you elaborate?

It is the cost of 'make LOCALVERSION=x' vs. 'make LOCALVERSION=y'.

Too many drivers will be recompiled for no good reason as of today.
I claim no consumer below drivers/ and sound/ has a valid usecase for version.h.
But, someone else has to take the energy and argue them out of the tree.

With the proposed change every consumer of asm-generic/mshyperv.h will be dirty,
see 'touch include/asm-generic/mshyperv.h' for the impact. Therefore I think
only the two existing c files should include this header, in case the provided
information has a true value for the consumer.


Olaf
Michael Kelley (LINUX) Sept. 27, 2022, 4:54 a.m. UTC | #5
From: Olaf Hering <olaf@aepfle.de> Sent: Monday, September 26, 2022 12:57 AM
> 
> Sat, 24 Sep 2022 05:31:34 +0000 "Michael Kelley (LINUX)" <mikelley@microsoft.com>:
> 
> > From: Olaf Hering <olaf@aepfle.de> Sent: Friday, September 23, 2022 2:09 PM
> 
> > > A very long time ago I removed most usage of version.h AFAIR,
> > Could you elaborate?
> 
> It is the cost of 'make LOCALVERSION=x' vs. 'make LOCALVERSION=y'.
> 
> Too many drivers will be recompiled for no good reason as of today.
> I claim no consumer below drivers/ and sound/ has a valid usecase for version.h.
> But, someone else has to take the energy and argue them out of the tree.
> 
> With the proposed change every consumer of asm-generic/mshyperv.h will be dirty,
> see 'touch include/asm-generic/mshyperv.h' for the impact. Therefore I think
> only the two existing c files should include this header, in case the provided
> information has a true value for the consumer.
> 

Thanks, Olaf.  That makes sense and I agree.

Li Kunyu -- that means you should "undo" part of your patch.  Keep the use
of LINUX_VERSION_CODE and the #include of <linux/version.h> in the two
.c files, and pass LINUX_VERSION_CODE as an argument to hv_generate_guest_id().
Remove the #include of <linux/version.h> from include/asm-generic/mshyperv.h.

Michael
diff mbox series

Patch

diff --git a/arch/arm64/hyperv/mshyperv.c b/arch/arm64/hyperv/mshyperv.c
index bbbe351e9045..3863fd226e0e 100644
--- a/arch/arm64/hyperv/mshyperv.c
+++ b/arch/arm64/hyperv/mshyperv.c
@@ -38,7 +38,7 @@  static int __init hyperv_init(void)
 		return 0;
 
 	/* Setup the guest ID */
-	guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
+	guest_id = hv_generate_guest_id();
 	hv_set_vpreg(HV_REGISTER_GUEST_OSID, guest_id);
 
 	/* Get the features and hints from Hyper-V */
diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c
index 3de6d8b53367..93770791b858 100644
--- a/arch/x86/hyperv/hv_init.c
+++ b/arch/x86/hyperv/hv_init.c
@@ -426,7 +426,7 @@  void __init hyperv_init(void)
 	 * 1. Register the guest ID
 	 * 2. Enable the hypercall and register the hypercall page
 	 */
-	guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
+	guest_id = hv_generate_guest_id();
 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
 
 	/* Hyper-V requires to write guest os id via ghcb in SNP IVM. */
diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h
index c05d2ce9b6cd..7f4a23cee56f 100644
--- a/include/asm-generic/mshyperv.h
+++ b/include/asm-generic/mshyperv.h
@@ -25,6 +25,7 @@ 
 #include <linux/nmi.h>
 #include <asm/ptrace.h>
 #include <asm/hyperv-tlfs.h>
+#include <linux/version.h>
 
 struct ms_hyperv_info {
 	u32 features;
@@ -105,15 +106,12 @@  static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
 }
 
 /* Generate the guest OS identifier as described in the Hyper-V TLFS */
-static inline  __u64 generate_guest_id(__u64 d_info1, __u64 kernel_version,
-				       __u64 d_info2)
+static inline u64 hv_generate_guest_id(void)
 {
-	__u64 guest_id = 0;
+	u64 guest_id;
 
-	guest_id = (((__u64)HV_LINUX_VENDOR_ID) << 48);
-	guest_id |= (d_info1 << 48);
-	guest_id |= (kernel_version << 16);
-	guest_id |= d_info2;
+	guest_id = (((u64)HV_LINUX_VENDOR_ID) << 48);
+	guest_id |= (((u64)LINUX_VERSION_CODE) << 16);
 
 	return guest_id;
 }