xref: /linux/drivers/hv/hv_utils_transport.c (revision 221533629550e920580ab428f13ffebf54063b95)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Kernel/userspace transport abstraction for Hyper-V util driver.
4  *
5  * Copyright (C) 2015, Vitaly Kuznetsov <vkuznets@redhat.com>
6  */
7 
8 #include <linux/slab.h>
9 #include <linux/fs.h>
10 #include <linux/poll.h>
11 
12 #include "hyperv_vmbus.h"
13 #include "hv_utils_transport.h"
14 
15 static DEFINE_SPINLOCK(hvt_list_lock);
16 static LIST_HEAD(hvt_list);
17 
hvt_reset(struct hvutil_transport * hvt)18 static void hvt_reset(struct hvutil_transport *hvt)
19 {
20 	kfree(hvt->outmsg);
21 	hvt->outmsg = NULL;
22 	hvt->outmsg_len = 0;
23 	if (hvt->on_reset)
24 		hvt->on_reset();
25 }
26 
hvt_op_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)27 static ssize_t hvt_op_read(struct file *file, char __user *buf,
28 			   size_t count, loff_t *ppos)
29 {
30 	struct hvutil_transport *hvt;
31 	int ret;
32 
33 	hvt = container_of(file->f_op, struct hvutil_transport, fops);
34 
35 	if (wait_event_interruptible(hvt->outmsg_q, hvt->outmsg_len > 0 ||
36 				     hvt->mode != HVUTIL_TRANSPORT_CHARDEV))
37 		return -EINTR;
38 
39 	mutex_lock(&hvt->lock);
40 
41 	if (hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
42 		ret = -EBADF;
43 		goto out_unlock;
44 	}
45 
46 	if (!hvt->outmsg) {
47 		ret = -EAGAIN;
48 		goto out_unlock;
49 	}
50 
51 	if (count < hvt->outmsg_len) {
52 		ret = -EINVAL;
53 		goto out_unlock;
54 	}
55 
56 	if (!copy_to_user(buf, hvt->outmsg, hvt->outmsg_len))
57 		ret = hvt->outmsg_len;
58 	else
59 		ret = -EFAULT;
60 
61 	kfree(hvt->outmsg);
62 	hvt->outmsg = NULL;
63 	hvt->outmsg_len = 0;
64 
65 	if (hvt->on_read)
66 		hvt->on_read();
67 	hvt->on_read = NULL;
68 
69 out_unlock:
70 	mutex_unlock(&hvt->lock);
71 	return ret;
72 }
73 
hvt_op_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)74 static ssize_t hvt_op_write(struct file *file, const char __user *buf,
75 			    size_t count, loff_t *ppos)
76 {
77 	struct hvutil_transport *hvt;
78 	u8 *inmsg;
79 	int ret;
80 
81 	hvt = container_of(file->f_op, struct hvutil_transport, fops);
82 
83 	inmsg = memdup_user(buf, count);
84 	if (IS_ERR(inmsg))
85 		return PTR_ERR(inmsg);
86 
87 	if (hvt->mode == HVUTIL_TRANSPORT_DESTROY)
88 		ret = -EBADF;
89 	else
90 		ret = hvt->on_msg(inmsg, count);
91 
92 	kfree(inmsg);
93 
94 	return ret ? ret : count;
95 }
96 
hvt_op_poll(struct file * file,poll_table * wait)97 static __poll_t hvt_op_poll(struct file *file, poll_table *wait)
98 {
99 	struct hvutil_transport *hvt;
100 
101 	hvt = container_of(file->f_op, struct hvutil_transport, fops);
102 
103 	poll_wait(file, &hvt->outmsg_q, wait);
104 
105 	if (hvt->mode == HVUTIL_TRANSPORT_DESTROY)
106 		return EPOLLERR | EPOLLHUP;
107 
108 	if (hvt->outmsg_len > 0)
109 		return EPOLLIN | EPOLLRDNORM;
110 
111 	return 0;
112 }
113 
hvt_op_open(struct inode * inode,struct file * file)114 static int hvt_op_open(struct inode *inode, struct file *file)
115 {
116 	struct hvutil_transport *hvt;
117 	int ret = 0;
118 	bool issue_reset = false;
119 
120 	hvt = container_of(file->f_op, struct hvutil_transport, fops);
121 
122 	mutex_lock(&hvt->lock);
123 
124 	if (hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
125 		ret = -EBADF;
126 	} else if (hvt->mode == HVUTIL_TRANSPORT_INIT) {
127 		/*
128 		 * Switching to CHARDEV mode. We switch bach to INIT when
129 		 * device gets released.
130 		 */
131 		hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
132 	} else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
133 		/*
134 		 * We're switching from netlink communication to using char
135 		 * device. Issue the reset first.
136 		 */
137 		issue_reset = true;
138 		hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
139 	} else {
140 		ret = -EBUSY;
141 	}
142 
143 	if (issue_reset)
144 		hvt_reset(hvt);
145 
146 	mutex_unlock(&hvt->lock);
147 
148 	return ret;
149 }
150 
hvt_transport_free(struct hvutil_transport * hvt)151 static void hvt_transport_free(struct hvutil_transport *hvt)
152 {
153 	misc_deregister(&hvt->mdev);
154 	kfree(hvt->outmsg);
155 	kfree(hvt);
156 }
157 
hvt_op_release(struct inode * inode,struct file * file)158 static int hvt_op_release(struct inode *inode, struct file *file)
159 {
160 	struct hvutil_transport *hvt;
161 	int mode_old;
162 
163 	hvt = container_of(file->f_op, struct hvutil_transport, fops);
164 
165 	mutex_lock(&hvt->lock);
166 	mode_old = hvt->mode;
167 	if (hvt->mode != HVUTIL_TRANSPORT_DESTROY)
168 		hvt->mode = HVUTIL_TRANSPORT_INIT;
169 	/*
170 	 * Cleanup message buffers to avoid spurious messages when the daemon
171 	 * connects back.
172 	 */
173 	hvt_reset(hvt);
174 
175 	if (mode_old == HVUTIL_TRANSPORT_DESTROY)
176 		complete(&hvt->release);
177 
178 	mutex_unlock(&hvt->lock);
179 
180 	return 0;
181 }
182 
hvt_cn_callback(struct cn_msg * msg,struct netlink_skb_parms * nsp)183 static void hvt_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
184 {
185 	struct hvutil_transport *hvt, *hvt_found = NULL;
186 
187 	spin_lock(&hvt_list_lock);
188 	list_for_each_entry(hvt, &hvt_list, list) {
189 		if (hvt->cn_id.idx == msg->id.idx &&
190 		    hvt->cn_id.val == msg->id.val) {
191 			hvt_found = hvt;
192 			break;
193 		}
194 	}
195 	spin_unlock(&hvt_list_lock);
196 	if (!hvt_found) {
197 		pr_warn("%s: spurious message received!\n", __func__);
198 		return;
199 	}
200 
201 	/*
202 	 * Switching to NETLINK mode. Switching to CHARDEV happens when someone
203 	 * opens the device.
204 	 */
205 	mutex_lock(&hvt->lock);
206 	if (hvt->mode == HVUTIL_TRANSPORT_INIT)
207 		hvt->mode = HVUTIL_TRANSPORT_NETLINK;
208 
209 	if (hvt->mode == HVUTIL_TRANSPORT_NETLINK)
210 		hvt_found->on_msg(msg->data, msg->len);
211 	else
212 		pr_warn("%s: unexpected netlink message!\n", __func__);
213 	mutex_unlock(&hvt->lock);
214 }
215 
hvutil_transport_send(struct hvutil_transport * hvt,void * msg,int len,void (* on_read_cb)(void))216 int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len,
217 			  void (*on_read_cb)(void))
218 {
219 	struct cn_msg *cn_msg;
220 	int ret = 0;
221 
222 	if (hvt->mode == HVUTIL_TRANSPORT_INIT ||
223 	    hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
224 		return -EINVAL;
225 	} else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
226 		cn_msg = kzalloc(sizeof(*cn_msg) + len, GFP_ATOMIC);
227 		if (!cn_msg)
228 			return -ENOMEM;
229 		cn_msg->id.idx = hvt->cn_id.idx;
230 		cn_msg->id.val = hvt->cn_id.val;
231 		cn_msg->len = len;
232 		memcpy(cn_msg->data, msg, len);
233 		ret = cn_netlink_send(cn_msg, 0, 0, GFP_ATOMIC);
234 		kfree(cn_msg);
235 		/*
236 		 * We don't know when netlink messages are delivered but unlike
237 		 * in CHARDEV mode we're not blocked and we can send next
238 		 * messages right away.
239 		 */
240 		if (on_read_cb)
241 			on_read_cb();
242 		return ret;
243 	}
244 	/* HVUTIL_TRANSPORT_CHARDEV */
245 	mutex_lock(&hvt->lock);
246 	if (hvt->mode != HVUTIL_TRANSPORT_CHARDEV) {
247 		ret = -EINVAL;
248 		goto out_unlock;
249 	}
250 
251 	if (hvt->outmsg) {
252 		/* Previous message wasn't received */
253 		ret = -EFAULT;
254 		goto out_unlock;
255 	}
256 	hvt->outmsg = kzalloc(len, GFP_KERNEL);
257 	if (hvt->outmsg) {
258 		memcpy(hvt->outmsg, msg, len);
259 		hvt->outmsg_len = len;
260 		hvt->on_read = on_read_cb;
261 		wake_up_interruptible(&hvt->outmsg_q);
262 	} else {
263 		ret = -ENOMEM;
264 	}
265 out_unlock:
266 	mutex_unlock(&hvt->lock);
267 	return ret;
268 }
269 
hvutil_transport_init(const char * name,u32 cn_idx,u32 cn_val,int (* on_msg)(void *,int),void (* on_reset)(void))270 struct hvutil_transport *hvutil_transport_init(const char *name,
271 					       u32 cn_idx, u32 cn_val,
272 					       int (*on_msg)(void *, int),
273 					       void (*on_reset)(void))
274 {
275 	struct hvutil_transport *hvt;
276 
277 	hvt = kzalloc(sizeof(*hvt), GFP_KERNEL);
278 	if (!hvt)
279 		return NULL;
280 
281 	hvt->cn_id.idx = cn_idx;
282 	hvt->cn_id.val = cn_val;
283 
284 	hvt->mdev.minor = MISC_DYNAMIC_MINOR;
285 	hvt->mdev.name = name;
286 
287 	hvt->fops.owner = THIS_MODULE;
288 	hvt->fops.read = hvt_op_read;
289 	hvt->fops.write = hvt_op_write;
290 	hvt->fops.poll = hvt_op_poll;
291 	hvt->fops.open = hvt_op_open;
292 	hvt->fops.release = hvt_op_release;
293 
294 	hvt->mdev.fops = &hvt->fops;
295 
296 	init_waitqueue_head(&hvt->outmsg_q);
297 	mutex_init(&hvt->lock);
298 	init_completion(&hvt->release);
299 
300 	spin_lock(&hvt_list_lock);
301 	list_add(&hvt->list, &hvt_list);
302 	spin_unlock(&hvt_list_lock);
303 
304 	hvt->on_msg = on_msg;
305 	hvt->on_reset = on_reset;
306 
307 	if (misc_register(&hvt->mdev))
308 		goto err_free_hvt;
309 
310 	/* Use cn_id.idx/cn_id.val to determine if we need to setup netlink */
311 	if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0 &&
312 	    cn_add_callback(&hvt->cn_id, name, hvt_cn_callback))
313 		goto err_free_hvt;
314 
315 	return hvt;
316 
317 err_free_hvt:
318 	spin_lock(&hvt_list_lock);
319 	list_del(&hvt->list);
320 	spin_unlock(&hvt_list_lock);
321 	kfree(hvt);
322 	return NULL;
323 }
324 
hvutil_transport_destroy(struct hvutil_transport * hvt)325 void hvutil_transport_destroy(struct hvutil_transport *hvt)
326 {
327 	int mode_old;
328 
329 	mutex_lock(&hvt->lock);
330 	mode_old = hvt->mode;
331 	hvt->mode = HVUTIL_TRANSPORT_DESTROY;
332 	wake_up_interruptible(&hvt->outmsg_q);
333 	mutex_unlock(&hvt->lock);
334 
335 	/*
336 	 * In case we were in 'chardev' mode we still have an open fd so we
337 	 * have to defer freeing the device. Netlink interface can be freed
338 	 * now.
339 	 */
340 	spin_lock(&hvt_list_lock);
341 	list_del(&hvt->list);
342 	spin_unlock(&hvt_list_lock);
343 	if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0)
344 		cn_del_callback(&hvt->cn_id);
345 
346 	if (mode_old == HVUTIL_TRANSPORT_CHARDEV)
347 		wait_for_completion(&hvt->release);
348 
349 	hvt_transport_free(hvt);
350 }
351