diff mbox series

[v15,3/9] ptp: Reorganize ptp_kvm module to make it arch-independent.

Message ID 20201111062211.33144-4-jianyong.wu@arm.com (mailing list archive)
State Not Applicable
Delegated to: Netdev Maintainers
Headers show
Series Enable ptp_kvm for arm/arm64 | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Jianyong Wu Nov. 11, 2020, 6:22 a.m. UTC
Currently, ptp_kvm modules implementation is only for x86 which includes
large part of arch-specific code.  This patch moves all of this code
into a new arch related file in the same directory.

Signed-off-by: Jianyong Wu <jianyong.wu@arm.com>
---
 drivers/ptp/Makefile                        |  1 +
 drivers/ptp/ptp_kvm.h                       | 11 +++
 drivers/ptp/{ptp_kvm.c => ptp_kvm_common.c} | 85 +++++-------------
 drivers/ptp/ptp_kvm_x86.c                   | 95 +++++++++++++++++++++
 4 files changed, 130 insertions(+), 62 deletions(-)
 create mode 100644 drivers/ptp/ptp_kvm.h
 rename drivers/ptp/{ptp_kvm.c => ptp_kvm_common.c} (60%)
 create mode 100644 drivers/ptp/ptp_kvm_x86.c

Comments

kernel test robot Nov. 11, 2020, 1:10 p.m. UTC | #1
Hi Jianyong,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v5.10-rc3 next-20201111]
[cannot apply to tip/timers/core kvmarm/next kvm/linux-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Jianyong-Wu/Enable-ptp_kvm-for-arm-arm64/20201111-142600
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git eccc876724927ff3b9ff91f36f7b6b159e948f0c
config: i386-randconfig-p002-20201111 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/8cc52e023b81a9f561cc2094ecdd7266f5e29230
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Jianyong-Wu/Enable-ptp_kvm-for-arm-arm64/20201111-142600
        git checkout 8cc52e023b81a9f561cc2094ecdd7266f5e29230
        # save the attached .config to linux build tree
        make W=1 ARCH=i386 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/ptp/ptp_kvm_x86.c:21:5: warning: no previous prototype for 'kvm_arch_ptp_init' [-Wmissing-prototypes]
      21 | int kvm_arch_ptp_init(void)
         |     ^~~~~~~~~~~~~~~~~
>> drivers/ptp/ptp_kvm_x86.c:41:5: warning: no previous prototype for 'kvm_arch_ptp_get_clock' [-Wmissing-prototypes]
      41 | int kvm_arch_ptp_get_clock(struct timespec64 *ts)
         |     ^~~~~~~~~~~~~~~~~~~~~~
>> drivers/ptp/ptp_kvm_x86.c:59:5: warning: no previous prototype for 'kvm_arch_ptp_get_crosststamp' [-Wmissing-prototypes]
      59 | int kvm_arch_ptp_get_crosststamp(u64 *cycle, struct timespec64 *tspec,
         |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +/kvm_arch_ptp_init +21 drivers/ptp/ptp_kvm_x86.c

    20	
  > 21	int kvm_arch_ptp_init(void)
    22	{
    23		long ret;
    24	
    25		if (!kvm_para_available())
    26			return -ENODEV;
    27	
    28		clock_pair_gpa = slow_virt_to_phys(&clock_pair);
    29		hv_clock = pvclock_get_pvti_cpu0_va();
    30		if (!hv_clock)
    31			return -ENODEV;
    32	
    33		ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING, clock_pair_gpa,
    34				     KVM_CLOCK_PAIRING_WALLCLOCK);
    35		if (ret == -KVM_ENOSYS || ret == -KVM_EOPNOTSUPP)
    36			return -ENODEV;
    37	
    38		return 0;
    39	}
    40	
  > 41	int kvm_arch_ptp_get_clock(struct timespec64 *ts)
    42	{
    43		unsigned long ret;
    44	
    45		ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING,
    46				     clock_pair_gpa,
    47				     KVM_CLOCK_PAIRING_WALLCLOCK);
    48		if (ret != 0) {
    49			pr_err_ratelimited("clock offset hypercall ret %lu\n", ret);
    50			return -EOPNOTSUPP;
    51		}
    52	
    53		ts->tv_sec = clock_pair.sec;
    54		ts->tv_nsec = clock_pair.nsec;
    55	
    56		return 0;
    57	}
    58	
  > 59	int kvm_arch_ptp_get_crosststamp(u64 *cycle, struct timespec64 *tspec,

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
kernel test robot Nov. 11, 2020, 2:36 p.m. UTC | #2
Hi Jianyong,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v5.10-rc3 next-20201111]
[cannot apply to tip/timers/core kvmarm/next kvm/linux-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Jianyong-Wu/Enable-ptp_kvm-for-arm-arm64/20201111-142600
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git eccc876724927ff3b9ff91f36f7b6b159e948f0c
config: x86_64-randconfig-a005-20201111 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 874b0a0b9db93f5d3350ffe6b5efda2d908415d0)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install x86_64 cross compiling tool for clang build
        # apt-get install binutils-x86-64-linux-gnu
        # https://github.com/0day-ci/linux/commit/8cc52e023b81a9f561cc2094ecdd7266f5e29230
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Jianyong-Wu/Enable-ptp_kvm-for-arm-arm64/20201111-142600
        git checkout 8cc52e023b81a9f561cc2094ecdd7266f5e29230
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> drivers/ptp/ptp_kvm_x86.c:21:5: warning: no previous prototype for function 'kvm_arch_ptp_init' [-Wmissing-prototypes]
   int kvm_arch_ptp_init(void)
       ^
   drivers/ptp/ptp_kvm_x86.c:21:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int kvm_arch_ptp_init(void)
   ^
   static 
