--- /dev/null
+From 001f60e1f662a6dee1630a2915401aaf5959d479 Mon Sep 17 00:00:00 2001
+From: Joao Martins <joao.m.martins@oracle.com>
+Date: Wed, 8 Nov 2017 17:19:54 +0000
+Subject: ptp_kvm: probe for kvm guest availability
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+From: Joao Martins <joao.m.martins@oracle.com>
+
+commit 001f60e1f662a6dee1630a2915401aaf5959d479 upstream.
+
+In the event of moving pvclock_pvti_cpu0_va() definition to common
+pvclock code, this function would return a value on non KVM guests.
+Later on this would fail with a GPF on ptp_kvm_init when running on a
+Xen guest. Therefore, ptp_kvm_init() should check whether it is running
+in a KVM guest.
+
+Signed-off-by: Joao Martins <joao.m.martins@oracle.com>
+Acked-by: Radim Krčmář <rkrcmar@redhat.com>
+Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
+Signed-off-by: Juergen Gross <jgross@suse.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ drivers/ptp/ptp_kvm.c | 3 +++
+ 1 file changed, 3 insertions(+)
+
+--- a/drivers/ptp/ptp_kvm.c
++++ b/drivers/ptp/ptp_kvm.c
+@@ -178,6 +178,9 @@ static int __init ptp_kvm_init(void)
+ {
+ long ret;
+
++ if (!kvm_para_available())
++ return -ENODEV;
++
+ clock_pair_gpa = slow_virt_to_phys(&clock_pair);
+ hv_clock = pvclock_pvti_cpu0_va();
+
s390-smp-fix-calling-smp_call_ipl_cpu-from-ipl-cpu.patch
nvmet-rdma-add-unlikely-for-response-allocated-check.patch
nvmet-rdma-fix-null-dereference-under-heavy-load.patch
+usb-dwc3-gadget-clear-req-needs_extra_trb-flag-on-cleanup.patch
+xhci-fix-leaking-usb3-shared_hcd-at-xhci-removal.patch
+ptp_kvm-probe-for-kvm-guest-availability.patch
+x86-pvclock-add-setter-for-pvclock_pvti_cpu0_va.patch
+x86-xen-time-set-pvclock-flags-on-xen_time_init.patch
+x86-xen-time-setup-vcpu-0-time-info-page.patch
+x86-xen-time-output-xen-sched_clock-time-from-0.patch
+xen-fix-x86-sched_clock-interface-for-xen.patch
--- /dev/null
+From bd6742249b9ca918565e4e3abaa06665e587f4b5 Mon Sep 17 00:00:00 2001
+From: Jack Pham <jackp@codeaurora.org>
+Date: Thu, 10 Jan 2019 12:39:55 -0800
+Subject: usb: dwc3: gadget: Clear req->needs_extra_trb flag on cleanup
+
+From: Jack Pham <jackp@codeaurora.org>
+
+commit bd6742249b9ca918565e4e3abaa06665e587f4b5 upstream.
+
+OUT endpoint requests may somtimes have this flag set when
+preparing to be submitted to HW indicating that there is an
+additional TRB chained to the request for alignment purposes.
+If that request is removed before the controller can execute the
+transfer (e.g. ep_dequeue/ep_disable), the request will not go
+through the dwc3_gadget_ep_cleanup_completed_request() handler
+and will not have its needs_extra_trb flag cleared when
+dwc3_gadget_giveback() is called. This same request could be
+later requeued for a new transfer that does not require an
+extra TRB and if it is successfully completed, the cleanup
+and TRB reclamation will incorrectly process the additional TRB
+which belongs to the next request, and incorrectly advances the
+TRB dequeue pointer, thereby messing up calculation of the next
+requeust's actual/remaining count when it completes.
+
+The right thing to do here is to ensure that the flag is cleared
+before it is given back to the function driver. A good place
+to do that is in dwc3_gadget_del_and_unmap_request().
+
+Fixes: c6267a51639b ("usb: dwc3: gadget: align transfers to wMaxPacketSize")
+Cc: stable@vger.kernel.org
+Signed-off-by: Jack Pham <jackp@codeaurora.org>
+Signed-off-by: Felipe Balbi <felipe.balbi@linux.intel.com>
+[jackp: backport to <= 4.20: replaced 'needs_extra_trb' with 'unaligned'
+ and 'zero' members in patch and reworded commit text]
+Signed-off-by: Jack Pham <jackp@codeaurora.org>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ drivers/usb/dwc3/gadget.c | 2 ++
+ 1 file changed, 2 insertions(+)
+
+--- a/drivers/usb/dwc3/gadget.c
++++ b/drivers/usb/dwc3/gadget.c
+@@ -182,6 +182,8 @@ void dwc3_gadget_del_and_unmap_request(s
+ req->started = false;
+ list_del(&req->list);
+ req->remaining = 0;
++ req->unaligned = false;
++ req->zero = false;
+
+ if (req->request.status == -EINPROGRESS)
+ req->request.status = status;
--- /dev/null
+From 9f08890ab906abaf9d4c1bad8111755cbd302260 Mon Sep 17 00:00:00 2001
+From: Joao Martins <joao.m.martins@oracle.com>
+Date: Wed, 8 Nov 2017 17:19:55 +0000
+Subject: x86/pvclock: add setter for pvclock_pvti_cpu0_va
+
+From: Joao Martins <joao.m.martins@oracle.com>
+
+commit 9f08890ab906abaf9d4c1bad8111755cbd302260 upstream.
+
+Right now there is only a pvclock_pvti_cpu0_va() which is defined
+on kvmclock since:
+
+commit dac16fba6fc5
+("x86/vdso: Get pvclock data from the vvar VMA instead of the fixmap")
+
+The only user of this interface so far is kvm. This commit adds a
+setter function for the pvti page and moves pvclock_pvti_cpu0_va
+to pvclock, which is a more generic place to have it; and would
+allow other PV clocksources to use it, such as Xen.
+
+While moving pvclock_pvti_cpu0_va into pvclock, rename also this
+function to pvclock_get_pvti_cpu0_va (including its call sites)
+to be symmetric with the setter (pvclock_set_pvti_cpu0_va).
+
+Signed-off-by: Joao Martins <joao.m.martins@oracle.com>
+Acked-by: Andy Lutomirski <luto@kernel.org>
+Acked-by: Paolo Bonzini <pbonzini@redhat.com>
+Acked-by: Thomas Gleixner <tglx@linutronix.de>
+Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
+Signed-off-by: Juergen Gross <jgross@suse.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ arch/x86/entry/vdso/vma.c | 2 +-
+ arch/x86/include/asm/pvclock.h | 19 ++++++++++---------
+ arch/x86/kernel/kvmclock.c | 7 +------
+ arch/x86/kernel/pvclock.c | 14 ++++++++++++++
+ drivers/ptp/ptp_kvm.c | 2 +-
+ 5 files changed, 27 insertions(+), 17 deletions(-)
+
+--- a/arch/x86/entry/vdso/vma.c
++++ b/arch/x86/entry/vdso/vma.c
+@@ -112,7 +112,7 @@ static int vvar_fault(const struct vm_sp
+ __pa_symbol(&__vvar_page) >> PAGE_SHIFT);
+ } else if (sym_offset == image->sym_pvclock_page) {
+ struct pvclock_vsyscall_time_info *pvti =
+- pvclock_pvti_cpu0_va();
++ pvclock_get_pvti_cpu0_va();
+ if (pvti && vclock_was_used(VCLOCK_PVCLOCK)) {
+ ret = vm_insert_pfn(
+ vma,
+--- a/arch/x86/include/asm/pvclock.h
++++ b/arch/x86/include/asm/pvclock.h
+@@ -5,15 +5,6 @@
+ #include <linux/clocksource.h>
+ #include <asm/pvclock-abi.h>
+
+-#ifdef CONFIG_KVM_GUEST
+-extern struct pvclock_vsyscall_time_info *pvclock_pvti_cpu0_va(void);
+-#else
+-static inline struct pvclock_vsyscall_time_info *pvclock_pvti_cpu0_va(void)
+-{
+- return NULL;
+-}
+-#endif
+-
+ /* some helper functions for xen and kvm pv clock sources */
+ u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src);
+ u8 pvclock_read_flags(struct pvclock_vcpu_time_info *src);
+@@ -102,4 +93,14 @@ struct pvclock_vsyscall_time_info {
+
+ #define PVTI_SIZE sizeof(struct pvclock_vsyscall_time_info)
+
++#ifdef CONFIG_PARAVIRT_CLOCK
++void pvclock_set_pvti_cpu0_va(struct pvclock_vsyscall_time_info *pvti);
++struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void);
++#else
++static inline struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void)
++{
++ return NULL;
++}
++#endif
++
+ #endif /* _ASM_X86_PVCLOCK_H */
+--- a/arch/x86/kernel/kvmclock.c
++++ b/arch/x86/kernel/kvmclock.c
+@@ -47,12 +47,6 @@ early_param("no-kvmclock", parse_no_kvmc
+ static struct pvclock_vsyscall_time_info *hv_clock;
+ static struct pvclock_wall_clock wall_clock;
+
+-struct pvclock_vsyscall_time_info *pvclock_pvti_cpu0_va(void)
+-{
+- return hv_clock;
+-}
+-EXPORT_SYMBOL_GPL(pvclock_pvti_cpu0_va);
+-
+ /*
+ * The wallclock is the time of day when we booted. Since then, some time may
+ * have elapsed since the hypervisor wrote the data. So we try to account for
+@@ -335,6 +329,7 @@ int __init kvm_setup_vsyscall_timeinfo(v
+ return 1;
+ }
+
++ pvclock_set_pvti_cpu0_va(hv_clock);
+ put_cpu();
+
+ kvm_clock.archdata.vclock_mode = VCLOCK_PVCLOCK;
+--- a/arch/x86/kernel/pvclock.c
++++ b/arch/x86/kernel/pvclock.c
+@@ -25,8 +25,10 @@
+
+ #include <asm/fixmap.h>
+ #include <asm/pvclock.h>
++#include <asm/vgtod.h>
+
+ static u8 valid_flags __read_mostly = 0;
++static struct pvclock_vsyscall_time_info *pvti_cpu0_va __read_mostly;
+
+ void pvclock_set_flags(u8 flags)
+ {
+@@ -144,3 +146,15 @@ void pvclock_read_wallclock(struct pvclo
+
+ set_normalized_timespec(ts, now.tv_sec, now.tv_nsec);
+ }
++
++void pvclock_set_pvti_cpu0_va(struct pvclock_vsyscall_time_info *pvti)
++{
++ WARN_ON(vclock_was_used(VCLOCK_PVCLOCK));
++ pvti_cpu0_va = pvti;
++}
++
++struct pvclock_vsyscall_time_info *pvclock_get_pvti_cpu0_va(void)
++{
++ return pvti_cpu0_va;
++}
++EXPORT_SYMBOL_GPL(pvclock_get_pvti_cpu0_va);
+--- a/drivers/ptp/ptp_kvm.c
++++ b/drivers/ptp/ptp_kvm.c
+@@ -182,7 +182,7 @@ static int __init ptp_kvm_init(void)
+ return -ENODEV;
+
+ clock_pair_gpa = slow_virt_to_phys(&clock_pair);
+- hv_clock = pvclock_pvti_cpu0_va();
++ hv_clock = pvclock_get_pvti_cpu0_va();
+
+ if (!hv_clock)
+ return -ENODEV;
--- /dev/null
+From 38669ba205d178d2d38bfd194a196d65a44d5af2 Mon Sep 17 00:00:00 2001
+From: Pavel Tatashin <pasha.tatashin@oracle.com>
+Date: Thu, 19 Jul 2018 16:55:32 -0400
+Subject: x86/xen/time: Output xen sched_clock time from 0
+
+From: Pavel Tatashin <pasha.tatashin@oracle.com>
+
+commit 38669ba205d178d2d38bfd194a196d65a44d5af2 upstream.
+
+It is expected for sched_clock() to output data from 0, when system boots.
+
+Add an offset xen_sched_clock_offset (similarly how it is done in other
+hypervisors i.e. kvm_sched_clock_offset) to count sched_clock() from 0,
+when time is first initialized.
+
+Signed-off-by: Pavel Tatashin <pasha.tatashin@oracle.com>
+Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
+Cc: steven.sistare@oracle.com
+Cc: daniel.m.jordan@oracle.com
+Cc: linux@armlinux.org.uk
+Cc: schwidefsky@de.ibm.com
+Cc: heiko.carstens@de.ibm.com
+Cc: john.stultz@linaro.org
+Cc: sboyd@codeaurora.org
+Cc: hpa@zytor.com
+Cc: douly.fnst@cn.fujitsu.com
+Cc: peterz@infradead.org
+Cc: prarit@redhat.com
+Cc: feng.tang@intel.com
+Cc: pmladek@suse.com
+Cc: gnomes@lxorguk.ukuu.org.uk
+Cc: linux-s390@vger.kernel.org
+Cc: boris.ostrovsky@oracle.com
+Cc: jgross@suse.com
+Cc: pbonzini@redhat.com
+Link: https://lkml.kernel.org/r/20180719205545.16512-14-pasha.tatashin@oracle.com
+Signed-off-by: Juergen Gross <jgross@suse.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ arch/x86/xen/time.c | 11 ++++++++++-
+ 1 file changed, 10 insertions(+), 1 deletion(-)
+
+--- a/arch/x86/xen/time.c
++++ b/arch/x86/xen/time.c
+@@ -31,6 +31,8 @@
+ /* Xen may fire a timer up to this many ns early */
+ #define TIMER_SLOP 100000
+
++static u64 xen_sched_clock_offset __read_mostly;
++
+ /* Get the TSC speed from Xen */
+ static unsigned long xen_tsc_khz(void)
+ {
+@@ -57,6 +59,11 @@ static u64 xen_clocksource_get_cycles(st
+ return xen_clocksource_read();
+ }
+
++static u64 xen_sched_clock(void)
++{
++ return xen_clocksource_read() - xen_sched_clock_offset;
++}
++
+ static void xen_read_wallclock(struct timespec *ts)
+ {
+ struct shared_info *s = HYPERVISOR_shared_info;
+@@ -367,7 +374,7 @@ void xen_timer_resume(void)
+ }
+
+ static const struct pv_time_ops xen_time_ops __initconst = {
+- .sched_clock = xen_clocksource_read,
++ .sched_clock = xen_sched_clock,
+ .steal_clock = xen_steal_clock,
+ };
+
+@@ -505,6 +512,7 @@ static void __init xen_time_init(void)
+
+ void __ref xen_init_time_ops(void)
+ {
++ xen_sched_clock_offset = xen_clocksource_read();
+ pv_time_ops = xen_time_ops;
+
+ x86_init.timers.timer_init = xen_time_init;
+@@ -547,6 +555,7 @@ void __init xen_hvm_init_time_ops(void)
+ return;
+ }
+
++ xen_sched_clock_offset = xen_clocksource_read();
+ pv_time_ops = xen_time_ops;
+ x86_init.timers.setup_percpu_clockev = xen_time_init;
+ x86_cpuinit.setup_percpu_clockev = xen_hvm_setup_cpu_clockevents;
--- /dev/null
+From b888808093113ae7d63d213272d01fea4b8329ed Mon Sep 17 00:00:00 2001
+From: Joao Martins <joao.m.martins@oracle.com>
+Date: Wed, 8 Nov 2017 17:19:56 +0000
+Subject: x86/xen/time: set pvclock flags on xen_time_init()
+
+From: Joao Martins <joao.m.martins@oracle.com>
+
+commit b888808093113ae7d63d213272d01fea4b8329ed upstream.
+
+Specifically check for PVCLOCK_TSC_STABLE_BIT and if this bit is set,
+then set it too on pvclock flags. This allows Xen clocksource to use it
+and thus speeding up xen_clocksource_read() callers (i.e. sched_clock())
+
+Signed-off-by: Joao Martins <joao.m.martins@oracle.com>
+Reviewed-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
+Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
+Signed-off-by: Juergen Gross <jgross@suse.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ arch/x86/xen/time.c | 9 +++++++++
+ 1 file changed, 9 insertions(+)
+
+--- a/arch/x86/xen/time.c
++++ b/arch/x86/xen/time.c
+@@ -373,6 +373,7 @@ static const struct pv_time_ops xen_time
+
+ static void __init xen_time_init(void)
+ {
++ struct pvclock_vcpu_time_info *pvti;
+ int cpu = smp_processor_id();
+ struct timespec tp;
+
+@@ -396,6 +397,14 @@ static void __init xen_time_init(void)
+
+ setup_force_cpu_cap(X86_FEATURE_TSC);
+
++ /*
++ * We check ahead on the primary time info if this
++ * bit is supported hence speeding up Xen clocksource.
++ */
++ pvti = &__this_cpu_read(xen_vcpu)->time;
++ if (pvti->flags & PVCLOCK_TSC_STABLE_BIT)
++ pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
++
+ xen_setup_runstate_info(cpu);
+ xen_setup_timer(cpu);
+ xen_setup_cpu_clockevents();
--- /dev/null
+From 2229f70b5bbb025e1394b61007938a68060afbfb Mon Sep 17 00:00:00 2001
+From: Joao Martins <joao.m.martins@oracle.com>
+Date: Wed, 8 Nov 2017 17:19:57 +0000
+Subject: x86/xen/time: setup vcpu 0 time info page
+
+From: Joao Martins <joao.m.martins@oracle.com>
+
+commit 2229f70b5bbb025e1394b61007938a68060afbfb upstream.
+
+In order to support pvclock vdso on xen we need to setup the time
+info page for vcpu 0 and register the page with Xen using the
+VCPUOP_register_vcpu_time_memory_area hypercall. This hypercall
+will also forcefully update the pvti which will set some of the
+necessary flags for vdso. Afterwards we check if it supports the
+PVCLOCK_TSC_STABLE_BIT flag which is mandatory for having
+vdso/vsyscall support. And if so, it will set the cpu 0 pvti that
+will be later on used when mapping the vdso image.
+
+The xen headers are also updated to include the new hypercall for
+registering the secondary vcpu_time_info struct.
+
+Signed-off-by: Joao Martins <joao.m.martins@oracle.com>
+Reviewed-by: Juergen Gross <jgross@suse.com>
+Reviewed-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
+Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
+Signed-off-by: Juergen Gross <jgross@suse.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ arch/x86/xen/suspend.c | 4 +
+ arch/x86/xen/time.c | 90 ++++++++++++++++++++++++++++++++++++++++++-
+ arch/x86/xen/xen-ops.h | 2
+ include/xen/interface/vcpu.h | 42 ++++++++++++++++++++
+ 4 files changed, 137 insertions(+), 1 deletion(-)
+
+--- a/arch/x86/xen/suspend.c
++++ b/arch/x86/xen/suspend.c
+@@ -22,6 +22,8 @@ static DEFINE_PER_CPU(u64, spec_ctrl);
+
+ void xen_arch_pre_suspend(void)
+ {
++ xen_save_time_memory_area();
++
+ if (xen_pv_domain())
+ xen_pv_pre_suspend();
+ }
+@@ -32,6 +34,8 @@ void xen_arch_post_suspend(int cancelled
+ xen_pv_post_suspend(cancelled);
+ else
+ xen_hvm_post_suspend(cancelled);
++
++ xen_restore_time_memory_area();
+ }
+
+ static void xen_vcpu_notify_restore(void *data)
+--- a/arch/x86/xen/time.c
++++ b/arch/x86/xen/time.c
+@@ -371,6 +371,92 @@ static const struct pv_time_ops xen_time
+ .steal_clock = xen_steal_clock,
+ };
+
++static struct pvclock_vsyscall_time_info *xen_clock __read_mostly;
++
++void xen_save_time_memory_area(void)
++{
++ struct vcpu_register_time_memory_area t;
++ int ret;
++
++ if (!xen_clock)
++ return;
++
++ t.addr.v = NULL;
++
++ ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area, 0, &t);
++ if (ret != 0)
++ pr_notice("Cannot save secondary vcpu_time_info (err %d)",
++ ret);
++ else
++ clear_page(xen_clock);
++}
++
++void xen_restore_time_memory_area(void)
++{
++ struct vcpu_register_time_memory_area t;
++ int ret;
++
++ if (!xen_clock)
++ return;
++
++ t.addr.v = &xen_clock->pvti;
++
++ ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area, 0, &t);
++
++ /*
++ * We don't disable VCLOCK_PVCLOCK entirely if it fails to register the
++ * secondary time info with Xen or if we migrated to a host without the
++ * necessary flags. On both of these cases what happens is either
++ * process seeing a zeroed out pvti or seeing no PVCLOCK_TSC_STABLE_BIT
++ * bit set. Userspace checks the latter and if 0, it discards the data
++ * in pvti and fallbacks to a system call for a reliable timestamp.
++ */
++ if (ret != 0)
++ pr_notice("Cannot restore secondary vcpu_time_info (err %d)",
++ ret);
++}
++
++static void xen_setup_vsyscall_time_info(void)
++{
++ struct vcpu_register_time_memory_area t;
++ struct pvclock_vsyscall_time_info *ti;
++ int ret;
++
++ ti = (struct pvclock_vsyscall_time_info *)get_zeroed_page(GFP_KERNEL);
++ if (!ti)
++ return;
++
++ t.addr.v = &ti->pvti;
++
++ ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area, 0, &t);
++ if (ret) {
++ pr_notice("xen: VCLOCK_PVCLOCK not supported (err %d)\n", ret);
++ free_page((unsigned long)ti);
++ return;
++ }
++
++ /*
++ * If primary time info had this bit set, secondary should too since
++ * it's the same data on both just different memory regions. But we
++ * still check it in case hypervisor is buggy.
++ */
++ if (!(ti->pvti.flags & PVCLOCK_TSC_STABLE_BIT)) {
++ t.addr.v = NULL;
++ ret = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_time_memory_area,
++ 0, &t);
++ if (!ret)
++ free_page((unsigned long)ti);
++
++ pr_notice("xen: VCLOCK_PVCLOCK not supported (tsc unstable)\n");
++ return;
++ }
++
++ xen_clock = ti;
++ pvclock_set_pvti_cpu0_va(xen_clock);
++
++ xen_clocksource.archdata.vclock_mode = VCLOCK_PVCLOCK;
++}
++
+ static void __init xen_time_init(void)
+ {
+ struct pvclock_vcpu_time_info *pvti;
+@@ -402,8 +488,10 @@ static void __init xen_time_init(void)
+ * bit is supported hence speeding up Xen clocksource.
+ */
+ pvti = &__this_cpu_read(xen_vcpu)->time;
+- if (pvti->flags & PVCLOCK_TSC_STABLE_BIT)
++ if (pvti->flags & PVCLOCK_TSC_STABLE_BIT) {
+ pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
++ xen_setup_vsyscall_time_info();
++ }
+
+ xen_setup_runstate_info(cpu);
+ xen_setup_timer(cpu);
+--- a/arch/x86/xen/xen-ops.h
++++ b/arch/x86/xen/xen-ops.h
+@@ -70,6 +70,8 @@ void xen_setup_runstate_info(int cpu);
+ void xen_teardown_timer(int cpu);
+ u64 xen_clocksource_read(void);
+ void xen_setup_cpu_clockevents(void);
++void xen_save_time_memory_area(void);
++void xen_restore_time_memory_area(void);
+ void __init xen_init_time_ops(void);
+ void __init xen_hvm_init_time_ops(void);
+
+--- a/include/xen/interface/vcpu.h
++++ b/include/xen/interface/vcpu.h
+@@ -178,4 +178,46 @@ DEFINE_GUEST_HANDLE_STRUCT(vcpu_register
+
+ /* Send an NMI to the specified VCPU. @extra_arg == NULL. */
+ #define VCPUOP_send_nmi 11
++
++/*
++ * Get the physical ID information for a pinned vcpu's underlying physical
++ * processor. The physical ID informmation is architecture-specific.
++ * On x86: id[31:0]=apic_id, id[63:32]=acpi_id.
++ * This command returns -EINVAL if it is not a valid operation for this VCPU.
++ */
++#define VCPUOP_get_physid 12 /* arg == vcpu_get_physid_t */
++struct vcpu_get_physid {
++ uint64_t phys_id;
++};
++DEFINE_GUEST_HANDLE_STRUCT(vcpu_get_physid);
++#define xen_vcpu_physid_to_x86_apicid(physid) ((uint32_t)(physid))
++#define xen_vcpu_physid_to_x86_acpiid(physid) ((uint32_t)((physid) >> 32))
++
++/*
++ * Register a memory location to get a secondary copy of the vcpu time
++ * parameters. The master copy still exists as part of the vcpu shared
++ * memory area, and this secondary copy is updated whenever the master copy
++ * is updated (and using the same versioning scheme for synchronisation).
++ *
++ * The intent is that this copy may be mapped (RO) into userspace so
++ * that usermode can compute system time using the time info and the
++ * tsc. Usermode will see an array of vcpu_time_info structures, one
++ * for each vcpu, and choose the right one by an existing mechanism
++ * which allows it to get the current vcpu number (such as via a
++ * segment limit). It can then apply the normal algorithm to compute
++ * system time from the tsc.
++ *
++ * @extra_arg == pointer to vcpu_register_time_info_memory_area structure.
++ */
++#define VCPUOP_register_vcpu_time_memory_area 13
++DEFINE_GUEST_HANDLE_STRUCT(vcpu_time_info);
++struct vcpu_register_time_memory_area {
++ union {
++ GUEST_HANDLE(vcpu_time_info) h;
++ struct pvclock_vcpu_time_info *v;
++ uint64_t p;
++ } addr;
++};
++DEFINE_GUEST_HANDLE_STRUCT(vcpu_register_time_memory_area);
++
+ #endif /* __XEN_PUBLIC_VCPU_H__ */
--- /dev/null
+From 867cefb4cb1012f42cada1c7d1f35ac8dd276071 Mon Sep 17 00:00:00 2001
+From: Juergen Gross <jgross@suse.com>
+Date: Mon, 14 Jan 2019 13:44:13 +0100
+Subject: xen: Fix x86 sched_clock() interface for xen
+
+From: Juergen Gross <jgross@suse.com>
+
+commit 867cefb4cb1012f42cada1c7d1f35ac8dd276071 upstream.
+
+Commit f94c8d11699759 ("sched/clock, x86/tsc: Rework the x86 'unstable'
+sched_clock() interface") broke Xen guest time handling across
+migration:
+
+[ 187.249951] Freezing user space processes ... (elapsed 0.001 seconds) done.
+[ 187.251137] OOM killer disabled.
+[ 187.251137] Freezing remaining freezable tasks ... (elapsed 0.001 seconds) done.
+[ 187.252299] suspending xenstore...
+[ 187.266987] xen:grant_table: Grant tables using version 1 layout
+[18446743811.706476] OOM killer enabled.
+[18446743811.706478] Restarting tasks ... done.
+[18446743811.720505] Setting capacity to 16777216
+
+Fix that by setting xen_sched_clock_offset at resume time to ensure a
+monotonic clock value.
+
+[boris: replaced pr_info() with pr_info_once() in xen_callback_vector()
+ to avoid printing with incorrect timestamp during resume (as we
+ haven't re-adjusted the clock yet)]
+
+Fixes: f94c8d11699759 ("sched/clock, x86/tsc: Rework the x86 'unstable' sched_clock() interface")
+Cc: <stable@vger.kernel.org> # 4.11
+Reported-by: Hans van Kranenburg <hans.van.kranenburg@mendix.com>
+Signed-off-by: Juergen Gross <jgross@suse.com>
+Tested-by: Hans van Kranenburg <hans.van.kranenburg@mendix.com>
+Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
+Signed-off-by: Juergen Gross <jgross@suse.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+
+---
+ arch/x86/xen/time.c | 12 +++++++++---
+ drivers/xen/events/events_base.c | 2 +-
+ 2 files changed, 10 insertions(+), 4 deletions(-)
+
+--- a/arch/x86/xen/time.c
++++ b/arch/x86/xen/time.c
+@@ -361,8 +361,6 @@ void xen_timer_resume(void)
+ {
+ int cpu;
+
+- pvclock_resume();
+-
+ if (xen_clockevent != &xen_vcpuop_clockevent)
+ return;
+
+@@ -379,12 +377,15 @@ static const struct pv_time_ops xen_time
+ };
+
+ static struct pvclock_vsyscall_time_info *xen_clock __read_mostly;
++static u64 xen_clock_value_saved;
+
+ void xen_save_time_memory_area(void)
+ {
+ struct vcpu_register_time_memory_area t;
+ int ret;
+
++ xen_clock_value_saved = xen_clocksource_read() - xen_sched_clock_offset;
++
+ if (!xen_clock)
+ return;
+
+@@ -404,7 +405,7 @@ void xen_restore_time_memory_area(void)
+ int ret;
+
+ if (!xen_clock)
+- return;
++ goto out;
+
+ t.addr.v = &xen_clock->pvti;
+
+@@ -421,6 +422,11 @@ void xen_restore_time_memory_area(void)
+ if (ret != 0)
+ pr_notice("Cannot restore secondary vcpu_time_info (err %d)",
+ ret);
++
++out:
++ /* Need pvclock_resume() before using xen_clocksource_read(). */
++ pvclock_resume();
++ xen_sched_clock_offset = xen_clocksource_read() - xen_clock_value_saved;
+ }
+
+ static void xen_setup_vsyscall_time_info(void)
+--- a/drivers/xen/events/events_base.c
++++ b/drivers/xen/events/events_base.c
+@@ -1650,7 +1650,7 @@ void xen_callback_vector(void)
+ xen_have_vector_callback = 0;
+ return;
+ }
+- pr_info("Xen HVM callback vector for event delivery is enabled\n");
++ pr_info_once("Xen HVM callback vector for event delivery is enabled\n");
+ alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
+ xen_hvm_callback_vector);
+ }
--- /dev/null
+From f068090426ea8d72c408ebd42953a82a88e2282c Mon Sep 17 00:00:00 2001
+From: Mathias Nyman <mathias.nyman@linux.intel.com>
+Date: Fri, 9 Nov 2018 17:21:17 +0200
+Subject: xhci: Fix leaking USB3 shared_hcd at xhci removal
+
+From: Mathias Nyman <mathias.nyman@linux.intel.com>
+
+commit f068090426ea8d72c408ebd42953a82a88e2282c upstream.
+
+Ensure that the shared_hcd pointer is valid when calling usb_put_hcd()
+
+The shared_hcd is removed and freed in xhci by first calling
+usb_remove_hcd(xhci->shared_hcd), and later
+usb_put_hcd(xhci->shared_hcd)
+
+Afer commit fe190ed0d602 ("xhci: Do not halt the host until both HCD have
+disconnected their devices.") the shared_hcd was never properly put as
+xhci->shared_hcd was set to NULL before usb_put_hcd(xhci->shared_hcd) was
+called.
+
+shared_hcd (USB3) is removed before primary hcd (USB2).
+While removing the primary hcd we might need to handle xhci interrupts
+to cleanly remove last USB2 devices, therefore we need to set
+xhci->shared_hcd to NULL before removing the primary hcd to let xhci
+interrupt handler know shared_hcd is no longer available.
+
+xhci-plat.c, xhci-histb.c and xhci-mtk first create both their hcd's before
+adding them. so to keep the correct reverse removal order use a temporary
+shared_hcd variable for them.
+For more details see commit 4ac53087d6d4 ("usb: xhci: plat: Create both
+HCDs before adding them")
+
+Fixes: fe190ed0d602 ("xhci: Do not halt the host until both HCD have disconnected their devices.")
+Cc: Joel Stanley <joel@jms.id.au>
+Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
+Cc: Thierry Reding <treding@nvidia.com>
+Cc: Jianguo Sun <sunjianguo1@huawei.com>
+Cc: <stable@vger.kernel.org>
+Reported-by: Jack Pham <jackp@codeaurora.org>
+Tested-by: Jack Pham <jackp@codeaurora.org>
+Tested-by: Peter Chen <peter.chen@nxp.com>
+Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
+Signed-off-by: Sudip Mukherjee <sudipm.mukherjee@gmail.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ drivers/usb/host/xhci-mtk.c | 6 ++++--
+ drivers/usb/host/xhci-pci.c | 1 +
+ drivers/usb/host/xhci-plat.c | 6 ++++--
+ drivers/usb/host/xhci-tegra.c | 1 +
+ drivers/usb/host/xhci.c | 2 --
+ 5 files changed, 10 insertions(+), 6 deletions(-)
+
+--- a/drivers/usb/host/xhci-mtk.c
++++ b/drivers/usb/host/xhci-mtk.c
+@@ -724,14 +724,16 @@ static int xhci_mtk_remove(struct platfo
+ struct xhci_hcd_mtk *mtk = platform_get_drvdata(dev);
+ struct usb_hcd *hcd = mtk->hcd;
+ struct xhci_hcd *xhci = hcd_to_xhci(hcd);
++ struct usb_hcd *shared_hcd = xhci->shared_hcd;
+
+- usb_remove_hcd(xhci->shared_hcd);
++ usb_remove_hcd(shared_hcd);
++ xhci->shared_hcd = NULL;
+ xhci_mtk_phy_power_off(mtk);
+ xhci_mtk_phy_exit(mtk);
+ device_init_wakeup(&dev->dev, false);
+
+ usb_remove_hcd(hcd);
+- usb_put_hcd(xhci->shared_hcd);
++ usb_put_hcd(shared_hcd);
+ usb_put_hcd(hcd);
+ xhci_mtk_sch_exit(mtk);
+ xhci_mtk_clks_disable(mtk);
+--- a/drivers/usb/host/xhci-pci.c
++++ b/drivers/usb/host/xhci-pci.c
+@@ -370,6 +370,7 @@ static void xhci_pci_remove(struct pci_d
+ if (xhci->shared_hcd) {
+ usb_remove_hcd(xhci->shared_hcd);
+ usb_put_hcd(xhci->shared_hcd);
++ xhci->shared_hcd = NULL;
+ }
+
+ /* Workaround for spurious wakeups at shutdown with HSW */
+--- a/drivers/usb/host/xhci-plat.c
++++ b/drivers/usb/host/xhci-plat.c
+@@ -332,14 +332,16 @@ static int xhci_plat_remove(struct platf
+ struct usb_hcd *hcd = platform_get_drvdata(dev);
+ struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+ struct clk *clk = xhci->clk;
++ struct usb_hcd *shared_hcd = xhci->shared_hcd;
+
+ xhci->xhc_state |= XHCI_STATE_REMOVING;
+
+- usb_remove_hcd(xhci->shared_hcd);
++ usb_remove_hcd(shared_hcd);
++ xhci->shared_hcd = NULL;
+ usb_phy_shutdown(hcd->usb_phy);
+
+ usb_remove_hcd(hcd);
+- usb_put_hcd(xhci->shared_hcd);
++ usb_put_hcd(shared_hcd);
+
+ if (!IS_ERR(clk))
+ clk_disable_unprepare(clk);
+--- a/drivers/usb/host/xhci-tegra.c
++++ b/drivers/usb/host/xhci-tegra.c
+@@ -1178,6 +1178,7 @@ static int tegra_xusb_remove(struct plat
+
+ usb_remove_hcd(xhci->shared_hcd);
+ usb_put_hcd(xhci->shared_hcd);
++ xhci->shared_hcd = NULL;
+ usb_remove_hcd(tegra->hcd);
+ usb_put_hcd(tegra->hcd);
+
+--- a/drivers/usb/host/xhci.c
++++ b/drivers/usb/host/xhci.c
+@@ -669,8 +669,6 @@ static void xhci_stop(struct usb_hcd *hc
+
+ /* Only halt host and free memory after both hcds are removed */
+ if (!usb_hcd_is_primary_hcd(hcd)) {
+- /* usb core will free this hcd shortly, unset pointer */
+- xhci->shared_hcd = NULL;
+ mutex_unlock(&xhci->mutex);
+ return;
+ }