From nobody Fri Jan 13 15:40:21 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Ntlx16P6pz2r0tg; Fri, 13 Jan 2023 15:40:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Ntlx15wBTz4KpH; Fri, 13 Jan 2023 15:40:21 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673624421; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KfrTh1cFNCf9rzf745SVMpSNzDnRaXF/RW1U7FO06vE=; b=lZt0IG4vBSrhcgPV+ZDSUNYyq/kXq2XALSiKp1dvf7NQIYHHR2OCRKA1TwVlKWu8DLGOZH dR0rixBS1nPYl9bp40uPvy9pVN6ZU1mgxEKa0Aw8vfRAnvQrHnI0AzasGn5yr8LNfppqwQ tgRRmK8VRe3DBK0TX5grLcKFCWvjrMD1BNW1Doueg8fXjn0wFgCMTEtC6SnlaLYvuoBRwa s39ObYzSBz2G57T4G6/QcfimzTY+qKgxEd4+W+FrFZ9GtQVRUKzIP1MGdc8+uLUD5+1WRK Enm70hGDLaL0IsgNo9QrR59SEO/bRLPcGFbb+aOxR7NkZQBnPAbLSHtzlQM+0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673624421; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KfrTh1cFNCf9rzf745SVMpSNzDnRaXF/RW1U7FO06vE=; b=ga7qoYoYADd+39cc4knYFM88F/LXQteRbcoGdBJGamR7WLvGrHbvG1e86DCDuguuggthKT lqFBOONukLci1ppDExNWkN0UoY0DVLlK/qeIoqvaZBRHVx6HfeA70jXSumPgxVtGwwqXnn wOvxfq7ctojTNpNdr4cCord0I9HVqsbWrsgRZvX/NF6liz1bb6mMOvuSv0auUaYCvQV7Gs IHsM6GsMLpv7tkh3ZMXvADRz1lq4yJezhLA9MI1U8C4mLVKKNweIWvjcy6FIExymO3D1vK kCD1GQ/LxAct4MZo76/f+uyyyVutf7GBqSmP4Xx4KwkO3NKCfknMPaF711ba5w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1673624421; a=rsa-sha256; cv=none; b=gkRTZ7EUry1iTyhr4quWb/f52u5zwKi02rRd+wPs5vc7Kkj28jvh/1vRYMfVEZygEvuFT2 ej8qvYHqguNRC1TL+B0n5GFNrHRbnrxLHGzrtGd6d6ia7mrmjdCuouOQoBWJrfC9gu5Iei /bNFtNa+bKSNN70w8+XHXxG8Ca17tMRNwi0ujHf9A7XA7SlrEtVRauMcfqj8se9lYYTkxu khypdM5AYUg8qbJHOLXT2YJiXYEIoedXN2Cmzi53Z+N1OgaSIgFol5dA8rvaMy8QogGp0s nwm7Q3z0E64xKGCqtNNETI9Ie0/GzorVPuy24KK0n64IJI/rWmZhVUz/stDlKg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Ntlx14bwCz1CFf; Fri, 13 Jan 2023 15:40:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 30DFeLo4047032; Fri, 13 Jan 2023 15:40:21 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30DFeLxp047031; Fri, 13 Jan 2023 15:40:21 GMT (envelope-from git) Date: Fri, 13 Jan 2023 15:40:21 GMT Message-Id: <202301131540.30DFeLxp047031@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 568f552b0410 - main - kvmclock: Fix initialization when EARLY_AP_STARTUP is not defined List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 568f552b0410f4f72db9ce710f7803acca23f79f Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=568f552b0410f4f72db9ce710f7803acca23f79f commit 568f552b0410f4f72db9ce710f7803acca23f79f Author: Mark Johnston AuthorDate: 2023-01-13 15:01:00 +0000 Commit: Mark Johnston CommitDate: 2023-01-13 15:37:37 +0000 kvmclock: Fix initialization when EARLY_AP_STARTUP is not defined To attach to the hypervisor, kvmclock needs to write a per-CPU MSR. When EARLY_AP_STARTUP is not defined, device attach happens too early: APs are not yet spun up, so smp_rendezvous only runs the callback on the local CPU. As a result, the timecounter only gets initialized on the BSP, and then timekeeping is broken on SMP systems. Implement handling for !EARLY_AP_STARTUP kernels: keep track of the CPU on which device attach ran, and then use a SI_SUB_SMP SYSINIT to register the rest of the CPUs with the hypervisor. Reported by: Shrikanth R Kamath Reviewed by: kib, jhb (earlier versions) Sponsored by: Klara, Inc. Sponsored by: Juniper Networks, Inc. MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D37705 --- sys/dev/kvm_clock/kvm_clock.c | 46 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/sys/dev/kvm_clock/kvm_clock.c b/sys/dev/kvm_clock/kvm_clock.c index 81c617755385..e6ae03ca01c0 100644 --- a/sys/dev/kvm_clock/kvm_clock.c +++ b/sys/dev/kvm_clock/kvm_clock.c @@ -71,10 +71,14 @@ struct kvm_clock_softc { struct pvclock_vcpu_time_info *timeinfos; u_int msr_tc; u_int msr_wc; +#ifndef EARLY_AP_STARTUP + int firstcpu; +#endif }; static struct pvclock_wall_clock *kvm_clock_get_wallclock(void *arg); -static void kvm_clock_system_time_enable(struct kvm_clock_softc *sc); +static void kvm_clock_system_time_enable(struct kvm_clock_softc *sc, + const cpuset_t *cpus); static void kvm_clock_system_time_enable_pcpu(void *arg); static void kvm_clock_setup_sysctl(device_t); @@ -88,9 +92,10 @@ kvm_clock_get_wallclock(void *arg) } static void -kvm_clock_system_time_enable(struct kvm_clock_softc *sc) +kvm_clock_system_time_enable(struct kvm_clock_softc *sc, const cpuset_t *cpus) { - smp_rendezvous(NULL, kvm_clock_system_time_enable_pcpu, NULL, sc); + smp_rendezvous_cpus(*cpus, NULL, kvm_clock_system_time_enable_pcpu, + NULL, sc); } static void @@ -104,6 +109,32 @@ kvm_clock_system_time_enable_pcpu(void *arg) wrmsr(sc->msr_tc, vtophys(&(sc->timeinfos)[curcpu]) | 1); } +#ifndef EARLY_AP_STARTUP +static void +kvm_clock_init_smp(void *arg __unused) +{ + devclass_t kvm_clock_devclass; + cpuset_t cpus; + struct kvm_clock_softc *sc; + + kvm_clock_devclass = devclass_find(KVM_CLOCK_DEVNAME); + sc = devclass_get_softc(kvm_clock_devclass, 0); + if (sc == NULL || mp_ncpus == 1) + return; + + /* + * Register with the hypervisor on all CPUs except the one that + * registered in kvm_clock_attach(). + */ + cpus = all_cpus; + KASSERT(CPU_ISSET(sc->firstcpu, &cpus), + ("%s: invalid first CPU %d", __func__, sc->firstcpu)); + CPU_CLR(sc->firstcpu, &cpus); + kvm_clock_system_time_enable(sc, &cpus); +} +SYSINIT(kvm_clock, SI_SUB_SMP, SI_ORDER_ANY, kvm_clock_init_smp, NULL); +#endif + static void kvm_clock_identify(driver_t *driver, device_t parent) { @@ -150,7 +181,12 @@ kvm_clock_attach(device_t dev) /* Set up 'struct pvclock_vcpu_time_info' page(s): */ sc->timeinfos = kmem_malloc(mp_ncpus * sizeof(struct pvclock_vcpu_time_info), M_WAITOK | M_ZERO); - kvm_clock_system_time_enable(sc); +#ifdef EARLY_AP_STARTUP + kvm_clock_system_time_enable(sc, &all_cpus); +#else + sc->firstcpu = curcpu; + kvm_clock_system_time_enable_pcpu(sc); +#endif /* * Init pvclock; register KVM clock wall clock, register KVM clock @@ -192,7 +228,7 @@ kvm_clock_resume(device_t dev) * conservatively assume that the system time must be re-inited in * suspend/resume scenarios. */ - kvm_clock_system_time_enable(device_get_softc(dev)); + kvm_clock_system_time_enable(device_get_softc(dev), &all_cpus); pvclock_resume(); inittodr(time_second); return (0);