pvcalls-front.c 32 KB

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