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
25 static DEFINE_SPINLOCK(hvt_list_lock);
26 static struct list_head hvt_list = LIST_HEAD_INIT(hvt_list);
27
hvt_reset(struct hvutil_transport * hvt)28 static void hvt_reset(struct hvutil_transport *hvt)
29 {
30 mutex_lock(&hvt->outmsg_lock);
31 kfree(hvt->outmsg);
32 hvt->outmsg = NULL;
33 hvt->outmsg_len = 0;
34 mutex_unlock(&hvt->outmsg_lock);
35 if (hvt->on_reset)
36 hvt->on_reset();
37 }
38
hvt_op_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)39 static ssize_t hvt_op_read(struct file *file, char __user *buf,
40 size_t count, loff_t *ppos)
41 {
42 struct hvutil_transport *hvt;
43 int ret;
44
45 hvt = container_of(file->f_op, struct hvutil_transport, fops);
46
47 if (wait_event_interruptible(hvt->outmsg_q, hvt->outmsg_len > 0))
48 return -EINTR;
49
50 mutex_lock(&hvt->outmsg_lock);
51 if (!hvt->outmsg) {
52 ret = -EAGAIN;
53 goto out_unlock;
54 }
55
56 if (count < hvt->outmsg_len) {
57 ret = -EINVAL;
58 goto out_unlock;
59 }
60
61 if (!copy_to_user(buf, hvt->outmsg, hvt->outmsg_len))
62 ret = hvt->outmsg_len;
63 else
64 ret = -EFAULT;
65
66 kfree(hvt->outmsg);
67 hvt->outmsg = NULL;
68 hvt->outmsg_len = 0;
69
70 out_unlock:
71 mutex_unlock(&hvt->outmsg_lock);
72 return ret;
73 }
74
hvt_op_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)75 static ssize_t hvt_op_write(struct file *file, const char __user *buf,
76 size_t count, loff_t *ppos)
77 {
78 struct hvutil_transport *hvt;
79 u8 *inmsg;
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->on_msg(inmsg, count))
88 return -EFAULT;
89 kfree(inmsg);
90
91 return count;
92 }
93
hvt_op_poll(struct file * file,poll_table * wait)94 static unsigned int hvt_op_poll(struct file *file, poll_table *wait)
95 {
96 struct hvutil_transport *hvt;
97
98 hvt = container_of(file->f_op, struct hvutil_transport, fops);
99
100 poll_wait(file, &hvt->outmsg_q, wait);
101 if (hvt->outmsg_len > 0)
102 return POLLIN | POLLRDNORM;
103
104 return 0;
105 }
106
hvt_op_open(struct inode * inode,struct file * file)107 static int hvt_op_open(struct inode *inode, struct file *file)
108 {
109 struct hvutil_transport *hvt;
110
111 hvt = container_of(file->f_op, struct hvutil_transport, fops);
112
113 /*
114 * Switching to CHARDEV mode. We switch bach to INIT when device
115 * gets released.
116 */
117 if (hvt->mode == HVUTIL_TRANSPORT_INIT)
118 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
119 else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
120 /*
121 * We're switching from netlink communication to using char
122 * device. Issue the reset first.
123 */
124 hvt_reset(hvt);
125 hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
126 } else
127 return -EBUSY;
128
129 return 0;
130 }
131
hvt_op_release(struct inode * inode,struct file * file)132 static int hvt_op_release(struct inode *inode, struct file *file)
133 {
134 struct hvutil_transport *hvt;
135
136 hvt = container_of(file->f_op, struct hvutil_transport, fops);
137
138 hvt->mode = HVUTIL_TRANSPORT_INIT;
139 /*
140 * Cleanup message buffers to avoid spurious messages when the daemon
141 * connects back.
142 */
143 hvt_reset(hvt);
144
145 return 0;
146 }
147
hvt_cn_callback(struct cn_msg * msg,struct netlink_skb_parms * nsp)148 static void hvt_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
149 {
150 struct hvutil_transport *hvt, *hvt_found = NULL;
151
152 spin_lock(&hvt_list_lock);
153 list_for_each_entry(hvt, &hvt_list, list) {
154 if (hvt->cn_id.idx == msg->id.idx &&
155 hvt->cn_id.val == msg->id.val) {
156 hvt_found = hvt;
157 break;
158 }
159 }
160 spin_unlock(&hvt_list_lock);
161 if (!hvt_found) {
162 pr_warn("hvt_cn_callback: spurious message received!\n");
163 return;
164 }
165
166 /*
167 * Switching to NETLINK mode. Switching to CHARDEV happens when someone
168 * opens the device.
169 */
170 if (hvt->mode == HVUTIL_TRANSPORT_INIT)
171 hvt->mode = HVUTIL_TRANSPORT_NETLINK;
172
173 if (hvt->mode == HVUTIL_TRANSPORT_NETLINK)
174 hvt_found->on_msg(msg->data, msg->len);
175 else
176 pr_warn("hvt_cn_callback: unexpected netlink message!\n");
177 }
178
hvutil_transport_send(struct hvutil_transport * hvt,void * msg,int len)179 int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len)
180 {
181 struct cn_msg *cn_msg;
182 int ret = 0;
183
184 if (hvt->mode == HVUTIL_TRANSPORT_INIT) {
185 return -EINVAL;
186 } else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
187 cn_msg = kzalloc(sizeof(*cn_msg) + len, GFP_ATOMIC);
188 if (!cn_msg)
189 return -ENOMEM;
190 cn_msg->id.idx = hvt->cn_id.idx;
191 cn_msg->id.val = hvt->cn_id.val;
192 cn_msg->len = len;
193 memcpy(cn_msg->data, msg, len);
194 ret = cn_netlink_send(cn_msg, 0, 0, GFP_ATOMIC);
195 kfree(cn_msg);
196 return ret;
197 }
198 /* HVUTIL_TRANSPORT_CHARDEV */
199 mutex_lock(&hvt->outmsg_lock);
200 if (hvt->outmsg) {
201 /* Previous message wasn't received */
202 ret = -EFAULT;
203 goto out_unlock;
204 }
205 hvt->outmsg = kzalloc(len, GFP_KERNEL);
206 if (hvt->outmsg) {
207 memcpy(hvt->outmsg, msg, len);
208 hvt->outmsg_len = len;
209 wake_up_interruptible(&hvt->outmsg_q);
210 } else
211 ret = -ENOMEM;
212 out_unlock:
213 mutex_unlock(&hvt->outmsg_lock);
214 return ret;
215 }
216
hvutil_transport_init(const char * name,u32 cn_idx,u32 cn_val,int (* on_msg)(void *,int),void (* on_reset)(void))217 struct hvutil_transport *hvutil_transport_init(const char *name,
218 u32 cn_idx, u32 cn_val,
219 int (*on_msg)(void *, int),
220 void (*on_reset)(void))
221 {
222 struct hvutil_transport *hvt;
223
224 hvt = kzalloc(sizeof(*hvt), GFP_KERNEL);
225 if (!hvt)
226 return NULL;
227
228 hvt->cn_id.idx = cn_idx;
229 hvt->cn_id.val = cn_val;
230
231 hvt->mdev.minor = MISC_DYNAMIC_MINOR;
232 hvt->mdev.name = name;
233
234 hvt->fops.owner = THIS_MODULE;
235 hvt->fops.read = hvt_op_read;
236 hvt->fops.write = hvt_op_write;
237 hvt->fops.poll = hvt_op_poll;
238 hvt->fops.open = hvt_op_open;
239 hvt->fops.release = hvt_op_release;
240
241 hvt->mdev.fops = &hvt->fops;
242
243 init_waitqueue_head(&hvt->outmsg_q);
244 mutex_init(&hvt->outmsg_lock);
245
246 spin_lock(&hvt_list_lock);
247 list_add(&hvt->list, &hvt_list);
248 spin_unlock(&hvt_list_lock);
249
250 hvt->on_msg = on_msg;
251 hvt->on_reset = on_reset;
252
253 if (misc_register(&hvt->mdev))
254 goto err_free_hvt;
255
256 /* Use cn_id.idx/cn_id.val to determine if we need to setup netlink */
257 if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0 &&
258 cn_add_callback(&hvt->cn_id, name, hvt_cn_callback))
259 goto err_free_hvt;
260
261 return hvt;
262
263 err_free_hvt:
264 kfree(hvt);
265 return NULL;
266 }
267
hvutil_transport_destroy(struct hvutil_transport * hvt)268 void hvutil_transport_destroy(struct hvutil_transport *hvt)
269 {
270 spin_lock(&hvt_list_lock);
271 list_del(&hvt->list);
272 spin_unlock(&hvt_list_lock);
273 if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0)
274 cn_del_callback(&hvt->cn_id);
275 misc_deregister(&hvt->mdev);
276 kfree(hvt->outmsg);
277 kfree(hvt);
278 }
279