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