hv_utils_transport.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. out_unlock:
  62. mutex_unlock(&hvt->lock);
  63. return ret;
  64. }
  65. static ssize_t hvt_op_write(struct file *file, const char __user *buf,
  66. size_t count, loff_t *ppos)
  67. {
  68. struct hvutil_transport *hvt;
  69. u8 *inmsg;
  70. int ret;
  71. hvt = container_of(file->f_op, struct hvutil_transport, fops);
  72. inmsg = memdup_user(buf, count);
  73. if (IS_ERR(inmsg))
  74. return PTR_ERR(inmsg);
  75. if (hvt->mode == HVUTIL_TRANSPORT_DESTROY)
  76. ret = -EBADF;
  77. else
  78. ret = hvt->on_msg(inmsg, count);
  79. kfree(inmsg);
  80. return ret ? ret : count;
  81. }
  82. static unsigned int hvt_op_poll(struct file *file, poll_table *wait)
  83. {
  84. struct hvutil_transport *hvt;
  85. hvt = container_of(file->f_op, struct hvutil_transport, fops);
  86. poll_wait(file, &hvt->outmsg_q, wait);
  87. if (hvt->mode == HVUTIL_TRANSPORT_DESTROY)
  88. return POLLERR | POLLHUP;
  89. if (hvt->outmsg_len > 0)
  90. return POLLIN | POLLRDNORM;
  91. return 0;
  92. }
  93. static int hvt_op_open(struct inode *inode, struct file *file)
  94. {
  95. struct hvutil_transport *hvt;
  96. int ret = 0;
  97. bool issue_reset = false;
  98. hvt = container_of(file->f_op, struct hvutil_transport, fops);
  99. mutex_lock(&hvt->lock);
  100. if (hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
  101. ret = -EBADF;
  102. } else if (hvt->mode == HVUTIL_TRANSPORT_INIT) {
  103. /*
  104. * Switching to CHARDEV mode. We switch bach to INIT when
  105. * device gets released.
  106. */
  107. hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
  108. }
  109. else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
  110. /*
  111. * We're switching from netlink communication to using char
  112. * device. Issue the reset first.
  113. */
  114. issue_reset = true;
  115. hvt->mode = HVUTIL_TRANSPORT_CHARDEV;
  116. } else {
  117. ret = -EBUSY;
  118. }
  119. if (issue_reset)
  120. hvt_reset(hvt);
  121. mutex_unlock(&hvt->lock);
  122. return ret;
  123. }
  124. static void hvt_transport_free(struct hvutil_transport *hvt)
  125. {
  126. misc_deregister(&hvt->mdev);
  127. kfree(hvt->outmsg);
  128. kfree(hvt);
  129. }
  130. static int hvt_op_release(struct inode *inode, struct file *file)
  131. {
  132. struct hvutil_transport *hvt;
  133. int mode_old;
  134. hvt = container_of(file->f_op, struct hvutil_transport, fops);
  135. mutex_lock(&hvt->lock);
  136. mode_old = hvt->mode;
  137. if (hvt->mode != HVUTIL_TRANSPORT_DESTROY)
  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. mutex_unlock(&hvt->lock);
  145. if (mode_old == HVUTIL_TRANSPORT_DESTROY)
  146. hvt_transport_free(hvt);
  147. return 0;
  148. }
  149. static void hvt_cn_callback(struct cn_msg *msg, struct netlink_skb_parms *nsp)
  150. {
  151. struct hvutil_transport *hvt, *hvt_found = NULL;
  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. * Switching to NETLINK mode. Switching to CHARDEV happens when someone
  167. * opens the device.
  168. */
  169. mutex_lock(&hvt->lock);
  170. if (hvt->mode == HVUTIL_TRANSPORT_INIT)
  171. hvt->mode = HVUTIL_TRANSPORT_NETLINK;
  172. if (hvt->mode == HVUTIL_TRANSPORT_NETLINK)
  173. hvt_found->on_msg(msg->data, msg->len);
  174. else
  175. pr_warn("hvt_cn_callback: unexpected netlink message!\n");
  176. mutex_unlock(&hvt->lock);
  177. }
  178. int hvutil_transport_send(struct hvutil_transport *hvt, void *msg, int len)
  179. {
  180. struct cn_msg *cn_msg;
  181. int ret = 0;
  182. if (hvt->mode == HVUTIL_TRANSPORT_INIT ||
  183. hvt->mode == HVUTIL_TRANSPORT_DESTROY) {
  184. return -EINVAL;
  185. } else if (hvt->mode == HVUTIL_TRANSPORT_NETLINK) {
  186. cn_msg = kzalloc(sizeof(*cn_msg) + len, GFP_ATOMIC);
  187. if (!cn_msg)
  188. return -ENOMEM;
  189. cn_msg->id.idx = hvt->cn_id.idx;
  190. cn_msg->id.val = hvt->cn_id.val;
  191. cn_msg->len = len;
  192. memcpy(cn_msg->data, msg, len);
  193. ret = cn_netlink_send(cn_msg, 0, 0, GFP_ATOMIC);
  194. kfree(cn_msg);
  195. return ret;
  196. }
  197. /* HVUTIL_TRANSPORT_CHARDEV */
  198. mutex_lock(&hvt->lock);
  199. if (hvt->mode != HVUTIL_TRANSPORT_CHARDEV) {
  200. ret = -EINVAL;
  201. goto out_unlock;
  202. }
  203. if (hvt->outmsg) {
  204. /* Previous message wasn't received */
  205. ret = -EFAULT;
  206. goto out_unlock;
  207. }
  208. hvt->outmsg = kzalloc(len, GFP_KERNEL);
  209. if (hvt->outmsg) {
  210. memcpy(hvt->outmsg, msg, len);
  211. hvt->outmsg_len = len;
  212. wake_up_interruptible(&hvt->outmsg_q);
  213. } else
  214. ret = -ENOMEM;
  215. out_unlock:
  216. mutex_unlock(&hvt->lock);
  217. return ret;
  218. }
  219. struct hvutil_transport *hvutil_transport_init(const char *name,
  220. u32 cn_idx, u32 cn_val,
  221. int (*on_msg)(void *, int),
  222. void (*on_reset)(void))
  223. {
  224. struct hvutil_transport *hvt;
  225. hvt = kzalloc(sizeof(*hvt), GFP_KERNEL);
  226. if (!hvt)
  227. return NULL;
  228. hvt->cn_id.idx = cn_idx;
  229. hvt->cn_id.val = cn_val;
  230. hvt->mdev.minor = MISC_DYNAMIC_MINOR;
  231. hvt->mdev.name = name;
  232. hvt->fops.owner = THIS_MODULE;
  233. hvt->fops.read = hvt_op_read;
  234. hvt->fops.write = hvt_op_write;
  235. hvt->fops.poll = hvt_op_poll;
  236. hvt->fops.open = hvt_op_open;
  237. hvt->fops.release = hvt_op_release;
  238. hvt->mdev.fops = &hvt->fops;
  239. init_waitqueue_head(&hvt->outmsg_q);
  240. mutex_init(&hvt->lock);
  241. spin_lock(&hvt_list_lock);
  242. list_add(&hvt->list, &hvt_list);
  243. spin_unlock(&hvt_list_lock);
  244. hvt->on_msg = on_msg;
  245. hvt->on_reset = on_reset;
  246. if (misc_register(&hvt->mdev))
  247. goto err_free_hvt;
  248. /* Use cn_id.idx/cn_id.val to determine if we need to setup netlink */
  249. if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0 &&
  250. cn_add_callback(&hvt->cn_id, name, hvt_cn_callback))
  251. goto err_free_hvt;
  252. return hvt;
  253. err_free_hvt:
  254. spin_lock(&hvt_list_lock);
  255. list_del(&hvt->list);
  256. spin_unlock(&hvt_list_lock);
  257. kfree(hvt);
  258. return NULL;
  259. }
  260. void hvutil_transport_destroy(struct hvutil_transport *hvt)
  261. {
  262. int mode_old;
  263. mutex_lock(&hvt->lock);
  264. mode_old = hvt->mode;
  265. hvt->mode = HVUTIL_TRANSPORT_DESTROY;
  266. wake_up_interruptible(&hvt->outmsg_q);
  267. mutex_unlock(&hvt->lock);
  268. /*
  269. * In case we were in 'chardev' mode we still have an open fd so we
  270. * have to defer freeing the device. Netlink interface can be freed
  271. * now.
  272. */
  273. spin_lock(&hvt_list_lock);
  274. list_del(&hvt->list);
  275. spin_unlock(&hvt_list_lock);
  276. if (hvt->cn_id.idx > 0 && hvt->cn_id.val > 0)
  277. cn_del_callback(&hvt->cn_id);
  278. if (mode_old != HVUTIL_TRANSPORT_CHARDEV)
  279. hvt_transport_free(hvt);
  280. }