2023-07-31 02:04:02

by zhanghao1

[permalink] [raw]
Subject: [PATCH] A new virtio pci driver is added for listening to vcpus inside guest. Each vcpu creates a corresponding thread to periodically send data to qemu's back-end watchdog device.

The driver is mainly used with the back-end watchdog device of qemu.

Signed-off-by: zhanghao1 <[email protected]>
---
drivers/virtio/Kconfig | 9 +
drivers/virtio/Makefile | 1 +
drivers/virtio/virtio_vcpu_stall_detector.c | 299 ++++++++++++++++++++
3 files changed, 309 insertions(+)
create mode 100644 drivers/virtio/virtio_vcpu_stall_detector.c

diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
index 0a53a61231c2..869323e345a1 100644
--- a/drivers/virtio/Kconfig
+++ b/drivers/virtio/Kconfig
@@ -173,4 +173,13 @@ config VIRTIO_DMA_SHARED_BUFFER
This option adds a flavor of dma buffers that are backed by
virtio resources.

+config VIRTIO_VCPU_WATCHDOG
+ tristate "Virtio vcpu watchdog driver"
+ depends on VIRTIO_PCI
+ help
+ When this driver is bound inside a KVM guest, it will
+ periodically "pet" an PCI virtio watchdog device from each vCPU
+ and allow the host to detect vCPU stalls.
+
+ If you do not intend to run this kernel as a guest, say N.
endif # VIRTIO_MENU
diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile
index 8e98d24917cc..c7341f078a34 100644
--- a/drivers/virtio/Makefile
+++ b/drivers/virtio/Makefile
@@ -12,3 +12,4 @@ obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.o
obj-$(CONFIG_VIRTIO_VDPA) += virtio_vdpa.o
obj-$(CONFIG_VIRTIO_MEM) += virtio_mem.o
obj-$(CONFIG_VIRTIO_DMA_SHARED_BUFFER) += virtio_dma_buf.o
+obj-$(CONFIG_VIRTIO_VCPU_WATCHDOG) += virtio_vcpu_stall_detector.o
diff --git a/drivers/virtio/virtio_vcpu_stall_detector.c b/drivers/virtio/virtio_vcpu_stall_detector.c
new file mode 100644
index 000000000000..58344ca528be
--- /dev/null
+++ b/drivers/virtio/virtio_vcpu_stall_detector.c
@@ -0,0 +1,299 @@
+// SPDX-License-Identifier: GPL-2.0-only
+//
+// VCPU stall detector.
+// Copyright (C) Kylin Software, 2023
+
+#include <linux/cpu.h>
+#include <linux/init.h>
+#include <linux/io.h>
+#include <linux/kernel.h>
+
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/module.h>
+#include <linux/nmi.h>
+#include <uapi/linux/virtio_ids.h>
+#include <linux/virtio_config.h>
+#include <linux/param.h>
+#include <linux/percpu.h>
+#include <linux/slab.h>
+
+#define VCPU_STALL_REG_STATUS (0x00)
+#define VCPU_STALL_REG_LOAD_CNT (0x04)
+#define VCPU_STALL_REG_CURRENT_CNT (0x08)
+#define VCPU_STALL_REG_CLOCK_FREQ_HZ (0x0C)
+#define VCPU_STALL_REG_LEN (0x10)
+#define VCPU_STALL_REG_TIMEOUT_SEC (0x14)
+
+#define VCPU_STALL_DEFAULT_CLOCK_HZ (10)
+#define VCPU_STALL_MAX_CLOCK_HZ (100)
+#define VCPU_STALL_DEFAULT_TIMEOUT_SEC (8)
+#define VCPU_STALL_MAX_TIMEOUT_SEC (600)
+
+struct vcpu_stall_detect_config {
+ u32 clock_freq_hz;
+ u32 stall_timeout_sec;
+
+ enum cpuhp_state hp_online;
+};
+
+struct vcpu_stall_priv {
+ struct hrtimer vcpu_hrtimer;
+ struct virtio_device *vdev;
+ u32 cpu_id;
+};
+
+struct vcpu_stall {
+ struct vcpu_stall_priv *priv;
+ struct virtqueue *vq;
+ spinlock_t lock;
+ struct pet_event {
+ u32 cpu_id;
+ bool is_initialized;
+ u32 ticks;
+ } pet_event;
+};
+
+static const struct virtio_device_id vcpu_stall_id_table[] = {
+ { VIRTIO_ID_WATCHDOG, VIRTIO_DEV_ANY_ID },
+ { 0, },
+};
+
+/* The vcpu stall configuration structure which applies to all the CPUs */
+static struct vcpu_stall_detect_config vcpu_stall_config;
+static struct vcpu_stall *vcpu_stall;
+
+static struct vcpu_stall_priv __percpu *vcpu_stall_detectors;
+
+static enum hrtimer_restart
+vcpu_stall_detect_timer_fn(struct hrtimer *hrtimer)
+{
+ u32 ticks, ping_timeout_ms;
+ struct scatterlist sg;
+ int unused, err = 0;
+
+ struct vcpu_stall_priv *vcpu_stall_detector =
+ this_cpu_ptr(vcpu_stall->priv);
+
+ /* Reload the stall detector counter register every
+ * `ping_timeout_ms` to prevent the virtual device
+ * from decrementing it to 0. The virtual device decrements this
+ * register at 'clock_freq_hz' frequency.
+ */
+ ticks = vcpu_stall_config.clock_freq_hz *
+ vcpu_stall_config.stall_timeout_sec;
+
+ spin_lock(&vcpu_stall->lock);
+ while (virtqueue_get_buf(vcpu_stall->vq, &unused))
+ ;
+ vcpu_stall->pet_event.ticks = cpu_to_virtio32(vcpu_stall_detector->vdev, ticks);
+ vcpu_stall->pet_event.is_initialized = true;
+ vcpu_stall->pet_event.cpu_id = vcpu_stall_detector->cpu_id;
+
+ sg_init_one(&sg, &vcpu_stall->pet_event, sizeof(vcpu_stall->pet_event));
+ err = virtqueue_add_outbuf(vcpu_stall->vq, &sg, 1, vcpu_stall, GFP_ATOMIC);
+ if (!err)
+ virtqueue_kick(vcpu_stall->vq);
+ else
+ pr_err("cpu:%d failed to add outbuf, err:%d\n", vcpu_stall_detector->cpu_id, err);
+
+ spin_unlock(&vcpu_stall->lock);
+
+ ping_timeout_ms = vcpu_stall_config.stall_timeout_sec *
+ MSEC_PER_SEC / 2;
+ hrtimer_forward_now(hrtimer,
+ ms_to_ktime(ping_timeout_ms));
+ return HRTIMER_RESTART;
+}
+
+static int start_stall_detector_cpu(unsigned int cpu)
+{
+ u32 ticks, ping_timeout_ms;
+ struct scatterlist sg;
+ struct hrtimer *vcpu_hrtimer;
+ int err = 0;
+
+ struct vcpu_stall_priv *vcpu_stall_detector =
+ this_cpu_ptr(vcpu_stall->priv);
+
+ vcpu_stall_detector->cpu_id = cpu;
+
+ vcpu_hrtimer = &vcpu_stall_detector->vcpu_hrtimer;
+
+ /* Compute the number of ticks required for the stall detector
+ * counter register based on the internal clock frequency and the
+ * timeout value given from the device tree.
+ */
+ ticks = vcpu_stall_config.clock_freq_hz *
+ vcpu_stall_config.stall_timeout_sec;
+ vcpu_stall->pet_event.ticks = cpu_to_virtio32(vcpu_stall_detector->vdev, ticks);
+
+ /* Pet the stall detector at half of its expiration timeout
+ * to prevent spurious resets.
+ */
+ ping_timeout_ms = vcpu_stall_config.stall_timeout_sec *
+ MSEC_PER_SEC / 2;
+
+ hrtimer_init(vcpu_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+ vcpu_hrtimer->function = vcpu_stall_detect_timer_fn;
+
+ vcpu_stall->pet_event.is_initialized = true;
+
+ spin_lock(&vcpu_stall->lock);
+ vcpu_stall->pet_event.cpu_id = cpu;
+ sg_init_one(&sg, &vcpu_stall->pet_event, sizeof(vcpu_stall->pet_event));
+ err = virtqueue_add_outbuf(vcpu_stall->vq, &sg, 1, vcpu_stall, GFP_ATOMIC);
+ if (!err)
+ virtqueue_kick(vcpu_stall->vq);
+
+ spin_unlock(&vcpu_stall->lock);
+
+ hrtimer_start(vcpu_hrtimer, ms_to_ktime(ping_timeout_ms),
+ HRTIMER_MODE_REL_PINNED);
+ return err;
+}
+
+static int stop_stall_detector_cpu(unsigned int cpu)
+{
+ int err = 0;
+ struct scatterlist sg;
+
+ struct vcpu_stall_priv *vcpu_stall_detector =
+ per_cpu_ptr(vcpu_stall_detectors, cpu);
+
+ /* Disable the stall detector for the current CPU */
+ hrtimer_cancel(&vcpu_stall_detector->vcpu_hrtimer);
+ vcpu_stall->pet_event.is_initialized = false;
+ vcpu_stall->pet_event.cpu_id = cpu;
+
+ spin_lock(&vcpu_stall->lock);
+ sg_init_one(&sg, &vcpu_stall->pet_event, sizeof(vcpu_stall->pet_event));
+ err = virtqueue_add_outbuf(vcpu_stall->vq, &sg, 1, vcpu_stall, GFP_ATOMIC);
+ if (!err)
+ virtqueue_kick(vcpu_stall->vq);
+
+ spin_unlock(&vcpu_stall->lock);
+
+ return err;
+}
+
+static int vcpu_stall_detect_probe(struct virtio_device *vdev)
+{
+ int ret, cpu;
+ u32 clock_freq_hz = VCPU_STALL_DEFAULT_CLOCK_HZ;
+ u32 stall_timeout_sec = VCPU_STALL_DEFAULT_TIMEOUT_SEC;
+
+ vcpu_stall = kzalloc(sizeof(struct vcpu_stall), GFP_KERNEL);
+ if (!vcpu_stall) {
+ ret = -ENOMEM;
+ goto err;
+ }
+ vdev->priv = vcpu_stall;
+
+ vcpu_stall->priv = devm_alloc_percpu(&vdev->dev,
+ typeof(struct vcpu_stall_priv));
+ if (!vcpu_stall->priv) {
+ ret = -ENOMEM;
+ goto failed_priv;
+ }
+
+ for_each_possible_cpu(cpu) {
+ struct vcpu_stall_priv *priv;
+
+ priv = per_cpu_ptr(vcpu_stall->priv, cpu);
+ priv->vdev = vdev;
+ }
+
+ ret = virtio_cread_feature(vdev, VCPU_STALL_REG_CLOCK_FREQ_HZ,
+ struct vcpu_stall_detect_config, clock_freq_hz,
+ &clock_freq_hz);
+ if (ret || !clock_freq_hz) {
+ if (!(clock_freq_hz > 0 &&
+ clock_freq_hz < VCPU_STALL_MAX_CLOCK_HZ)) {
+ dev_warn(&vdev->dev, "clk out of range\n");
+ clock_freq_hz = VCPU_STALL_DEFAULT_CLOCK_HZ;
+ }
+ }
+ ret = virtio_cread_feature(vdev, VCPU_STALL_REG_TIMEOUT_SEC,
+ struct vcpu_stall_detect_config, stall_timeout_sec,
+ &stall_timeout_sec);
+ if (ret || !stall_timeout_sec) {
+ if (!(stall_timeout_sec > 0 &&
+ stall_timeout_sec < VCPU_STALL_MAX_TIMEOUT_SEC)) {
+ dev_warn(&vdev->dev, "stall timeout out of range\n");
+ stall_timeout_sec = VCPU_STALL_DEFAULT_TIMEOUT_SEC;
+ }
+ }
+
+ vcpu_stall_config = (struct vcpu_stall_detect_config) {
+ .clock_freq_hz = clock_freq_hz,
+ .stall_timeout_sec = stall_timeout_sec
+ };
+
+ /* find virtqueue for guest to send pet event to host */
+ vcpu_stall->vq = virtio_find_single_vq(vdev, NULL, "pet-event");
+ if (IS_ERR(vcpu_stall->vq)) {
+ dev_err(&vdev->dev, "failed to find vq\n");
+ goto failed_priv;
+ }
+
+ spin_lock_init(&vcpu_stall->lock);
+
+ ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
+ "virt/vcpu_stall_detector:online",
+ start_stall_detector_cpu,
+ stop_stall_detector_cpu);
+ if (ret < 0) {
+ dev_err(&vdev->dev, "failed to install cpu hotplug\n");
+ goto failed_priv;
+ }
+
+ vcpu_stall_config.hp_online = ret;
+ return 0;
+
+
+failed_priv:
+ kfree(vcpu_stall);
+err:
+ return ret;
+}
+
+static void vcpu_stall_detect_remove(struct virtio_device *vdev)
+{
+ int cpu;
+
+ cpuhp_remove_state(vcpu_stall_config.hp_online);
+
+ for_each_possible_cpu(cpu)
+ stop_stall_detector_cpu(cpu);
+}
+
+static unsigned int features_legacy[] = {
+ VCPU_STALL_REG_STATUS, VCPU_STALL_REG_LOAD_CNT, VCPU_STALL_REG_CURRENT_CNT,
+ VCPU_STALL_REG_CLOCK_FREQ_HZ, VCPU_STALL_REG_LEN, VCPU_STALL_REG_TIMEOUT_SEC
+};
+
+
+static unsigned int features[] = {
+ VCPU_STALL_REG_STATUS, VCPU_STALL_REG_LOAD_CNT, VCPU_STALL_REG_CURRENT_CNT,
+ VCPU_STALL_REG_CLOCK_FREQ_HZ, VCPU_STALL_REG_LEN, VCPU_STALL_REG_TIMEOUT_SEC
+};
+
+static struct virtio_driver vcpu_stall_detect_driver = {
+ .feature_table = features,
+ .feature_table_size = ARRAY_SIZE(features),
+ .feature_table_legacy = features_legacy,
+ .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
+ .driver.name = KBUILD_MODNAME,
+ .driver.owner = THIS_MODULE,
+ .id_table = vcpu_stall_id_table,
+ .probe = vcpu_stall_detect_probe,
+ .remove = vcpu_stall_detect_remove,
+};
+
+module_virtio_driver(vcpu_stall_detect_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_DEVICE_TABLE(virtio, vcpu_stall_id_table);
+MODULE_AUTHOR("zhanghao1 <[email protected]>");
+MODULE_DESCRIPTION("VCPU stall detector");
--
2.25.1


No virus found
Checked by Hillstone Network AntiVirus


2023-08-10 21:29:10

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] A new virtio pci driver is added for listening to vcpus inside guest. Each vcpu creates a corresponding thread to periodically send data to qemu's back-end watchdog device.

Hi zhanghao1,

kernel test robot noticed the following build warnings:

[auto build test WARNING on linus/master]
[also build test WARNING on v6.5-rc5 next-20230809]
[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#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/zhanghao1/A-new-virtio-pci-driver-is-added-for-listening-to-vcpus-inside-guest-Each-vcpu-creates-a-corresponding-thread-to-periodi/20230731-092546
base: linus/master
patch link: https://lore.kernel.org/r/20230731012405.234611-1-zhanghao1%40kylinos.cn
patch subject: [PATCH] A new virtio pci driver is added for listening to vcpus inside guest. Each vcpu creates a corresponding thread to periodically send data to qemu's back-end watchdog device.
config: sparc64-randconfig-r071-20230811 (https://download.01.org/0day-ci/archive/20230811/[email protected]/config)
compiler: sparc64-linux-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230811/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)
>> drivers/virtio/virtio_vcpu_stall_detector.c:76:17: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected void const [noderef] __percpu *__vpp_verify @@ got struct vcpu_stall_priv * @@
drivers/virtio/virtio_vcpu_stall_detector.c:76:17: sparse: expected void const [noderef] __percpu *__vpp_verify
drivers/virtio/virtio_vcpu_stall_detector.c:76:17: sparse: got struct vcpu_stall_priv *
>> drivers/virtio/virtio_vcpu_stall_detector.c:89:37: sparse: sparse: incorrect type in assignment (different base types) @@ expected unsigned int [usertype] ticks @@ got restricted __virtio32 @@
drivers/virtio/virtio_vcpu_stall_detector.c:89:37: sparse: expected unsigned int [usertype] ticks
drivers/virtio/virtio_vcpu_stall_detector.c:89:37: sparse: got restricted __virtio32
drivers/virtio/virtio_vcpu_stall_detector.c:117:17: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected void const [noderef] __percpu *__vpp_verify @@ got struct vcpu_stall_priv * @@
drivers/virtio/virtio_vcpu_stall_detector.c:117:17: sparse: expected void const [noderef] __percpu *__vpp_verify
drivers/virtio/virtio_vcpu_stall_detector.c:117:17: sparse: got struct vcpu_stall_priv *
drivers/virtio/virtio_vcpu_stall_detector.c:129:37: sparse: sparse: incorrect type in assignment (different base types) @@ expected unsigned int [usertype] ticks @@ got restricted __virtio32 @@
drivers/virtio/virtio_vcpu_stall_detector.c:129:37: sparse: expected unsigned int [usertype] ticks
drivers/virtio/virtio_vcpu_stall_detector.c:129:37: sparse: got restricted __virtio32
>> drivers/virtio/virtio_vcpu_stall_detector.c:193:26: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct vcpu_stall_priv *priv @@ got struct vcpu_stall_priv [noderef] __percpu * @@
drivers/virtio/virtio_vcpu_stall_detector.c:193:26: sparse: expected struct vcpu_stall_priv *priv
drivers/virtio/virtio_vcpu_stall_detector.c:193:26: sparse: got struct vcpu_stall_priv [noderef] __percpu *
drivers/virtio/virtio_vcpu_stall_detector.c:203:24: sparse: sparse: incorrect type in initializer (different address spaces) @@ expected void const [noderef] __percpu *__vpp_verify @@ got struct vcpu_stall_priv * @@
drivers/virtio/virtio_vcpu_stall_detector.c:203:24: sparse: expected void const [noderef] __percpu *__vpp_verify
drivers/virtio/virtio_vcpu_stall_detector.c:203:24: sparse: got struct vcpu_stall_priv *
>> drivers/virtio/virtio_vcpu_stall_detector.c:207:15: sparse: sparse: no generic selection for 'unsigned int virtio_cread_v'
>> drivers/virtio/virtio_vcpu_stall_detector.c:207:15: sparse: sparse: incompatible types in comparison expression (different base types):
>> drivers/virtio/virtio_vcpu_stall_detector.c:207:15: sparse: bad type *
>> drivers/virtio/virtio_vcpu_stall_detector.c:207:15: sparse: unsigned int *
>> drivers/virtio/virtio_vcpu_stall_detector.c:207:15: sparse: sparse: no generic selection for 'unsigned int [addressable] virtio_cread_v'
drivers/virtio/virtio_vcpu_stall_detector.c:217:15: sparse: sparse: no generic selection for 'unsigned int virtio_cread_v'
drivers/virtio/virtio_vcpu_stall_detector.c:217:15: sparse: sparse: incompatible types in comparison expression (different base types):
drivers/virtio/virtio_vcpu_stall_detector.c:217:15: sparse: bad type *
drivers/virtio/virtio_vcpu_stall_detector.c:217:15: sparse: unsigned int *
drivers/virtio/virtio_vcpu_stall_detector.c:217:15: sparse: sparse: no generic selection for 'unsigned int [addressable] virtio_cread_v'

vim +76 drivers/virtio/virtio_vcpu_stall_detector.c

67
68 static enum hrtimer_restart
69 vcpu_stall_detect_timer_fn(struct hrtimer *hrtimer)
70 {
71 u32 ticks, ping_timeout_ms;
72 struct scatterlist sg;
73 int unused, err = 0;
74
75 struct vcpu_stall_priv *vcpu_stall_detector =
> 76 this_cpu_ptr(vcpu_stall->priv);
77
78 /* Reload the stall detector counter register every
79 * `ping_timeout_ms` to prevent the virtual device
80 * from decrementing it to 0. The virtual device decrements this
81 * register at 'clock_freq_hz' frequency.
82 */
83 ticks = vcpu_stall_config.clock_freq_hz *
84 vcpu_stall_config.stall_timeout_sec;
85
86 spin_lock(&vcpu_stall->lock);
87 while (virtqueue_get_buf(vcpu_stall->vq, &unused))
88 ;
> 89 vcpu_stall->pet_event.ticks = cpu_to_virtio32(vcpu_stall_detector->vdev, ticks);
90 vcpu_stall->pet_event.is_initialized = true;
91 vcpu_stall->pet_event.cpu_id = vcpu_stall_detector->cpu_id;
92
93 sg_init_one(&sg, &vcpu_stall->pet_event, sizeof(vcpu_stall->pet_event));
94 err = virtqueue_add_outbuf(vcpu_stall->vq, &sg, 1, vcpu_stall, GFP_ATOMIC);
95 if (!err)
96 virtqueue_kick(vcpu_stall->vq);
97 else
98 pr_err("cpu:%d failed to add outbuf, err:%d\n", vcpu_stall_detector->cpu_id, err);
99
100 spin_unlock(&vcpu_stall->lock);
101
102 ping_timeout_ms = vcpu_stall_config.stall_timeout_sec *
103 MSEC_PER_SEC / 2;
104 hrtimer_forward_now(hrtimer,
105 ms_to_ktime(ping_timeout_ms));
106 return HRTIMER_RESTART;
107 }
108
109 static int start_stall_detector_cpu(unsigned int cpu)
110 {
111 u32 ticks, ping_timeout_ms;
112 struct scatterlist sg;
113 struct hrtimer *vcpu_hrtimer;
114 int err = 0;
115
116 struct vcpu_stall_priv *vcpu_stall_detector =
117 this_cpu_ptr(vcpu_stall->priv);
118
119 vcpu_stall_detector->cpu_id = cpu;
120
121 vcpu_hrtimer = &vcpu_stall_detector->vcpu_hrtimer;
122
123 /* Compute the number of ticks required for the stall detector
124 * counter register based on the internal clock frequency and the
125 * timeout value given from the device tree.
126 */
127 ticks = vcpu_stall_config.clock_freq_hz *
128 vcpu_stall_config.stall_timeout_sec;
129 vcpu_stall->pet_event.ticks = cpu_to_virtio32(vcpu_stall_detector->vdev, ticks);
130
131 /* Pet the stall detector at half of its expiration timeout
132 * to prevent spurious resets.
133 */
134 ping_timeout_ms = vcpu_stall_config.stall_timeout_sec *
135 MSEC_PER_SEC / 2;
136
137 hrtimer_init(vcpu_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
138 vcpu_hrtimer->function = vcpu_stall_detect_timer_fn;
139
140 vcpu_stall->pet_event.is_initialized = true;
141
142 spin_lock(&vcpu_stall->lock);
143 vcpu_stall->pet_event.cpu_id = cpu;
144 sg_init_one(&sg, &vcpu_stall->pet_event, sizeof(vcpu_stall->pet_event));
145 err = virtqueue_add_outbuf(vcpu_stall->vq, &sg, 1, vcpu_stall, GFP_ATOMIC);
146 if (!err)
147 virtqueue_kick(vcpu_stall->vq);
148
149 spin_unlock(&vcpu_stall->lock);
150
151 hrtimer_start(vcpu_hrtimer, ms_to_ktime(ping_timeout_ms),
152 HRTIMER_MODE_REL_PINNED);
153 return err;
154 }
155
156 static int stop_stall_detector_cpu(unsigned int cpu)
157 {
158 int err = 0;
159 struct scatterlist sg;
160
161 struct vcpu_stall_priv *vcpu_stall_detector =
162 per_cpu_ptr(vcpu_stall_detectors, cpu);
163
164 /* Disable the stall detector for the current CPU */
165 hrtimer_cancel(&vcpu_stall_detector->vcpu_hrtimer);
166 vcpu_stall->pet_event.is_initialized = false;
167 vcpu_stall->pet_event.cpu_id = cpu;
168
169 spin_lock(&vcpu_stall->lock);
170 sg_init_one(&sg, &vcpu_stall->pet_event, sizeof(vcpu_stall->pet_event));
171 err = virtqueue_add_outbuf(vcpu_stall->vq, &sg, 1, vcpu_stall, GFP_ATOMIC);
172 if (!err)
173 virtqueue_kick(vcpu_stall->vq);
174
175 spin_unlock(&vcpu_stall->lock);
176
177 return err;
178 }
179
180 static int vcpu_stall_detect_probe(struct virtio_device *vdev)
181 {
182 int ret, cpu;
183 u32 clock_freq_hz = VCPU_STALL_DEFAULT_CLOCK_HZ;
184 u32 stall_timeout_sec = VCPU_STALL_DEFAULT_TIMEOUT_SEC;
185
186 vcpu_stall = kzalloc(sizeof(struct vcpu_stall), GFP_KERNEL);
187 if (!vcpu_stall) {
188 ret = -ENOMEM;
189 goto err;
190 }
191 vdev->priv = vcpu_stall;
192
> 193 vcpu_stall->priv = devm_alloc_percpu(&vdev->dev,
194 typeof(struct vcpu_stall_priv));
195 if (!vcpu_stall->priv) {
196 ret = -ENOMEM;
197 goto failed_priv;
198 }
199
200 for_each_possible_cpu(cpu) {
201 struct vcpu_stall_priv *priv;
202
203 priv = per_cpu_ptr(vcpu_stall->priv, cpu);
204 priv->vdev = vdev;
205 }
206
> 207 ret = virtio_cread_feature(vdev, VCPU_STALL_REG_CLOCK_FREQ_HZ,
208 struct vcpu_stall_detect_config, clock_freq_hz,
209 &clock_freq_hz);
210 if (ret || !clock_freq_hz) {
211 if (!(clock_freq_hz > 0 &&
212 clock_freq_hz < VCPU_STALL_MAX_CLOCK_HZ)) {
213 dev_warn(&vdev->dev, "clk out of range\n");
214 clock_freq_hz = VCPU_STALL_DEFAULT_CLOCK_HZ;
215 }
216 }
217 ret = virtio_cread_feature(vdev, VCPU_STALL_REG_TIMEOUT_SEC,
218 struct vcpu_stall_detect_config, stall_timeout_sec,
219 &stall_timeout_sec);
220 if (ret || !stall_timeout_sec) {
221 if (!(stall_timeout_sec > 0 &&
222 stall_timeout_sec < VCPU_STALL_MAX_TIMEOUT_SEC)) {
223 dev_warn(&vdev->dev, "stall timeout out of range\n");
224 stall_timeout_sec = VCPU_STALL_DEFAULT_TIMEOUT_SEC;
225 }
226 }
227
228 vcpu_stall_config = (struct vcpu_stall_detect_config) {
229 .clock_freq_hz = clock_freq_hz,
230 .stall_timeout_sec = stall_timeout_sec
231 };
232
233 /* find virtqueue for guest to send pet event to host */
234 vcpu_stall->vq = virtio_find_single_vq(vdev, NULL, "pet-event");
235 if (IS_ERR(vcpu_stall->vq)) {
236 dev_err(&vdev->dev, "failed to find vq\n");
237 goto failed_priv;
238 }
239
240 spin_lock_init(&vcpu_stall->lock);
241
242 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
243 "virt/vcpu_stall_detector:online",
244 start_stall_detector_cpu,
245 stop_stall_detector_cpu);
246 if (ret < 0) {
247 dev_err(&vdev->dev, "failed to install cpu hotplug\n");
248 goto failed_priv;
249 }
250
251 vcpu_stall_config.hp_online = ret;
252 return 0;
253
254
255 failed_priv:
256 kfree(vcpu_stall);
257 err:
258 return ret;
259 }
260

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki