hv_utils_transport.c 8.0 KB

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