From patchwork Thu Sep 16 18:15:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12499897 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=-17.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT 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 8668AC433F5 for ; Thu, 16 Sep 2021 18:21:53 +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 4E94960F12 for ; Thu, 16 Sep 2021 18:21:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 4E94960F12 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:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=ZT5kITQb4XWiKEkhBW9QHXE6HIy5w2ux+qEUL7T3FKk=; b=hgjlT8oUNwYBpqeaaM52Cr8MNX /12RXOjxEjS1H/Erx51gfhqUTRm7s1vsxBYh7T9dTBl5is4ZHqcFtSUbLho+jQ/zHPo6U6DyERoWn eRO5yqMkU2OzzbMDU8ZfMoz/O1XooWpNjThz8wpkcIsUM+Ye+z74dg53ooFifPMNAhtm1LeeXdPAm kWfIbvETXYb9gudaPuaqO/F0XxgQTw/LsYuh2atMZc0Vj5z6nb4O369cobgY1bY9SRWR1kgAHTVf2 KW7s1+nGpTb28i5NSohC+IrnVHRT+vV0vXecCcKqpZpViRIUb9N9rCJGV+15uG0ltzo37QFqGi57R k2bqwANw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mQvzW-00C448-3u; Thu, 16 Sep 2021 18:20:11 +0000 Received: from mail-qv1-xf49.google.com ([2607:f8b0:4864:20::f49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mQvvF-00C1wk-GQ for linux-arm-kernel@lists.infradead.org; Thu, 16 Sep 2021 18:15:47 +0000 Received: by mail-qv1-xf49.google.com with SMTP id h18-20020ad446f2000000b0037a7b48ba05so62898171qvw.19 for ; Thu, 16 Sep 2021 11:15:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=ompqiNMWI2hga8pPlI1DdLrAhAHA2ErmtWHohcgkDnQ=; b=EIa4PYxeIB/jdTr4Bj3rabymZMfiRDUda4Z/1Om5oXzv+tZ0fVo/aVqKHI2InGS1OU j9lrVhRl1gkVZxnjRo2Omv6GbaDFwuTFsTyhpJYtmlVggm0E+aaYOkyBo2IY8jh1btCe GCVbynojpKzy/F6leYHtS6kaa6OBYmyZ/yg5VpAo7eUg7Ay9lq3XlUxJEOuwtyxYRwM7 U8r/qmsNggetc0Jtt0lQFuvAKtC4IYRkZQuGIYG8PCS2CcI+gaR0uAWDft1vZecQzTlV 1E3D6XDkidTe09oTgISDpS3sGZjyIeQ2ZsLL11W4oTK5YDPcDw9j6SfA/hsX0uy5AbmI bz8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ompqiNMWI2hga8pPlI1DdLrAhAHA2ErmtWHohcgkDnQ=; b=za0kJhPtxvdChoyKxG7j3u+sGQtOgJ+c/mc4taO2o5yYhDtngXV6LA9dMTXn9oLNe7 evosheHn0LMwKFPlsz4HiJgQw7c1jJybr8FHyTQI/pgu64OzeD4Id7I6CAtIebsQ/ZSy J8BKH5J4WdPPeQoqdbRzYsyCLvX/5Ghxt1iClvIr9JUq4pSSrtZFc8BhbB4O5E5j6x27 20v9cCs0/VbIv+LFpyx+s2D4pG3N13Q32Bl7QW6Kbuqks0oph6YZKDhQQ4yOaEHqa9qE iDRsHZsfANfFpOIb0fOC69ytpn+Z+UibVIkSPrLz8z6NPlYdpBP238QSt0gEYO21jGQY rPfw== X-Gm-Message-State: AOAM532mMOw2WM/3fHvwTLkl3DoxKnGyiIWUUWL7OGB3gmnwN1AoI7J2 s8Nt/4BUUZqBZluGQPLGI31oyJzeuUM= X-Google-Smtp-Source: ABdhPJyMtDkmedSBXQ9JneBR51sdoiXY3FTPAb7hN3Y4cUearQHH1ZnZgDcI1O9vcz/G/4rXwSUCJqg2mWg= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6214:13ee:: with SMTP id ch14mr6861648qvb.43.1631816143780; Thu, 16 Sep 2021 11:15:43 -0700 (PDT) Date: Thu, 16 Sep 2021 18:15:33 +0000 In-Reply-To: <20210916181538.968978-1-oupton@google.com> Message-Id: <20210916181538.968978-3-oupton@google.com> Mime-Version: 1.0 References: <20210916181538.968978-1-oupton@google.com> X-Mailer: git-send-email 2.33.0.464.g1972c5931b-goog Subject: [PATCH v8 2/7] KVM: x86: extract KVM_GET_CLOCK/KVM_SET_CLOCK to separate functions From: Oliver Upton To: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu Cc: Paolo Bonzini , Sean Christopherson , 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 , Will Deacon , Catalin Marinas , Oliver Upton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210916_111545_623117_737E1B74 X-CRM114-Status: GOOD ( 15.73 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Paolo Bonzini no functional change intended. Signed-off-by: Paolo Bonzini Signed-off-by: Oliver Upton --- arch/x86/kvm/x86.c | 99 ++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 47 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 1082b48418c3..c910cf31958f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5829,6 +5829,54 @@ 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; + u64 now_ns; + + now_ns = get_kvmclock_ns(kvm); + user_ns.clock = now_ns; + user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0; + memset(&user_ns.pad, 0, sizeof(user_ns.pad)); + + 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_ns; + + if (copy_from_user(&data, argp, sizeof(data))) + return -EFAULT; + + if (data.flags) + return -EINVAL; + + kvm_hv_invalidate_tsc_page(kvm); + kvm_start_pvclock_update(kvm); + pvclock_update_vm_gtod_copy(kvm); + + /* + * This pairs with kvm_guest_time_update(): when masterclock is + * in use, we use master_kernel_ns + kvmclock_offset to set + * unsigned 'system_time' so if we use get_kvmclock_ns() (which + * is slightly ahead) here we risk going negative on unsigned + * 'system_time' when 'data.clock' is very small. + */ + if (kvm->arch.use_master_clock) + now_ns = ka->master_kernel_ns; + else + now_ns = get_kvmclock_base_ns(); + ka->kvmclock_offset = data.clock - now_ns; + kvm_end_pvclock_update(kvm); + return 0; +} + long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { @@ -6072,55 +6120,12 @@ long kvm_arch_vm_ioctl(struct file *filp, break; } #endif - case KVM_SET_CLOCK: { - struct kvm_arch *ka = &kvm->arch; - struct kvm_clock_data user_ns; - u64 now_ns; - - r = -EFAULT; - if (copy_from_user(&user_ns, argp, sizeof(user_ns))) - goto out; - - r = -EINVAL; - if (user_ns.flags) - goto out; - - r = 0; - - kvm_hv_invalidate_tsc_page(kvm); - kvm_start_pvclock_update(kvm); - pvclock_update_vm_gtod_copy(kvm); - - /* - * This pairs with kvm_guest_time_update(): when masterclock is - * in use, we use master_kernel_ns + kvmclock_offset to set - * unsigned 'system_time' so if we use get_kvmclock_ns() (which - * is slightly ahead) here we risk going negative on unsigned - * 'system_time' when 'user_ns.clock' is very small. - */ - if (kvm->arch.use_master_clock) - now_ns = ka->master_kernel_ns; - else - now_ns = get_kvmclock_base_ns(); - ka->kvmclock_offset = user_ns.clock - now_ns; - kvm_end_pvclock_update(kvm); + case KVM_SET_CLOCK: + r = kvm_vm_ioctl_set_clock(kvm, argp); break; - } - case KVM_GET_CLOCK: { - struct kvm_clock_data user_ns; - u64 now_ns; - - now_ns = get_kvmclock_ns(kvm); - user_ns.clock = now_ns; - user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0; - memset(&user_ns.pad, 0, sizeof(user_ns.pad)); - - r = -EFAULT; - if (copy_to_user(argp, &user_ns, sizeof(user_ns))) - goto out; - r = 0; + case KVM_GET_CLOCK: + r = kvm_vm_ioctl_get_clock(kvm, argp); break; - } case KVM_MEMORY_ENCRYPT_OP: { r = -ENOTTY; if (kvm_x86_ops.mem_enc_op)