From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7EB41C4338F for ; Fri, 30 Jul 2021 18:25:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5A58C60F4B for ; Fri, 30 Jul 2021 18:25:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230413AbhG3SZG (ORCPT ); Fri, 30 Jul 2021 14:25:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230184AbhG3SZE (ORCPT ); Fri, 30 Jul 2021 14:25:04 -0400 Received: from mail-lj1-x22e.google.com (mail-lj1-x22e.google.com [IPv6:2a00:1450:4864:20::22e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA304C0613C1 for ; Fri, 30 Jul 2021 11:24:58 -0700 (PDT) Received: by mail-lj1-x22e.google.com with SMTP id h11so13556413ljo.12 for ; Fri, 30 Jul 2021 11:24:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=rqMYq2CWXiNrNOrfwEzFO/KTNa6FWzj2w0cC6kBlhtc=; b=VPhZ21dZARDbHcMs08hZe0sJhMKOZMXsX2u8qhO8X6scpIJjrtbj4VWZHqI/4GigEj 1qCt/sKqN3deIP+2fzjE3nzzmsNkXbLWrrBiEDQbv/9qhkfbnK4MP+3rujkEHb43uefs Klk8/GZ07sxxhBQe4K96vYdQIMlQZKjelr+UfdlhMywAp5dQb+bkzptisPTZc55NTOrX vA2XEY9DS0n9G+3pBSEnCw7PHygN2LSM3y5Qs9Ck3BdGZnZsAXOML62gO/2iAY5L5gJJ upjTgnsf4tdR0tbZqgggxPVSsL4qpHlHttYumbNL9TmR6gqv4tNidTszuHH8uh7caf+B cD5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=rqMYq2CWXiNrNOrfwEzFO/KTNa6FWzj2w0cC6kBlhtc=; b=hGkL7rFk8TkD4RSVFNERJAuqXM0bTi2ImGKnINELs/UUX76qtw2LJ7H6BDRtL+uJ0t hmknOGjAnHr4neTvLEsGPgUUkO/A6MMcpyHTaYx2bllct+ze1kTARfmyDaOTWiBLYyON Ind2VCsTI8rVxDuuWWClzlH16ukwk9tAu0gMzW9ABSdm9f7fvDKovQTKn6f+ghdm8Icw mukbaEyR98J0IlF2tc3Cq7duqk/nIkIDhUu38hHflkhDLNC4AnMrX5XTjica4n+vglA5 sy5l3KyMWbE2YP4jjGuj9Fbf8EhgOXfOQc30Ml4nXFXaApNRERcqHH6vKscGBrBy7poV hQIQ== X-Gm-Message-State: AOAM533IksvtQ9c12jmj59P/1ylGGWH/CEs9D8AtZ6sJcat2o3GVyB55 9IT7PI3XCMbjrAmnbVDKpmsmjoemqCHbW5G/qzl6gg== X-Google-Smtp-Source: ABdhPJzdtPl6p6Ma5FhWcVM12t2+r5Hw1Cc+5/G0Nha/v+f1rF5QoRu64gmbzCNjcgoiXH4m7bT9LOCk65t0MJXdddw= X-Received: by 2002:a2e:535c:: with SMTP id t28mr2435423ljd.129.1627669496637; Fri, 30 Jul 2021 11:24:56 -0700 (PDT) MIME-Version: 1.0 References: <20210729173300.181775-1-oupton@google.com> <20210729173300.181775-2-oupton@google.com> In-Reply-To: From: Oliver Upton Date: Fri, 30 Jul 2021 11:24:45 -0700 Message-ID: Subject: Re: [PATCH v5 01/13] KVM: x86: Report host tsc and realtime values in KVM_GET_CLOCK To: Sean Christopherson Cc: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, Paolo Bonzini , Marc Zyngier , Peter Shier , Jim Mattson , David Matlack , Ricardo Koller , Jing Zhang , Raghavendra Rao Anata , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Andrew Jones Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org On Fri, Jul 30, 2021 at 10:48 AM Sean Christopherson wrote: > > On Thu, Jul 29, 2021, Oliver Upton wrote: > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > index 916c976e99ab..e052c7afaac4 100644 > > --- a/arch/x86/kvm/x86.c > > +++ b/arch/x86/kvm/x86.c > > @@ -2782,17 +2782,24 @@ static void kvm_gen_update_masterclock(struct kvm *kvm) > > #endif > > } > > > > -u64 get_kvmclock_ns(struct kvm *kvm) > > +/* > > + * Returns true if realtime and TSC values were written back to the caller. > > + * Returns false if a clock triplet cannot be obtained, such as if the host's > > + * realtime clock is not based on the TSC. > > + */ > > +static bool get_kvmclock_and_realtime(struct kvm *kvm, u64 *kvmclock_ns, > > + u64 *realtime_ns, u64 *tsc) > > { > > struct kvm_arch *ka = &kvm->arch; > > struct pvclock_vcpu_time_info hv_clock; > > unsigned long flags; > > - u64 ret; > > + bool ret = false; > > > > spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags); > > if (!ka->use_master_clock) { > > spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags); > > - return get_kvmclock_base_ns() + ka->kvmclock_offset; > > + *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset; > > + return false; > > } > > > > hv_clock.tsc_timestamp = ka->master_cycle_now; > > @@ -2803,18 +2810,36 @@ u64 get_kvmclock_ns(struct kvm *kvm) > > get_cpu(); > > > > if (__this_cpu_read(cpu_tsc_khz)) { > > + struct timespec64 ts; > > + u64 tsc_val; > > + > > kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, > > &hv_clock.tsc_shift, > > &hv_clock.tsc_to_system_mul); > > - ret = __pvclock_read_cycles(&hv_clock, rdtsc()); > > + > > + if (kvm_get_walltime_and_clockread(&ts, &tsc_val)) { > > + *realtime_ns = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; > > + *tsc = tsc_val; > > + ret = true; > > + } > > + > > + *kvmclock_ns = __pvclock_read_cycles(&hv_clock, tsc_val); > > This is buggy, as tsc_val is not initialized if kvm_get_walltime_and_clockread() > returns false. This also straight up fails to compile on 32-bit kernels because > kvm_get_walltime_and_clockread() is wrapped with CONFIG_X86_64. Pssh... works on my machine ;-) > > A gross way to resolve both issues would be (see below regarding 'data'): > > if (__this_cpu_read(cpu_tsc_khz)) { > #ifdef CONFIG_X86_64 > struct timespec64 ts; > > if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { > data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; > data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; > } else > #endif > data->host_tsc = rdtsc(); > > kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, > &hv_clock.tsc_shift, > &hv_clock.tsc_to_system_mul); > > data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc); > } else { > data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; > } > I'll mull on this if there's any cleaner way to fix it, but thanks for the suggested fix! I missed the x86_64 ifdefs first time around. > > } else > > Not your code, but this needs braces. Ack. > > > - ret = get_kvmclock_base_ns() + ka->kvmclock_offset; > > + *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset; > > > > put_cpu(); > > > > return ret; > > } > > ... > > > @@ -5820,6 +5845,68 @@ int kvm_arch_pm_notifier(struct kvm *kvm, unsigned long state) > > } > > #endif /* CONFIG_HAVE_KVM_PM_NOTIFIER */ > > > > +static int kvm_vm_ioctl_get_clock(struct kvm *kvm, > > + void __user *argp) > > +{ > > + struct kvm_clock_data data; > > + > > + memset(&data, 0, sizeof(data)); > > + > > + if (get_kvmclock_and_realtime(kvm, &data.clock, &data.realtime, > > + &data.host_tsc)) > > + data.flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; > > + > > + if (kvm->arch.use_master_clock) > > + data.flags |= KVM_CLOCK_TSC_STABLE; > > I know nothing about the actual behavior, but this appears to have a potential > race (though it came from the existing code). get_kvmclock_and_realtime() checks > arch.use_master_clock under pvclock_gtod_sync_lock, but this does not. Even if > that's functionally ok, it's a needless complication. > > Fixing that weirdness would also provide an opportunity to clean up the API, > e.g. the boolean return is not exactly straightforward. The simplest approach > would be to take "struct kvm_clock_data *data" in get_kvmclock_and_realtime() > instead of multiple params. Then that helper can set the flags accordingly, thus > avoiding the question of whether or not there's a race and eliminating the boolean > return. E.g. Yeah, agreed. I think it may be best to separate fixing the mess from the new API here. > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index e052c7afaac4..872a53a7c467 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -2782,25 +2782,20 @@ static void kvm_gen_update_masterclock(struct kvm *kvm) > #endif > } > > -/* > - * Returns true if realtime and TSC values were written back to the caller. > - * Returns false if a clock triplet cannot be obtained, such as if the host's > - * realtime clock is not based on the TSC. > - */ > -static bool get_kvmclock_and_realtime(struct kvm *kvm, u64 *kvmclock_ns, > - u64 *realtime_ns, u64 *tsc) > +static void get_kvmclock_and_realtime(struct kvm *kvm, > + struct kvm_clock_data *data) > { > struct kvm_arch *ka = &kvm->arch; > struct pvclock_vcpu_time_info hv_clock; > unsigned long flags; > - bool ret = false; > > spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags); > if (!ka->use_master_clock) { > spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags); > - *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset; > - return false; > + data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; > + return; > } > + data->flags |= KVM_CLOCK_TSC_STABLE; > > hv_clock.tsc_timestamp = ka->master_cycle_now; > hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; > @@ -2810,34 +2805,40 @@ static bool get_kvmclock_and_realtime(struct kvm *kvm, u64 *kvmclock_ns, > get_cpu(); > > if (__this_cpu_read(cpu_tsc_khz)) { > +#ifdef CONFIG_X86_64 > struct timespec64 ts; > - u64 tsc_val; > + > + if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { > + data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; > + data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; > + } else > +#endif > + data->host_tsc = rdtsc(); > > kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, > &hv_clock.tsc_shift, > &hv_clock.tsc_to_system_mul); > > - if (kvm_get_walltime_and_clockread(&ts, &tsc_val)) { > - *realtime_ns = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; > - *tsc = tsc_val; > - ret = true; > - } > - > - *kvmclock_ns = __pvclock_read_cycles(&hv_clock, tsc_val); > - } else > - *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset; > + data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc); > + } else { > + data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; > + } > > put_cpu(); > - > - return ret; > } > > u64 get_kvmclock_ns(struct kvm *kvm) > { > - u64 kvmclock_ns, realtime_ns, tsc; > + struct kvm_clock_data data; > > - get_kvmclock_and_realtime(kvm, &kvmclock_ns, &realtime_ns, &tsc); > - return kvmclock_ns; > + /* > + * Zero flags as it's accessed RMW, leave everything else uninitialized > + * as clock is always written and no other fields are consumed. > + */ > + data.flags = 0; > + > + get_kvmclock_and_realtime(kvm, &data); > + return data.clock; > } > > static void kvm_setup_pvclock_page(struct kvm_vcpu *v, > @@ -5852,12 +5853,7 @@ static int kvm_vm_ioctl_get_clock(struct kvm *kvm, > > memset(&data, 0, sizeof(data)); > > - if (get_kvmclock_and_realtime(kvm, &data.clock, &data.realtime, > - &data.host_tsc)) > - data.flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; > - > - if (kvm->arch.use_master_clock) > - data.flags |= KVM_CLOCK_TSC_STABLE; > + get_kvmclock_and_realtime(kvm, &data); > > if (copy_to_user(argp, &data, sizeof(data))) > return -EFAULT; > > > + > > + if (copy_to_user(argp, &data, sizeof(data))) > > + return -EFAULT; > > + > > + return 0; > > +} > > + > > +static int kvm_vm_ioctl_set_clock(struct kvm *kvm, > > + void __user *argp) > > +{ > > + struct kvm_arch *ka = &kvm->arch; > > + struct kvm_clock_data data; > > + u64 now_raw_ns; > > + > > + if (copy_from_user(&data, argp, sizeof(data))) > > + return -EFAULT; > > + > > + if (data.flags & ~KVM_CLOCK_REALTIME) > > + return -EINVAL; > > Huh, this an odd ABI, the output of KVM_GET_CLOCK isn't legal input to KVM_SET_CLOCK. > The existing code has the same behavior, so apparently it's ok, just odd. Quite the head scratcher to me as well /shrug > > + > > + /* > > + * TODO: userspace has to take care of races with VCPU_RUN, so > > + * kvm_gen_update_masterclock() can be cut down to locked > > + * pvclock_update_vm_gtod_copy(). > > + */ > > + kvm_gen_update_masterclock(kvm); > > + > > + spin_lock_irq(&ka->pvclock_gtod_sync_lock); > > + if (data.flags & KVM_CLOCK_REALTIME) { > > + u64 now_real_ns = ktime_get_real_ns(); > > + > > + /* > > + * Avoid stepping the kvmclock backwards. > > + */ > > + if (now_real_ns > data.realtime) > > + data.clock += now_real_ns - data.realtime; > > + } > > + > > + if (ka->use_master_clock) > > + now_raw_ns = ka->master_kernel_ns; > > + else > > + now_raw_ns = get_kvmclock_base_ns(); > > + ka->kvmclock_offset = data.clock - now_raw_ns; > > + spin_unlock_irq(&ka->pvclock_gtod_sync_lock); > > + > > + kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE); > > + return 0; > > +} > > + > > long kvm_arch_vm_ioctl(struct file *filp, > > unsigned int ioctl, unsigned long arg) > > { > > @@ -6064,57 +6151,11 @@ long kvm_arch_vm_ioctl(struct file *filp, > > } > > #endif > > case KVM_SET_CLOCK: { > > The curly braces can be dropped, both here and in KVM_GET_CLOCK. Ack. > > } > > case KVM_GET_CLOCK: { > > ... > > > } As always, thanks for the careful review Sean! -- Best, Oliver From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.6 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C468C432BE for ; Fri, 30 Jul 2021 18:25:04 +0000 (UTC) Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by mail.kernel.org (Postfix) with ESMTP id C333960F9B for ; Fri, 30 Jul 2021 18:25:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C333960F9B Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.cs.columbia.edu Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 2B58D4B0EC; Fri, 30 Jul 2021 14:25:03 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Authentication-Results: mm01.cs.columbia.edu (amavisd-new); dkim=softfail (fail, message has been altered) header.i=@google.com Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PYG27Xw8lIGs; Fri, 30 Jul 2021 14:25:01 -0400 (EDT) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id A0B264B0D4; Fri, 30 Jul 2021 14:25:01 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id D857E4B0B4 for ; Fri, 30 Jul 2021 14:24:59 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id EG91TxtnEHUM for ; Fri, 30 Jul 2021 14:24:58 -0400 (EDT) Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id 60EE44A49C for ; Fri, 30 Jul 2021 14:24:58 -0400 (EDT) Received: by mail-lj1-f171.google.com with SMTP id n6so13579327ljp.9 for ; Fri, 30 Jul 2021 11:24:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=rqMYq2CWXiNrNOrfwEzFO/KTNa6FWzj2w0cC6kBlhtc=; b=VPhZ21dZARDbHcMs08hZe0sJhMKOZMXsX2u8qhO8X6scpIJjrtbj4VWZHqI/4GigEj 1qCt/sKqN3deIP+2fzjE3nzzmsNkXbLWrrBiEDQbv/9qhkfbnK4MP+3rujkEHb43uefs Klk8/GZ07sxxhBQe4K96vYdQIMlQZKjelr+UfdlhMywAp5dQb+bkzptisPTZc55NTOrX vA2XEY9DS0n9G+3pBSEnCw7PHygN2LSM3y5Qs9Ck3BdGZnZsAXOML62gO/2iAY5L5gJJ upjTgnsf4tdR0tbZqgggxPVSsL4qpHlHttYumbNL9TmR6gqv4tNidTszuHH8uh7caf+B cD5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=rqMYq2CWXiNrNOrfwEzFO/KTNa6FWzj2w0cC6kBlhtc=; b=V+Ftqy0MV/MMu3IsO4m2q5nISFLuWk0oUT+5xBkOWbG36wfULP5SfroYzMpCjMCXqw KP/QsdIHU0wIk2fFfe+EfyhECyKDEhEpmsgG5TeB/b7YxtaPNDUw4rxMaG/UkfOnne8R DONkTyLrxsZA88E7Pip22rTaCac7krUr6MLjQHGyJhHXqnvu49+xIacvu4GmHCRbGWZ2 e0KPJCl2J6ZhMw93OwP4gXkiGIwwfDxkjklgogofYkvMcI50WJ8tHR5J/5md0qD33zwL 2NthzQKqNjpslxNbh6ov7kzORuYFzmEvqX5egyYVSsycOcaOIZUwclUJJBeZ7CLKbg94 nQGg== X-Gm-Message-State: AOAM531ZNPp0XyquDkCfDCAI+4vDA4Z1gYOHcpqUHzl2Z8Ms8HVRRlr0 KC4tmbRe7jR8TFcPM0En5v0wW5wF6cQ9UufpzWv9hQ== X-Google-Smtp-Source: ABdhPJzdtPl6p6Ma5FhWcVM12t2+r5Hw1Cc+5/G0Nha/v+f1rF5QoRu64gmbzCNjcgoiXH4m7bT9LOCk65t0MJXdddw= X-Received: by 2002:a2e:535c:: with SMTP id t28mr2435423ljd.129.1627669496637; Fri, 30 Jul 2021 11:24:56 -0700 (PDT) MIME-Version: 1.0 References: <20210729173300.181775-1-oupton@google.com> <20210729173300.181775-2-oupton@google.com> In-Reply-To: From: Oliver Upton Date: Fri, 30 Jul 2021 11:24:45 -0700 Message-ID: Subject: Re: [PATCH v5 01/13] KVM: x86: Report host tsc and realtime values in KVM_GET_CLOCK To: Sean Christopherson Cc: kvm@vger.kernel.org, Marc Zyngier , Peter Shier , Raghavendra Rao Anata , David Matlack , Paolo Bonzini , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, Jim Mattson X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu On Fri, Jul 30, 2021 at 10:48 AM Sean Christopherson wrote: > > On Thu, Jul 29, 2021, Oliver Upton wrote: > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > index 916c976e99ab..e052c7afaac4 100644 > > --- a/arch/x86/kvm/x86.c > > +++ b/arch/x86/kvm/x86.c > > @@ -2782,17 +2782,24 @@ static void kvm_gen_update_masterclock(struct kvm *kvm) > > #endif > > } > > > > -u64 get_kvmclock_ns(struct kvm *kvm) > > +/* > > + * Returns true if realtime and TSC values were written back to the caller. > > + * Returns false if a clock triplet cannot be obtained, such as if the host's > > + * realtime clock is not based on the TSC. > > + */ > > +static bool get_kvmclock_and_realtime(struct kvm *kvm, u64 *kvmclock_ns, > > + u64 *realtime_ns, u64 *tsc) > > { > > struct kvm_arch *ka = &kvm->arch; > > struct pvclock_vcpu_time_info hv_clock; > > unsigned long flags; > > - u64 ret; > > + bool ret = false; > > > > spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags); > > if (!ka->use_master_clock) { > > spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags); > > - return get_kvmclock_base_ns() + ka->kvmclock_offset; > > + *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset; > > + return false; > > } > > > > hv_clock.tsc_timestamp = ka->master_cycle_now; > > @@ -2803,18 +2810,36 @@ u64 get_kvmclock_ns(struct kvm *kvm) > > get_cpu(); > > > > if (__this_cpu_read(cpu_tsc_khz)) { > > + struct timespec64 ts; > > + u64 tsc_val; > > + > > kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, > > &hv_clock.tsc_shift, > > &hv_clock.tsc_to_system_mul); > > - ret = __pvclock_read_cycles(&hv_clock, rdtsc()); > > + > > + if (kvm_get_walltime_and_clockread(&ts, &tsc_val)) { > > + *realtime_ns = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; > > + *tsc = tsc_val; > > + ret = true; > > + } > > + > > + *kvmclock_ns = __pvclock_read_cycles(&hv_clock, tsc_val); > > This is buggy, as tsc_val is not initialized if kvm_get_walltime_and_clockread() > returns false. This also straight up fails to compile on 32-bit kernels because > kvm_get_walltime_and_clockread() is wrapped with CONFIG_X86_64. Pssh... works on my machine ;-) > > A gross way to resolve both issues would be (see below regarding 'data'): > > if (__this_cpu_read(cpu_tsc_khz)) { > #ifdef CONFIG_X86_64 > struct timespec64 ts; > > if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { > data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; > data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; > } else > #endif > data->host_tsc = rdtsc(); > > kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, > &hv_clock.tsc_shift, > &hv_clock.tsc_to_system_mul); > > data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc); > } else { > data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; > } > I'll mull on this if there's any cleaner way to fix it, but thanks for the suggested fix! I missed the x86_64 ifdefs first time around. > > } else > > Not your code, but this needs braces. Ack. > > > - ret = get_kvmclock_base_ns() + ka->kvmclock_offset; > > + *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset; > > > > put_cpu(); > > > > return ret; > > } > > ... > > > @@ -5820,6 +5845,68 @@ int kvm_arch_pm_notifier(struct kvm *kvm, unsigned long state) > > } > > #endif /* CONFIG_HAVE_KVM_PM_NOTIFIER */ > > > > +static int kvm_vm_ioctl_get_clock(struct kvm *kvm, > > + void __user *argp) > > +{ > > + struct kvm_clock_data data; > > + > > + memset(&data, 0, sizeof(data)); > > + > > + if (get_kvmclock_and_realtime(kvm, &data.clock, &data.realtime, > > + &data.host_tsc)) > > + data.flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; > > + > > + if (kvm->arch.use_master_clock) > > + data.flags |= KVM_CLOCK_TSC_STABLE; > > I know nothing about the actual behavior, but this appears to have a potential > race (though it came from the existing code). get_kvmclock_and_realtime() checks > arch.use_master_clock under pvclock_gtod_sync_lock, but this does not. Even if > that's functionally ok, it's a needless complication. > > Fixing that weirdness would also provide an opportunity to clean up the API, > e.g. the boolean return is not exactly straightforward. The simplest approach > would be to take "struct kvm_clock_data *data" in get_kvmclock_and_realtime() > instead of multiple params. Then that helper can set the flags accordingly, thus > avoiding the question of whether or not there's a race and eliminating the boolean > return. E.g. Yeah, agreed. I think it may be best to separate fixing the mess from the new API here. > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index e052c7afaac4..872a53a7c467 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -2782,25 +2782,20 @@ static void kvm_gen_update_masterclock(struct kvm *kvm) > #endif > } > > -/* > - * Returns true if realtime and TSC values were written back to the caller. > - * Returns false if a clock triplet cannot be obtained, such as if the host's > - * realtime clock is not based on the TSC. > - */ > -static bool get_kvmclock_and_realtime(struct kvm *kvm, u64 *kvmclock_ns, > - u64 *realtime_ns, u64 *tsc) > +static void get_kvmclock_and_realtime(struct kvm *kvm, > + struct kvm_clock_data *data) > { > struct kvm_arch *ka = &kvm->arch; > struct pvclock_vcpu_time_info hv_clock; > unsigned long flags; > - bool ret = false; > > spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags); > if (!ka->use_master_clock) { > spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags); > - *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset; > - return false; > + data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; > + return; > } > + data->flags |= KVM_CLOCK_TSC_STABLE; > > hv_clock.tsc_timestamp = ka->master_cycle_now; > hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; > @@ -2810,34 +2805,40 @@ static bool get_kvmclock_and_realtime(struct kvm *kvm, u64 *kvmclock_ns, > get_cpu(); > > if (__this_cpu_read(cpu_tsc_khz)) { > +#ifdef CONFIG_X86_64 > struct timespec64 ts; > - u64 tsc_val; > + > + if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { > + data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; > + data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; > + } else > +#endif > + data->host_tsc = rdtsc(); > > kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, > &hv_clock.tsc_shift, > &hv_clock.tsc_to_system_mul); > > - if (kvm_get_walltime_and_clockread(&ts, &tsc_val)) { > - *realtime_ns = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; > - *tsc = tsc_val; > - ret = true; > - } > - > - *kvmclock_ns = __pvclock_read_cycles(&hv_clock, tsc_val); > - } else > - *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset; > + data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc); > + } else { > + data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; > + } > > put_cpu(); > - > - return ret; > } > > u64 get_kvmclock_ns(struct kvm *kvm) > { > - u64 kvmclock_ns, realtime_ns, tsc; > + struct kvm_clock_data data; > > - get_kvmclock_and_realtime(kvm, &kvmclock_ns, &realtime_ns, &tsc); > - return kvmclock_ns; > + /* > + * Zero flags as it's accessed RMW, leave everything else uninitialized > + * as clock is always written and no other fields are consumed. > + */ > + data.flags = 0; > + > + get_kvmclock_and_realtime(kvm, &data); > + return data.clock; > } > > static void kvm_setup_pvclock_page(struct kvm_vcpu *v, > @@ -5852,12 +5853,7 @@ static int kvm_vm_ioctl_get_clock(struct kvm *kvm, > > memset(&data, 0, sizeof(data)); > > - if (get_kvmclock_and_realtime(kvm, &data.clock, &data.realtime, > - &data.host_tsc)) > - data.flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; > - > - if (kvm->arch.use_master_clock) > - data.flags |= KVM_CLOCK_TSC_STABLE; > + get_kvmclock_and_realtime(kvm, &data); > > if (copy_to_user(argp, &data, sizeof(data))) > return -EFAULT; > > > + > > + if (copy_to_user(argp, &data, sizeof(data))) > > + return -EFAULT; > > + > > + return 0; > > +} > > + > > +static int kvm_vm_ioctl_set_clock(struct kvm *kvm, > > + void __user *argp) > > +{ > > + struct kvm_arch *ka = &kvm->arch; > > + struct kvm_clock_data data; > > + u64 now_raw_ns; > > + > > + if (copy_from_user(&data, argp, sizeof(data))) > > + return -EFAULT; > > + > > + if (data.flags & ~KVM_CLOCK_REALTIME) > > + return -EINVAL; > > Huh, this an odd ABI, the output of KVM_GET_CLOCK isn't legal input to KVM_SET_CLOCK. > The existing code has the same behavior, so apparently it's ok, just odd. Quite the head scratcher to me as well /shrug > > + > > + /* > > + * TODO: userspace has to take care of races with VCPU_RUN, so > > + * kvm_gen_update_masterclock() can be cut down to locked > > + * pvclock_update_vm_gtod_copy(). > > + */ > > + kvm_gen_update_masterclock(kvm); > > + > > + spin_lock_irq(&ka->pvclock_gtod_sync_lock); > > + if (data.flags & KVM_CLOCK_REALTIME) { > > + u64 now_real_ns = ktime_get_real_ns(); > > + > > + /* > > + * Avoid stepping the kvmclock backwards. > > + */ > > + if (now_real_ns > data.realtime) > > + data.clock += now_real_ns - data.realtime; > > + } > > + > > + if (ka->use_master_clock) > > + now_raw_ns = ka->master_kernel_ns; > > + else > > + now_raw_ns = get_kvmclock_base_ns(); > > + ka->kvmclock_offset = data.clock - now_raw_ns; > > + spin_unlock_irq(&ka->pvclock_gtod_sync_lock); > > + > > + kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE); > > + return 0; > > +} > > + > > long kvm_arch_vm_ioctl(struct file *filp, > > unsigned int ioctl, unsigned long arg) > > { > > @@ -6064,57 +6151,11 @@ long kvm_arch_vm_ioctl(struct file *filp, > > } > > #endif > > case KVM_SET_CLOCK: { > > The curly braces can be dropped, both here and in KVM_GET_CLOCK. Ack. > > } > > case KVM_GET_CLOCK: { > > ... > > > } As always, thanks for the careful review Sean! -- Best, Oliver _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3571AC4338F for ; Fri, 30 Jul 2021 18:27:56 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id E980A60F4B for ; Fri, 30 Jul 2021 18:27:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org E980A60F4B Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=uRF4TejlbBFv8X35eNq+hXaKlUh9j3f0Nuqfcl+usXI=; b=j0htBHBlsWNWW5 gC9B8ZpZRrLf1RyvUyJDsfd96vMJgcYSmM85+89xySo5X3LaqCeJ99pzujISY40PvQYduWNm6iStB KY795KxhixvgO0C1mDrf4l381eQ8z+JH5fimrXnnZDAHZhbazLxvvNBBq0zPR/bb7/iVJOco2JMYZ QDWu4/IqSRWvzaMbZANqXop4ppK0lRZOXID7/DNYj9PJ6N09mai+GkasdHiGCgNk0+yOm7V/vv3LO 6LvISmEe0cizruo0PcmBTOUurqMBiOAAjd8Hbx++8CPZZRxZ3JSy16uT4NpIzyp1y73MBxi50WGLc TYUsmhmq3iekgBLZO9/A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m9XC9-009w87-KX; Fri, 30 Jul 2021 18:25:17 +0000 Received: from mail-lj1-x22f.google.com ([2a00:1450:4864:20::22f]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m9XBq-009w7J-Rk for linux-arm-kernel@lists.infradead.org; Fri, 30 Jul 2021 18:25:00 +0000 Received: by mail-lj1-x22f.google.com with SMTP id r23so13612414lji.3 for ; Fri, 30 Jul 2021 11:24:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=rqMYq2CWXiNrNOrfwEzFO/KTNa6FWzj2w0cC6kBlhtc=; b=VPhZ21dZARDbHcMs08hZe0sJhMKOZMXsX2u8qhO8X6scpIJjrtbj4VWZHqI/4GigEj 1qCt/sKqN3deIP+2fzjE3nzzmsNkXbLWrrBiEDQbv/9qhkfbnK4MP+3rujkEHb43uefs Klk8/GZ07sxxhBQe4K96vYdQIMlQZKjelr+UfdlhMywAp5dQb+bkzptisPTZc55NTOrX vA2XEY9DS0n9G+3pBSEnCw7PHygN2LSM3y5Qs9Ck3BdGZnZsAXOML62gO/2iAY5L5gJJ upjTgnsf4tdR0tbZqgggxPVSsL4qpHlHttYumbNL9TmR6gqv4tNidTszuHH8uh7caf+B cD5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=rqMYq2CWXiNrNOrfwEzFO/KTNa6FWzj2w0cC6kBlhtc=; b=NnakwJC3JF65PXahsoO9X5OlPnwNC1fXanBP/N5yvpXC19AC/IWC3VQ1gjJJd3BJPO XNp64iqvF+mcbaj2ARxtlI1twZ0BGv96Pz09tm7h1rGRDZipXWv3eplrJOs9iIdHw+4l Yw6y7GksgY/L/l3y32jL/OeS4MoJTb4qjFpaiaFUjuzT0T6jIQKeS/iVmAnukJ0gRXMJ lyxjVTbpYTykzyWVmyvvDjMt4LjPDsee75UvOOKUKzduBC3NzZUHKsIiP7SRNIl5MwkD Vxd0LYSFZ0Thc4dSfTf9ijd8rofxvVvmB6vsa//lXnxvAhx4ZyCQQhsZFqz+TH91X9lo f1vw== X-Gm-Message-State: AOAM5334NdNJBW65MtvK+9zS3DEZopyeKTyAqRQD2Q+/P9D7wPEkTfzK /qm/Dvme6WC1ELtL95YyOfDfamVjurnenL8/GhUyPw== X-Google-Smtp-Source: ABdhPJzdtPl6p6Ma5FhWcVM12t2+r5Hw1Cc+5/G0Nha/v+f1rF5QoRu64gmbzCNjcgoiXH4m7bT9LOCk65t0MJXdddw= X-Received: by 2002:a2e:535c:: with SMTP id t28mr2435423ljd.129.1627669496637; Fri, 30 Jul 2021 11:24:56 -0700 (PDT) MIME-Version: 1.0 References: <20210729173300.181775-1-oupton@google.com> <20210729173300.181775-2-oupton@google.com> In-Reply-To: From: Oliver Upton Date: Fri, 30 Jul 2021 11:24:45 -0700 Message-ID: Subject: Re: [PATCH v5 01/13] KVM: x86: Report host tsc and realtime values in KVM_GET_CLOCK To: Sean Christopherson Cc: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, Paolo Bonzini , Marc Zyngier , Peter Shier , Jim Mattson , David Matlack , Ricardo Koller , Jing Zhang , Raghavendra Rao Anata , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Andrew Jones X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210730_112458_964207_C14FEEED X-CRM114-Status: GOOD ( 40.65 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, Jul 30, 2021 at 10:48 AM Sean Christopherson wrote: > > On Thu, Jul 29, 2021, Oliver Upton wrote: > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > index 916c976e99ab..e052c7afaac4 100644 > > --- a/arch/x86/kvm/x86.c > > +++ b/arch/x86/kvm/x86.c > > @@ -2782,17 +2782,24 @@ static void kvm_gen_update_masterclock(struct kvm *kvm) > > #endif > > } > > > > -u64 get_kvmclock_ns(struct kvm *kvm) > > +/* > > + * Returns true if realtime and TSC values were written back to the caller. > > + * Returns false if a clock triplet cannot be obtained, such as if the host's > > + * realtime clock is not based on the TSC. > > + */ > > +static bool get_kvmclock_and_realtime(struct kvm *kvm, u64 *kvmclock_ns, > > + u64 *realtime_ns, u64 *tsc) > > { > > struct kvm_arch *ka = &kvm->arch; > > struct pvclock_vcpu_time_info hv_clock; > > unsigned long flags; > > - u64 ret; > > + bool ret = false; > > > > spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags); > > if (!ka->use_master_clock) { > > spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags); > > - return get_kvmclock_base_ns() + ka->kvmclock_offset; > > + *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset; > > + return false; > > } > > > > hv_clock.tsc_timestamp = ka->master_cycle_now; > > @@ -2803,18 +2810,36 @@ u64 get_kvmclock_ns(struct kvm *kvm) > > get_cpu(); > > > > if (__this_cpu_read(cpu_tsc_khz)) { > > + struct timespec64 ts; > > + u64 tsc_val; > > + > > kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, > > &hv_clock.tsc_shift, > > &hv_clock.tsc_to_system_mul); > > - ret = __pvclock_read_cycles(&hv_clock, rdtsc()); > > + > > + if (kvm_get_walltime_and_clockread(&ts, &tsc_val)) { > > + *realtime_ns = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; > > + *tsc = tsc_val; > > + ret = true; > > + } > > + > > + *kvmclock_ns = __pvclock_read_cycles(&hv_clock, tsc_val); > > This is buggy, as tsc_val is not initialized if kvm_get_walltime_and_clockread() > returns false. This also straight up fails to compile on 32-bit kernels because > kvm_get_walltime_and_clockread() is wrapped with CONFIG_X86_64. Pssh... works on my machine ;-) > > A gross way to resolve both issues would be (see below regarding 'data'): > > if (__this_cpu_read(cpu_tsc_khz)) { > #ifdef CONFIG_X86_64 > struct timespec64 ts; > > if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { > data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; > data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; > } else > #endif > data->host_tsc = rdtsc(); > > kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, > &hv_clock.tsc_shift, > &hv_clock.tsc_to_system_mul); > > data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc); > } else { > data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; > } > I'll mull on this if there's any cleaner way to fix it, but thanks for the suggested fix! I missed the x86_64 ifdefs first time around. > > } else > > Not your code, but this needs braces. Ack. > > > - ret = get_kvmclock_base_ns() + ka->kvmclock_offset; > > + *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset; > > > > put_cpu(); > > > > return ret; > > } > > ... > > > @@ -5820,6 +5845,68 @@ int kvm_arch_pm_notifier(struct kvm *kvm, unsigned long state) > > } > > #endif /* CONFIG_HAVE_KVM_PM_NOTIFIER */ > > > > +static int kvm_vm_ioctl_get_clock(struct kvm *kvm, > > + void __user *argp) > > +{ > > + struct kvm_clock_data data; > > + > > + memset(&data, 0, sizeof(data)); > > + > > + if (get_kvmclock_and_realtime(kvm, &data.clock, &data.realtime, > > + &data.host_tsc)) > > + data.flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; > > + > > + if (kvm->arch.use_master_clock) > > + data.flags |= KVM_CLOCK_TSC_STABLE; > > I know nothing about the actual behavior, but this appears to have a potential > race (though it came from the existing code). get_kvmclock_and_realtime() checks > arch.use_master_clock under pvclock_gtod_sync_lock, but this does not. Even if > that's functionally ok, it's a needless complication. > > Fixing that weirdness would also provide an opportunity to clean up the API, > e.g. the boolean return is not exactly straightforward. The simplest approach > would be to take "struct kvm_clock_data *data" in get_kvmclock_and_realtime() > instead of multiple params. Then that helper can set the flags accordingly, thus > avoiding the question of whether or not there's a race and eliminating the boolean > return. E.g. Yeah, agreed. I think it may be best to separate fixing the mess from the new API here. > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index e052c7afaac4..872a53a7c467 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -2782,25 +2782,20 @@ static void kvm_gen_update_masterclock(struct kvm *kvm) > #endif > } > > -/* > - * Returns true if realtime and TSC values were written back to the caller. > - * Returns false if a clock triplet cannot be obtained, such as if the host's > - * realtime clock is not based on the TSC. > - */ > -static bool get_kvmclock_and_realtime(struct kvm *kvm, u64 *kvmclock_ns, > - u64 *realtime_ns, u64 *tsc) > +static void get_kvmclock_and_realtime(struct kvm *kvm, > + struct kvm_clock_data *data) > { > struct kvm_arch *ka = &kvm->arch; > struct pvclock_vcpu_time_info hv_clock; > unsigned long flags; > - bool ret = false; > > spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags); > if (!ka->use_master_clock) { > spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags); > - *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset; > - return false; > + data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; > + return; > } > + data->flags |= KVM_CLOCK_TSC_STABLE; > > hv_clock.tsc_timestamp = ka->master_cycle_now; > hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; > @@ -2810,34 +2805,40 @@ static bool get_kvmclock_and_realtime(struct kvm *kvm, u64 *kvmclock_ns, > get_cpu(); > > if (__this_cpu_read(cpu_tsc_khz)) { > +#ifdef CONFIG_X86_64 > struct timespec64 ts; > - u64 tsc_val; > + > + if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { > + data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; > + data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; > + } else > +#endif > + data->host_tsc = rdtsc(); > > kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, > &hv_clock.tsc_shift, > &hv_clock.tsc_to_system_mul); > > - if (kvm_get_walltime_and_clockread(&ts, &tsc_val)) { > - *realtime_ns = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; > - *tsc = tsc_val; > - ret = true; > - } > - > - *kvmclock_ns = __pvclock_read_cycles(&hv_clock, tsc_val); > - } else > - *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset; > + data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc); > + } else { > + data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset; > + } > > put_cpu(); > - > - return ret; > } > > u64 get_kvmclock_ns(struct kvm *kvm) > { > - u64 kvmclock_ns, realtime_ns, tsc; > + struct kvm_clock_data data; > > - get_kvmclock_and_realtime(kvm, &kvmclock_ns, &realtime_ns, &tsc); > - return kvmclock_ns; > + /* > + * Zero flags as it's accessed RMW, leave everything else uninitialized > + * as clock is always written and no other fields are consumed. > + */ > + data.flags = 0; > + > + get_kvmclock_and_realtime(kvm, &data); > + return data.clock; > } > > static void kvm_setup_pvclock_page(struct kvm_vcpu *v, > @@ -5852,12 +5853,7 @@ static int kvm_vm_ioctl_get_clock(struct kvm *kvm, > > memset(&data, 0, sizeof(data)); > > - if (get_kvmclock_and_realtime(kvm, &data.clock, &data.realtime, > - &data.host_tsc)) > - data.flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; > - > - if (kvm->arch.use_master_clock) > - data.flags |= KVM_CLOCK_TSC_STABLE; > + get_kvmclock_and_realtime(kvm, &data); > > if (copy_to_user(argp, &data, sizeof(data))) > return -EFAULT; > > > + > > + if (copy_to_user(argp, &data, sizeof(data))) > > + return -EFAULT; > > + > > + return 0; > > +} > > + > > +static int kvm_vm_ioctl_set_clock(struct kvm *kvm, > > + void __user *argp) > > +{ > > + struct kvm_arch *ka = &kvm->arch; > > + struct kvm_clock_data data; > > + u64 now_raw_ns; > > + > > + if (copy_from_user(&data, argp, sizeof(data))) > > + return -EFAULT; > > + > > + if (data.flags & ~KVM_CLOCK_REALTIME) > > + return -EINVAL; > > Huh, this an odd ABI, the output of KVM_GET_CLOCK isn't legal input to KVM_SET_CLOCK. > The existing code has the same behavior, so apparently it's ok, just odd. Quite the head scratcher to me as well /shrug > > + > > + /* > > + * TODO: userspace has to take care of races with VCPU_RUN, so > > + * kvm_gen_update_masterclock() can be cut down to locked > > + * pvclock_update_vm_gtod_copy(). > > + */ > > + kvm_gen_update_masterclock(kvm); > > + > > + spin_lock_irq(&ka->pvclock_gtod_sync_lock); > > + if (data.flags & KVM_CLOCK_REALTIME) { > > + u64 now_real_ns = ktime_get_real_ns(); > > + > > + /* > > + * Avoid stepping the kvmclock backwards. > > + */ > > + if (now_real_ns > data.realtime) > > + data.clock += now_real_ns - data.realtime; > > + } > > + > > + if (ka->use_master_clock) > > + now_raw_ns = ka->master_kernel_ns; > > + else > > + now_raw_ns = get_kvmclock_base_ns(); > > + ka->kvmclock_offset = data.clock - now_raw_ns; > > + spin_unlock_irq(&ka->pvclock_gtod_sync_lock); > > + > > + kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE); > > + return 0; > > +} > > + > > long kvm_arch_vm_ioctl(struct file *filp, > > unsigned int ioctl, unsigned long arg) > > { > > @@ -6064,57 +6151,11 @@ long kvm_arch_vm_ioctl(struct file *filp, > > } > > #endif > > case KVM_SET_CLOCK: { > > The curly braces can be dropped, both here and in KVM_GET_CLOCK. Ack. > > } > > case KVM_GET_CLOCK: { > > ... > > > } As always, thanks for the careful review Sean! -- Best, Oliver _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel