pvcalls-front.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. /*
  2. * (c) 2017 Stefano Stabellini <stefano@aporeto.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/net.h>
  16. #include <linux/socket.h>
  17. #include <net/sock.h>
  18. #include <xen/events.h>
  19. #include <xen/grant_table.h>
  20. #include <xen/xen.h>
  21. #include <xen/xenbus.h>
  22. #include <xen/interface/io/pvcalls.h>
  23. #include "pvcalls-front.h"
  24. #define PVCALLS_INVALID_ID UINT_MAX
  25. #define PVCALLS_RING_ORDER XENBUS_MAX_RING_GRANT_ORDER
  26. #define PVCALLS_NR_RSP_PER_RING __CONST_RING_SIZE(xen_pvcalls, XEN_PAGE_SIZE)
  27. #define PVCALLS_FRONT_MAX_SPIN 5000
  28. static struct proto pvcalls_proto = {
  29. .name = "PVCalls",
  30. .owner = THIS_MODULE,
  31. .obj_size = sizeof(struct sock),
  32. };
  33. struct pvcalls_bedata {
  34. struct xen_pvcalls_front_ring ring;
  35. grant_ref_t ref;
  36. int irq;
  37. struct list_head socket_mappings;
  38. spinlock_t socket_lock;
  39. wait_queue_head_t inflight_req;
  40. struct xen_pvcalls_response rsp[PVCALLS_NR_RSP_PER_RING];
  41. };
  42. /* Only one front/back connection supported. */
  43. static struct xenbus_device *pvcalls_front_dev;
  44. static atomic_t pvcalls_refcount;
  45. /* first increment refcount, then proceed */
  46. #define pvcalls_enter() { \
  47. atomic_inc(&pvcalls_refcount); \
  48. }
  49. /* first complete other operations, then decrement refcount */
  50. #define pvcalls_exit() { \
  51. atomic_dec(&pvcalls_refcount); \
  52. }
  53. struct sock_mapping {
  54. bool active_socket;
  55. struct list_head list;
  56. struct socket *sock;
  57. atomic_t refcount;
  58. union {
  59. struct {
  60. int irq;
  61. grant_ref_t ref;
  62. struct pvcalls_data_intf *ring;
  63. struct pvcalls_data data;
  64. struct mutex in_mutex;
  65. struct mutex out_mutex;
  66. wait_queue_head_t inflight_conn_req;
  67. } active;
  68. struct {
  69. /*
  70. * Socket status, needs to be 64-bit aligned due to the
  71. * test_and_* functions which have this requirement on arm64.
  72. */
  73. #define PVCALLS_STATUS_UNINITALIZED 0
  74. #define PVCALLS_STATUS_BIND 1
  75. #define PVCALLS_STATUS_LISTEN 2
  76. uint8_t status __attribute__((aligned(8)));
  77. /*
  78. * Internal state-machine flags.
  79. * Only one accept operation can be inflight for a socket.
  80. * Only one poll operation can be inflight for a given socket.
  81. * flags needs to be 64-bit aligned due to the test_and_*
  82. * functions which have this requirement on arm64.
  83. */
  84. #define PVCALLS_FLAG_ACCEPT_INFLIGHT 0
  85. #define PVCALLS_FLAG_POLL_INFLIGHT 1
  86. #define PVCALLS_FLAG_POLL_RET 2
  87. uint8_t flags __attribute__((aligned(8)));
  88. uint32_t inflight_req_id;
  89. struct sock_mapping *accept_map;
  90. wait_queue_head_t inflight_accept_req;
  91. } passive;
  92. };
  93. };
  94. static inline struct sock_mapping *pvcalls_enter_sock(struct socket *sock)
  95. {
  96. struct sock_mapping *map;
  97. if (!pvcalls_front_dev ||
  98. dev_get_drvdata(&pvcalls_front_dev->dev) == NULL)
  99. return ERR_PTR(-ENOTCONN);
  100. map = (struct sock_mapping *)sock->sk->sk_send_head;
  101. if (map == NULL)
  102. return ERR_PTR(-ENOTSOCK);
  103. pvcalls_enter();
  104. atomic_inc(&map->refcount);
  105. return map;
  106. }
  107. static inline void pvcalls_exit_sock(struct socket *sock)
  108. {
  109. struct sock_mapping *map;
  110. map = (struct sock_mapping *)sock->sk->sk_send_head;
  111. atomic_dec(&map->refcount);
  112. pvcalls_exit();
  113. }
  114. static inline int get_request(struct pvcalls_bedata *bedata, int *req_id)
  115. {
  116. *req_id = bedata->ring.req_prod_pvt & (RING_SIZE(&bedata->ring) - 1);
  117. if (RING_FULL(&bedata->ring) ||
  118. bedata->rsp[*req_id].req_id != PVCALLS_INVALID_ID)
  119. return -EAGAIN;
  120. return 0;
  121. }
  122. static bool pvcalls_front_write_todo(struct sock_mapping *map)
  123. {
  124. struct pvcalls_data_intf *intf = map->active.ring;
  125. RING_IDX cons, prod, size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
  126. int32_t error;
  127. error = intf->out_error;
  128. if (error == -ENOTCONN)
  129. return false;
  130. if (error != 0)
  131. return true;
  132. cons = intf->out_cons;
  133. prod = intf->out_prod;
  134. return !!(size - pvcalls_queued(prod, cons, size));
  135. }
  136. static bool pvcalls_front_read_todo(struct sock_mapping *map)
  137. {
  138. struct pvcalls_data_intf *intf = map->active.ring;
  139. RING_IDX cons, prod;
  140. int32_t error;
  141. cons = intf->in_cons;
  142. prod = intf->in_prod;
  143. error = intf->in_error;
  144. return (error != 0 ||
  145. pvcalls_queued(prod, cons,
  146. XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER)) != 0);
  147. }
  148. static irqreturn_t pvcalls_front_event_handler(int irq, void *dev_id)
  149. {
  150. struct xenbus_device *dev = dev_id;
  151. struct pvcalls_bedata *bedata;
  152. struct xen_pvcalls_response *rsp;
  153. uint8_t *src, *dst;
  154. int req_id = 0, more = 0, done = 0;
  155. if (dev == NULL)
  156. return IRQ_HANDLED;
  157. pvcalls_enter();
  158. bedata = dev_get_drvdata(&dev->dev);
  159. if (bedata == NULL) {
  160. pvcalls_exit();
  161. return IRQ_HANDLED;
  162. }
  163. again:
  164. while (RING_HAS_UNCONSUMED_RESPONSES(&bedata->ring)) {
  165. rsp = RING_GET_RESPONSE(&bedata->ring, bedata->ring.rsp_cons);
  166. req_id = rsp->req_id;
  167. if (rsp->cmd == PVCALLS_POLL) {
  168. struct sock_mapping *map = (struct sock_mapping *)(uintptr_t)
  169. rsp->u.poll.id;
  170. clear_bit(PVCALLS_FLAG_POLL_INFLIGHT,
  171. (void *)&map->passive.flags);
  172. /*
  173. * clear INFLIGHT, then set RET. It pairs with
  174. * the checks at the beginning of
  175. * pvcalls_front_poll_passive.
  176. */
  177. smp_wmb();
  178. set_bit(PVCALLS_FLAG_POLL_RET,
  179. (void *)&map->passive.flags);
  180. } else {
  181. dst = (uint8_t *)&bedata->rsp[req_id] +
  182. sizeof(rsp->req_id);
  183. src = (uint8_t *)rsp + sizeof(rsp->req_id);
  184. memcpy(dst, src, sizeof(*rsp) - sizeof(rsp->req_id));
  185. /*
  186. * First copy the rest of the data, then req_id. It is
  187. * paired with the barrier when accessing bedata->rsp.
  188. */
  189. smp_wmb();
  190. bedata->rsp[req_id].req_id = req_id;
  191. }
  192. done = 1;
  193. bedata->ring.rsp_cons++;
  194. }
  195. RING_FINAL_CHECK_FOR_RESPONSES(&bedata->ring, more);
  196. if (more)
  197. goto again;
  198. if (done)
  199. wake_up(&bedata->inflight_req);
  200. pvcalls_exit();
  201. return IRQ_HANDLED;
  202. }
  203. static void pvcalls_front_free_map(struct pvcalls_bedata *bedata,
  204. struct sock_mapping *map)
  205. {
  206. int i;
  207. unbind_from_irqhandler(map->active.irq, map);
  208. spin_lock(&bedata->socket_lock);
  209. if (!list_empty(&map->list))
  210. list_del_init(&map->list);
  211. spin_unlock(&bedata->socket_lock);
  212. for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
  213. gnttab_end_foreign_access(map->active.ring->ref[i], 0, 0);
  214. gnttab_end_foreign_access(map->active.ref, 0, 0);
  215. free_page((unsigned long)map->active.ring);
  216. kfree(map);
  217. }
  218. static irqreturn_t pvcalls_front_conn_handler(int irq, void *sock_map)
  219. {
  220. struct sock_mapping *map = sock_map;
  221. if (map == NULL)
  222. return IRQ_HANDLED;
  223. wake_up_interruptible(&map->active.inflight_conn_req);
  224. return IRQ_HANDLED;
  225. }
  226. int pvcalls_front_socket(struct socket *sock)
  227. {
  228. struct pvcalls_bedata *bedata;
  229. struct sock_mapping *map = NULL;
  230. struct xen_pvcalls_request *req;
  231. int notify, req_id, ret;
  232. /*
  233. * PVCalls only supports domain AF_INET,
  234. * type SOCK_STREAM and protocol 0 sockets for now.
  235. *
  236. * Check socket type here, AF_INET and protocol checks are done
  237. * by the caller.
  238. */
  239. if (sock->type != SOCK_STREAM)
  240. return -EOPNOTSUPP;
  241. pvcalls_enter();
  242. if (!pvcalls_front_dev) {
  243. pvcalls_exit();
  244. return -EACCES;
  245. }
  246. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  247. map = kzalloc(sizeof(*map), GFP_KERNEL);
  248. if (map == NULL) {
  249. pvcalls_exit();
  250. return -ENOMEM;
  251. }
  252. spin_lock(&bedata->socket_lock);
  253. ret = get_request(bedata, &req_id);
  254. if (ret < 0) {
  255. kfree(map);
  256. spin_unlock(&bedata->socket_lock);
  257. pvcalls_exit();
  258. return ret;
  259. }
  260. /*
  261. * sock->sk->sk_send_head is not used for ip sockets: reuse the
  262. * field to store a pointer to the struct sock_mapping
  263. * corresponding to the socket. This way, we can easily get the
  264. * struct sock_mapping from the struct socket.
  265. */
  266. sock->sk->sk_send_head = (void *)map;
  267. list_add_tail(&map->list, &bedata->socket_mappings);
  268. req = RING_GET_REQUEST(&bedata->ring, req_id);
  269. req->req_id = req_id;
  270. req->cmd = PVCALLS_SOCKET;
  271. req->u.socket.id = (uintptr_t) map;
  272. req->u.socket.domain = AF_INET;
  273. req->u.socket.type = SOCK_STREAM;
  274. req->u.socket.protocol = IPPROTO_IP;
  275. bedata->ring.req_prod_pvt++;
  276. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  277. spin_unlock(&bedata->socket_lock);
  278. if (notify)
  279. notify_remote_via_irq(bedata->irq);
  280. wait_event(bedata->inflight_req,
  281. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  282. /* read req_id, then the content */
  283. smp_rmb();
  284. ret = bedata->rsp[req_id].ret;
  285. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  286. pvcalls_exit();
  287. return ret;
  288. }
  289. static void free_active_ring(struct sock_mapping *map)
  290. {
  291. if (!map->active.ring)
  292. return;
  293. free_pages((unsigned long)map->active.data.in,
  294. map->active.ring->ring_order);
  295. free_page((unsigned long)map->active.ring);
  296. }
  297. static int alloc_active_ring(struct sock_mapping *map)
  298. {
  299. void *bytes;
  300. map->active.ring = (struct pvcalls_data_intf *)
  301. get_zeroed_page(GFP_KERNEL);
  302. if (!map->active.ring)
  303. goto out;
  304. map->active.ring->ring_order = PVCALLS_RING_ORDER;
  305. bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  306. PVCALLS_RING_ORDER);
  307. if (!bytes)
  308. goto out;
  309. map->active.data.in = bytes;
  310. map->active.data.out = bytes +
  311. XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
  312. return 0;
  313. out:
  314. free_active_ring(map);
  315. return -ENOMEM;
  316. }
  317. static int create_active(struct sock_mapping *map, int *evtchn)
  318. {
  319. void *bytes;
  320. int ret = -ENOMEM, irq = -1, i;
  321. *evtchn = -1;
  322. init_waitqueue_head(&map->active.inflight_conn_req);
  323. bytes = map->active.data.in;
  324. for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
  325. map->active.ring->ref[i] = gnttab_grant_foreign_access(
  326. pvcalls_front_dev->otherend_id,
  327. pfn_to_gfn(virt_to_pfn(bytes) + i), 0);
  328. map->active.ref = gnttab_grant_foreign_access(
  329. pvcalls_front_dev->otherend_id,
  330. pfn_to_gfn(virt_to_pfn((void *)map->active.ring)), 0);
  331. ret = xenbus_alloc_evtchn(pvcalls_front_dev, evtchn);
  332. if (ret)
  333. goto out_error;
  334. irq = bind_evtchn_to_irqhandler(*evtchn, pvcalls_front_conn_handler,
  335. 0, "pvcalls-frontend", map);
  336. if (irq < 0) {
  337. ret = irq;
  338. goto out_error;
  339. }
  340. map->active.irq = irq;
  341. map->active_socket = true;
  342. mutex_init(&map->active.in_mutex);
  343. mutex_init(&map->active.out_mutex);
  344. return 0;
  345. out_error:
  346. if (*evtchn >= 0)
  347. xenbus_free_evtchn(pvcalls_front_dev, *evtchn);
  348. return ret;
  349. }
  350. int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
  351. int addr_len, int flags)
  352. {
  353. struct pvcalls_bedata *bedata;
  354. struct sock_mapping *map = NULL;
  355. struct xen_pvcalls_request *req;
  356. int notify, req_id, ret, evtchn;
  357. if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
  358. return -EOPNOTSUPP;
  359. map = pvcalls_enter_sock(sock);
  360. if (IS_ERR(map))
  361. return PTR_ERR(map);
  362. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  363. ret = alloc_active_ring(map);
  364. if (ret < 0) {
  365. pvcalls_exit_sock(sock);
  366. return ret;
  367. }
  368. spin_lock(&bedata->socket_lock);
  369. ret = get_request(bedata, &req_id);
  370. if (ret < 0) {
  371. spin_unlock(&bedata->socket_lock);
  372. free_active_ring(map);
  373. pvcalls_exit_sock(sock);
  374. return ret;
  375. }
  376. ret = create_active(map, &evtchn);
  377. if (ret < 0) {
  378. spin_unlock(&bedata->socket_lock);
  379. free_active_ring(map);
  380. pvcalls_exit_sock(sock);
  381. return ret;
  382. }
  383. req = RING_GET_REQUEST(&bedata->ring, req_id);
  384. req->req_id = req_id;
  385. req->cmd = PVCALLS_CONNECT;
  386. req->u.connect.id = (uintptr_t)map;
  387. req->u.connect.len = addr_len;
  388. req->u.connect.flags = flags;
  389. req->u.connect.ref = map->active.ref;
  390. req->u.connect.evtchn = evtchn;
  391. memcpy(req->u.connect.addr, addr, sizeof(*addr));
  392. map->sock = sock;
  393. bedata->ring.req_prod_pvt++;
  394. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  395. spin_unlock(&bedata->socket_lock);
  396. if (notify)
  397. notify_remote_via_irq(bedata->irq);
  398. wait_event(bedata->inflight_req,
  399. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  400. /* read req_id, then the content */
  401. smp_rmb();
  402. ret = bedata->rsp[req_id].ret;
  403. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  404. pvcalls_exit_sock(sock);
  405. return ret;
  406. }
  407. static int __write_ring(struct pvcalls_data_intf *intf,
  408. struct pvcalls_data *data,
  409. struct iov_iter *msg_iter,
  410. int len)
  411. {
  412. RING_IDX cons, prod, size, masked_prod, masked_cons;
  413. RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
  414. int32_t error;
  415. error = intf->out_error;
  416. if (error < 0)
  417. return error;
  418. cons = intf->out_cons;
  419. prod = intf->out_prod;
  420. /* read indexes before continuing */
  421. virt_mb();
  422. size = pvcalls_queued(prod, cons, array_size);
  423. if (size > array_size)
  424. return -EINVAL;
  425. if (size == array_size)
  426. return 0;
  427. if (len > array_size - size)
  428. len = array_size - size;
  429. masked_prod = pvcalls_mask(prod, array_size);
  430. masked_cons = pvcalls_mask(cons, array_size);
  431. if (masked_prod < masked_cons) {
  432. len = copy_from_iter(data->out + masked_prod, len, msg_iter);
  433. } else {
  434. if (len > array_size - masked_prod) {
  435. int ret = copy_from_iter(data->out + masked_prod,
  436. array_size - masked_prod, msg_iter);
  437. if (ret != array_size - masked_prod) {
  438. len = ret;
  439. goto out;
  440. }
  441. len = ret + copy_from_iter(data->out, len - ret, msg_iter);
  442. } else {
  443. len = copy_from_iter(data->out + masked_prod, len, msg_iter);
  444. }
  445. }
  446. out:
  447. /* write to ring before updating pointer */
  448. virt_wmb();
  449. intf->out_prod += len;
  450. return len;
  451. }
  452. int pvcalls_front_sendmsg(struct socket *sock, struct msghdr *msg,
  453. size_t len)
  454. {
  455. struct sock_mapping *map;
  456. int sent, tot_sent = 0;
  457. int count = 0, flags;
  458. flags = msg->msg_flags;
  459. if (flags & (MSG_CONFIRM|MSG_DONTROUTE|MSG_EOR|MSG_OOB))
  460. return -EOPNOTSUPP;
  461. map = pvcalls_enter_sock(sock);
  462. if (IS_ERR(map))
  463. return PTR_ERR(map);
  464. mutex_lock(&map->active.out_mutex);
  465. if ((flags & MSG_DONTWAIT) && !pvcalls_front_write_todo(map)) {
  466. mutex_unlock(&map->active.out_mutex);
  467. pvcalls_exit_sock(sock);
  468. return -EAGAIN;
  469. }
  470. if (len > INT_MAX)
  471. len = INT_MAX;
  472. again:
  473. count++;
  474. sent = __write_ring(map->active.ring,
  475. &map->active.data, &msg->msg_iter,
  476. len);
  477. if (sent > 0) {
  478. len -= sent;
  479. tot_sent += sent;
  480. notify_remote_via_irq(map->active.irq);
  481. }
  482. if (sent >= 0 && len > 0 && count < PVCALLS_FRONT_MAX_SPIN)
  483. goto again;
  484. if (sent < 0)
  485. tot_sent = sent;
  486. mutex_unlock(&map->active.out_mutex);
  487. pvcalls_exit_sock(sock);
  488. return tot_sent;
  489. }
  490. static int __read_ring(struct pvcalls_data_intf *intf,
  491. struct pvcalls_data *data,
  492. struct iov_iter *msg_iter,
  493. size_t len, int flags)
  494. {
  495. RING_IDX cons, prod, size, masked_prod, masked_cons;
  496. RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
  497. int32_t error;
  498. cons = intf->in_cons;
  499. prod = intf->in_prod;
  500. error = intf->in_error;
  501. /* get pointers before reading from the ring */
  502. virt_rmb();
  503. size = pvcalls_queued(prod, cons, array_size);
  504. masked_prod = pvcalls_mask(prod, array_size);
  505. masked_cons = pvcalls_mask(cons, array_size);
  506. if (size == 0)
  507. return error ?: size;
  508. if (len > size)
  509. len = size;
  510. if (masked_prod > masked_cons) {
  511. len = copy_to_iter(data->in + masked_cons, len, msg_iter);
  512. } else {
  513. if (len > (array_size - masked_cons)) {
  514. int ret = copy_to_iter(data->in + masked_cons,
  515. array_size - masked_cons, msg_iter);
  516. if (ret != array_size - masked_cons) {
  517. len = ret;
  518. goto out;
  519. }
  520. len = ret + copy_to_iter(data->in, len - ret, msg_iter);
  521. } else {
  522. len = copy_to_iter(data->in + masked_cons, len, msg_iter);
  523. }
  524. }
  525. out:
  526. /* read data from the ring before increasing the index */
  527. virt_mb();
  528. if (!(flags & MSG_PEEK))
  529. intf->in_cons += len;
  530. return len;
  531. }
  532. int pvcalls_front_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  533. int flags)
  534. {
  535. int ret;
  536. struct sock_mapping *map;
  537. if (flags & (MSG_CMSG_CLOEXEC|MSG_ERRQUEUE|MSG_OOB|MSG_TRUNC))
  538. return -EOPNOTSUPP;
  539. map = pvcalls_enter_sock(sock);
  540. if (IS_ERR(map))
  541. return PTR_ERR(map);
  542. mutex_lock(&map->active.in_mutex);
  543. if (len > XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER))
  544. len = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
  545. while (!(flags & MSG_DONTWAIT) && !pvcalls_front_read_todo(map)) {
  546. wait_event_interruptible(map->active.inflight_conn_req,
  547. pvcalls_front_read_todo(map));
  548. }
  549. ret = __read_ring(map->active.ring, &map->active.data,
  550. &msg->msg_iter, len, flags);
  551. if (ret > 0)
  552. notify_remote_via_irq(map->active.irq);
  553. if (ret == 0)
  554. ret = (flags & MSG_DONTWAIT) ? -EAGAIN : 0;
  555. if (ret == -ENOTCONN)
  556. ret = 0;
  557. mutex_unlock(&map->active.in_mutex);
  558. pvcalls_exit_sock(sock);
  559. return ret;
  560. }
  561. int pvcalls_front_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
  562. {
  563. struct pvcalls_bedata *bedata;
  564. struct sock_mapping *map = NULL;
  565. struct xen_pvcalls_request *req;
  566. int notify, req_id, ret;
  567. if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
  568. return -EOPNOTSUPP;
  569. map = pvcalls_enter_sock(sock);
  570. if (IS_ERR(map))
  571. return PTR_ERR(map);
  572. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  573. spin_lock(&bedata->socket_lock);
  574. ret = get_request(bedata, &req_id);
  575. if (ret < 0) {
  576. spin_unlock(&bedata->socket_lock);
  577. pvcalls_exit_sock(sock);
  578. return ret;
  579. }
  580. req = RING_GET_REQUEST(&bedata->ring, req_id);
  581. req->req_id = req_id;
  582. map->sock = sock;
  583. req->cmd = PVCALLS_BIND;
  584. req->u.bind.id = (uintptr_t)map;
  585. memcpy(req->u.bind.addr, addr, sizeof(*addr));
  586. req->u.bind.len = addr_len;
  587. init_waitqueue_head(&map->passive.inflight_accept_req);
  588. map->active_socket = false;
  589. bedata->ring.req_prod_pvt++;
  590. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  591. spin_unlock(&bedata->socket_lock);
  592. if (notify)
  593. notify_remote_via_irq(bedata->irq);
  594. wait_event(bedata->inflight_req,
  595. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  596. /* read req_id, then the content */
  597. smp_rmb();
  598. ret = bedata->rsp[req_id].ret;
  599. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  600. map->passive.status = PVCALLS_STATUS_BIND;
  601. pvcalls_exit_sock(sock);
  602. return 0;
  603. }
  604. int pvcalls_front_listen(struct socket *sock, int backlog)
  605. {
  606. struct pvcalls_bedata *bedata;
  607. struct sock_mapping *map;
  608. struct xen_pvcalls_request *req;
  609. int notify, req_id, ret;
  610. map = pvcalls_enter_sock(sock);
  611. if (IS_ERR(map))
  612. return PTR_ERR(map);
  613. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  614. if (map->passive.status != PVCALLS_STATUS_BIND) {
  615. pvcalls_exit_sock(sock);
  616. return -EOPNOTSUPP;
  617. }
  618. spin_lock(&bedata->socket_lock);
  619. ret = get_request(bedata, &req_id);
  620. if (ret < 0) {
  621. spin_unlock(&bedata->socket_lock);
  622. pvcalls_exit_sock(sock);
  623. return ret;
  624. }
  625. req = RING_GET_REQUEST(&bedata->ring, req_id);
  626. req->req_id = req_id;
  627. req->cmd = PVCALLS_LISTEN;
  628. req->u.listen.id = (uintptr_t) map;
  629. req->u.listen.backlog = backlog;
  630. bedata->ring.req_prod_pvt++;
  631. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  632. spin_unlock(&bedata->socket_lock);
  633. if (notify)
  634. notify_remote_via_irq(bedata->irq);
  635. wait_event(bedata->inflight_req,
  636. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  637. /* read req_id, then the content */
  638. smp_rmb();
  639. ret = bedata->rsp[req_id].ret;
  640. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  641. map->passive.status = PVCALLS_STATUS_LISTEN;
  642. pvcalls_exit_sock(sock);
  643. return ret;
  644. }
  645. int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags)
  646. {
  647. struct pvcalls_bedata *bedata;
  648. struct sock_mapping *map;
  649. struct sock_mapping *map2 = NULL;
  650. struct xen_pvcalls_request *req;
  651. int notify, req_id, ret, evtchn, nonblock;
  652. map = pvcalls_enter_sock(sock);
  653. if (IS_ERR(map))
  654. return PTR_ERR(map);
  655. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  656. if (map->passive.status != PVCALLS_STATUS_LISTEN) {
  657. pvcalls_exit_sock(sock);
  658. return -EINVAL;
  659. }
  660. nonblock = flags & SOCK_NONBLOCK;
  661. /*
  662. * Backend only supports 1 inflight accept request, will return
  663. * errors for the others
  664. */
  665. if (test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  666. (void *)&map->passive.flags)) {
  667. req_id = READ_ONCE(map->passive.inflight_req_id);
  668. if (req_id != PVCALLS_INVALID_ID &&
  669. READ_ONCE(bedata->rsp[req_id].req_id) == req_id) {
  670. map2 = map->passive.accept_map;
  671. goto received;
  672. }
  673. if (nonblock) {
  674. pvcalls_exit_sock(sock);
  675. return -EAGAIN;
  676. }
  677. if (wait_event_interruptible(map->passive.inflight_accept_req,
  678. !test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  679. (void *)&map->passive.flags))) {
  680. pvcalls_exit_sock(sock);
  681. return -EINTR;
  682. }
  683. }
  684. map2 = kzalloc(sizeof(*map2), GFP_KERNEL);
  685. if (map2 == NULL) {
  686. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  687. (void *)&map->passive.flags);
  688. pvcalls_exit_sock(sock);
  689. return -ENOMEM;
  690. }
  691. ret = alloc_active_ring(map2);
  692. if (ret < 0) {
  693. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  694. (void *)&map->passive.flags);
  695. kfree(map2);
  696. pvcalls_exit_sock(sock);
  697. return ret;
  698. }
  699. spin_lock(&bedata->socket_lock);
  700. ret = get_request(bedata, &req_id);
  701. if (ret < 0) {
  702. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  703. (void *)&map->passive.flags);
  704. spin_unlock(&bedata->socket_lock);
  705. free_active_ring(map2);
  706. kfree(map2);
  707. pvcalls_exit_sock(sock);
  708. return ret;
  709. }
  710. ret = create_active(map2, &evtchn);
  711. if (ret < 0) {
  712. free_active_ring(map2);
  713. kfree(map2);
  714. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  715. (void *)&map->passive.flags);
  716. spin_unlock(&bedata->socket_lock);
  717. pvcalls_exit_sock(sock);
  718. return ret;
  719. }
  720. list_add_tail(&map2->list, &bedata->socket_mappings);
  721. req = RING_GET_REQUEST(&bedata->ring, req_id);
  722. req->req_id = req_id;
  723. req->cmd = PVCALLS_ACCEPT;
  724. req->u.accept.id = (uintptr_t) map;
  725. req->u.accept.ref = map2->active.ref;
  726. req->u.accept.id_new = (uintptr_t) map2;
  727. req->u.accept.evtchn = evtchn;
  728. map->passive.accept_map = map2;
  729. bedata->ring.req_prod_pvt++;
  730. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  731. spin_unlock(&bedata->socket_lock);
  732. if (notify)
  733. notify_remote_via_irq(bedata->irq);
  734. /* We could check if we have received a response before returning. */
  735. if (nonblock) {
  736. WRITE_ONCE(map->passive.inflight_req_id, req_id);
  737. pvcalls_exit_sock(sock);
  738. return -EAGAIN;
  739. }
  740. if (wait_event_interruptible(bedata->inflight_req,
  741. READ_ONCE(bedata->rsp[req_id].req_id) == req_id)) {
  742. pvcalls_exit_sock(sock);
  743. return -EINTR;
  744. }
  745. /* read req_id, then the content */
  746. smp_rmb();
  747. received:
  748. map2->sock = newsock;
  749. newsock->sk = sk_alloc(sock_net(sock->sk), PF_INET, GFP_KERNEL, &pvcalls_proto, false);
  750. if (!newsock->sk) {
  751. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  752. map->passive.inflight_req_id = PVCALLS_INVALID_ID;
  753. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  754. (void *)&map->passive.flags);
  755. pvcalls_front_free_map(bedata, map2);
  756. pvcalls_exit_sock(sock);
  757. return -ENOMEM;
  758. }
  759. newsock->sk->sk_send_head = (void *)map2;
  760. ret = bedata->rsp[req_id].ret;
  761. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  762. map->passive.inflight_req_id = PVCALLS_INVALID_ID;
  763. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, (void *)&map->passive.flags);
  764. wake_up(&map->passive.inflight_accept_req);
  765. pvcalls_exit_sock(sock);
  766. return ret;
  767. }
  768. static __poll_t pvcalls_front_poll_passive(struct file *file,
  769. struct pvcalls_bedata *bedata,
  770. struct sock_mapping *map,
  771. poll_table *wait)
  772. {
  773. int notify, req_id, ret;
  774. struct xen_pvcalls_request *req;
  775. if (test_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  776. (void *)&map->passive.flags)) {
  777. uint32_t req_id = READ_ONCE(map->passive.inflight_req_id);
  778. if (req_id != PVCALLS_INVALID_ID &&
  779. READ_ONCE(bedata->rsp[req_id].req_id) == req_id)
  780. return EPOLLIN | EPOLLRDNORM;
  781. poll_wait(file, &map->passive.inflight_accept_req, wait);
  782. return 0;
  783. }
  784. if (test_and_clear_bit(PVCALLS_FLAG_POLL_RET,
  785. (void *)&map->passive.flags))
  786. return EPOLLIN | EPOLLRDNORM;
  787. /*
  788. * First check RET, then INFLIGHT. No barriers necessary to
  789. * ensure execution ordering because of the conditional
  790. * instructions creating control dependencies.
  791. */
  792. if (test_and_set_bit(PVCALLS_FLAG_POLL_INFLIGHT,
  793. (void *)&map->passive.flags)) {
  794. poll_wait(file, &bedata->inflight_req, wait);
  795. return 0;
  796. }
  797. spin_lock(&bedata->socket_lock);
  798. ret = get_request(bedata, &req_id);
  799. if (ret < 0) {
  800. spin_unlock(&bedata->socket_lock);
  801. return ret;
  802. }
  803. req = RING_GET_REQUEST(&bedata->ring, req_id);
  804. req->req_id = req_id;
  805. req->cmd = PVCALLS_POLL;
  806. req->u.poll.id = (uintptr_t) map;
  807. bedata->ring.req_prod_pvt++;
  808. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  809. spin_unlock(&bedata->socket_lock);
  810. if (notify)
  811. notify_remote_via_irq(bedata->irq);
  812. poll_wait(file, &bedata->inflight_req, wait);
  813. return 0;
  814. }
  815. static __poll_t pvcalls_front_poll_active(struct file *file,
  816. struct pvcalls_bedata *bedata,
  817. struct sock_mapping *map,
  818. poll_table *wait)
  819. {
  820. __poll_t mask = 0;
  821. int32_t in_error, out_error;
  822. struct pvcalls_data_intf *intf = map->active.ring;
  823. out_error = intf->out_error;
  824. in_error = intf->in_error;
  825. poll_wait(file, &map->active.inflight_conn_req, wait);
  826. if (pvcalls_front_write_todo(map))
  827. mask |= EPOLLOUT | EPOLLWRNORM;
  828. if (pvcalls_front_read_todo(map))
  829. mask |= EPOLLIN | EPOLLRDNORM;
  830. if (in_error != 0 || out_error != 0)
  831. mask |= EPOLLERR;
  832. return mask;
  833. }
  834. __poll_t pvcalls_front_poll(struct file *file, struct socket *sock,
  835. poll_table *wait)
  836. {
  837. struct pvcalls_bedata *bedata;
  838. struct sock_mapping *map;
  839. __poll_t ret;
  840. map = pvcalls_enter_sock(sock);
  841. if (IS_ERR(map))
  842. return EPOLLNVAL;
  843. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  844. if (map->active_socket)
  845. ret = pvcalls_front_poll_active(file, bedata, map, wait);
  846. else
  847. ret = pvcalls_front_poll_passive(file, bedata, map, wait);
  848. pvcalls_exit_sock(sock);
  849. return ret;
  850. }
  851. int pvcalls_front_release(struct socket *sock)
  852. {
  853. struct pvcalls_bedata *bedata;
  854. struct sock_mapping *map;
  855. int req_id, notify, ret;
  856. struct xen_pvcalls_request *req;
  857. if (sock->sk == NULL)
  858. return 0;
  859. map = pvcalls_enter_sock(sock);
  860. if (IS_ERR(map)) {
  861. if (PTR_ERR(map) == -ENOTCONN)
  862. return -EIO;
  863. else
  864. return 0;
  865. }
  866. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  867. spin_lock(&bedata->socket_lock);
  868. ret = get_request(bedata, &req_id);
  869. if (ret < 0) {
  870. spin_unlock(&bedata->socket_lock);
  871. pvcalls_exit_sock(sock);
  872. return ret;
  873. }
  874. sock->sk->sk_send_head = NULL;
  875. req = RING_GET_REQUEST(&bedata->ring, req_id);
  876. req->req_id = req_id;
  877. req->cmd = PVCALLS_RELEASE;
  878. req->u.release.id = (uintptr_t)map;
  879. bedata->ring.req_prod_pvt++;
  880. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  881. spin_unlock(&bedata->socket_lock);
  882. if (notify)
  883. notify_remote_via_irq(bedata->irq);
  884. wait_event(bedata->inflight_req,
  885. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  886. if (map->active_socket) {
  887. /*
  888. * Set in_error and wake up inflight_conn_req to force
  889. * recvmsg waiters to exit.
  890. */
  891. map->active.ring->in_error = -EBADF;
  892. wake_up_interruptible(&map->active.inflight_conn_req);
  893. /*
  894. * We need to make sure that sendmsg/recvmsg on this socket have
  895. * not started before we've cleared sk_send_head here. The
  896. * easiest way to guarantee this is to see that no pvcalls
  897. * (other than us) is in progress on this socket.
  898. */
  899. while (atomic_read(&map->refcount) > 1)
  900. cpu_relax();
  901. pvcalls_front_free_map(bedata, map);
  902. } else {
  903. wake_up(&bedata->inflight_req);
  904. wake_up(&map->passive.inflight_accept_req);
  905. while (atomic_read(&map->refcount) > 1)
  906. cpu_relax();
  907. spin_lock(&bedata->socket_lock);
  908. list_del(&map->list);
  909. spin_unlock(&bedata->socket_lock);
  910. if (READ_ONCE(map->passive.inflight_req_id) != PVCALLS_INVALID_ID &&
  911. READ_ONCE(map->passive.inflight_req_id) != 0) {
  912. pvcalls_front_free_map(bedata,
  913. map->passive.accept_map);
  914. }
  915. kfree(map);
  916. }
  917. WRITE_ONCE(bedata->rsp[req_id].req_id, PVCALLS_INVALID_ID);
  918. pvcalls_exit();
  919. return 0;
  920. }
  921. static const struct xenbus_device_id pvcalls_front_ids[] = {
  922. { "pvcalls" },
  923. { "" }
  924. };
  925. static int pvcalls_front_remove(struct xenbus_device *dev)
  926. {
  927. struct pvcalls_bedata *bedata;
  928. struct sock_mapping *map = NULL, *n;
  929. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  930. dev_set_drvdata(&dev->dev, NULL);
  931. pvcalls_front_dev = NULL;
  932. if (bedata->irq >= 0)
  933. unbind_from_irqhandler(bedata->irq, dev);
  934. list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
  935. map->sock->sk->sk_send_head = NULL;
  936. if (map->active_socket) {
  937. map->active.ring->in_error = -EBADF;
  938. wake_up_interruptible(&map->active.inflight_conn_req);
  939. }
  940. }
  941. smp_mb();
  942. while (atomic_read(&pvcalls_refcount) > 0)
  943. cpu_relax();
  944. list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
  945. if (map->active_socket) {
  946. /* No need to lock, refcount is 0 */
  947. pvcalls_front_free_map(bedata, map);
  948. } else {
  949. list_del(&map->list);
  950. kfree(map);
  951. }
  952. }
  953. if (bedata->ref != -1)
  954. gnttab_end_foreign_access(bedata->ref, 0, 0);
  955. kfree(bedata->ring.sring);
  956. kfree(bedata);
  957. xenbus_switch_state(dev, XenbusStateClosed);
  958. return 0;
  959. }
  960. static int pvcalls_front_probe(struct xenbus_device *dev,
  961. const struct xenbus_device_id *id)
  962. {
  963. int ret = -ENOMEM, evtchn, i;
  964. unsigned int max_page_order, function_calls, len;
  965. char *versions;
  966. grant_ref_t gref_head = 0;
  967. struct xenbus_transaction xbt;
  968. struct pvcalls_bedata *bedata = NULL;
  969. struct xen_pvcalls_sring *sring;
  970. if (pvcalls_front_dev != NULL) {
  971. dev_err(&dev->dev, "only one PV Calls connection supported\n");
  972. return -EINVAL;
  973. }
  974. versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
  975. if (IS_ERR(versions))
  976. return PTR_ERR(versions);
  977. if (!len)
  978. return -EINVAL;
  979. if (strcmp(versions, "1")) {
  980. kfree(versions);
  981. return -EINVAL;
  982. }
  983. kfree(versions);
  984. max_page_order = xenbus_read_unsigned(dev->otherend,
  985. "max-page-order", 0);
  986. if (max_page_order < PVCALLS_RING_ORDER)
  987. return -ENODEV;
  988. function_calls = xenbus_read_unsigned(dev->otherend,
  989. "function-calls", 0);
  990. /* See XENBUS_FUNCTIONS_CALLS in pvcalls.h */
  991. if (function_calls != 1)
  992. return -ENODEV;
  993. pr_info("%s max-page-order is %u\n", __func__, max_page_order);
  994. bedata = kzalloc(sizeof(struct pvcalls_bedata), GFP_KERNEL);
  995. if (!bedata)
  996. return -ENOMEM;
  997. dev_set_drvdata(&dev->dev, bedata);
  998. pvcalls_front_dev = dev;
  999. init_waitqueue_head(&bedata->inflight_req);
  1000. INIT_LIST_HEAD(&bedata->socket_mappings);
  1001. spin_lock_init(&bedata->socket_lock);
  1002. bedata->irq = -1;
  1003. bedata->ref = -1;
  1004. for (i = 0; i < PVCALLS_NR_RSP_PER_RING; i++)
  1005. bedata->rsp[i].req_id = PVCALLS_INVALID_ID;
  1006. sring = (struct xen_pvcalls_sring *) __get_free_page(GFP_KERNEL |
  1007. __GFP_ZERO);
  1008. if (!sring)
  1009. goto error;
  1010. SHARED_RING_INIT(sring);
  1011. FRONT_RING_INIT(&bedata->ring, sring, XEN_PAGE_SIZE);
  1012. ret = xenbus_alloc_evtchn(dev, &evtchn);
  1013. if (ret)
  1014. goto error;
  1015. bedata->irq = bind_evtchn_to_irqhandler(evtchn,
  1016. pvcalls_front_event_handler,
  1017. 0, "pvcalls-frontend", dev);
  1018. if (bedata->irq < 0) {
  1019. ret = bedata->irq;
  1020. goto error;
  1021. }
  1022. ret = gnttab_alloc_grant_references(1, &gref_head);
  1023. if (ret < 0)
  1024. goto error;
  1025. ret = gnttab_claim_grant_reference(&gref_head);
  1026. if (ret < 0)
  1027. goto error;
  1028. bedata->ref = ret;
  1029. gnttab_grant_foreign_access_ref(bedata->ref, dev->otherend_id,
  1030. virt_to_gfn((void *)sring), 0);
  1031. again:
  1032. ret = xenbus_transaction_start(&xbt);
  1033. if (ret) {
  1034. xenbus_dev_fatal(dev, ret, "starting transaction");
  1035. goto error;
  1036. }
  1037. ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
  1038. if (ret)
  1039. goto error_xenbus;
  1040. ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", bedata->ref);
  1041. if (ret)
  1042. goto error_xenbus;
  1043. ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
  1044. evtchn);
  1045. if (ret)
  1046. goto error_xenbus;
  1047. ret = xenbus_transaction_end(xbt, 0);
  1048. if (ret) {
  1049. if (ret == -EAGAIN)
  1050. goto again;
  1051. xenbus_dev_fatal(dev, ret, "completing transaction");
  1052. goto error;
  1053. }
  1054. xenbus_switch_state(dev, XenbusStateInitialised);
  1055. return 0;
  1056. error_xenbus:
  1057. xenbus_transaction_end(xbt, 1);
  1058. xenbus_dev_fatal(dev, ret, "writing xenstore");
  1059. error:
  1060. pvcalls_front_remove(dev);
  1061. return ret;
  1062. }
  1063. static void pvcalls_front_changed(struct xenbus_device *dev,
  1064. enum xenbus_state backend_state)
  1065. {
  1066. switch (backend_state) {
  1067. case XenbusStateReconfiguring:
  1068. case XenbusStateReconfigured:
  1069. case XenbusStateInitialising:
  1070. case XenbusStateInitialised:
  1071. case XenbusStateUnknown:
  1072. break;
  1073. case XenbusStateInitWait:
  1074. break;
  1075. case XenbusStateConnected:
  1076. xenbus_switch_state(dev, XenbusStateConnected);
  1077. break;
  1078. case XenbusStateClosed:
  1079. if (dev->state == XenbusStateClosed)
  1080. break;
  1081. /* Missed the backend's CLOSING state */
  1082. /* fall through */
  1083. case XenbusStateClosing:
  1084. xenbus_frontend_closed(dev);
  1085. break;
  1086. }
  1087. }
  1088. static struct xenbus_driver pvcalls_front_driver = {
  1089. .ids = pvcalls_front_ids,
  1090. .probe = pvcalls_front_probe,
  1091. .remove = pvcalls_front_remove,
  1092. .otherend_changed = pvcalls_front_changed,
  1093. };
  1094. static int __init pvcalls_frontend_init(void)
  1095. {
  1096. if (!xen_domain())
  1097. return -ENODEV;
  1098. pr_info("Initialising Xen pvcalls frontend driver\n");
  1099. return xenbus_register_frontend(&pvcalls_front_driver);
  1100. }
  1101. module_init(pvcalls_frontend_init);
  1102. MODULE_DESCRIPTION("Xen PV Calls frontend driver");
  1103. MODULE_AUTHOR("Stefano Stabellini <sstabellini@kernel.org>");
  1104. MODULE_LICENSE("GPL");