>> drivers/ptp/ptp_kvm_x86.c:41:5: warning: no previous prototype for function 'kvm_arch_ptp_get_clock' [-Wmissing-prototypes]
   int kvm_arch_ptp_get_clock(struct timespec64 *ts)
       ^
   drivers/ptp/ptp_kvm_x86.c:41:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int kvm_arch_ptp_get_clock(struct timespec64 *ts)
   ^
   static 
>> drivers/ptp/ptp_kvm_x86.c:59:5: warning: no previous prototype for function 'kvm_arch_ptp_get_crosststamp' [-Wmissing-prototypes]
   int kvm_arch_ptp_get_crosststamp(u64 *cycle, struct timespec64 *tspec,
       ^
   drivers/ptp/ptp_kvm_x86.c:59:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   int kvm_arch_ptp_get_crosststamp(u64 *cycle, struct timespec64 *tspec,
   ^
   static 
   3 warnings generated.

vim +/kvm_arch_ptp_init +21 drivers/ptp/ptp_kvm_x86.c

    20	
  > 21	int kvm_arch_ptp_init(void)
    22	{
    23		long ret;
    24	
    25		if (!kvm_para_available())
    26			return -ENODEV;
    27	
    28		clock_pair_gpa = slow_virt_to_phys(&clock_pair);
    29		hv_clock = pvclock_get_pvti_cpu0_va();
    30		if (!hv_clock)
    31			return -ENODEV;
    32	
    33		ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING, clock_pair_gpa,
    34				     KVM_CLOCK_PAIRING_WALLCLOCK);
    35		if (ret == -KVM_ENOSYS || ret == -KVM_EOPNOTSUPP)
    36			return -ENODEV;
    37	
    38		return 0;
    39	}
    40	
  > 41	int kvm_arch_ptp_get_clock(struct timespec64 *ts)
    42	{
    43		unsigned long ret;
    44	
    45		ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING,
    46				     clock_pair_gpa,
    47				     KVM_CLOCK_PAIRING_WALLCLOCK);
    48		if (ret != 0) {
    49			pr_err_ratelimited("clock offset hypercall ret %lu\n", ret);
    50			return -EOPNOTSUPP;
    51		}
    52	
    53		ts->tv_sec = clock_pair.sec;
    54		ts->tv_nsec = clock_pair.nsec;
    55	
    56		return 0;
    57	}
    58	
  > 59	int kvm_arch_ptp_get_crosststamp(u64 *cycle, struct timespec64 *tspec,

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/drivers/ptp/Makefile b/drivers/ptp/Makefile
index 7aff75f745dc..699a4e4d19c2 100644
--- a/drivers/ptp/Makefile
+++ b/drivers/ptp/Makefile
@@ -4,6 +4,7 @@ 
 #
 
 ptp-y					:= ptp_clock.o ptp_chardev.o ptp_sysfs.o
+ptp_kvm-$(CONFIG_X86)			:= ptp_kvm_x86.o ptp_kvm_common.o
 obj-$(CONFIG_PTP_1588_CLOCK)		+= ptp.o
 obj-$(CONFIG_PTP_1588_CLOCK_DTE)	+= ptp_dte.o
 obj-$(CONFIG_PTP_1588_CLOCK_INES)	+= ptp_ines.o
diff --git a/drivers/ptp/ptp_kvm.h b/drivers/ptp/ptp_kvm.h
new file mode 100644
index 000000000000..46fe4c7c377d
--- /dev/null
+++ b/drivers/ptp/ptp_kvm.h
@@ -0,0 +1,11 @@ 
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Virtual PTP 1588 clock for use with KVM guests
+ *
+ * Copyright (C) 2017 Red Hat Inc.
+ */
+
+int kvm_arch_ptp_init(void);
+int kvm_arch_ptp_get_clock(struct timespec64 *ts);
+int kvm_arch_ptp_get_crosststamp(u64 *cycle,
+		struct timespec64 *tspec, void *cs);
diff --git a/drivers/ptp/ptp_kvm.c b/drivers/ptp/ptp_kvm_common.c
similarity index 60%
rename from drivers/ptp/ptp_kvm.c
rename to drivers/ptp/ptp_kvm_common.c
index 658d33fc3195..2df4ff3c518f 100644
--- a/drivers/ptp/ptp_kvm.c
+++ b/drivers/ptp/ptp_kvm_common.c
@@ -8,15 +8,16 @@ 
 #include <linux/err.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
+#include <linux/slab.h>
 #include <linux/module.h>
 #include <uapi/linux/kvm_para.h>
 #include <asm/kvm_para.h>
-#include <asm/pvclock.h>
-#include <asm/kvmclock.h>
 #include <uapi/asm/kvm_para.h>
 
 #include <linux/ptp_clock_kernel.h>
 
+#include "ptp_kvm.h"
+
 struct kvm_ptp_clock {
 	struct ptp_clock *ptp_clock;
 	struct ptp_clock_info caps;
@@ -24,56 +25,29 @@  struct kvm_ptp_clock {
 
 static DEFINE_SPINLOCK(kvm_ptp_lock);
 
-static struct pvclock_vsyscall_time_info *hv_clock;
-
-static struct kvm_clock_pairing clock_pair;
-static phys_addr_t clock_pair_gpa;
-
 static int ptp_kvm_get_time_fn(ktime_t *device_time,
 			       struct system_counterval_t *system_counter,
 			       void *ctx)
 {
-	unsigned long ret;
+	long ret;
+	u64 cycle;
 	struct timespec64 tspec;
-	unsigned version;
-	int cpu;
-	struct pvclock_vcpu_time_info *src;
+	struct clocksource *cs;
 
 	spin_lock(&kvm_ptp_lock);
 
 	preempt_disable_notrace();
-	cpu = smp_processor_id();
-	src = &hv_clock[cpu].pvti;
-
-	do {
-		/*
-		 * We are using a TSC value read in the hosts
-		 * kvm_hc_clock_pairing handling.
-		 * So any changes to tsc_to_system_mul
-		 * and tsc_shift or any other pvclock
-		 * data invalidate that measurement.
-		 */
-		version = pvclock_read_begin(src);
-
-		ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING,
-				     clock_pair_gpa,
-				     KVM_CLOCK_PAIRING_WALLCLOCK);
-		if (ret != 0) {
-			pr_err_ratelimited("clock pairing hypercall ret %lu\n", ret);
-			spin_unlock(&kvm_ptp_lock);
-			preempt_enable_notrace();
-			return -EOPNOTSUPP;
-		}
-
-		tspec.tv_sec = clock_pair.sec;
-		tspec.tv_nsec = clock_pair.nsec;
-		ret = __pvclock_read_cycles(src, clock_pair.tsc);
-	} while (pvclock_read_retry(src, version));
+	ret = kvm_arch_ptp_get_crosststamp(&cycle, &tspec, &cs);
+	if (ret) {
+		spin_unlock(&kvm_ptp_lock);
+		preempt_enable_notrace();
+		return ret;
+	}
 
 	preempt_enable_notrace();
 
-	system_counter->cycles = ret;
-	system_counter->cs = &kvm_clock;
+	system_counter->cycles = cycle;
+	system_counter->cs = cs;
 
 	*device_time = timespec64_to_ktime(tspec);
 
@@ -111,22 +85,17 @@  static int ptp_kvm_settime(struct ptp_clock_info *ptp,
 
 static int ptp_kvm_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
 {
-	unsigned long ret;
+	long ret;
 	struct timespec64 tspec;
 
 	spin_lock(&kvm_ptp_lock);
 
-	ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING,
-			     clock_pair_gpa,
-			     KVM_CLOCK_PAIRING_WALLCLOCK);
-	if (ret != 0) {
-		pr_err_ratelimited("clock offset hypercall ret %lu\n", ret);
+	ret = kvm_arch_ptp_get_clock(&tspec);
+	if (ret) {
 		spin_unlock(&kvm_ptp_lock);
-		return -EOPNOTSUPP;
+		return ret;
 	}
 
-	tspec.tv_sec = clock_pair.sec;
-	tspec.tv_nsec = clock_pair.nsec;
 	spin_unlock(&kvm_ptp_lock);
 
 	memcpy(ts, &tspec, sizeof(struct timespec64));
@@ -168,19 +137,11 @@  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_get_pvti_cpu0_va();
-
-	if (!hv_clock)
-		return -ENODEV;
-
-	ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING, clock_pair_gpa,
-			KVM_CLOCK_PAIRING_WALLCLOCK);
-	if (ret == -KVM_ENOSYS || ret == -KVM_EOPNOTSUPP)
-		return -ENODEV;
+	ret = kvm_arch_ptp_init();
+	if (ret) {
+		pr_err("fail to initialize ptp_kvm");
+		return ret;
+	}
 
 	kvm_ptp_clock.caps = ptp_kvm_caps;
 
diff --git a/drivers/ptp/ptp_kvm_x86.c b/drivers/ptp/ptp_kvm_x86.c
new file mode 100644
index 000000000000..ed19083653a4
--- /dev/null
+++ b/drivers/ptp/ptp_kvm_x86.c
@@ -0,0 +1,95 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Virtual PTP 1588 clock for use with KVM guests
+ *
+ * Copyright (C) 2017 Red Hat Inc.
+ */
+
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <asm/pvclock.h>
+#include <asm/kvmclock.h>
+#include <linux/module.h>
+#include <uapi/asm/kvm_para.h>
+#include <uapi/linux/kvm_para.h>
+#include <linux/ptp_clock_kernel.h>
+
+phys_addr_t clock_pair_gpa;
+struct kvm_clock_pairing clock_pair;
+struct pvclock_vsyscall_time_info *hv_clock;
+
+int kvm_arch_ptp_init(void)
+{
+	long ret;
+
+	if (!kvm_para_available())
+		return -ENODEV;
+
+	clock_pair_gpa = slow_virt_to_phys(&clock_pair);
+	hv_clock = pvclock_get_pvti_cpu0_va();
+	if (!hv_clock)
+		return -ENODEV;
+
+	ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING, clock_pair_gpa,
+			     KVM_CLOCK_PAIRING_WALLCLOCK);
+	if (ret == -KVM_ENOSYS || ret == -KVM_EOPNOTSUPP)
+		return -ENODEV;
+
+	return 0;
+}
+
+int kvm_arch_ptp_get_clock(struct timespec64 *ts)
+{
+	unsigned long ret;
+
+	ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING,
+			     clock_pair_gpa,
+			     KVM_CLOCK_PAIRING_WALLCLOCK);
+	if (ret != 0) {
+		pr_err_ratelimited("clock offset hypercall ret %lu\n", ret);
+		return -EOPNOTSUPP;
+	}
+
+	ts->tv_sec = clock_pair.sec;
+	ts->tv_nsec = clock_pair.nsec;
+
+	return 0;
+}
+
+int kvm_arch_ptp_get_crosststamp(u64 *cycle, struct timespec64 *tspec,
+			      struct clocksource **cs)
+{
+	unsigned long ret;
+	unsigned int version;
+	int cpu;
+	struct pvclock_vcpu_time_info *src;
+
+	cpu = smp_processor_id();
+	src = &hv_clock[cpu].pvti;
+
+	do {
+		/*
+		 * We are using a TSC value read in the hosts
+		 * kvm_hc_clock_pairing handling.
+		 * So any changes to tsc_to_system_mul
+		 * and tsc_shift or any other pvclock
+		 * data invalidate that measurement.
+		 */
+		version = pvclock_read_begin(src);
+
+		ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING,
+				     clock_pair_gpa,
+				     KVM_CLOCK_PAIRING_WALLCLOCK);
+		if (ret != 0) {
+			pr_err_ratelimited("clock pairing hypercall ret %lu\n", ret);
+			return -EOPNOTSUPP;
+		}
+		tspec->tv_sec = clock_pair.sec;
+		tspec->tv_nsec = clock_pair.nsec;
+		*cycle = __pvclock_read_cycles(src, clock_pair.tsc);
+	} while (pvclock_read_retry(src, version));
+
+	*cs = &kvm_clock;
+
+	return 0;
+}