vhost.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. /* Copyright (C) 2009 Red Hat, Inc.
  2. * Copyright (C) 2006 Rusty Russell IBM Corporation
  3. *
  4. * Author: Michael S. Tsirkin <mst@redhat.com>
  5. *
  6. * Inspiration, some code, and most witty comments come from
  7. * Documentation/lguest/lguest.c, by Rusty Russell
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2.
  10. *
  11. * Generic code for virtio server in host kernel.
  12. */
  13. #include <linux/eventfd.h>
  14. #include <linux/vhost.h>
  15. #include <linux/virtio_net.h>
  16. #include <linux/mm.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/mutex.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/poll.h>
  21. #include <linux/file.h>
  22. #include <linux/highmem.h>
  23. #include <linux/slab.h>
  24. #include <linux/kthread.h>
  25. #include <linux/cgroup.h>
  26. #include <linux/net.h>
  27. #include <linux/if_packet.h>
  28. #include <linux/if_arp.h>
  29. #include <net/sock.h>
  30. #include "vhost.h"
  31. enum {
  32. VHOST_MEMORY_MAX_NREGIONS = 64,
  33. VHOST_MEMORY_F_LOG = 0x1,
  34. };
  35. static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
  36. poll_table *pt)
  37. {
  38. struct vhost_poll *poll;
  39. poll = container_of(pt, struct vhost_poll, table);
  40. poll->wqh = wqh;
  41. add_wait_queue(wqh, &poll->wait);
  42. }
  43. static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
  44. void *key)
  45. {
  46. struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
  47. if (!((unsigned long)key & poll->mask))
  48. return 0;
  49. vhost_poll_queue(poll);
  50. return 0;
  51. }
  52. /* Init poll structure */
  53. void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
  54. unsigned long mask, struct vhost_dev *dev)
  55. {
  56. struct vhost_work *work = &poll->work;
  57. init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
  58. init_poll_funcptr(&poll->table, vhost_poll_func);
  59. poll->mask = mask;
  60. poll->dev = dev;
  61. INIT_LIST_HEAD(&work->node);
  62. work->fn = fn;
  63. init_waitqueue_head(&work->done);
  64. work->flushing = 0;
  65. work->queue_seq = work->done_seq = 0;
  66. }
  67. /* Start polling a file. We add ourselves to file's wait queue. The caller must
  68. * keep a reference to a file until after vhost_poll_stop is called. */
  69. void vhost_poll_start(struct vhost_poll *poll, struct file *file)
  70. {
  71. unsigned long mask;
  72. mask = file->f_op->poll(file, &poll->table);
  73. if (mask)
  74. vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
  75. }
  76. /* Stop polling a file. After this function returns, it becomes safe to drop the
  77. * file reference. You must also flush afterwards. */
  78. void vhost_poll_stop(struct vhost_poll *poll)
  79. {
  80. remove_wait_queue(poll->wqh, &poll->wait);
  81. }
  82. /* Flush any work that has been scheduled. When calling this, don't hold any
  83. * locks that are also used by the callback. */
  84. void vhost_poll_flush(struct vhost_poll *poll)
  85. {
  86. struct vhost_work *work = &poll->work;
  87. unsigned seq;
  88. int left;
  89. int flushing;
  90. spin_lock_irq(&poll->dev->work_lock);
  91. seq = work->queue_seq;
  92. work->flushing++;
  93. spin_unlock_irq(&poll->dev->work_lock);
  94. wait_event(work->done, ({
  95. spin_lock_irq(&poll->dev->work_lock);
  96. left = seq - work->done_seq <= 0;
  97. spin_unlock_irq(&poll->dev->work_lock);
  98. left;
  99. }));
  100. spin_lock_irq(&poll->dev->work_lock);
  101. flushing = --work->flushing;
  102. spin_unlock_irq(&poll->dev->work_lock);
  103. BUG_ON(flushing < 0);
  104. }
  105. void vhost_poll_queue(struct vhost_poll *poll)
  106. {
  107. struct vhost_dev *dev = poll->dev;
  108. struct vhost_work *work = &poll->work;
  109. unsigned long flags;
  110. spin_lock_irqsave(&dev->work_lock, flags);
  111. if (list_empty(&work->node)) {
  112. list_add_tail(&work->node, &dev->work_list);
  113. work->queue_seq++;
  114. wake_up_process(dev->worker);
  115. }
  116. spin_unlock_irqrestore(&dev->work_lock, flags);
  117. }
  118. static void vhost_vq_reset(struct vhost_dev *dev,
  119. struct vhost_virtqueue *vq)
  120. {
  121. vq->num = 1;
  122. vq->desc = NULL;
  123. vq->avail = NULL;
  124. vq->used = NULL;
  125. vq->last_avail_idx = 0;
  126. vq->avail_idx = 0;
  127. vq->last_used_idx = 0;
  128. vq->used_flags = 0;
  129. vq->used_flags = 0;
  130. vq->log_used = false;
  131. vq->log_addr = -1ull;
  132. vq->hdr_size = 0;
  133. vq->private_data = NULL;
  134. vq->log_base = NULL;
  135. vq->error_ctx = NULL;
  136. vq->error = NULL;
  137. vq->kick = NULL;
  138. vq->call_ctx = NULL;
  139. vq->call = NULL;
  140. vq->log_ctx = NULL;
  141. }
  142. static int vhost_worker(void *data)
  143. {
  144. struct vhost_dev *dev = data;
  145. struct vhost_work *work = NULL;
  146. unsigned uninitialized_var(seq);
  147. for (;;) {
  148. /* mb paired w/ kthread_stop */
  149. set_current_state(TASK_INTERRUPTIBLE);
  150. spin_lock_irq(&dev->work_lock);
  151. if (work) {
  152. work->done_seq = seq;
  153. if (work->flushing)
  154. wake_up_all(&work->done);
  155. }
  156. if (kthread_should_stop()) {
  157. spin_unlock_irq(&dev->work_lock);
  158. __set_current_state(TASK_RUNNING);
  159. return 0;
  160. }
  161. if (!list_empty(&dev->work_list)) {
  162. work = list_first_entry(&dev->work_list,
  163. struct vhost_work, node);
  164. list_del_init(&work->node);
  165. seq = work->queue_seq;
  166. } else
  167. work = NULL;
  168. spin_unlock_irq(&dev->work_lock);
  169. if (work) {
  170. __set_current_state(TASK_RUNNING);
  171. work->fn(work);
  172. } else
  173. schedule();
  174. }
  175. }
  176. long vhost_dev_init(struct vhost_dev *dev,
  177. struct vhost_virtqueue *vqs, int nvqs)
  178. {
  179. int i;
  180. dev->vqs = vqs;
  181. dev->nvqs = nvqs;
  182. mutex_init(&dev->mutex);
  183. dev->log_ctx = NULL;
  184. dev->log_file = NULL;
  185. dev->memory = NULL;
  186. dev->mm = NULL;
  187. spin_lock_init(&dev->work_lock);
  188. INIT_LIST_HEAD(&dev->work_list);
  189. dev->worker = NULL;
  190. for (i = 0; i < dev->nvqs; ++i) {
  191. dev->vqs[i].dev = dev;
  192. mutex_init(&dev->vqs[i].mutex);
  193. vhost_vq_reset(dev, dev->vqs + i);
  194. if (dev->vqs[i].handle_kick)
  195. vhost_poll_init(&dev->vqs[i].poll,
  196. dev->vqs[i].handle_kick, POLLIN, dev);
  197. }
  198. return 0;
  199. }
  200. /* Caller should have device mutex */
  201. long vhost_dev_check_owner(struct vhost_dev *dev)
  202. {
  203. /* Are you the owner? If not, I don't think you mean to do that */
  204. return dev->mm == current->mm ? 0 : -EPERM;
  205. }
  206. /* Caller should have device mutex */
  207. static long vhost_dev_set_owner(struct vhost_dev *dev)
  208. {
  209. struct task_struct *worker;
  210. int err;
  211. /* Is there an owner already? */
  212. if (dev->mm) {
  213. err = -EBUSY;
  214. goto err_mm;
  215. }
  216. /* No owner, become one */
  217. dev->mm = get_task_mm(current);
  218. worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
  219. if (IS_ERR(worker)) {
  220. err = PTR_ERR(worker);
  221. goto err_worker;
  222. }
  223. dev->worker = worker;
  224. err = cgroup_attach_task_current_cg(worker);
  225. if (err)
  226. goto err_cgroup;
  227. wake_up_process(worker); /* avoid contributing to loadavg */
  228. return 0;
  229. err_cgroup:
  230. kthread_stop(worker);
  231. err_worker:
  232. if (dev->mm)
  233. mmput(dev->mm);
  234. dev->mm = NULL;
  235. err_mm:
  236. return err;
  237. }
  238. /* Caller should have device mutex */
  239. long vhost_dev_reset_owner(struct vhost_dev *dev)
  240. {
  241. struct vhost_memory *memory;
  242. /* Restore memory to default empty mapping. */
  243. memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
  244. if (!memory)
  245. return -ENOMEM;
  246. vhost_dev_cleanup(dev);
  247. memory->nregions = 0;
  248. dev->memory = memory;
  249. return 0;
  250. }
  251. /* Caller should have device mutex */
  252. void vhost_dev_cleanup(struct vhost_dev *dev)
  253. {
  254. int i;
  255. for (i = 0; i < dev->nvqs; ++i) {
  256. if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
  257. vhost_poll_stop(&dev->vqs[i].poll);
  258. vhost_poll_flush(&dev->vqs[i].poll);
  259. }
  260. if (dev->vqs[i].error_ctx)
  261. eventfd_ctx_put(dev->vqs[i].error_ctx);
  262. if (dev->vqs[i].error)
  263. fput(dev->vqs[i].error);
  264. if (dev->vqs[i].kick)
  265. fput(dev->vqs[i].kick);
  266. if (dev->vqs[i].call_ctx)
  267. eventfd_ctx_put(dev->vqs[i].call_ctx);
  268. if (dev->vqs[i].call)
  269. fput(dev->vqs[i].call);
  270. vhost_vq_reset(dev, dev->vqs + i);
  271. }
  272. if (dev->log_ctx)
  273. eventfd_ctx_put(dev->log_ctx);
  274. dev->log_ctx = NULL;
  275. if (dev->log_file)
  276. fput(dev->log_file);
  277. dev->log_file = NULL;
  278. /* No one will access memory at this point */
  279. kfree(dev->memory);
  280. dev->memory = NULL;
  281. if (dev->mm)
  282. mmput(dev->mm);
  283. dev->mm = NULL;
  284. WARN_ON(!list_empty(&dev->work_list));
  285. kthread_stop(dev->worker);
  286. }
  287. static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
  288. {
  289. u64 a = addr / VHOST_PAGE_SIZE / 8;
  290. /* Make sure 64 bit math will not overflow. */
  291. if (a > ULONG_MAX - (unsigned long)log_base ||
  292. a + (unsigned long)log_base > ULONG_MAX)
  293. return -EFAULT;
  294. return access_ok(VERIFY_WRITE, log_base + a,
  295. (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
  296. }
  297. /* Caller should have vq mutex and device mutex. */
  298. static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
  299. int log_all)
  300. {
  301. int i;
  302. if (!mem)
  303. return 0;
  304. for (i = 0; i < mem->nregions; ++i) {
  305. struct vhost_memory_region *m = mem->regions + i;
  306. unsigned long a = m->userspace_addr;
  307. if (m->memory_size > ULONG_MAX)
  308. return 0;
  309. else if (!access_ok(VERIFY_WRITE, (void __user *)a,
  310. m->memory_size))
  311. return 0;
  312. else if (log_all && !log_access_ok(log_base,
  313. m->guest_phys_addr,
  314. m->memory_size))
  315. return 0;
  316. }
  317. return 1;
  318. }
  319. /* Can we switch to this memory table? */
  320. /* Caller should have device mutex but not vq mutex */
  321. static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
  322. int log_all)
  323. {
  324. int i;
  325. for (i = 0; i < d->nvqs; ++i) {
  326. int ok;
  327. mutex_lock(&d->vqs[i].mutex);
  328. /* If ring is inactive, will check when it's enabled. */
  329. if (d->vqs[i].private_data)
  330. ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
  331. log_all);
  332. else
  333. ok = 1;
  334. mutex_unlock(&d->vqs[i].mutex);
  335. if (!ok)
  336. return 0;
  337. }
  338. return 1;
  339. }
  340. static int vq_access_ok(unsigned int num,
  341. struct vring_desc __user *desc,
  342. struct vring_avail __user *avail,
  343. struct vring_used __user *used)
  344. {
  345. return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
  346. access_ok(VERIFY_READ, avail,
  347. sizeof *avail + num * sizeof *avail->ring) &&
  348. access_ok(VERIFY_WRITE, used,
  349. sizeof *used + num * sizeof *used->ring);
  350. }
  351. /* Can we log writes? */
  352. /* Caller should have device mutex but not vq mutex */
  353. int vhost_log_access_ok(struct vhost_dev *dev)
  354. {
  355. return memory_access_ok(dev, dev->memory, 1);
  356. }
  357. /* Verify access for write logging. */
  358. /* Caller should have vq mutex and device mutex */
  359. static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
  360. {
  361. return vq_memory_access_ok(log_base, vq->dev->memory,
  362. vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
  363. (!vq->log_used || log_access_ok(log_base, vq->log_addr,
  364. sizeof *vq->used +
  365. vq->num * sizeof *vq->used->ring));
  366. }
  367. /* Can we start vq? */
  368. /* Caller should have vq mutex and device mutex */
  369. int vhost_vq_access_ok(struct vhost_virtqueue *vq)
  370. {
  371. return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) &&
  372. vq_log_access_ok(vq, vq->log_base);
  373. }
  374. static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
  375. {
  376. struct vhost_memory mem, *newmem, *oldmem;
  377. unsigned long size = offsetof(struct vhost_memory, regions);
  378. if (copy_from_user(&mem, m, size))
  379. return -EFAULT;
  380. if (mem.padding)
  381. return -EOPNOTSUPP;
  382. if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
  383. return -E2BIG;
  384. newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
  385. if (!newmem)
  386. return -ENOMEM;
  387. memcpy(newmem, &mem, size);
  388. if (copy_from_user(newmem->regions, m->regions,
  389. mem.nregions * sizeof *m->regions)) {
  390. kfree(newmem);
  391. return -EFAULT;
  392. }
  393. if (!memory_access_ok(d, newmem, vhost_has_feature(d, VHOST_F_LOG_ALL))) {
  394. kfree(newmem);
  395. return -EFAULT;
  396. }
  397. oldmem = d->memory;
  398. rcu_assign_pointer(d->memory, newmem);
  399. synchronize_rcu();
  400. kfree(oldmem);
  401. return 0;
  402. }
  403. static int init_used(struct vhost_virtqueue *vq,
  404. struct vring_used __user *used)
  405. {
  406. int r = put_user(vq->used_flags, &used->flags);
  407. if (r)
  408. return r;
  409. return get_user(vq->last_used_idx, &used->idx);
  410. }
  411. static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
  412. {
  413. struct file *eventfp, *filep = NULL,
  414. *pollstart = NULL, *pollstop = NULL;
  415. struct eventfd_ctx *ctx = NULL;
  416. u32 __user *idxp = argp;
  417. struct vhost_virtqueue *vq;
  418. struct vhost_vring_state s;
  419. struct vhost_vring_file f;
  420. struct vhost_vring_addr a;
  421. u32 idx;
  422. long r;
  423. r = get_user(idx, idxp);
  424. if (r < 0)
  425. return r;
  426. if (idx >= d->nvqs)
  427. return -ENOBUFS;
  428. vq = d->vqs + idx;
  429. mutex_lock(&vq->mutex);
  430. switch (ioctl) {
  431. case VHOST_SET_VRING_NUM:
  432. /* Resizing ring with an active backend?
  433. * You don't want to do that. */
  434. if (vq->private_data) {
  435. r = -EBUSY;
  436. break;
  437. }
  438. if (copy_from_user(&s, argp, sizeof s)) {
  439. r = -EFAULT;
  440. break;
  441. }
  442. if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
  443. r = -EINVAL;
  444. break;
  445. }
  446. vq->num = s.num;
  447. break;
  448. case VHOST_SET_VRING_BASE:
  449. /* Moving base with an active backend?
  450. * You don't want to do that. */
  451. if (vq->private_data) {
  452. r = -EBUSY;
  453. break;
  454. }
  455. if (copy_from_user(&s, argp, sizeof s)) {
  456. r = -EFAULT;
  457. break;
  458. }
  459. if (s.num > 0xffff) {
  460. r = -EINVAL;
  461. break;
  462. }
  463. vq->last_avail_idx = s.num;
  464. /* Forget the cached index value. */
  465. vq->avail_idx = vq->last_avail_idx;
  466. break;
  467. case VHOST_GET_VRING_BASE:
  468. s.index = idx;
  469. s.num = vq->last_avail_idx;
  470. if (copy_to_user(argp, &s, sizeof s))
  471. r = -EFAULT;
  472. break;
  473. case VHOST_SET_VRING_ADDR:
  474. if (copy_from_user(&a, argp, sizeof a)) {
  475. r = -EFAULT;
  476. break;
  477. }
  478. if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
  479. r = -EOPNOTSUPP;
  480. break;
  481. }
  482. /* For 32bit, verify that the top 32bits of the user
  483. data are set to zero. */
  484. if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
  485. (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
  486. (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
  487. r = -EFAULT;
  488. break;
  489. }
  490. if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
  491. (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
  492. (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
  493. r = -EINVAL;
  494. break;
  495. }
  496. /* We only verify access here if backend is configured.
  497. * If it is not, we don't as size might not have been setup.
  498. * We will verify when backend is configured. */
  499. if (vq->private_data) {
  500. if (!vq_access_ok(vq->num,
  501. (void __user *)(unsigned long)a.desc_user_addr,
  502. (void __user *)(unsigned long)a.avail_user_addr,
  503. (void __user *)(unsigned long)a.used_user_addr)) {
  504. r = -EINVAL;
  505. break;
  506. }
  507. /* Also validate log access for used ring if enabled. */
  508. if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
  509. !log_access_ok(vq->log_base, a.log_guest_addr,
  510. sizeof *vq->used +
  511. vq->num * sizeof *vq->used->ring)) {
  512. r = -EINVAL;
  513. break;
  514. }
  515. }
  516. r = init_used(vq, (struct vring_used __user *)(unsigned long)
  517. a.used_user_addr);
  518. if (r)
  519. break;
  520. vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
  521. vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
  522. vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
  523. vq->log_addr = a.log_guest_addr;
  524. vq->used = (void __user *)(unsigned long)a.used_user_addr;
  525. break;
  526. case VHOST_SET_VRING_KICK:
  527. if (copy_from_user(&f, argp, sizeof f)) {
  528. r = -EFAULT;
  529. break;
  530. }
  531. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  532. if (IS_ERR(eventfp)) {
  533. r = PTR_ERR(eventfp);
  534. break;
  535. }
  536. if (eventfp != vq->kick) {
  537. pollstop = filep = vq->kick;
  538. pollstart = vq->kick = eventfp;
  539. } else
  540. filep = eventfp;
  541. break;
  542. case VHOST_SET_VRING_CALL:
  543. if (copy_from_user(&f, argp, sizeof f)) {
  544. r = -EFAULT;
  545. break;
  546. }
  547. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  548. if (IS_ERR(eventfp)) {
  549. r = PTR_ERR(eventfp);
  550. break;
  551. }
  552. if (eventfp != vq->call) {
  553. filep = vq->call;
  554. ctx = vq->call_ctx;
  555. vq->call = eventfp;
  556. vq->call_ctx = eventfp ?
  557. eventfd_ctx_fileget(eventfp) : NULL;
  558. } else
  559. filep = eventfp;
  560. break;
  561. case VHOST_SET_VRING_ERR:
  562. if (copy_from_user(&f, argp, sizeof f)) {
  563. r = -EFAULT;
  564. break;
  565. }
  566. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  567. if (IS_ERR(eventfp)) {
  568. r = PTR_ERR(eventfp);
  569. break;
  570. }
  571. if (eventfp != vq->error) {
  572. filep = vq->error;
  573. vq->error = eventfp;
  574. ctx = vq->error_ctx;
  575. vq->error_ctx = eventfp ?
  576. eventfd_ctx_fileget(eventfp) : NULL;
  577. } else
  578. filep = eventfp;
  579. break;
  580. default:
  581. r = -ENOIOCTLCMD;
  582. }
  583. if (pollstop && vq->handle_kick)
  584. vhost_poll_stop(&vq->poll);
  585. if (ctx)
  586. eventfd_ctx_put(ctx);
  587. if (filep)
  588. fput(filep);
  589. if (pollstart && vq->handle_kick)
  590. vhost_poll_start(&vq->poll, vq->kick);
  591. mutex_unlock(&vq->mutex);
  592. if (pollstop && vq->handle_kick)
  593. vhost_poll_flush(&vq->poll);
  594. return r;
  595. }
  596. /* Caller must have device mutex */
  597. long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
  598. {
  599. void __user *argp = (void __user *)arg;
  600. struct file *eventfp, *filep = NULL;
  601. struct eventfd_ctx *ctx = NULL;
  602. u64 p;
  603. long r;
  604. int i, fd;
  605. /* If you are not the owner, you can become one */
  606. if (ioctl == VHOST_SET_OWNER) {
  607. r = vhost_dev_set_owner(d);
  608. goto done;
  609. }
  610. /* You must be the owner to do anything else */
  611. r = vhost_dev_check_owner(d);
  612. if (r)
  613. goto done;
  614. switch (ioctl) {
  615. case VHOST_SET_MEM_TABLE:
  616. r = vhost_set_memory(d, argp);
  617. break;
  618. case VHOST_SET_LOG_BASE:
  619. if (copy_from_user(&p, argp, sizeof p)) {
  620. r = -EFAULT;
  621. break;
  622. }
  623. if ((u64)(unsigned long)p != p) {
  624. r = -EFAULT;
  625. break;
  626. }
  627. for (i = 0; i < d->nvqs; ++i) {
  628. struct vhost_virtqueue *vq;
  629. void __user *base = (void __user *)(unsigned long)p;
  630. vq = d->vqs + i;
  631. mutex_lock(&vq->mutex);
  632. /* If ring is inactive, will check when it's enabled. */
  633. if (vq->private_data && !vq_log_access_ok(vq, base))
  634. r = -EFAULT;
  635. else
  636. vq->log_base = base;
  637. mutex_unlock(&vq->mutex);
  638. }
  639. break;
  640. case VHOST_SET_LOG_FD:
  641. r = get_user(fd, (int __user *)argp);
  642. if (r < 0)
  643. break;
  644. eventfp = fd == -1 ? NULL : eventfd_fget(fd);
  645. if (IS_ERR(eventfp)) {
  646. r = PTR_ERR(eventfp);
  647. break;
  648. }
  649. if (eventfp != d->log_file) {
  650. filep = d->log_file;
  651. ctx = d->log_ctx;
  652. d->log_ctx = eventfp ?
  653. eventfd_ctx_fileget(eventfp) : NULL;
  654. } else
  655. filep = eventfp;
  656. for (i = 0; i < d->nvqs; ++i) {
  657. mutex_lock(&d->vqs[i].mutex);
  658. d->vqs[i].log_ctx = d->log_ctx;
  659. mutex_unlock(&d->vqs[i].mutex);
  660. }
  661. if (ctx)
  662. eventfd_ctx_put(ctx);
  663. if (filep)
  664. fput(filep);
  665. break;
  666. default:
  667. r = vhost_set_vring(d, ioctl, argp);
  668. break;
  669. }
  670. done:
  671. return r;
  672. }
  673. static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
  674. __u64 addr, __u32 len)
  675. {
  676. struct vhost_memory_region *reg;
  677. int i;
  678. /* linear search is not brilliant, but we really have on the order of 6
  679. * regions in practice */
  680. for (i = 0; i < mem->nregions; ++i) {
  681. reg = mem->regions + i;
  682. if (reg->guest_phys_addr <= addr &&
  683. reg->guest_phys_addr + reg->memory_size - 1 >= addr)
  684. return reg;
  685. }
  686. return NULL;
  687. }
  688. /* TODO: This is really inefficient. We need something like get_user()
  689. * (instruction directly accesses the data, with an exception table entry
  690. * returning -EFAULT). See Documentation/x86/exception-tables.txt.
  691. */
  692. static int set_bit_to_user(int nr, void __user *addr)
  693. {
  694. unsigned long log = (unsigned long)addr;
  695. struct page *page;
  696. void *base;
  697. int bit = nr + (log % PAGE_SIZE) * 8;
  698. int r;
  699. r = get_user_pages_fast(log, 1, 1, &page);
  700. if (r < 0)
  701. return r;
  702. BUG_ON(r != 1);
  703. base = kmap_atomic(page, KM_USER0);
  704. set_bit(bit, base);
  705. kunmap_atomic(base, KM_USER0);
  706. set_page_dirty_lock(page);
  707. put_page(page);
  708. return 0;
  709. }
  710. static int log_write(void __user *log_base,
  711. u64 write_address, u64 write_length)
  712. {
  713. int r;
  714. if (!write_length)
  715. return 0;
  716. write_address /= VHOST_PAGE_SIZE;
  717. for (;;) {
  718. u64 base = (u64)(unsigned long)log_base;
  719. u64 log = base + write_address / 8;
  720. int bit = write_address % 8;
  721. if ((u64)(unsigned long)log != log)
  722. return -EFAULT;
  723. r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
  724. if (r < 0)
  725. return r;
  726. if (write_length <= VHOST_PAGE_SIZE)
  727. break;
  728. write_length -= VHOST_PAGE_SIZE;
  729. write_address += VHOST_PAGE_SIZE;
  730. }
  731. return r;
  732. }
  733. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  734. unsigned int log_num, u64 len)
  735. {
  736. int i, r;
  737. /* Make sure data written is seen before log. */
  738. smp_wmb();
  739. for (i = 0; i < log_num; ++i) {
  740. u64 l = min(log[i].len, len);
  741. r = log_write(vq->log_base, log[i].addr, l);
  742. if (r < 0)
  743. return r;
  744. len -= l;
  745. if (!len)
  746. return 0;
  747. }
  748. if (vq->log_ctx)
  749. eventfd_signal(vq->log_ctx, 1);
  750. /* Length written exceeds what we have stored. This is a bug. */
  751. BUG();
  752. return 0;
  753. }
  754. static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
  755. struct iovec iov[], int iov_size)
  756. {
  757. const struct vhost_memory_region *reg;
  758. struct vhost_memory *mem;
  759. struct iovec *_iov;
  760. u64 s = 0;
  761. int ret = 0;
  762. rcu_read_lock();
  763. mem = rcu_dereference(dev->memory);
  764. while ((u64)len > s) {
  765. u64 size;
  766. if (unlikely(ret >= iov_size)) {
  767. ret = -ENOBUFS;
  768. break;
  769. }
  770. reg = find_region(mem, addr, len);
  771. if (unlikely(!reg)) {
  772. ret = -EFAULT;
  773. break;
  774. }
  775. _iov = iov + ret;
  776. size = reg->memory_size - addr + reg->guest_phys_addr;
  777. _iov->iov_len = min((u64)len, size);
  778. _iov->iov_base = (void __user *)(unsigned long)
  779. (reg->userspace_addr + addr - reg->guest_phys_addr);
  780. s += size;
  781. addr += size;
  782. ++ret;
  783. }
  784. rcu_read_unlock();
  785. return ret;
  786. }
  787. /* Each buffer in the virtqueues is actually a chain of descriptors. This
  788. * function returns the next descriptor in the chain,
  789. * or -1U if we're at the end. */
  790. static unsigned next_desc(struct vring_desc *desc)
  791. {
  792. unsigned int next;
  793. /* If this descriptor says it doesn't chain, we're done. */
  794. if (!(desc->flags & VRING_DESC_F_NEXT))
  795. return -1U;
  796. /* Check they're not leading us off end of descriptors. */
  797. next = desc->next;
  798. /* Make sure compiler knows to grab that: we don't want it changing! */
  799. /* We will use the result as an index in an array, so most
  800. * architectures only need a compiler barrier here. */
  801. read_barrier_depends();
  802. return next;
  803. }
  804. static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
  805. struct iovec iov[], unsigned int iov_size,
  806. unsigned int *out_num, unsigned int *in_num,
  807. struct vhost_log *log, unsigned int *log_num,
  808. struct vring_desc *indirect)
  809. {
  810. struct vring_desc desc;
  811. unsigned int i = 0, count, found = 0;
  812. int ret;
  813. /* Sanity check */
  814. if (unlikely(indirect->len % sizeof desc)) {
  815. vq_err(vq, "Invalid length in indirect descriptor: "
  816. "len 0x%llx not multiple of 0x%zx\n",
  817. (unsigned long long)indirect->len,
  818. sizeof desc);
  819. return -EINVAL;
  820. }
  821. ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
  822. ARRAY_SIZE(vq->indirect));
  823. if (unlikely(ret < 0)) {
  824. vq_err(vq, "Translation failure %d in indirect.\n", ret);
  825. return ret;
  826. }
  827. /* We will use the result as an address to read from, so most
  828. * architectures only need a compiler barrier here. */
  829. read_barrier_depends();
  830. count = indirect->len / sizeof desc;
  831. /* Buffers are chained via a 16 bit next field, so
  832. * we can have at most 2^16 of these. */
  833. if (unlikely(count > USHRT_MAX + 1)) {
  834. vq_err(vq, "Indirect buffer length too big: %d\n",
  835. indirect->len);
  836. return -E2BIG;
  837. }
  838. do {
  839. unsigned iov_count = *in_num + *out_num;
  840. if (unlikely(++found > count)) {
  841. vq_err(vq, "Loop detected: last one at %u "
  842. "indirect size %u\n",
  843. i, count);
  844. return -EINVAL;
  845. }
  846. if (unlikely(memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
  847. sizeof desc))) {
  848. vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
  849. i, (size_t)indirect->addr + i * sizeof desc);
  850. return -EINVAL;
  851. }
  852. if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
  853. vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
  854. i, (size_t)indirect->addr + i * sizeof desc);
  855. return -EINVAL;
  856. }
  857. ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
  858. iov_size - iov_count);
  859. if (unlikely(ret < 0)) {
  860. vq_err(vq, "Translation failure %d indirect idx %d\n",
  861. ret, i);
  862. return ret;
  863. }
  864. /* If this is an input descriptor, increment that count. */
  865. if (desc.flags & VRING_DESC_F_WRITE) {
  866. *in_num += ret;
  867. if (unlikely(log)) {
  868. log[*log_num].addr = desc.addr;
  869. log[*log_num].len = desc.len;
  870. ++*log_num;
  871. }
  872. } else {
  873. /* If it's an output descriptor, they're all supposed
  874. * to come before any input descriptors. */
  875. if (unlikely(*in_num)) {
  876. vq_err(vq, "Indirect descriptor "
  877. "has out after in: idx %d\n", i);
  878. return -EINVAL;
  879. }
  880. *out_num += ret;
  881. }
  882. } while ((i = next_desc(&desc)) != -1);
  883. return 0;
  884. }
  885. /* This looks in the virtqueue and for the first available buffer, and converts
  886. * it to an iovec for convenient access. Since descriptors consist of some
  887. * number of output then some number of input descriptors, it's actually two
  888. * iovecs, but we pack them into one and note how many of each there were.
  889. *
  890. * This function returns the descriptor number found, or vq->num (which is
  891. * never a valid descriptor number) if none was found. A negative code is
  892. * returned on error. */
  893. int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
  894. struct iovec iov[], unsigned int iov_size,
  895. unsigned int *out_num, unsigned int *in_num,
  896. struct vhost_log *log, unsigned int *log_num)
  897. {
  898. struct vring_desc desc;
  899. unsigned int i, head, found = 0;
  900. u16 last_avail_idx;
  901. int ret;
  902. /* Check it isn't doing very strange things with descriptor numbers. */
  903. last_avail_idx = vq->last_avail_idx;
  904. if (unlikely(get_user(vq->avail_idx, &vq->avail->idx))) {
  905. vq_err(vq, "Failed to access avail idx at %p\n",
  906. &vq->avail->idx);
  907. return -EFAULT;
  908. }
  909. if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
  910. vq_err(vq, "Guest moved used index from %u to %u",
  911. last_avail_idx, vq->avail_idx);
  912. return -EFAULT;
  913. }
  914. /* If there's nothing new since last we looked, return invalid. */
  915. if (vq->avail_idx == last_avail_idx)
  916. return vq->num;
  917. /* Only get avail ring entries after they have been exposed by guest. */
  918. smp_rmb();
  919. /* Grab the next descriptor number they're advertising, and increment
  920. * the index we've seen. */
  921. if (unlikely(get_user(head,
  922. &vq->avail->ring[last_avail_idx % vq->num]))) {
  923. vq_err(vq, "Failed to read head: idx %d address %p\n",
  924. last_avail_idx,
  925. &vq->avail->ring[last_avail_idx % vq->num]);
  926. return -EFAULT;
  927. }
  928. /* If their number is silly, that's an error. */
  929. if (unlikely(head >= vq->num)) {
  930. vq_err(vq, "Guest says index %u > %u is available",
  931. head, vq->num);
  932. return -EINVAL;
  933. }
  934. /* When we start there are none of either input nor output. */
  935. *out_num = *in_num = 0;
  936. if (unlikely(log))
  937. *log_num = 0;
  938. i = head;
  939. do {
  940. unsigned iov_count = *in_num + *out_num;
  941. if (unlikely(i >= vq->num)) {
  942. vq_err(vq, "Desc index is %u > %u, head = %u",
  943. i, vq->num, head);
  944. return -EINVAL;
  945. }
  946. if (unlikely(++found > vq->num)) {
  947. vq_err(vq, "Loop detected: last one at %u "
  948. "vq size %u head %u\n",
  949. i, vq->num, head);
  950. return -EINVAL;
  951. }
  952. ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
  953. if (unlikely(ret)) {
  954. vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
  955. i, vq->desc + i);
  956. return -EFAULT;
  957. }
  958. if (desc.flags & VRING_DESC_F_INDIRECT) {
  959. ret = get_indirect(dev, vq, iov, iov_size,
  960. out_num, in_num,
  961. log, log_num, &desc);
  962. if (unlikely(ret < 0)) {
  963. vq_err(vq, "Failure detected "
  964. "in indirect descriptor at idx %d\n", i);
  965. return ret;
  966. }
  967. continue;
  968. }
  969. ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
  970. iov_size - iov_count);
  971. if (unlikely(ret < 0)) {
  972. vq_err(vq, "Translation failure %d descriptor idx %d\n",
  973. ret, i);
  974. return ret;
  975. }
  976. if (desc.flags & VRING_DESC_F_WRITE) {
  977. /* If this is an input descriptor,
  978. * increment that count. */
  979. *in_num += ret;
  980. if (unlikely(log)) {
  981. log[*log_num].addr = desc.addr;
  982. log[*log_num].len = desc.len;
  983. ++*log_num;
  984. }
  985. } else {
  986. /* If it's an output descriptor, they're all supposed
  987. * to come before any input descriptors. */
  988. if (unlikely(*in_num)) {
  989. vq_err(vq, "Descriptor has out after in: "
  990. "idx %d\n", i);
  991. return -EINVAL;
  992. }
  993. *out_num += ret;
  994. }
  995. } while ((i = next_desc(&desc)) != -1);
  996. /* On success, increment avail index. */
  997. vq->last_avail_idx++;
  998. return head;
  999. }
  1000. /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
  1001. void vhost_discard_vq_desc(struct vhost_virtqueue *vq)
  1002. {
  1003. vq->last_avail_idx--;
  1004. }
  1005. /* After we've used one of their buffers, we tell them about it. We'll then
  1006. * want to notify the guest, using eventfd. */
  1007. int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
  1008. {
  1009. struct vring_used_elem __user *used;
  1010. /* The virtqueue contains a ring of used buffers. Get a pointer to the
  1011. * next entry in that used ring. */
  1012. used = &vq->used->ring[vq->last_used_idx % vq->num];
  1013. if (put_user(head, &used->id)) {
  1014. vq_err(vq, "Failed to write used id");
  1015. return -EFAULT;
  1016. }
  1017. if (put_user(len, &used->len)) {
  1018. vq_err(vq, "Failed to write used len");
  1019. return -EFAULT;
  1020. }
  1021. /* Make sure buffer is written before we update index. */
  1022. smp_wmb();
  1023. if (put_user(vq->last_used_idx + 1, &vq->used->idx)) {
  1024. vq_err(vq, "Failed to increment used idx");
  1025. return -EFAULT;
  1026. }
  1027. if (unlikely(vq->log_used)) {
  1028. /* Make sure data is seen before log. */
  1029. smp_wmb();
  1030. /* Log used ring entry write. */
  1031. log_write(vq->log_base,
  1032. vq->log_addr +
  1033. ((void __user *)used - (void __user *)vq->used),
  1034. sizeof *used);
  1035. /* Log used index update. */
  1036. log_write(vq->log_base,
  1037. vq->log_addr + offsetof(struct vring_used, idx),
  1038. sizeof vq->used->idx);
  1039. if (vq->log_ctx)
  1040. eventfd_signal(vq->log_ctx, 1);
  1041. }
  1042. vq->last_used_idx++;
  1043. return 0;
  1044. }
  1045. /* This actually signals the guest, using eventfd. */
  1046. void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1047. {
  1048. __u16 flags;
  1049. /* Flush out used index updates. This is paired
  1050. * with the barrier that the Guest executes when enabling
  1051. * interrupts. */
  1052. smp_mb();
  1053. if (get_user(flags, &vq->avail->flags)) {
  1054. vq_err(vq, "Failed to get flags");
  1055. return;
  1056. }
  1057. /* If they don't want an interrupt, don't signal, unless empty. */
  1058. if ((flags & VRING_AVAIL_F_NO_INTERRUPT) &&
  1059. (vq->avail_idx != vq->last_avail_idx ||
  1060. !vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY)))
  1061. return;
  1062. /* Signal the Guest tell them we used something up. */
  1063. if (vq->call_ctx)
  1064. eventfd_signal(vq->call_ctx, 1);
  1065. }
  1066. /* And here's the combo meal deal. Supersize me! */
  1067. void vhost_add_used_and_signal(struct vhost_dev *dev,
  1068. struct vhost_virtqueue *vq,
  1069. unsigned int head, int len)
  1070. {
  1071. vhost_add_used(vq, head, len);
  1072. vhost_signal(dev, vq);
  1073. }
  1074. /* OK, now we need to know about added descriptors. */
  1075. bool vhost_enable_notify(struct vhost_virtqueue *vq)
  1076. {
  1077. u16 avail_idx;
  1078. int r;
  1079. if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
  1080. return false;
  1081. vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
  1082. r = put_user(vq->used_flags, &vq->used->flags);
  1083. if (r) {
  1084. vq_err(vq, "Failed to enable notification at %p: %d\n",
  1085. &vq->used->flags, r);
  1086. return false;
  1087. }
  1088. /* They could have slipped one in as we were doing that: make
  1089. * sure it's written, then check again. */
  1090. smp_mb();
  1091. r = get_user(avail_idx, &vq->avail->idx);
  1092. if (r) {
  1093. vq_err(vq, "Failed to check avail idx at %p: %d\n",
  1094. &vq->avail->idx, r);
  1095. return false;
  1096. }
  1097. return avail_idx != vq->last_avail_idx;
  1098. }
  1099. /* We don't need to be notified again. */
  1100. void vhost_disable_notify(struct vhost_virtqueue *vq)
  1101. {
  1102. int r;
  1103. if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
  1104. return;
  1105. vq->used_flags |= VRING_USED_F_NO_NOTIFY;
  1106. r = put_user(vq->used_flags, &vq->used->flags);
  1107. if (r)
  1108. vq_err(vq, "Failed to enable notification at %p: %d\n",
  1109. &vq->used->flags, r);
  1110. }