Subject: linux-next: manual merge of the hyperv tree with Linus' tree

From: Stephen Rothwell <[email-masked]>
Date: Fri, 5 Feb 2021 19:02:02 +1100

Hi all,

Today's linux-next merge of the hyperv tree got a conflict in:

  arch/x86/hyperv/hv_init.c

between commit:

  fff7b5e6ee63 ("x86/hyperv: Initialize clockevents after LAPIC is initialized")

from Linus' tree and commits:

  a06c2e7df586 ("x86/hyperv: extract partition ID from Microsoft Hypervisor if necessary")
  fa2c411b58fe ("x86/hyperv: implement an MSI domain for root partition")

from the hyperv tree.

I fixed it up (see below) and can carry the fix as necessary. This
is now fixed as far as linux-next is concerned, but any non trivial
conflicts should be mentioned to your upstream maintainer when your tree
is submitted for merging.  You may also want to consider cooperating
with the maintainer of the conflicting tree to minimise any particularly
complex conflicts.

-- 
Cheers,
Stephen Rothwell

diff --cc arch/x86/hyperv/hv_init.c
index 6375967a8244,5ad48e8033e3..000000000000
--- a/arch/x86/hyperv/hv_init.c
+++ b/arch/x86/hyperv/hv_init.c
@@@ -26,9 -26,11 +27,13 @@@
  #include <linux/cpuhotplug.h>
  #include <linux/syscore_ops.h>
  #include <clocksource/hyperv_timer.h>
+ #include <linux/highmem.h>
+ 
+ u64 hv_current_partition_id = ~0ull;
+ EXPORT_SYMBOL_GPL(hv_current_partition_id);
  
 +int hyperv_init_cpuhp;
 +
  void *hv_hypercall_pg;
  EXPORT_SYMBOL_GPL(hv_hypercall_pg);
  
@@@ -315,25 -339,24 +342,43 @@@ static struct syscore_ops hv_syscore_op
  	.resume		= hv_resume,
  };
  
 +static void (* __initdata old_setup_percpu_clockev)(void);
 +
 +static void __init hv_stimer_setup_percpu_clockev(void)
 +{
 +	/*
 +	 * Ignore any errors in setting up stimer clockevents
 +	 * as we can run with the LAPIC timer as a fallback.
 +	 */
 +	(void)hv_stimer_alloc();
 +
 +	/*
 +	 * Still register the LAPIC timer, because the direct-mode STIMER is
 +	 * not supported by old versions of Hyper-V. This also allows users
 +	 * to switch to LAPIC timer via /sys, if they want to.
 +	 */
 +	if (old_setup_percpu_clockev)
 +		old_setup_percpu_clockev();
 +}
 +
+ static void __init hv_get_partition_id(void)
+ {
+ 	struct hv_get_partition_id *output_page;
+ 	u64 status;
+ 	unsigned long flags;
+ 
+ 	local_irq_save(flags);
+ 	output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
+ 	status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
+ 	if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS) {
+ 		/* No point in proceeding if this failed */
+ 		pr_err("Failed to get partition ID: %lld\n", status);
+ 		BUG();
+ 	}
+ 	hv_current_partition_id = output_page->partition_id;
+ 	local_irq_restore(flags);
+ }
+ 
  /*
   * This function is to be invoked early in the boot sequence after the
   * hypervisor has been detected.
@@@ -408,18 -437,41 +459,45 @@@ void __init hyperv_init(void
  
  	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
  	hypercall_msr.enable = 1;
- 	hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
- 	wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+ 
+ 	if (hv_root_partition) {
+ 		struct page *pg;
+ 		void *src, *dst;
+ 
+ 		/*
+ 		 * For the root partition, the hypervisor will set up its
+ 		 * hypercall page. The hypervisor guarantees it will not show
+ 		 * up in the root's address space. The root can't change the
+ 		 * location of the hypercall page.
+ 		 *
+ 		 * Order is important here. We must enable the hypercall page
+ 		 * so it is populated with code, then copy the code to an
+ 		 * executable page.
+ 		 */
+ 		wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+ 
+ 		pg = vmalloc_to_page(hv_hypercall_pg);
+ 		dst = kmap(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);
+ 	} else {
+ 		hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
+ 		wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
+ 	}
  
  	/*
 -	 * Ignore any errors in setting up stimer clockevents
 -	 * as we can run with the LAPIC timer as a fallback.
 +	 * hyperv_init() is called before LAPIC is initialized: see
 +	 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
 +	 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
 +	 * depends on LAPIC, so hv_stimer_alloc() should be called from
 +	 * x86_init.timers.setup_percpu_clockev.
  	 */
 -	(void)hv_stimer_alloc();
 +	old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
 +	x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
  
  	hv_apic_init();
  
@@@ -427,7 -479,20 +505,22 @@@
  
  	register_syscore_ops(&hv_syscore_ops);
  
 +	hyperv_init_cpuhp = cpuhp;
++
+ 	if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
+ 		hv_get_partition_id();
+ 
+ 	BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
+ 
+ #ifdef CONFIG_PCI_MSI
+ 	/*
+ 	 * If we're running as root, we want to create our own PCI MSI domain.
+ 	 * We can't set this in hv_pci_init because that would be too late.
+ 	 */
+ 	if (hv_root_partition)
+ 		x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
+ #endif
+ 
  	return;
  
  remove_cpuhp_state: