1 // SPDX-License-Identifier: GPL-2.0-only
4 * Branden Bonaby <brandonbonaby94@gmail.com>
7 #include <linux/hyperv.h>
8 #include <linux/debugfs.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
12 #include "hyperv_vmbus.h"
14 static struct dentry *hv_debug_root;
16 static int hv_debugfs_delay_get(void *data, u64 *val)
22 static int hv_debugfs_delay_set(void *data, u64 val)
30 DEFINE_DEBUGFS_ATTRIBUTE(hv_debugfs_delay_fops, hv_debugfs_delay_get,
31 hv_debugfs_delay_set, "%llu\n");
33 static int hv_debugfs_state_get(void *data, u64 *val)
39 static int hv_debugfs_state_set(void *data, u64 val)
44 *(bool *)data = false;
50 DEFINE_DEBUGFS_ATTRIBUTE(hv_debugfs_state_fops, hv_debugfs_state_get,
51 hv_debugfs_state_set, "%llu\n");
53 /* Setup delay files to store test values */
54 static int hv_debug_delay_files(struct hv_device *dev, struct dentry *root)
56 struct vmbus_channel *channel = dev->channel;
57 char *buffer = "fuzz_test_buffer_interrupt_delay";
58 char *message = "fuzz_test_message_delay";
59 int *buffer_val = &channel->fuzz_testing_interrupt_delay;
60 int *message_val = &channel->fuzz_testing_message_delay;
61 struct dentry *buffer_file, *message_file;
63 buffer_file = debugfs_create_file(buffer, 0644, root,
65 &hv_debugfs_delay_fops);
66 if (IS_ERR(buffer_file)) {
67 pr_debug("debugfs_hyperv: file %s not created\n", buffer);
68 return PTR_ERR(buffer_file);
71 message_file = debugfs_create_file(message, 0644, root,
73 &hv_debugfs_delay_fops);
74 if (IS_ERR(message_file)) {
75 pr_debug("debugfs_hyperv: file %s not created\n", message);
76 return PTR_ERR(message_file);
82 /* Setup test state value for vmbus device */
83 static int hv_debug_set_test_state(struct hv_device *dev, struct dentry *root)
85 struct vmbus_channel *channel = dev->channel;
86 bool *state = &channel->fuzz_testing_state;
87 char *status = "fuzz_test_state";
88 struct dentry *test_state;
90 test_state = debugfs_create_file(status, 0644, root,
92 &hv_debugfs_state_fops);
93 if (IS_ERR(test_state)) {
94 pr_debug("debugfs_hyperv: file %s not created\n", status);
95 return PTR_ERR(test_state);
101 /* Bind hv device to a dentry for debugfs */
102 static void hv_debug_set_dir_dentry(struct hv_device *dev, struct dentry *root)
105 dev->debug_dir = root;
108 /* Create all test dentry's and names for fuzz testing */
109 int hv_debug_add_dev_dir(struct hv_device *dev)
111 const char *device = dev_name(&dev->device);
112 char *delay_name = "delay";
113 struct dentry *delay, *dev_root;
116 if (!IS_ERR(hv_debug_root)) {
117 dev_root = debugfs_create_dir(device, hv_debug_root);
118 if (IS_ERR(dev_root)) {
119 pr_debug("debugfs_hyperv: hyperv/%s/ not created\n",
121 return PTR_ERR(dev_root);
123 hv_debug_set_test_state(dev, dev_root);
124 hv_debug_set_dir_dentry(dev, dev_root);
125 delay = debugfs_create_dir(delay_name, dev_root);
128 pr_debug("debugfs_hyperv: hyperv/%s/%s/ not created\n",
130 return PTR_ERR(delay);
132 ret = hv_debug_delay_files(dev, delay);
136 pr_debug("debugfs_hyperv: hyperv/ not in root debugfs path\n");
137 return PTR_ERR(hv_debug_root);
140 /* Remove dentry associated with released hv device */
141 void hv_debug_rm_dev_dir(struct hv_device *dev)
143 if (!IS_ERR(hv_debug_root))
144 debugfs_remove_recursive(dev->debug_dir);
147 /* Remove all dentrys associated with vmbus testing */
148 void hv_debug_rm_all_dir(void)
150 debugfs_remove_recursive(hv_debug_root);
153 /* Delay buffer/message reads on a vmbus channel */
154 void hv_debug_delay_test(struct vmbus_channel *channel, enum delay delay_type)
156 struct vmbus_channel *test_channel = channel->primary_channel ?
157 channel->primary_channel :
159 bool state = test_channel->fuzz_testing_state;
163 udelay(test_channel->fuzz_testing_interrupt_delay);
165 udelay(test_channel->fuzz_testing_message_delay);
169 /* Initialize top dentry for vmbus testing */
170 int hv_debug_init(void)
172 hv_debug_root = debugfs_create_dir("hyperv", NULL);
173 if (IS_ERR(hv_debug_root)) {
174 pr_debug("debugfs_hyperv: hyperv/ not created\n");
175 return PTR_ERR(hv_debug_root);