pvcalls-front.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * (c) 2017 Stefano Stabellini <[email protected]>
  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_free_map(struct pvcalls_bedata *bedata,
  196. struct sock_mapping *map)
  197. {
  198. int i;
  199. unbind_from_irqhandler(map->active.irq, map);
  200. spin_lock(&bedata->socket_lock);
  201. if (!list_empty(&map->list))
  202. list_del_init(&map->list);
  203. spin_unlock(&bedata->socket_lock);
  204. for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
  205. gnttab_end_foreign_access(map->active.ring->ref[i], NULL);
  206. gnttab_end_foreign_access(map->active.ref, NULL);
  207. free_active_ring(map);
  208. kfree(map);
  209. }
  210. static irqreturn_t pvcalls_front_conn_handler(int irq, void *sock_map)
  211. {
  212. struct sock_mapping *map = sock_map;
  213. if (map == NULL)
  214. return IRQ_HANDLED;
  215. wake_up_interruptible(&map->active.inflight_conn_req);
  216. return IRQ_HANDLED;
  217. }
  218. int pvcalls_front_socket(struct socket *sock)
  219. {
  220. struct pvcalls_bedata *bedata;
  221. struct sock_mapping *map = NULL;
  222. struct xen_pvcalls_request *req;
  223. int notify, req_id, ret;
  224. /*
  225. * PVCalls only supports domain AF_INET,
  226. * type SOCK_STREAM and protocol 0 sockets for now.
  227. *
  228. * Check socket type here, AF_INET and protocol checks are done
  229. * by the caller.
  230. */
  231. if (sock->type != SOCK_STREAM)
  232. return -EOPNOTSUPP;
  233. pvcalls_enter();
  234. if (!pvcalls_front_dev) {
  235. pvcalls_exit();
  236. return -EACCES;
  237. }
  238. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  239. map = kzalloc(sizeof(*map), GFP_KERNEL);
  240. if (map == NULL) {
  241. pvcalls_exit();
  242. return -ENOMEM;
  243. }
  244. spin_lock(&bedata->socket_lock);
  245. ret = get_request(bedata, &req_id);
  246. if (ret < 0) {
  247. kfree(map);
  248. spin_unlock(&bedata->socket_lock);
  249. pvcalls_exit();
  250. return ret;
  251. }
  252. /*
  253. * sock->sk->sk_send_head is not used for ip sockets: reuse the
  254. * field to store a pointer to the struct sock_mapping
  255. * corresponding to the socket. This way, we can easily get the
  256. * struct sock_mapping from the struct socket.
  257. */
  258. sock->sk->sk_send_head = (void *)map;
  259. list_add_tail(&map->list, &bedata->socket_mappings);
  260. req = RING_GET_REQUEST(&bedata->ring, req_id);
  261. req->req_id = req_id;
  262. req->cmd = PVCALLS_SOCKET;
  263. req->u.socket.id = (uintptr_t) map;
  264. req->u.socket.domain = AF_INET;
  265. req->u.socket.type = SOCK_STREAM;
  266. req->u.socket.protocol = IPPROTO_IP;
  267. bedata->ring.req_prod_pvt++;
  268. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  269. spin_unlock(&bedata->socket_lock);
  270. if (notify)
  271. notify_remote_via_irq(bedata->irq);
  272. wait_event(bedata->inflight_req,
  273. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  274. /* read req_id, then the content */
  275. smp_rmb();
  276. ret = bedata->rsp[req_id].ret;
  277. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  278. pvcalls_exit();
  279. return ret;
  280. }
  281. static void free_active_ring(struct sock_mapping *map)
  282. {
  283. if (!map->active.ring)
  284. return;
  285. free_pages_exact(map->active.data.in,
  286. PAGE_SIZE << map->active.ring->ring_order);
  287. free_page((unsigned long)map->active.ring);
  288. }
  289. static int alloc_active_ring(struct sock_mapping *map)
  290. {
  291. void *bytes;
  292. map->active.ring = (struct pvcalls_data_intf *)
  293. get_zeroed_page(GFP_KERNEL);
  294. if (!map->active.ring)
  295. goto out;
  296. map->active.ring->ring_order = PVCALLS_RING_ORDER;
  297. bytes = alloc_pages_exact(PAGE_SIZE << PVCALLS_RING_ORDER,
  298. GFP_KERNEL | __GFP_ZERO);
  299. if (!bytes)
  300. goto out;
  301. map->active.data.in = bytes;
  302. map->active.data.out = bytes +
  303. XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
  304. return 0;
  305. out:
  306. free_active_ring(map);
  307. return -ENOMEM;
  308. }
  309. static int create_active(struct sock_mapping *map, evtchn_port_t *evtchn)
  310. {
  311. void *bytes;
  312. int ret, irq = -1, i;
  313. *evtchn = 0;
  314. init_waitqueue_head(&map->active.inflight_conn_req);
  315. bytes = map->active.data.in;
  316. for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
  317. map->active.ring->ref[i] = gnttab_grant_foreign_access(
  318. pvcalls_front_dev->otherend_id,
  319. pfn_to_gfn(virt_to_pfn(bytes) + i), 0);
  320. map->active.ref = gnttab_grant_foreign_access(
  321. pvcalls_front_dev->otherend_id,
  322. pfn_to_gfn(virt_to_pfn((void *)map->active.ring)), 0);
  323. ret = xenbus_alloc_evtchn(pvcalls_front_dev, evtchn);
  324. if (ret)
  325. goto out_error;
  326. irq = bind_evtchn_to_irqhandler(*evtchn, pvcalls_front_conn_handler,
  327. 0, "pvcalls-frontend", map);
  328. if (irq < 0) {
  329. ret = irq;
  330. goto out_error;
  331. }
  332. map->active.irq = irq;
  333. map->active_socket = true;
  334. mutex_init(&map->active.in_mutex);
  335. mutex_init(&map->active.out_mutex);
  336. return 0;
  337. out_error:
  338. if (*evtchn > 0)
  339. xenbus_free_evtchn(pvcalls_front_dev, *evtchn);
  340. return ret;
  341. }
  342. int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
  343. int addr_len, int flags)
  344. {
  345. struct pvcalls_bedata *bedata;
  346. struct sock_mapping *map = NULL;
  347. struct xen_pvcalls_request *req;
  348. int notify, req_id, ret;
  349. evtchn_port_t evtchn;
  350. if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
  351. return -EOPNOTSUPP;
  352. map = pvcalls_enter_sock(sock);
  353. if (IS_ERR(map))
  354. return PTR_ERR(map);
  355. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  356. ret = alloc_active_ring(map);
  357. if (ret < 0) {
  358. pvcalls_exit_sock(sock);
  359. return ret;
  360. }
  361. spin_lock(&bedata->socket_lock);
  362. ret = get_request(bedata, &req_id);
  363. if (ret < 0) {
  364. spin_unlock(&bedata->socket_lock);
  365. free_active_ring(map);
  366. pvcalls_exit_sock(sock);
  367. return ret;
  368. }
  369. ret = create_active(map, &evtchn);
  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. req = RING_GET_REQUEST(&bedata->ring, req_id);
  377. req->req_id = req_id;
  378. req->cmd = PVCALLS_CONNECT;
  379. req->u.connect.id = (uintptr_t)map;
  380. req->u.connect.len = addr_len;
  381. req->u.connect.flags = flags;
  382. req->u.connect.ref = map->active.ref;
  383. req->u.connect.evtchn = evtchn;
  384. memcpy(req->u.connect.addr, addr, sizeof(*addr));
  385. map->sock = sock;
  386. bedata->ring.req_prod_pvt++;
  387. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  388. spin_unlock(&bedata->socket_lock);
  389. if (notify)
  390. notify_remote_via_irq(bedata->irq);
  391. wait_event(bedata->inflight_req,
  392. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  393. /* read req_id, then the content */
  394. smp_rmb();
  395. ret = bedata->rsp[req_id].ret;
  396. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  397. pvcalls_exit_sock(sock);
  398. return ret;
  399. }
  400. static int __write_ring(struct pvcalls_data_intf *intf,
  401. struct pvcalls_data *data,
  402. struct iov_iter *msg_iter,
  403. int len)
  404. {
  405. RING_IDX cons, prod, size, masked_prod, masked_cons;
  406. RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
  407. int32_t error;
  408. error = intf->out_error;
  409. if (error < 0)
  410. return error;
  411. cons = intf->out_cons;
  412. prod = intf->out_prod;
  413. /* read indexes before continuing */
  414. virt_mb();
  415. size = pvcalls_queued(prod, cons, array_size);
  416. if (size > array_size)
  417. return -EINVAL;
  418. if (size == array_size)
  419. return 0;
  420. if (len > array_size - size)
  421. len = array_size - size;
  422. masked_prod = pvcalls_mask(prod, array_size);
  423. masked_cons = pvcalls_mask(cons, array_size);
  424. if (masked_prod < masked_cons) {
  425. len = copy_from_iter(data->out + masked_prod, len, msg_iter);
  426. } else {
  427. if (len > array_size - masked_prod) {
  428. int ret = copy_from_iter(data->out + masked_prod,
  429. array_size - masked_prod, msg_iter);
  430. if (ret != array_size - masked_prod) {
  431. len = ret;
  432. goto out;
  433. }
  434. len = ret + copy_from_iter(data->out, len - ret, msg_iter);
  435. } else {
  436. len = copy_from_iter(data->out + masked_prod, len, msg_iter);
  437. }
  438. }
  439. out:
  440. /* write to ring before updating pointer */
  441. virt_wmb();
  442. intf->out_prod += len;
  443. return len;
  444. }
  445. int pvcalls_front_sendmsg(struct socket *sock, struct msghdr *msg,
  446. size_t len)
  447. {
  448. struct sock_mapping *map;
  449. int sent, tot_sent = 0;
  450. int count = 0, flags;
  451. flags = msg->msg_flags;
  452. if (flags & (MSG_CONFIRM|MSG_DONTROUTE|MSG_EOR|MSG_OOB))
  453. return -EOPNOTSUPP;
  454. map = pvcalls_enter_sock(sock);
  455. if (IS_ERR(map))
  456. return PTR_ERR(map);
  457. mutex_lock(&map->active.out_mutex);
  458. if ((flags & MSG_DONTWAIT) && !pvcalls_front_write_todo(map)) {
  459. mutex_unlock(&map->active.out_mutex);
  460. pvcalls_exit_sock(sock);
  461. return -EAGAIN;
  462. }
  463. if (len > INT_MAX)
  464. len = INT_MAX;
  465. again:
  466. count++;
  467. sent = __write_ring(map->active.ring,
  468. &map->active.data, &msg->msg_iter,
  469. len);
  470. if (sent > 0) {
  471. len -= sent;
  472. tot_sent += sent;
  473. notify_remote_via_irq(map->active.irq);
  474. }
  475. if (sent >= 0 && len > 0 && count < PVCALLS_FRONT_MAX_SPIN)
  476. goto again;
  477. if (sent < 0)
  478. tot_sent = sent;
  479. mutex_unlock(&map->active.out_mutex);
  480. pvcalls_exit_sock(sock);
  481. return tot_sent;
  482. }
  483. static int __read_ring(struct pvcalls_data_intf *intf,
  484. struct pvcalls_data *data,
  485. struct iov_iter *msg_iter,
  486. size_t len, int flags)
  487. {
  488. RING_IDX cons, prod, size, masked_prod, masked_cons;
  489. RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
  490. int32_t error;
  491. cons = intf->in_cons;
  492. prod = intf->in_prod;
  493. error = intf->in_error;
  494. /* get pointers before reading from the ring */
  495. virt_rmb();
  496. size = pvcalls_queued(prod, cons, array_size);
  497. masked_prod = pvcalls_mask(prod, array_size);
  498. masked_cons = pvcalls_mask(cons, array_size);
  499. if (size == 0)
  500. return error ?: size;
  501. if (len > size)
  502. len = size;
  503. if (masked_prod > masked_cons) {
  504. len = copy_to_iter(data->in + masked_cons, len, msg_iter);
  505. } else {
  506. if (len > (array_size - masked_cons)) {
  507. int ret = copy_to_iter(data->in + masked_cons,
  508. array_size - masked_cons, msg_iter);
  509. if (ret != array_size - masked_cons) {
  510. len = ret;
  511. goto out;
  512. }
  513. len = ret + copy_to_iter(data->in, len - ret, msg_iter);
  514. } else {
  515. len = copy_to_iter(data->in + masked_cons, len, msg_iter);
  516. }
  517. }
  518. out:
  519. /* read data from the ring before increasing the index */
  520. virt_mb();
  521. if (!(flags & MSG_PEEK))
  522. intf->in_cons += len;
  523. return len;
  524. }
  525. int pvcalls_front_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  526. int flags)
  527. {
  528. int ret;
  529. struct sock_mapping *map;
  530. if (flags & (MSG_CMSG_CLOEXEC|MSG_ERRQUEUE|MSG_OOB|MSG_TRUNC))
  531. return -EOPNOTSUPP;
  532. map = pvcalls_enter_sock(sock);
  533. if (IS_ERR(map))
  534. return PTR_ERR(map);
  535. mutex_lock(&map->active.in_mutex);
  536. if (len > XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER))
  537. len = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
  538. while (!(flags & MSG_DONTWAIT) && !pvcalls_front_read_todo(map)) {
  539. wait_event_interruptible(map->active.inflight_conn_req,
  540. pvcalls_front_read_todo(map));
  541. }
  542. ret = __read_ring(map->active.ring, &map->active.data,
  543. &msg->msg_iter, len, flags);
  544. if (ret > 0)
  545. notify_remote_via_irq(map->active.irq);
  546. if (ret == 0)
  547. ret = (flags & MSG_DONTWAIT) ? -EAGAIN : 0;
  548. if (ret == -ENOTCONN)
  549. ret = 0;
  550. mutex_unlock(&map->active.in_mutex);
  551. pvcalls_exit_sock(sock);
  552. return ret;
  553. }
  554. int pvcalls_front_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
  555. {
  556. struct pvcalls_bedata *bedata;
  557. struct sock_mapping *map = NULL;
  558. struct xen_pvcalls_request *req;
  559. int notify, req_id, ret;
  560. if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
  561. return -EOPNOTSUPP;
  562. map = pvcalls_enter_sock(sock);
  563. if (IS_ERR(map))
  564. return PTR_ERR(map);
  565. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  566. spin_lock(&bedata->socket_lock);
  567. ret = get_request(bedata, &req_id);
  568. if (ret < 0) {
  569. spin_unlock(&bedata->socket_lock);
  570. pvcalls_exit_sock(sock);
  571. return ret;
  572. }
  573. req = RING_GET_REQUEST(&bedata->ring, req_id);
  574. req->req_id = req_id;
  575. map->sock = sock;
  576. req->cmd = PVCALLS_BIND;
  577. req->u.bind.id = (uintptr_t)map;
  578. memcpy(req->u.bind.addr, addr, sizeof(*addr));
  579. req->u.bind.len = addr_len;
  580. init_waitqueue_head(&map->passive.inflight_accept_req);
  581. map->active_socket = false;
  582. bedata->ring.req_prod_pvt++;
  583. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  584. spin_unlock(&bedata->socket_lock);
  585. if (notify)
  586. notify_remote_via_irq(bedata->irq);
  587. wait_event(bedata->inflight_req,
  588. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  589. /* read req_id, then the content */
  590. smp_rmb();
  591. ret = bedata->rsp[req_id].ret;
  592. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  593. map->passive.status = PVCALLS_STATUS_BIND;
  594. pvcalls_exit_sock(sock);
  595. return 0;
  596. }
  597. int pvcalls_front_listen(struct socket *sock, int backlog)
  598. {
  599. struct pvcalls_bedata *bedata;
  600. struct sock_mapping *map;
  601. struct xen_pvcalls_request *req;
  602. int notify, req_id, ret;
  603. map = pvcalls_enter_sock(sock);
  604. if (IS_ERR(map))
  605. return PTR_ERR(map);
  606. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  607. if (map->passive.status != PVCALLS_STATUS_BIND) {
  608. pvcalls_exit_sock(sock);
  609. return -EOPNOTSUPP;
  610. }
  611. spin_lock(&bedata->socket_lock);
  612. ret = get_request(bedata, &req_id);
  613. if (ret < 0) {
  614. spin_unlock(&bedata->socket_lock);
  615. pvcalls_exit_sock(sock);
  616. return ret;
  617. }
  618. req = RING_GET_REQUEST(&bedata->ring, req_id);
  619. req->req_id = req_id;
  620. req->cmd = PVCALLS_LISTEN;
  621. req->u.listen.id = (uintptr_t) map;
  622. req->u.listen.backlog = backlog;
  623. bedata->ring.req_prod_pvt++;
  624. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  625. spin_unlock(&bedata->socket_lock);
  626. if (notify)
  627. notify_remote_via_irq(bedata->irq);
  628. wait_event(bedata->inflight_req,
  629. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  630. /* read req_id, then the content */
  631. smp_rmb();
  632. ret = bedata->rsp[req_id].ret;
  633. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  634. map->passive.status = PVCALLS_STATUS_LISTEN;
  635. pvcalls_exit_sock(sock);
  636. return ret;
  637. }
  638. int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags)
  639. {
  640. struct pvcalls_bedata *bedata;
  641. struct sock_mapping *map;
  642. struct sock_mapping *map2 = NULL;
  643. struct xen_pvcalls_request *req;
  644. int notify, req_id, ret, nonblock;
  645. evtchn_port_t evtchn;
  646. map = pvcalls_enter_sock(sock);
  647. if (IS_ERR(map))
  648. return PTR_ERR(map);
  649. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  650. if (map->passive.status != PVCALLS_STATUS_LISTEN) {
  651. pvcalls_exit_sock(sock);
  652. return -EINVAL;
  653. }
  654. nonblock = flags & SOCK_NONBLOCK;
  655. /*
  656. * Backend only supports 1 inflight accept request, will return
  657. * errors for the others
  658. */
  659. if (test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  660. (void *)&map->passive.flags)) {
  661. req_id = READ_ONCE(map->passive.inflight_req_id);
  662. if (req_id != PVCALLS_INVALID_ID &&
  663. READ_ONCE(bedata->rsp[req_id].req_id) == req_id) {
  664. map2 = map->passive.accept_map;
  665. goto received;
  666. }
  667. if (nonblock) {
  668. pvcalls_exit_sock(sock);
  669. return -EAGAIN;
  670. }
  671. if (wait_event_interruptible(map->passive.inflight_accept_req,
  672. !test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  673. (void *)&map->passive.flags))) {
  674. pvcalls_exit_sock(sock);
  675. return -EINTR;
  676. }
  677. }
  678. map2 = kzalloc(sizeof(*map2), GFP_KERNEL);
  679. if (map2 == NULL) {
  680. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  681. (void *)&map->passive.flags);
  682. pvcalls_exit_sock(sock);
  683. return -ENOMEM;
  684. }
  685. ret = alloc_active_ring(map2);
  686. if (ret < 0) {
  687. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  688. (void *)&map->passive.flags);
  689. kfree(map2);
  690. pvcalls_exit_sock(sock);
  691. return ret;
  692. }
  693. spin_lock(&bedata->socket_lock);
  694. ret = get_request(bedata, &req_id);
  695. if (ret < 0) {
  696. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  697. (void *)&map->passive.flags);
  698. spin_unlock(&bedata->socket_lock);
  699. free_active_ring(map2);
  700. kfree(map2);
  701. pvcalls_exit_sock(sock);
  702. return ret;
  703. }
  704. ret = create_active(map2, &evtchn);
  705. if (ret < 0) {
  706. free_active_ring(map2);
  707. kfree(map2);
  708. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  709. (void *)&map->passive.flags);
  710. spin_unlock(&bedata->socket_lock);
  711. pvcalls_exit_sock(sock);
  712. return ret;
  713. }
  714. list_add_tail(&map2->list, &bedata->socket_mappings);
  715. req = RING_GET_REQUEST(&bedata->ring, req_id);
  716. req->req_id = req_id;
  717. req->cmd = PVCALLS_ACCEPT;
  718. req->u.accept.id = (uintptr_t) map;
  719. req->u.accept.ref = map2->active.ref;
  720. req->u.accept.id_new = (uintptr_t) map2;
  721. req->u.accept.evtchn = evtchn;
  722. map->passive.accept_map = map2;
  723. bedata->ring.req_prod_pvt++;
  724. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  725. spin_unlock(&bedata->socket_lock);
  726. if (notify)
  727. notify_remote_via_irq(bedata->irq);
  728. /* We could check if we have received a response before returning. */
  729. if (nonblock) {
  730. WRITE_ONCE(map->passive.inflight_req_id, req_id);
  731. pvcalls_exit_sock(sock);
  732. return -EAGAIN;
  733. }
  734. if (wait_event_interruptible(bedata->inflight_req,
  735. READ_ONCE(bedata->rsp[req_id].req_id) == req_id)) {
  736. pvcalls_exit_sock(sock);
  737. return -EINTR;
  738. }
  739. /* read req_id, then the content */
  740. smp_rmb();
  741. received:
  742. map2->sock = newsock;
  743. newsock->sk = sk_alloc(sock_net(sock->sk), PF_INET, GFP_KERNEL, &pvcalls_proto, false);
  744. if (!newsock->sk) {
  745. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  746. map->passive.inflight_req_id = PVCALLS_INVALID_ID;
  747. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  748. (void *)&map->passive.flags);
  749. pvcalls_front_free_map(bedata, map2);
  750. pvcalls_exit_sock(sock);
  751. return -ENOMEM;
  752. }
  753. newsock->sk->sk_send_head = (void *)map2;
  754. ret = bedata->rsp[req_id].ret;
  755. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  756. map->passive.inflight_req_id = PVCALLS_INVALID_ID;
  757. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, (void *)&map->passive.flags);
  758. wake_up(&map->passive.inflight_accept_req);
  759. pvcalls_exit_sock(sock);
  760. return ret;
  761. }
  762. static __poll_t pvcalls_front_poll_passive(struct file *file,
  763. struct pvcalls_bedata *bedata,
  764. struct sock_mapping *map,
  765. poll_table *wait)
  766. {
  767. int notify, req_id, ret;
  768. struct xen_pvcalls_request *req;
  769. if (test_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  770. (void *)&map->passive.flags)) {
  771. uint32_t req_id = READ_ONCE(map->passive.inflight_req_id);
  772. if (req_id != PVCALLS_INVALID_ID &&
  773. READ_ONCE(bedata->rsp[req_id].req_id) == req_id)
  774. return EPOLLIN | EPOLLRDNORM;
  775. poll_wait(file, &map->passive.inflight_accept_req, wait);
  776. return 0;
  777. }
  778. if (test_and_clear_bit(PVCALLS_FLAG_POLL_RET,
  779. (void *)&map->passive.flags))
  780. return EPOLLIN | EPOLLRDNORM;
  781. /*
  782. * First check RET, then INFLIGHT. No barriers necessary to
  783. * ensure execution ordering because of the conditional
  784. * instructions creating control dependencies.
  785. */
  786. if (test_and_set_bit(PVCALLS_FLAG_POLL_INFLIGHT,
  787. (void *)&map->passive.flags)) {
  788. poll_wait(file, &bedata->inflight_req, wait);
  789. return 0;
  790. }
  791. spin_lock(&bedata->socket_lock);
  792. ret = get_request(bedata, &req_id);
  793. if (ret < 0) {
  794. spin_unlock(&bedata->socket_lock);
  795. return ret;
  796. }
  797. req = RING_GET_REQUEST(&bedata->ring, req_id);
  798. req->req_id = req_id;
  799. req->cmd = PVCALLS_POLL;
  800. req->u.poll.id = (uintptr_t) map;
  801. bedata->ring.req_prod_pvt++;
  802. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  803. spin_unlock(&bedata->socket_lock);
  804. if (notify)
  805. notify_remote_via_irq(bedata->irq);
  806. poll_wait(file, &bedata->inflight_req, wait);
  807. return 0;
  808. }
  809. static __poll_t pvcalls_front_poll_active(struct file *file,
  810. struct pvcalls_bedata *bedata,
  811. struct sock_mapping *map,
  812. poll_table *wait)
  813. {
  814. __poll_t mask = 0;
  815. int32_t in_error, out_error;
  816. struct pvcalls_data_intf *intf = map->active.ring;
  817. out_error = intf->out_error;
  818. in_error = intf->in_error;
  819. poll_wait(file, &map->active.inflight_conn_req, wait);
  820. if (pvcalls_front_write_todo(map))
  821. mask |= EPOLLOUT | EPOLLWRNORM;
  822. if (pvcalls_front_read_todo(map))
  823. mask |= EPOLLIN | EPOLLRDNORM;
  824. if (in_error != 0 || out_error != 0)
  825. mask |= EPOLLERR;
  826. return mask;
  827. }
  828. __poll_t pvcalls_front_poll(struct file *file, struct socket *sock,
  829. poll_table *wait)
  830. {
  831. struct pvcalls_bedata *bedata;
  832. struct sock_mapping *map;
  833. __poll_t ret;
  834. map = pvcalls_enter_sock(sock);
  835. if (IS_ERR(map))
  836. return EPOLLNVAL;
  837. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  838. if (map->active_socket)
  839. ret = pvcalls_front_poll_active(file, bedata, map, wait);
  840. else
  841. ret = pvcalls_front_poll_passive(file, bedata, map, wait);
  842. pvcalls_exit_sock(sock);
  843. return ret;
  844. }
  845. int pvcalls_front_release(struct socket *sock)
  846. {
  847. struct pvcalls_bedata *bedata;
  848. struct sock_mapping *map;
  849. int req_id, notify, ret;
  850. struct xen_pvcalls_request *req;
  851. if (sock->sk == NULL)
  852. return 0;
  853. map = pvcalls_enter_sock(sock);
  854. if (IS_ERR(map)) {
  855. if (PTR_ERR(map) == -ENOTCONN)
  856. return -EIO;
  857. else
  858. return 0;
  859. }
  860. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  861. spin_lock(&bedata->socket_lock);
  862. ret = get_request(bedata, &req_id);
  863. if (ret < 0) {
  864. spin_unlock(&bedata->socket_lock);
  865. pvcalls_exit_sock(sock);
  866. return ret;
  867. }
  868. sock->sk->sk_send_head = NULL;
  869. req = RING_GET_REQUEST(&bedata->ring, req_id);
  870. req->req_id = req_id;
  871. req->cmd = PVCALLS_RELEASE;
  872. req->u.release.id = (uintptr_t)map;
  873. bedata->ring.req_prod_pvt++;
  874. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  875. spin_unlock(&bedata->socket_lock);
  876. if (notify)
  877. notify_remote_via_irq(bedata->irq);
  878. wait_event(bedata->inflight_req,
  879. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  880. if (map->active_socket) {
  881. /*
  882. * Set in_error and wake up inflight_conn_req to force
  883. * recvmsg waiters to exit.
  884. */
  885. map->active.ring->in_error = -EBADF;
  886. wake_up_interruptible(&map->active.inflight_conn_req);
  887. /*
  888. * We need to make sure that sendmsg/recvmsg on this socket have
  889. * not started before we've cleared sk_send_head here. The
  890. * easiest way to guarantee this is to see that no pvcalls
  891. * (other than us) is in progress on this socket.
  892. */
  893. while (atomic_read(&map->refcount) > 1)
  894. cpu_relax();
  895. pvcalls_front_free_map(bedata, map);
  896. } else {
  897. wake_up(&bedata->inflight_req);
  898. wake_up(&map->passive.inflight_accept_req);
  899. while (atomic_read(&map->refcount) > 1)
  900. cpu_relax();
  901. spin_lock(&bedata->socket_lock);
  902. list_del(&map->list);
  903. spin_unlock(&bedata->socket_lock);
  904. if (READ_ONCE(map->passive.inflight_req_id) != PVCALLS_INVALID_ID &&
  905. READ_ONCE(map->passive.inflight_req_id) != 0) {
  906. pvcalls_front_free_map(bedata,
  907. map->passive.accept_map);
  908. }
  909. kfree(map);
  910. }
  911. WRITE_ONCE(bedata->rsp[req_id].req_id, PVCALLS_INVALID_ID);
  912. pvcalls_exit();
  913. return 0;
  914. }
  915. static const struct xenbus_device_id pvcalls_front_ids[] = {
  916. { "pvcalls" },
  917. { "" }
  918. };
  919. static int pvcalls_front_remove(struct xenbus_device *dev)
  920. {
  921. struct pvcalls_bedata *bedata;
  922. struct sock_mapping *map = NULL, *n;
  923. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  924. dev_set_drvdata(&dev->dev, NULL);
  925. pvcalls_front_dev = NULL;
  926. if (bedata->irq >= 0)
  927. unbind_from_irqhandler(bedata->irq, dev);
  928. list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
  929. map->sock->sk->sk_send_head = NULL;
  930. if (map->active_socket) {
  931. map->active.ring->in_error = -EBADF;
  932. wake_up_interruptible(&map->active.inflight_conn_req);
  933. }
  934. }
  935. smp_mb();
  936. while (atomic_read(&pvcalls_refcount) > 0)
  937. cpu_relax();
  938. list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
  939. if (map->active_socket) {
  940. /* No need to lock, refcount is 0 */
  941. pvcalls_front_free_map(bedata, map);
  942. } else {
  943. list_del(&map->list);
  944. kfree(map);
  945. }
  946. }
  947. if (bedata->ref != -1)
  948. gnttab_end_foreign_access(bedata->ref, NULL);
  949. kfree(bedata->ring.sring);
  950. kfree(bedata);
  951. xenbus_switch_state(dev, XenbusStateClosed);
  952. return 0;
  953. }
  954. static int pvcalls_front_probe(struct xenbus_device *dev,
  955. const struct xenbus_device_id *id)
  956. {
  957. int ret = -ENOMEM, i;
  958. evtchn_port_t evtchn;
  959. unsigned int max_page_order, function_calls, len;
  960. char *versions;
  961. grant_ref_t gref_head = 0;
  962. struct xenbus_transaction xbt;
  963. struct pvcalls_bedata *bedata = NULL;
  964. struct xen_pvcalls_sring *sring;
  965. if (pvcalls_front_dev != NULL) {
  966. dev_err(&dev->dev, "only one PV Calls connection supported\n");
  967. return -EINVAL;
  968. }
  969. versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
  970. if (IS_ERR(versions))
  971. return PTR_ERR(versions);
  972. if (!len)
  973. return -EINVAL;
  974. if (strcmp(versions, "1")) {
  975. kfree(versions);
  976. return -EINVAL;
  977. }
  978. kfree(versions);
  979. max_page_order = xenbus_read_unsigned(dev->otherend,
  980. "max-page-order", 0);
  981. if (max_page_order < PVCALLS_RING_ORDER)
  982. return -ENODEV;
  983. function_calls = xenbus_read_unsigned(dev->otherend,
  984. "function-calls", 0);
  985. /* See XENBUS_FUNCTIONS_CALLS in pvcalls.h */
  986. if (function_calls != 1)
  987. return -ENODEV;
  988. pr_info("%s max-page-order is %u\n", __func__, max_page_order);
  989. bedata = kzalloc(sizeof(struct pvcalls_bedata), GFP_KERNEL);
  990. if (!bedata)
  991. return -ENOMEM;
  992. dev_set_drvdata(&dev->dev, bedata);
  993. pvcalls_front_dev = dev;
  994. init_waitqueue_head(&bedata->inflight_req);
  995. INIT_LIST_HEAD(&bedata->socket_mappings);
  996. spin_lock_init(&bedata->socket_lock);
  997. bedata->irq = -1;
  998. bedata->ref = -1;
  999. for (i = 0; i < PVCALLS_NR_RSP_PER_RING; i++)
  1000. bedata->rsp[i].req_id = PVCALLS_INVALID_ID;
  1001. sring = (struct xen_pvcalls_sring *) __get_free_page(GFP_KERNEL |
  1002. __GFP_ZERO);
  1003. if (!sring)
  1004. goto error;
  1005. SHARED_RING_INIT(sring);
  1006. FRONT_RING_INIT(&bedata->ring, sring, XEN_PAGE_SIZE);
  1007. ret = xenbus_alloc_evtchn(dev, &evtchn);
  1008. if (ret)
  1009. goto error;
  1010. bedata->irq = bind_evtchn_to_irqhandler(evtchn,
  1011. pvcalls_front_event_handler,
  1012. 0, "pvcalls-frontend", dev);
  1013. if (bedata->irq < 0) {
  1014. ret = bedata->irq;
  1015. goto error;
  1016. }
  1017. ret = gnttab_alloc_grant_references(1, &gref_head);
  1018. if (ret < 0)
  1019. goto error;
  1020. ret = gnttab_claim_grant_reference(&gref_head);
  1021. if (ret < 0)
  1022. goto error;
  1023. bedata->ref = ret;
  1024. gnttab_grant_foreign_access_ref(bedata->ref, dev->otherend_id,
  1025. virt_to_gfn((void *)sring), 0);
  1026. again:
  1027. ret = xenbus_transaction_start(&xbt);
  1028. if (ret) {
  1029. xenbus_dev_fatal(dev, ret, "starting transaction");
  1030. goto error;
  1031. }
  1032. ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
  1033. if (ret)
  1034. goto error_xenbus;
  1035. ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", bedata->ref);
  1036. if (ret)
  1037. goto error_xenbus;
  1038. ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
  1039. evtchn);
  1040. if (ret)
  1041. goto error_xenbus;
  1042. ret = xenbus_transaction_end(xbt, 0);
  1043. if (ret) {
  1044. if (ret == -EAGAIN)
  1045. goto again;
  1046. xenbus_dev_fatal(dev, ret, "completing transaction");
  1047. goto error;
  1048. }
  1049. xenbus_switch_state(dev, XenbusStateInitialised);
  1050. return 0;
  1051. error_xenbus:
  1052. xenbus_transaction_end(xbt, 1);
  1053. xenbus_dev_fatal(dev, ret, "writing xenstore");
  1054. error:
  1055. pvcalls_front_remove(dev);
  1056. return ret;
  1057. }
  1058. static void pvcalls_front_changed(struct xenbus_device *dev,
  1059. enum xenbus_state backend_state)
  1060. {
  1061. switch (backend_state) {
  1062. case XenbusStateReconfiguring:
  1063. case XenbusStateReconfigured:
  1064. case XenbusStateInitialising:
  1065. case XenbusStateInitialised:
  1066. case XenbusStateUnknown:
  1067. break;
  1068. case XenbusStateInitWait:
  1069. break;
  1070. case XenbusStateConnected:
  1071. xenbus_switch_state(dev, XenbusStateConnected);
  1072. break;
  1073. case XenbusStateClosed:
  1074. if (dev->state == XenbusStateClosed)
  1075. break;
  1076. /* Missed the backend's CLOSING state */
  1077. fallthrough;
  1078. case XenbusStateClosing:
  1079. xenbus_frontend_closed(dev);
  1080. break;
  1081. }
  1082. }
  1083. static struct xenbus_driver pvcalls_front_driver = {
  1084. .ids = pvcalls_front_ids,
  1085. .probe = pvcalls_front_probe,
  1086. .remove = pvcalls_front_remove,
  1087. .otherend_changed = pvcalls_front_changed,
  1088. .not_essential = true,
  1089. };
  1090. static int __init pvcalls_frontend_init(void)
  1091. {
  1092. if (!xen_domain())
  1093. return -ENODEV;
  1094. pr_info("Initialising Xen pvcalls frontend driver\n");
  1095. return xenbus_register_frontend(&pvcalls_front_driver);
  1096. }
  1097. module_init(pvcalls_frontend_init);
  1098. MODULE_DESCRIPTION("Xen PV Calls frontend driver");
  1099. MODULE_AUTHOR("Stefano Stabellini <[email protected]>");
  1100. MODULE_LICENSE("GPL");