pvcalls-back.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*
  2. * (c) 2017 Stefano Stabellini <stefano@aporeto.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/inet.h>
  15. #include <linux/kthread.h>
  16. #include <linux/list.h>
  17. #include <linux/radix-tree.h>
  18. #include <linux/module.h>
  19. #include <linux/semaphore.h>
  20. #include <linux/wait.h>
  21. #include <net/sock.h>
  22. #include <net/inet_common.h>
  23. #include <net/inet_connection_sock.h>
  24. #include <net/request_sock.h>
  25. #include <xen/events.h>
  26. #include <xen/grant_table.h>
  27. #include <xen/xen.h>
  28. #include <xen/xenbus.h>
  29. #include <xen/interface/io/pvcalls.h>
  30. #define PVCALLS_VERSIONS "1"
  31. #define MAX_RING_ORDER XENBUS_MAX_RING_GRANT_ORDER
  32. struct pvcalls_back_global {
  33. struct list_head frontends;
  34. struct semaphore frontends_lock;
  35. } pvcalls_back_global;
  36. /*
  37. * Per-frontend data structure. It contains pointers to the command
  38. * ring, its event channel, a list of active sockets and a tree of
  39. * passive sockets.
  40. */
  41. struct pvcalls_fedata {
  42. struct list_head list;
  43. struct xenbus_device *dev;
  44. struct xen_pvcalls_sring *sring;
  45. struct xen_pvcalls_back_ring ring;
  46. int irq;
  47. struct list_head socket_mappings;
  48. struct radix_tree_root socketpass_mappings;
  49. struct semaphore socket_lock;
  50. };
  51. struct pvcalls_ioworker {
  52. struct work_struct register_work;
  53. struct workqueue_struct *wq;
  54. };
  55. struct sock_mapping {
  56. struct list_head list;
  57. struct pvcalls_fedata *fedata;
  58. struct sockpass_mapping *sockpass;
  59. struct socket *sock;
  60. uint64_t id;
  61. grant_ref_t ref;
  62. struct pvcalls_data_intf *ring;
  63. void *bytes;
  64. struct pvcalls_data data;
  65. uint32_t ring_order;
  66. int irq;
  67. atomic_t read;
  68. atomic_t write;
  69. atomic_t io;
  70. atomic_t release;
  71. atomic_t eoi;
  72. void (*saved_data_ready)(struct sock *sk);
  73. struct pvcalls_ioworker ioworker;
  74. };
  75. struct sockpass_mapping {
  76. struct list_head list;
  77. struct pvcalls_fedata *fedata;
  78. struct socket *sock;
  79. uint64_t id;
  80. struct xen_pvcalls_request reqcopy;
  81. spinlock_t copy_lock;
  82. struct workqueue_struct *wq;
  83. struct work_struct register_work;
  84. void (*saved_data_ready)(struct sock *sk);
  85. };
  86. static irqreturn_t pvcalls_back_conn_event(int irq, void *sock_map);
  87. static int pvcalls_back_release_active(struct xenbus_device *dev,
  88. struct pvcalls_fedata *fedata,
  89. struct sock_mapping *map);
  90. static bool pvcalls_conn_back_read(void *opaque)
  91. {
  92. struct sock_mapping *map = (struct sock_mapping *)opaque;
  93. struct msghdr msg;
  94. struct kvec vec[2];
  95. RING_IDX cons, prod, size, wanted, array_size, masked_prod, masked_cons;
  96. int32_t error;
  97. struct pvcalls_data_intf *intf = map->ring;
  98. struct pvcalls_data *data = &map->data;
  99. unsigned long flags;
  100. int ret;
  101. array_size = XEN_FLEX_RING_SIZE(map->ring_order);
  102. cons = intf->in_cons;
  103. prod = intf->in_prod;
  104. error = intf->in_error;
  105. /* read the indexes first, then deal with the data */
  106. virt_mb();
  107. if (error)
  108. return false;
  109. size = pvcalls_queued(prod, cons, array_size);
  110. if (size >= array_size)
  111. return false;
  112. spin_lock_irqsave(&map->sock->sk->sk_receive_queue.lock, flags);
  113. if (skb_queue_empty(&map->sock->sk->sk_receive_queue)) {
  114. atomic_set(&map->read, 0);
  115. spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock,
  116. flags);
  117. return true;
  118. }
  119. spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock, flags);
  120. wanted = array_size - size;
  121. masked_prod = pvcalls_mask(prod, array_size);
  122. masked_cons = pvcalls_mask(cons, array_size);
  123. memset(&msg, 0, sizeof(msg));
  124. if (masked_prod < masked_cons) {
  125. vec[0].iov_base = data->in + masked_prod;
  126. vec[0].iov_len = wanted;
  127. iov_iter_kvec(&msg.msg_iter, ITER_KVEC|WRITE, vec, 1, wanted);
  128. } else {
  129. vec[0].iov_base = data->in + masked_prod;
  130. vec[0].iov_len = array_size - masked_prod;
  131. vec[1].iov_base = data->in;
  132. vec[1].iov_len = wanted - vec[0].iov_len;
  133. iov_iter_kvec(&msg.msg_iter, ITER_KVEC|WRITE, vec, 2, wanted);
  134. }
  135. atomic_set(&map->read, 0);
  136. ret = inet_recvmsg(map->sock, &msg, wanted, MSG_DONTWAIT);
  137. WARN_ON(ret > wanted);
  138. if (ret == -EAGAIN) /* shouldn't happen */
  139. return true;
  140. if (!ret)
  141. ret = -ENOTCONN;
  142. spin_lock_irqsave(&map->sock->sk->sk_receive_queue.lock, flags);
  143. if (ret > 0 && !skb_queue_empty(&map->sock->sk->sk_receive_queue))
  144. atomic_inc(&map->read);
  145. spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock, flags);
  146. /* write the data, then modify the indexes */
  147. virt_wmb();
  148. if (ret < 0) {
  149. atomic_set(&map->read, 0);
  150. intf->in_error = ret;
  151. } else
  152. intf->in_prod = prod + ret;
  153. /* update the indexes, then notify the other end */
  154. virt_wmb();
  155. notify_remote_via_irq(map->irq);
  156. return true;
  157. }
  158. static bool pvcalls_conn_back_write(struct sock_mapping *map)
  159. {
  160. struct pvcalls_data_intf *intf = map->ring;
  161. struct pvcalls_data *data = &map->data;
  162. struct msghdr msg;
  163. struct kvec vec[2];
  164. RING_IDX cons, prod, size, array_size;
  165. int ret;
  166. cons = intf->out_cons;
  167. prod = intf->out_prod;
  168. /* read the indexes before dealing with the data */
  169. virt_mb();
  170. array_size = XEN_FLEX_RING_SIZE(map->ring_order);
  171. size = pvcalls_queued(prod, cons, array_size);
  172. if (size == 0)
  173. return false;
  174. memset(&msg, 0, sizeof(msg));
  175. msg.msg_flags |= MSG_DONTWAIT;
  176. if (pvcalls_mask(prod, array_size) > pvcalls_mask(cons, array_size)) {
  177. vec[0].iov_base = data->out + pvcalls_mask(cons, array_size);
  178. vec[0].iov_len = size;
  179. iov_iter_kvec(&msg.msg_iter, ITER_KVEC|READ, vec, 1, size);
  180. } else {
  181. vec[0].iov_base = data->out + pvcalls_mask(cons, array_size);
  182. vec[0].iov_len = array_size - pvcalls_mask(cons, array_size);
  183. vec[1].iov_base = data->out;
  184. vec[1].iov_len = size - vec[0].iov_len;
  185. iov_iter_kvec(&msg.msg_iter, ITER_KVEC|READ, vec, 2, size);
  186. }
  187. atomic_set(&map->write, 0);
  188. ret = inet_sendmsg(map->sock, &msg, size);
  189. if (ret == -EAGAIN) {
  190. atomic_inc(&map->write);
  191. atomic_inc(&map->io);
  192. return true;
  193. }
  194. /* write the data, then update the indexes */
  195. virt_wmb();
  196. if (ret < 0) {
  197. intf->out_error = ret;
  198. } else {
  199. intf->out_error = 0;
  200. intf->out_cons = cons + ret;
  201. prod = intf->out_prod;
  202. }
  203. /* update the indexes, then notify the other end */
  204. virt_wmb();
  205. if (prod != cons + ret) {
  206. atomic_inc(&map->write);
  207. atomic_inc(&map->io);
  208. }
  209. notify_remote_via_irq(map->irq);
  210. return true;
  211. }
  212. static void pvcalls_back_ioworker(struct work_struct *work)
  213. {
  214. struct pvcalls_ioworker *ioworker = container_of(work,
  215. struct pvcalls_ioworker, register_work);
  216. struct sock_mapping *map = container_of(ioworker, struct sock_mapping,
  217. ioworker);
  218. unsigned int eoi_flags = XEN_EOI_FLAG_SPURIOUS;
  219. while (atomic_read(&map->io) > 0) {
  220. if (atomic_read(&map->release) > 0) {
  221. atomic_set(&map->release, 0);
  222. return;
  223. }
  224. if (atomic_read(&map->read) > 0 &&
  225. pvcalls_conn_back_read(map))
  226. eoi_flags = 0;
  227. if (atomic_read(&map->write) > 0 &&
  228. pvcalls_conn_back_write(map))
  229. eoi_flags = 0;
  230. if (atomic_read(&map->eoi) > 0 && !atomic_read(&map->write)) {
  231. atomic_set(&map->eoi, 0);
  232. xen_irq_lateeoi(map->irq, eoi_flags);
  233. eoi_flags = XEN_EOI_FLAG_SPURIOUS;
  234. }
  235. atomic_dec(&map->io);
  236. }
  237. }
  238. static int pvcalls_back_socket(struct xenbus_device *dev,
  239. struct xen_pvcalls_request *req)
  240. {
  241. struct pvcalls_fedata *fedata;
  242. int ret;
  243. struct xen_pvcalls_response *rsp;
  244. fedata = dev_get_drvdata(&dev->dev);
  245. if (req->u.socket.domain != AF_INET ||
  246. req->u.socket.type != SOCK_STREAM ||
  247. (req->u.socket.protocol != IPPROTO_IP &&
  248. req->u.socket.protocol != AF_INET))
  249. ret = -EAFNOSUPPORT;
  250. else
  251. ret = 0;
  252. /* leave the actual socket allocation for later */
  253. rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
  254. rsp->req_id = req->req_id;
  255. rsp->cmd = req->cmd;
  256. rsp->u.socket.id = req->u.socket.id;
  257. rsp->ret = ret;
  258. return 0;
  259. }
  260. static void pvcalls_sk_state_change(struct sock *sock)
  261. {
  262. struct sock_mapping *map = sock->sk_user_data;
  263. if (map == NULL)
  264. return;
  265. atomic_inc(&map->read);
  266. notify_remote_via_irq(map->irq);
  267. }
  268. static void pvcalls_sk_data_ready(struct sock *sock)
  269. {
  270. struct sock_mapping *map = sock->sk_user_data;
  271. struct pvcalls_ioworker *iow;
  272. if (map == NULL)
  273. return;
  274. iow = &map->ioworker;
  275. atomic_inc(&map->read);
  276. atomic_inc(&map->io);
  277. queue_work(iow->wq, &iow->register_work);
  278. }
  279. static struct sock_mapping *pvcalls_new_active_socket(
  280. struct pvcalls_fedata *fedata,
  281. uint64_t id,
  282. grant_ref_t ref,
  283. uint32_t evtchn,
  284. struct socket *sock)
  285. {
  286. int ret;
  287. struct sock_mapping *map;
  288. void *page;
  289. map = kzalloc(sizeof(*map), GFP_KERNEL);
  290. if (map == NULL)
  291. return NULL;
  292. map->fedata = fedata;
  293. map->sock = sock;
  294. map->id = id;
  295. map->ref = ref;
  296. ret = xenbus_map_ring_valloc(fedata->dev, &ref, 1, &page);
  297. if (ret < 0)
  298. goto out;
  299. map->ring = page;
  300. map->ring_order = map->ring->ring_order;
  301. /* first read the order, then map the data ring */
  302. virt_rmb();
  303. if (map->ring_order > MAX_RING_ORDER) {
  304. pr_warn("%s frontend requested ring_order %u, which is > MAX (%u)\n",
  305. __func__, map->ring_order, MAX_RING_ORDER);
  306. goto out;
  307. }
  308. ret = xenbus_map_ring_valloc(fedata->dev, map->ring->ref,
  309. (1 << map->ring_order), &page);
  310. if (ret < 0)
  311. goto out;
  312. map->bytes = page;
  313. ret = bind_interdomain_evtchn_to_irqhandler_lateeoi(
  314. fedata->dev->otherend_id, evtchn,
  315. pvcalls_back_conn_event, 0, "pvcalls-backend", map);
  316. if (ret < 0)
  317. goto out;
  318. map->irq = ret;
  319. map->data.in = map->bytes;
  320. map->data.out = map->bytes + XEN_FLEX_RING_SIZE(map->ring_order);
  321. map->ioworker.wq = alloc_workqueue("pvcalls_io", WQ_UNBOUND, 1);
  322. if (!map->ioworker.wq)
  323. goto out;
  324. atomic_set(&map->io, 1);
  325. INIT_WORK(&map->ioworker.register_work, pvcalls_back_ioworker);
  326. down(&fedata->socket_lock);
  327. list_add_tail(&map->list, &fedata->socket_mappings);
  328. up(&fedata->socket_lock);
  329. write_lock_bh(&map->sock->sk->sk_callback_lock);
  330. map->saved_data_ready = map->sock->sk->sk_data_ready;
  331. map->sock->sk->sk_user_data = map;
  332. map->sock->sk->sk_data_ready = pvcalls_sk_data_ready;
  333. map->sock->sk->sk_state_change = pvcalls_sk_state_change;
  334. write_unlock_bh(&map->sock->sk->sk_callback_lock);
  335. return map;
  336. out:
  337. down(&fedata->socket_lock);
  338. list_del(&map->list);
  339. pvcalls_back_release_active(fedata->dev, fedata, map);
  340. up(&fedata->socket_lock);
  341. return NULL;
  342. }
  343. static int pvcalls_back_connect(struct xenbus_device *dev,
  344. struct xen_pvcalls_request *req)
  345. {
  346. struct pvcalls_fedata *fedata;
  347. int ret = -EINVAL;
  348. struct socket *sock;
  349. struct sock_mapping *map;
  350. struct xen_pvcalls_response *rsp;
  351. struct sockaddr *sa = (struct sockaddr *)&req->u.connect.addr;
  352. fedata = dev_get_drvdata(&dev->dev);
  353. if (req->u.connect.len < sizeof(sa->sa_family) ||
  354. req->u.connect.len > sizeof(req->u.connect.addr) ||
  355. sa->sa_family != AF_INET)
  356. goto out;
  357. ret = sock_create(AF_INET, SOCK_STREAM, 0, &sock);
  358. if (ret < 0)
  359. goto out;
  360. ret = inet_stream_connect(sock, sa, req->u.connect.len, 0);
  361. if (ret < 0) {
  362. sock_release(sock);
  363. goto out;
  364. }
  365. map = pvcalls_new_active_socket(fedata,
  366. req->u.connect.id,
  367. req->u.connect.ref,
  368. req->u.connect.evtchn,
  369. sock);
  370. if (!map) {
  371. ret = -EFAULT;
  372. sock_release(sock);
  373. }
  374. out:
  375. rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
  376. rsp->req_id = req->req_id;
  377. rsp->cmd = req->cmd;
  378. rsp->u.connect.id = req->u.connect.id;
  379. rsp->ret = ret;
  380. return 0;
  381. }
  382. static int pvcalls_back_release_active(struct xenbus_device *dev,
  383. struct pvcalls_fedata *fedata,
  384. struct sock_mapping *map)
  385. {
  386. disable_irq(map->irq);
  387. if (map->sock->sk != NULL) {
  388. write_lock_bh(&map->sock->sk->sk_callback_lock);
  389. map->sock->sk->sk_user_data = NULL;
  390. map->sock->sk->sk_data_ready = map->saved_data_ready;
  391. write_unlock_bh(&map->sock->sk->sk_callback_lock);
  392. }
  393. atomic_set(&map->release, 1);
  394. flush_work(&map->ioworker.register_work);
  395. xenbus_unmap_ring_vfree(dev, map->bytes);
  396. xenbus_unmap_ring_vfree(dev, (void *)map->ring);
  397. unbind_from_irqhandler(map->irq, map);
  398. sock_release(map->sock);
  399. kfree(map);
  400. return 0;
  401. }
  402. static int pvcalls_back_release_passive(struct xenbus_device *dev,
  403. struct pvcalls_fedata *fedata,
  404. struct sockpass_mapping *mappass)
  405. {
  406. if (mappass->sock->sk != NULL) {
  407. write_lock_bh(&mappass->sock->sk->sk_callback_lock);
  408. mappass->sock->sk->sk_user_data = NULL;
  409. mappass->sock->sk->sk_data_ready = mappass->saved_data_ready;
  410. write_unlock_bh(&mappass->sock->sk->sk_callback_lock);
  411. }
  412. sock_release(mappass->sock);
  413. flush_workqueue(mappass->wq);
  414. destroy_workqueue(mappass->wq);
  415. kfree(mappass);
  416. return 0;
  417. }
  418. static int pvcalls_back_release(struct xenbus_device *dev,
  419. struct xen_pvcalls_request *req)
  420. {
  421. struct pvcalls_fedata *fedata;
  422. struct sock_mapping *map, *n;
  423. struct sockpass_mapping *mappass;
  424. int ret = 0;
  425. struct xen_pvcalls_response *rsp;
  426. fedata = dev_get_drvdata(&dev->dev);
  427. down(&fedata->socket_lock);
  428. list_for_each_entry_safe(map, n, &fedata->socket_mappings, list) {
  429. if (map->id == req->u.release.id) {
  430. list_del(&map->list);
  431. up(&fedata->socket_lock);
  432. ret = pvcalls_back_release_active(dev, fedata, map);
  433. goto out;
  434. }
  435. }
  436. mappass = radix_tree_lookup(&fedata->socketpass_mappings,
  437. req->u.release.id);
  438. if (mappass != NULL) {
  439. radix_tree_delete(&fedata->socketpass_mappings, mappass->id);
  440. up(&fedata->socket_lock);
  441. ret = pvcalls_back_release_passive(dev, fedata, mappass);
  442. } else
  443. up(&fedata->socket_lock);
  444. out:
  445. rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
  446. rsp->req_id = req->req_id;
  447. rsp->u.release.id = req->u.release.id;
  448. rsp->cmd = req->cmd;
  449. rsp->ret = ret;
  450. return 0;
  451. }
  452. static void __pvcalls_back_accept(struct work_struct *work)
  453. {
  454. struct sockpass_mapping *mappass = container_of(
  455. work, struct sockpass_mapping, register_work);
  456. struct sock_mapping *map;
  457. struct pvcalls_ioworker *iow;
  458. struct pvcalls_fedata *fedata;
  459. struct socket *sock;
  460. struct xen_pvcalls_response *rsp;
  461. struct xen_pvcalls_request *req;
  462. int notify;
  463. int ret = -EINVAL;
  464. unsigned long flags;
  465. fedata = mappass->fedata;
  466. /*
  467. * __pvcalls_back_accept can race against pvcalls_back_accept.
  468. * We only need to check the value of "cmd" on read. It could be
  469. * done atomically, but to simplify the code on the write side, we
  470. * use a spinlock.
  471. */
  472. spin_lock_irqsave(&mappass->copy_lock, flags);
  473. req = &mappass->reqcopy;
  474. if (req->cmd != PVCALLS_ACCEPT) {
  475. spin_unlock_irqrestore(&mappass->copy_lock, flags);
  476. return;
  477. }
  478. spin_unlock_irqrestore(&mappass->copy_lock, flags);
  479. sock = sock_alloc();
  480. if (sock == NULL)
  481. goto out_error;
  482. sock->type = mappass->sock->type;
  483. sock->ops = mappass->sock->ops;
  484. ret = inet_accept(mappass->sock, sock, O_NONBLOCK, true);
  485. if (ret == -EAGAIN) {
  486. sock_release(sock);
  487. return;
  488. }
  489. map = pvcalls_new_active_socket(fedata,
  490. req->u.accept.id_new,
  491. req->u.accept.ref,
  492. req->u.accept.evtchn,
  493. sock);
  494. if (!map) {
  495. ret = -EFAULT;
  496. sock_release(sock);
  497. goto out_error;
  498. }
  499. map->sockpass = mappass;
  500. iow = &map->ioworker;
  501. atomic_inc(&map->read);
  502. atomic_inc(&map->io);
  503. queue_work(iow->wq, &iow->register_work);
  504. out_error:
  505. rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
  506. rsp->req_id = req->req_id;
  507. rsp->cmd = req->cmd;
  508. rsp->u.accept.id = req->u.accept.id;
  509. rsp->ret = ret;
  510. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&fedata->ring, notify);
  511. if (notify)
  512. notify_remote_via_irq(fedata->irq);
  513. mappass->reqcopy.cmd = 0;
  514. }
  515. static void pvcalls_pass_sk_data_ready(struct sock *sock)
  516. {
  517. struct sockpass_mapping *mappass = sock->sk_user_data;
  518. struct pvcalls_fedata *fedata;
  519. struct xen_pvcalls_response *rsp;
  520. unsigned long flags;
  521. int notify;
  522. if (mappass == NULL)
  523. return;
  524. fedata = mappass->fedata;
  525. spin_lock_irqsave(&mappass->copy_lock, flags);
  526. if (mappass->reqcopy.cmd == PVCALLS_POLL) {
  527. rsp = RING_GET_RESPONSE(&fedata->ring,
  528. fedata->ring.rsp_prod_pvt++);
  529. rsp->req_id = mappass->reqcopy.req_id;
  530. rsp->u.poll.id = mappass->reqcopy.u.poll.id;
  531. rsp->cmd = mappass->reqcopy.cmd;
  532. rsp->ret = 0;
  533. mappass->reqcopy.cmd = 0;
  534. spin_unlock_irqrestore(&mappass->copy_lock, flags);
  535. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&fedata->ring, notify);
  536. if (notify)
  537. notify_remote_via_irq(mappass->fedata->irq);
  538. } else {
  539. spin_unlock_irqrestore(&mappass->copy_lock, flags);
  540. queue_work(mappass->wq, &mappass->register_work);
  541. }
  542. }
  543. static int pvcalls_back_bind(struct xenbus_device *dev,
  544. struct xen_pvcalls_request *req)
  545. {
  546. struct pvcalls_fedata *fedata;
  547. int ret;
  548. struct sockpass_mapping *map;
  549. struct xen_pvcalls_response *rsp;
  550. fedata = dev_get_drvdata(&dev->dev);
  551. map = kzalloc(sizeof(*map), GFP_KERNEL);
  552. if (map == NULL) {
  553. ret = -ENOMEM;
  554. goto out;
  555. }
  556. INIT_WORK(&map->register_work, __pvcalls_back_accept);
  557. spin_lock_init(&map->copy_lock);
  558. map->wq = alloc_workqueue("pvcalls_wq", WQ_UNBOUND, 1);
  559. if (!map->wq) {
  560. ret = -ENOMEM;
  561. goto out;
  562. }
  563. ret = sock_create(AF_INET, SOCK_STREAM, 0, &map->sock);
  564. if (ret < 0)
  565. goto out;
  566. ret = inet_bind(map->sock, (struct sockaddr *)&req->u.bind.addr,
  567. req->u.bind.len);
  568. if (ret < 0)
  569. goto out;
  570. map->fedata = fedata;
  571. map->id = req->u.bind.id;
  572. down(&fedata->socket_lock);
  573. ret = radix_tree_insert(&fedata->socketpass_mappings, map->id,
  574. map);
  575. up(&fedata->socket_lock);
  576. if (ret)
  577. goto out;
  578. write_lock_bh(&map->sock->sk->sk_callback_lock);
  579. map->saved_data_ready = map->sock->sk->sk_data_ready;
  580. map->sock->sk->sk_user_data = map;
  581. map->sock->sk->sk_data_ready = pvcalls_pass_sk_data_ready;
  582. write_unlock_bh(&map->sock->sk->sk_callback_lock);
  583. out:
  584. if (ret) {
  585. if (map && map->sock)
  586. sock_release(map->sock);
  587. if (map && map->wq)
  588. destroy_workqueue(map->wq);
  589. kfree(map);
  590. }
  591. rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
  592. rsp->req_id = req->req_id;
  593. rsp->cmd = req->cmd;
  594. rsp->u.bind.id = req->u.bind.id;
  595. rsp->ret = ret;
  596. return 0;
  597. }
  598. static int pvcalls_back_listen(struct xenbus_device *dev,
  599. struct xen_pvcalls_request *req)
  600. {
  601. struct pvcalls_fedata *fedata;
  602. int ret = -EINVAL;
  603. struct sockpass_mapping *map;
  604. struct xen_pvcalls_response *rsp;
  605. fedata = dev_get_drvdata(&dev->dev);
  606. down(&fedata->socket_lock);
  607. map = radix_tree_lookup(&fedata->socketpass_mappings, req->u.listen.id);
  608. up(&fedata->socket_lock);
  609. if (map == NULL)
  610. goto out;
  611. ret = inet_listen(map->sock, req->u.listen.backlog);
  612. out:
  613. rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
  614. rsp->req_id = req->req_id;
  615. rsp->cmd = req->cmd;
  616. rsp->u.listen.id = req->u.listen.id;
  617. rsp->ret = ret;
  618. return 0;
  619. }
  620. static int pvcalls_back_accept(struct xenbus_device *dev,
  621. struct xen_pvcalls_request *req)
  622. {
  623. struct pvcalls_fedata *fedata;
  624. struct sockpass_mapping *mappass;
  625. int ret = -EINVAL;
  626. struct xen_pvcalls_response *rsp;
  627. unsigned long flags;
  628. fedata = dev_get_drvdata(&dev->dev);
  629. down(&fedata->socket_lock);
  630. mappass = radix_tree_lookup(&fedata->socketpass_mappings,
  631. req->u.accept.id);
  632. up(&fedata->socket_lock);
  633. if (mappass == NULL)
  634. goto out_error;
  635. /*
  636. * Limitation of the current implementation: only support one
  637. * concurrent accept or poll call on one socket.
  638. */
  639. spin_lock_irqsave(&mappass->copy_lock, flags);
  640. if (mappass->reqcopy.cmd != 0) {
  641. spin_unlock_irqrestore(&mappass->copy_lock, flags);
  642. ret = -EINTR;
  643. goto out_error;
  644. }
  645. mappass->reqcopy = *req;
  646. spin_unlock_irqrestore(&mappass->copy_lock, flags);
  647. queue_work(mappass->wq, &mappass->register_work);
  648. /* Tell the caller we don't need to send back a notification yet */
  649. return -1;
  650. out_error:
  651. rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
  652. rsp->req_id = req->req_id;
  653. rsp->cmd = req->cmd;
  654. rsp->u.accept.id = req->u.accept.id;
  655. rsp->ret = ret;
  656. return 0;
  657. }
  658. static int pvcalls_back_poll(struct xenbus_device *dev,
  659. struct xen_pvcalls_request *req)
  660. {
  661. struct pvcalls_fedata *fedata;
  662. struct sockpass_mapping *mappass;
  663. struct xen_pvcalls_response *rsp;
  664. struct inet_connection_sock *icsk;
  665. struct request_sock_queue *queue;
  666. unsigned long flags;
  667. int ret;
  668. bool data;
  669. fedata = dev_get_drvdata(&dev->dev);
  670. down(&fedata->socket_lock);
  671. mappass = radix_tree_lookup(&fedata->socketpass_mappings,
  672. req->u.poll.id);
  673. up(&fedata->socket_lock);
  674. if (mappass == NULL)
  675. return -EINVAL;
  676. /*
  677. * Limitation of the current implementation: only support one
  678. * concurrent accept or poll call on one socket.
  679. */
  680. spin_lock_irqsave(&mappass->copy_lock, flags);
  681. if (mappass->reqcopy.cmd != 0) {
  682. ret = -EINTR;
  683. goto out;
  684. }
  685. mappass->reqcopy = *req;
  686. icsk = inet_csk(mappass->sock->sk);
  687. queue = &icsk->icsk_accept_queue;
  688. data = READ_ONCE(queue->rskq_accept_head) != NULL;
  689. if (data) {
  690. mappass->reqcopy.cmd = 0;
  691. ret = 0;
  692. goto out;
  693. }
  694. spin_unlock_irqrestore(&mappass->copy_lock, flags);
  695. /* Tell the caller we don't need to send back a notification yet */
  696. return -1;
  697. out:
  698. spin_unlock_irqrestore(&mappass->copy_lock, flags);
  699. rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
  700. rsp->req_id = req->req_id;
  701. rsp->cmd = req->cmd;
  702. rsp->u.poll.id = req->u.poll.id;
  703. rsp->ret = ret;
  704. return 0;
  705. }
  706. static int pvcalls_back_handle_cmd(struct xenbus_device *dev,
  707. struct xen_pvcalls_request *req)
  708. {
  709. int ret = 0;
  710. switch (req->cmd) {
  711. case PVCALLS_SOCKET:
  712. ret = pvcalls_back_socket(dev, req);
  713. break;
  714. case PVCALLS_CONNECT:
  715. ret = pvcalls_back_connect(dev, req);
  716. break;
  717. case PVCALLS_RELEASE:
  718. ret = pvcalls_back_release(dev, req);
  719. break;
  720. case PVCALLS_BIND:
  721. ret = pvcalls_back_bind(dev, req);
  722. break;
  723. case PVCALLS_LISTEN:
  724. ret = pvcalls_back_listen(dev, req);
  725. break;
  726. case PVCALLS_ACCEPT:
  727. ret = pvcalls_back_accept(dev, req);
  728. break;
  729. case PVCALLS_POLL:
  730. ret = pvcalls_back_poll(dev, req);
  731. break;
  732. default:
  733. {
  734. struct pvcalls_fedata *fedata;
  735. struct xen_pvcalls_response *rsp;
  736. fedata = dev_get_drvdata(&dev->dev);
  737. rsp = RING_GET_RESPONSE(
  738. &fedata->ring, fedata->ring.rsp_prod_pvt++);
  739. rsp->req_id = req->req_id;
  740. rsp->cmd = req->cmd;
  741. rsp->ret = -ENOTSUPP;
  742. break;
  743. }
  744. }
  745. return ret;
  746. }
  747. static void pvcalls_back_work(struct pvcalls_fedata *fedata)
  748. {
  749. int notify, notify_all = 0, more = 1;
  750. struct xen_pvcalls_request req;
  751. struct xenbus_device *dev = fedata->dev;
  752. while (more) {
  753. while (RING_HAS_UNCONSUMED_REQUESTS(&fedata->ring)) {
  754. RING_COPY_REQUEST(&fedata->ring,
  755. fedata->ring.req_cons++,
  756. &req);
  757. if (!pvcalls_back_handle_cmd(dev, &req)) {
  758. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(
  759. &fedata->ring, notify);
  760. notify_all += notify;
  761. }
  762. }
  763. if (notify_all) {
  764. notify_remote_via_irq(fedata->irq);
  765. notify_all = 0;
  766. }
  767. RING_FINAL_CHECK_FOR_REQUESTS(&fedata->ring, more);
  768. }
  769. }
  770. static irqreturn_t pvcalls_back_event(int irq, void *dev_id)
  771. {
  772. struct xenbus_device *dev = dev_id;
  773. struct pvcalls_fedata *fedata = NULL;
  774. unsigned int eoi_flags = XEN_EOI_FLAG_SPURIOUS;
  775. if (dev) {
  776. fedata = dev_get_drvdata(&dev->dev);
  777. if (fedata) {
  778. pvcalls_back_work(fedata);
  779. eoi_flags = 0;
  780. }
  781. }
  782. xen_irq_lateeoi(irq, eoi_flags);
  783. return IRQ_HANDLED;
  784. }
  785. static irqreturn_t pvcalls_back_conn_event(int irq, void *sock_map)
  786. {
  787. struct sock_mapping *map = sock_map;
  788. struct pvcalls_ioworker *iow;
  789. if (map == NULL || map->sock == NULL || map->sock->sk == NULL ||
  790. map->sock->sk->sk_user_data != map) {
  791. xen_irq_lateeoi(irq, 0);
  792. return IRQ_HANDLED;
  793. }
  794. iow = &map->ioworker;
  795. atomic_inc(&map->write);
  796. atomic_inc(&map->eoi);
  797. atomic_inc(&map->io);
  798. queue_work(iow->wq, &iow->register_work);
  799. return IRQ_HANDLED;
  800. }
  801. static int backend_connect(struct xenbus_device *dev)
  802. {
  803. int err, evtchn;
  804. grant_ref_t ring_ref;
  805. struct pvcalls_fedata *fedata = NULL;
  806. fedata = kzalloc(sizeof(struct pvcalls_fedata), GFP_KERNEL);
  807. if (!fedata)
  808. return -ENOMEM;
  809. fedata->irq = -1;
  810. err = xenbus_scanf(XBT_NIL, dev->otherend, "port", "%u",
  811. &evtchn);
  812. if (err != 1) {
  813. err = -EINVAL;
  814. xenbus_dev_fatal(dev, err, "reading %s/event-channel",
  815. dev->otherend);
  816. goto error;
  817. }
  818. err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-ref", "%u", &ring_ref);
  819. if (err != 1) {
  820. err = -EINVAL;
  821. xenbus_dev_fatal(dev, err, "reading %s/ring-ref",
  822. dev->otherend);
  823. goto error;
  824. }
  825. err = bind_interdomain_evtchn_to_irq_lateeoi(dev->otherend_id, evtchn);
  826. if (err < 0)
  827. goto error;
  828. fedata->irq = err;
  829. err = request_threaded_irq(fedata->irq, NULL, pvcalls_back_event,
  830. IRQF_ONESHOT, "pvcalls-back", dev);
  831. if (err < 0)
  832. goto error;
  833. err = xenbus_map_ring_valloc(dev, &ring_ref, 1,
  834. (void **)&fedata->sring);
  835. if (err < 0)
  836. goto error;
  837. BACK_RING_INIT(&fedata->ring, fedata->sring, XEN_PAGE_SIZE * 1);
  838. fedata->dev = dev;
  839. INIT_LIST_HEAD(&fedata->socket_mappings);
  840. INIT_RADIX_TREE(&fedata->socketpass_mappings, GFP_KERNEL);
  841. sema_init(&fedata->socket_lock, 1);
  842. dev_set_drvdata(&dev->dev, fedata);
  843. down(&pvcalls_back_global.frontends_lock);
  844. list_add_tail(&fedata->list, &pvcalls_back_global.frontends);
  845. up(&pvcalls_back_global.frontends_lock);
  846. return 0;
  847. error:
  848. if (fedata->irq >= 0)
  849. unbind_from_irqhandler(fedata->irq, dev);
  850. if (fedata->sring != NULL)
  851. xenbus_unmap_ring_vfree(dev, fedata->sring);
  852. kfree(fedata);
  853. return err;
  854. }
  855. static int backend_disconnect(struct xenbus_device *dev)
  856. {
  857. struct pvcalls_fedata *fedata;
  858. struct sock_mapping *map, *n;
  859. struct sockpass_mapping *mappass;
  860. struct radix_tree_iter iter;
  861. void **slot;
  862. fedata = dev_get_drvdata(&dev->dev);
  863. down(&fedata->socket_lock);
  864. list_for_each_entry_safe(map, n, &fedata->socket_mappings, list) {
  865. list_del(&map->list);
  866. pvcalls_back_release_active(dev, fedata, map);
  867. }
  868. radix_tree_for_each_slot(slot, &fedata->socketpass_mappings, &iter, 0) {
  869. mappass = radix_tree_deref_slot(slot);
  870. if (!mappass)
  871. continue;
  872. if (radix_tree_exception(mappass)) {
  873. if (radix_tree_deref_retry(mappass))
  874. slot = radix_tree_iter_retry(&iter);
  875. } else {
  876. radix_tree_delete(&fedata->socketpass_mappings,
  877. mappass->id);
  878. pvcalls_back_release_passive(dev, fedata, mappass);
  879. }
  880. }
  881. up(&fedata->socket_lock);
  882. unbind_from_irqhandler(fedata->irq, dev);
  883. xenbus_unmap_ring_vfree(dev, fedata->sring);
  884. list_del(&fedata->list);
  885. kfree(fedata);
  886. dev_set_drvdata(&dev->dev, NULL);
  887. return 0;
  888. }
  889. static int pvcalls_back_probe(struct xenbus_device *dev,
  890. const struct xenbus_device_id *id)
  891. {
  892. int err, abort;
  893. struct xenbus_transaction xbt;
  894. again:
  895. abort = 1;
  896. err = xenbus_transaction_start(&xbt);
  897. if (err) {
  898. pr_warn("%s cannot create xenstore transaction\n", __func__);
  899. return err;
  900. }
  901. err = xenbus_printf(xbt, dev->nodename, "versions", "%s",
  902. PVCALLS_VERSIONS);
  903. if (err) {
  904. pr_warn("%s write out 'versions' failed\n", __func__);
  905. goto abort;
  906. }
  907. err = xenbus_printf(xbt, dev->nodename, "max-page-order", "%u",
  908. MAX_RING_ORDER);
  909. if (err) {
  910. pr_warn("%s write out 'max-page-order' failed\n", __func__);
  911. goto abort;
  912. }
  913. err = xenbus_printf(xbt, dev->nodename, "function-calls",
  914. XENBUS_FUNCTIONS_CALLS);
  915. if (err) {
  916. pr_warn("%s write out 'function-calls' failed\n", __func__);
  917. goto abort;
  918. }
  919. abort = 0;
  920. abort:
  921. err = xenbus_transaction_end(xbt, abort);
  922. if (err) {
  923. if (err == -EAGAIN && !abort)
  924. goto again;
  925. pr_warn("%s cannot complete xenstore transaction\n", __func__);
  926. return err;
  927. }
  928. if (abort)
  929. return -EFAULT;
  930. xenbus_switch_state(dev, XenbusStateInitWait);
  931. return 0;
  932. }
  933. static void set_backend_state(struct xenbus_device *dev,
  934. enum xenbus_state state)
  935. {
  936. while (dev->state != state) {
  937. switch (dev->state) {
  938. case XenbusStateClosed:
  939. switch (state) {
  940. case XenbusStateInitWait:
  941. case XenbusStateConnected:
  942. xenbus_switch_state(dev, XenbusStateInitWait);
  943. break;
  944. case XenbusStateClosing:
  945. xenbus_switch_state(dev, XenbusStateClosing);
  946. break;
  947. default:
  948. WARN_ON(1);
  949. }
  950. break;
  951. case XenbusStateInitWait:
  952. case XenbusStateInitialised:
  953. switch (state) {
  954. case XenbusStateConnected:
  955. if (backend_connect(dev))
  956. return;
  957. xenbus_switch_state(dev, XenbusStateConnected);
  958. break;
  959. case XenbusStateClosing:
  960. case XenbusStateClosed:
  961. xenbus_switch_state(dev, XenbusStateClosing);
  962. break;
  963. default:
  964. WARN_ON(1);
  965. }
  966. break;
  967. case XenbusStateConnected:
  968. switch (state) {
  969. case XenbusStateInitWait:
  970. case XenbusStateClosing:
  971. case XenbusStateClosed:
  972. down(&pvcalls_back_global.frontends_lock);
  973. backend_disconnect(dev);
  974. up(&pvcalls_back_global.frontends_lock);
  975. xenbus_switch_state(dev, XenbusStateClosing);
  976. break;
  977. default:
  978. WARN_ON(1);
  979. }
  980. break;
  981. case XenbusStateClosing:
  982. switch (state) {
  983. case XenbusStateInitWait:
  984. case XenbusStateConnected:
  985. case XenbusStateClosed:
  986. xenbus_switch_state(dev, XenbusStateClosed);
  987. break;
  988. default:
  989. WARN_ON(1);
  990. }
  991. break;
  992. default:
  993. WARN_ON(1);
  994. }
  995. }
  996. }
  997. static void pvcalls_back_changed(struct xenbus_device *dev,
  998. enum xenbus_state frontend_state)
  999. {
  1000. switch (frontend_state) {
  1001. case XenbusStateInitialising:
  1002. set_backend_state(dev, XenbusStateInitWait);
  1003. break;
  1004. case XenbusStateInitialised:
  1005. case XenbusStateConnected:
  1006. set_backend_state(dev, XenbusStateConnected);
  1007. break;
  1008. case XenbusStateClosing:
  1009. set_backend_state(dev, XenbusStateClosing);
  1010. break;
  1011. case XenbusStateClosed:
  1012. set_backend_state(dev, XenbusStateClosed);
  1013. if (xenbus_dev_is_online(dev))
  1014. break;
  1015. device_unregister(&dev->dev);
  1016. break;
  1017. case XenbusStateUnknown:
  1018. set_backend_state(dev, XenbusStateClosed);
  1019. device_unregister(&dev->dev);
  1020. break;
  1021. default:
  1022. xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
  1023. frontend_state);
  1024. break;
  1025. }
  1026. }
  1027. static int pvcalls_back_remove(struct xenbus_device *dev)
  1028. {
  1029. return 0;
  1030. }
  1031. static int pvcalls_back_uevent(struct xenbus_device *xdev,
  1032. struct kobj_uevent_env *env)
  1033. {
  1034. return 0;
  1035. }
  1036. static const struct xenbus_device_id pvcalls_back_ids[] = {
  1037. { "pvcalls" },
  1038. { "" }
  1039. };
  1040. static struct xenbus_driver pvcalls_back_driver = {
  1041. .ids = pvcalls_back_ids,
  1042. .probe = pvcalls_back_probe,
  1043. .remove = pvcalls_back_remove,
  1044. .uevent = pvcalls_back_uevent,
  1045. .otherend_changed = pvcalls_back_changed,
  1046. };
  1047. static int __init pvcalls_back_init(void)
  1048. {
  1049. int ret;
  1050. if (!xen_domain())
  1051. return -ENODEV;
  1052. ret = xenbus_register_backend(&pvcalls_back_driver);
  1053. if (ret < 0)
  1054. return ret;
  1055. sema_init(&pvcalls_back_global.frontends_lock, 1);
  1056. INIT_LIST_HEAD(&pvcalls_back_global.frontends);
  1057. return 0;
  1058. }
  1059. module_init(pvcalls_back_init);
  1060. static void __exit pvcalls_back_fin(void)
  1061. {
  1062. struct pvcalls_fedata *fedata, *nfedata;
  1063. down(&pvcalls_back_global.frontends_lock);
  1064. list_for_each_entry_safe(fedata, nfedata,
  1065. &pvcalls_back_global.frontends, list) {
  1066. backend_disconnect(fedata->dev);
  1067. }
  1068. up(&pvcalls_back_global.frontends_lock);
  1069. xenbus_unregister_driver(&pvcalls_back_driver);
  1070. }
  1071. module_exit(pvcalls_back_fin);
  1072. MODULE_DESCRIPTION("Xen PV Calls backend driver");
  1073. MODULE_AUTHOR("Stefano Stabellini <sstabellini@kernel.org>");
  1074. MODULE_LICENSE("GPL");