xenbus_comms.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /******************************************************************************
  2. * xenbus_comms.c
  3. *
  4. * Low level code to talks to Xen Store: ringbuffer and event channel.
  5. *
  6. * Copyright (C) 2005 Rusty Russell, IBM Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version 2
  10. * as published by the Free Software Foundation; or, when distributed
  11. * separately from the Linux kernel or incorporated into other
  12. * software packages, subject to the following license:
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this source file (the "Software"), to deal in the Software without
  16. * restriction, including without limitation the rights to use, copy, modify,
  17. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  18. * and to permit persons to whom the Software is furnished to do so, subject to
  19. * the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  30. * IN THE SOFTWARE.
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #include <linux/wait.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/kthread.h>
  36. #include <linux/sched.h>
  37. #include <linux/err.h>
  38. #include <xen/xenbus.h>
  39. #include <asm/xen/hypervisor.h>
  40. #include <xen/events.h>
  41. #include <xen/page.h>
  42. #include "xenbus.h"
  43. /* A list of replies. Currently only one will ever be outstanding. */
  44. LIST_HEAD(xs_reply_list);
  45. /* A list of write requests. */
  46. LIST_HEAD(xb_write_list);
  47. DECLARE_WAIT_QUEUE_HEAD(xb_waitq);
  48. DEFINE_MUTEX(xb_write_mutex);
  49. /* Protect xenbus reader thread against save/restore. */
  50. DEFINE_MUTEX(xs_response_mutex);
  51. static int xenbus_irq;
  52. static struct task_struct *xenbus_task;
  53. static irqreturn_t wake_waiting(int irq, void *unused)
  54. {
  55. wake_up(&xb_waitq);
  56. return IRQ_HANDLED;
  57. }
  58. static int check_indexes(XENSTORE_RING_IDX cons, XENSTORE_RING_IDX prod)
  59. {
  60. return ((prod - cons) <= XENSTORE_RING_SIZE);
  61. }
  62. static void *get_output_chunk(XENSTORE_RING_IDX cons,
  63. XENSTORE_RING_IDX prod,
  64. char *buf, uint32_t *len)
  65. {
  66. *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
  67. if ((XENSTORE_RING_SIZE - (prod - cons)) < *len)
  68. *len = XENSTORE_RING_SIZE - (prod - cons);
  69. return buf + MASK_XENSTORE_IDX(prod);
  70. }
  71. static const void *get_input_chunk(XENSTORE_RING_IDX cons,
  72. XENSTORE_RING_IDX prod,
  73. const char *buf, uint32_t *len)
  74. {
  75. *len = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
  76. if ((prod - cons) < *len)
  77. *len = prod - cons;
  78. return buf + MASK_XENSTORE_IDX(cons);
  79. }
  80. static int xb_data_to_write(void)
  81. {
  82. struct xenstore_domain_interface *intf = xen_store_interface;
  83. return (intf->req_prod - intf->req_cons) != XENSTORE_RING_SIZE &&
  84. !list_empty(&xb_write_list);
  85. }
  86. /**
  87. * xb_write - low level write
  88. * @data: buffer to send
  89. * @len: length of buffer
  90. *
  91. * Returns number of bytes written or -err.
  92. */
  93. static int xb_write(const void *data, unsigned int len)
  94. {
  95. struct xenstore_domain_interface *intf = xen_store_interface;
  96. XENSTORE_RING_IDX cons, prod;
  97. unsigned int bytes = 0;
  98. while (len != 0) {
  99. void *dst;
  100. unsigned int avail;
  101. /* Read indexes, then verify. */
  102. cons = intf->req_cons;
  103. prod = intf->req_prod;
  104. if (!check_indexes(cons, prod)) {
  105. intf->req_cons = intf->req_prod = 0;
  106. return -EIO;
  107. }
  108. if (!xb_data_to_write())
  109. return bytes;
  110. /* Must write data /after/ reading the consumer index. */
  111. virt_mb();
  112. dst = get_output_chunk(cons, prod, intf->req, &avail);
  113. if (avail == 0)
  114. continue;
  115. if (avail > len)
  116. avail = len;
  117. memcpy(dst, data, avail);
  118. data += avail;
  119. len -= avail;
  120. bytes += avail;
  121. /* Other side must not see new producer until data is there. */
  122. virt_wmb();
  123. intf->req_prod += avail;
  124. /* Implies mb(): other side will see the updated producer. */
  125. if (prod <= intf->req_cons)
  126. notify_remote_via_evtchn(xen_store_evtchn);
  127. }
  128. return bytes;
  129. }
  130. static int xb_data_to_read(void)
  131. {
  132. struct xenstore_domain_interface *intf = xen_store_interface;
  133. return (intf->rsp_cons != intf->rsp_prod);
  134. }
  135. static int xb_read(void *data, unsigned int len)
  136. {
  137. struct xenstore_domain_interface *intf = xen_store_interface;
  138. XENSTORE_RING_IDX cons, prod;
  139. unsigned int bytes = 0;
  140. while (len != 0) {
  141. unsigned int avail;
  142. const char *src;
  143. /* Read indexes, then verify. */
  144. cons = intf->rsp_cons;
  145. prod = intf->rsp_prod;
  146. if (cons == prod)
  147. return bytes;
  148. if (!check_indexes(cons, prod)) {
  149. intf->rsp_cons = intf->rsp_prod = 0;
  150. return -EIO;
  151. }
  152. src = get_input_chunk(cons, prod, intf->rsp, &avail);
  153. if (avail == 0)
  154. continue;
  155. if (avail > len)
  156. avail = len;
  157. /* Must read data /after/ reading the producer index. */
  158. virt_rmb();
  159. memcpy(data, src, avail);
  160. data += avail;
  161. len -= avail;
  162. bytes += avail;
  163. /* Other side must not see free space until we've copied out */
  164. virt_mb();
  165. intf->rsp_cons += avail;
  166. /* Implies mb(): other side will see the updated consumer. */
  167. if (intf->rsp_prod - cons >= XENSTORE_RING_SIZE)
  168. notify_remote_via_evtchn(xen_store_evtchn);
  169. }
  170. return bytes;
  171. }
  172. static int process_msg(void)
  173. {
  174. static struct {
  175. struct xsd_sockmsg msg;
  176. char *body;
  177. union {
  178. void *alloc;
  179. struct xs_watch_event *watch;
  180. };
  181. bool in_msg;
  182. bool in_hdr;
  183. unsigned int read;
  184. } state;
  185. struct xb_req_data *req;
  186. int err;
  187. unsigned int len;
  188. if (!state.in_msg) {
  189. state.in_msg = true;
  190. state.in_hdr = true;
  191. state.read = 0;
  192. /*
  193. * We must disallow save/restore while reading a message.
  194. * A partial read across s/r leaves us out of sync with
  195. * xenstored.
  196. * xs_response_mutex is locked as long as we are processing one
  197. * message. state.in_msg will be true as long as we are holding
  198. * the lock here.
  199. */
  200. mutex_lock(&xs_response_mutex);
  201. if (!xb_data_to_read()) {
  202. /* We raced with save/restore: pending data 'gone'. */
  203. mutex_unlock(&xs_response_mutex);
  204. state.in_msg = false;
  205. return 0;
  206. }
  207. }
  208. if (state.in_hdr) {
  209. if (state.read != sizeof(state.msg)) {
  210. err = xb_read((void *)&state.msg + state.read,
  211. sizeof(state.msg) - state.read);
  212. if (err < 0)
  213. goto out;
  214. state.read += err;
  215. if (state.read != sizeof(state.msg))
  216. return 0;
  217. if (state.msg.len > XENSTORE_PAYLOAD_MAX) {
  218. err = -EINVAL;
  219. goto out;
  220. }
  221. }
  222. len = state.msg.len + 1;
  223. if (state.msg.type == XS_WATCH_EVENT)
  224. len += sizeof(*state.watch);
  225. state.alloc = kmalloc(len, GFP_NOIO | __GFP_HIGH);
  226. if (!state.alloc)
  227. return -ENOMEM;
  228. if (state.msg.type == XS_WATCH_EVENT)
  229. state.body = state.watch->body;
  230. else
  231. state.body = state.alloc;
  232. state.in_hdr = false;
  233. state.read = 0;
  234. }
  235. err = xb_read(state.body + state.read, state.msg.len - state.read);
  236. if (err < 0)
  237. goto out;
  238. state.read += err;
  239. if (state.read != state.msg.len)
  240. return 0;
  241. state.body[state.msg.len] = '\0';
  242. if (state.msg.type == XS_WATCH_EVENT) {
  243. state.watch->len = state.msg.len;
  244. err = xs_watch_msg(state.watch);
  245. } else {
  246. err = -ENOENT;
  247. mutex_lock(&xb_write_mutex);
  248. list_for_each_entry(req, &xs_reply_list, list) {
  249. if (req->msg.req_id == state.msg.req_id) {
  250. list_del(&req->list);
  251. err = 0;
  252. break;
  253. }
  254. }
  255. mutex_unlock(&xb_write_mutex);
  256. if (err)
  257. goto out;
  258. if (req->state == xb_req_state_wait_reply) {
  259. req->msg.req_id = req->caller_req_id;
  260. req->msg.type = state.msg.type;
  261. req->msg.len = state.msg.len;
  262. req->body = state.body;
  263. /* write body, then update state */
  264. virt_wmb();
  265. req->state = xb_req_state_got_reply;
  266. req->cb(req);
  267. } else
  268. kfree(req);
  269. }
  270. mutex_unlock(&xs_response_mutex);
  271. state.in_msg = false;
  272. state.alloc = NULL;
  273. return err;
  274. out:
  275. mutex_unlock(&xs_response_mutex);
  276. state.in_msg = false;
  277. kfree(state.alloc);
  278. state.alloc = NULL;
  279. return err;
  280. }
  281. static int process_writes(void)
  282. {
  283. static struct {
  284. struct xb_req_data *req;
  285. int idx;
  286. unsigned int written;
  287. } state;
  288. void *base;
  289. unsigned int len;
  290. int err = 0;
  291. if (!xb_data_to_write())
  292. return 0;
  293. mutex_lock(&xb_write_mutex);
  294. if (!state.req) {
  295. state.req = list_first_entry(&xb_write_list,
  296. struct xb_req_data, list);
  297. state.idx = -1;
  298. state.written = 0;
  299. }
  300. if (state.req->state == xb_req_state_aborted)
  301. goto out_err;
  302. while (state.idx < state.req->num_vecs) {
  303. if (state.idx < 0) {
  304. base = &state.req->msg;
  305. len = sizeof(state.req->msg);
  306. } else {
  307. base = state.req->vec[state.idx].iov_base;
  308. len = state.req->vec[state.idx].iov_len;
  309. }
  310. err = xb_write(base + state.written, len - state.written);
  311. if (err < 0)
  312. goto out_err;
  313. state.written += err;
  314. if (state.written != len)
  315. goto out;
  316. state.idx++;
  317. state.written = 0;
  318. }
  319. list_del(&state.req->list);
  320. state.req->state = xb_req_state_wait_reply;
  321. list_add_tail(&state.req->list, &xs_reply_list);
  322. state.req = NULL;
  323. out:
  324. mutex_unlock(&xb_write_mutex);
  325. return 0;
  326. out_err:
  327. state.req->msg.type = XS_ERROR;
  328. state.req->err = err;
  329. list_del(&state.req->list);
  330. if (state.req->state == xb_req_state_aborted)
  331. kfree(state.req);
  332. else {
  333. /* write err, then update state */
  334. virt_wmb();
  335. state.req->state = xb_req_state_got_reply;
  336. wake_up(&state.req->wq);
  337. }
  338. mutex_unlock(&xb_write_mutex);
  339. state.req = NULL;
  340. return err;
  341. }
  342. static int xb_thread_work(void)
  343. {
  344. return xb_data_to_read() || xb_data_to_write();
  345. }
  346. static int xenbus_thread(void *unused)
  347. {
  348. int err;
  349. while (!kthread_should_stop()) {
  350. if (wait_event_interruptible(xb_waitq, xb_thread_work()))
  351. continue;
  352. err = process_msg();
  353. if (err == -ENOMEM)
  354. schedule();
  355. else if (err)
  356. pr_warn_ratelimited("error %d while reading message\n",
  357. err);
  358. err = process_writes();
  359. if (err)
  360. pr_warn_ratelimited("error %d while writing message\n",
  361. err);
  362. }
  363. xenbus_task = NULL;
  364. return 0;
  365. }
  366. /**
  367. * xb_init_comms - Set up interrupt handler off store event channel.
  368. */
  369. int xb_init_comms(void)
  370. {
  371. struct xenstore_domain_interface *intf = xen_store_interface;
  372. if (intf->req_prod != intf->req_cons)
  373. pr_err("request ring is not quiescent (%08x:%08x)!\n",
  374. intf->req_cons, intf->req_prod);
  375. if (intf->rsp_prod != intf->rsp_cons) {
  376. pr_warn("response ring is not quiescent (%08x:%08x): fixing up\n",
  377. intf->rsp_cons, intf->rsp_prod);
  378. /* breaks kdump */
  379. if (!reset_devices)
  380. intf->rsp_cons = intf->rsp_prod;
  381. }
  382. if (xenbus_irq) {
  383. /* Already have an irq; assume we're resuming */
  384. rebind_evtchn_irq(xen_store_evtchn, xenbus_irq);
  385. } else {
  386. int err;
  387. err = bind_evtchn_to_irqhandler(xen_store_evtchn, wake_waiting,
  388. 0, "xenbus", &xb_waitq);
  389. if (err < 0) {
  390. pr_err("request irq failed %i\n", err);
  391. return err;
  392. }
  393. xenbus_irq = err;
  394. if (!xenbus_task) {
  395. xenbus_task = kthread_run(xenbus_thread, NULL,
  396. "xenbus");
  397. if (IS_ERR(xenbus_task))
  398. return PTR_ERR(xenbus_task);
  399. }
  400. }
  401. return 0;
  402. }
  403. void xb_deinit_comms(void)
  404. {
  405. unbind_from_irqhandler(xenbus_irq, &xb_waitq);
  406. xenbus_irq = 0;
  407. }