pvcalls-front.c 32 KB

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