vhost.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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/virtual/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/mmu_context.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/mutex.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/poll.h>
  22. #include <linux/file.h>
  23. #include <linux/highmem.h>
  24. #include <linux/slab.h>
  25. #include <linux/kthread.h>
  26. #include <linux/cgroup.h>
  27. #include "vhost.h"
  28. enum {
  29. VHOST_MEMORY_MAX_NREGIONS = 64,
  30. VHOST_MEMORY_F_LOG = 0x1,
  31. };
  32. static unsigned vhost_zcopy_mask __read_mostly;
  33. #define vhost_used_event(vq) ((u16 __user *)&vq->avail->ring[vq->num])
  34. #define vhost_avail_event(vq) ((u16 __user *)&vq->used->ring[vq->num])
  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. void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
  53. {
  54. INIT_LIST_HEAD(&work->node);
  55. work->fn = fn;
  56. init_waitqueue_head(&work->done);
  57. work->flushing = 0;
  58. work->queue_seq = work->done_seq = 0;
  59. }
  60. /* Init poll structure */
  61. void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
  62. unsigned long mask, struct vhost_dev *dev)
  63. {
  64. init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
  65. init_poll_funcptr(&poll->table, vhost_poll_func);
  66. poll->mask = mask;
  67. poll->dev = dev;
  68. vhost_work_init(&poll->work, fn);
  69. }
  70. /* Start polling a file. We add ourselves to file's wait queue. The caller must
  71. * keep a reference to a file until after vhost_poll_stop is called. */
  72. void vhost_poll_start(struct vhost_poll *poll, struct file *file)
  73. {
  74. unsigned long mask;
  75. mask = file->f_op->poll(file, &poll->table);
  76. if (mask)
  77. vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
  78. }
  79. /* Stop polling a file. After this function returns, it becomes safe to drop the
  80. * file reference. You must also flush afterwards. */
  81. void vhost_poll_stop(struct vhost_poll *poll)
  82. {
  83. remove_wait_queue(poll->wqh, &poll->wait);
  84. }
  85. static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
  86. unsigned seq)
  87. {
  88. int left;
  89. spin_lock_irq(&dev->work_lock);
  90. left = seq - work->done_seq;
  91. spin_unlock_irq(&dev->work_lock);
  92. return left <= 0;
  93. }
  94. static void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
  95. {
  96. unsigned seq;
  97. int flushing;
  98. spin_lock_irq(&dev->work_lock);
  99. seq = work->queue_seq;
  100. work->flushing++;
  101. spin_unlock_irq(&dev->work_lock);
  102. wait_event(work->done, vhost_work_seq_done(dev, work, seq));
  103. spin_lock_irq(&dev->work_lock);
  104. flushing = --work->flushing;
  105. spin_unlock_irq(&dev->work_lock);
  106. BUG_ON(flushing < 0);
  107. }
  108. /* Flush any work that has been scheduled. When calling this, don't hold any
  109. * locks that are also used by the callback. */
  110. void vhost_poll_flush(struct vhost_poll *poll)
  111. {
  112. vhost_work_flush(poll->dev, &poll->work);
  113. }
  114. void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
  115. {
  116. unsigned long flags;
  117. spin_lock_irqsave(&dev->work_lock, flags);
  118. if (list_empty(&work->node)) {
  119. list_add_tail(&work->node, &dev->work_list);
  120. work->queue_seq++;
  121. wake_up_process(dev->worker);
  122. }
  123. spin_unlock_irqrestore(&dev->work_lock, flags);
  124. }
  125. void vhost_poll_queue(struct vhost_poll *poll)
  126. {
  127. vhost_work_queue(poll->dev, &poll->work);
  128. }
  129. static void vhost_vq_reset(struct vhost_dev *dev,
  130. struct vhost_virtqueue *vq)
  131. {
  132. vq->num = 1;
  133. vq->desc = NULL;
  134. vq->avail = NULL;
  135. vq->used = NULL;
  136. vq->last_avail_idx = 0;
  137. vq->avail_idx = 0;
  138. vq->last_used_idx = 0;
  139. vq->signalled_used = 0;
  140. vq->signalled_used_valid = false;
  141. vq->used_flags = 0;
  142. vq->log_used = false;
  143. vq->log_addr = -1ull;
  144. vq->vhost_hlen = 0;
  145. vq->sock_hlen = 0;
  146. vq->private_data = NULL;
  147. vq->log_base = NULL;
  148. vq->error_ctx = NULL;
  149. vq->error = NULL;
  150. vq->kick = NULL;
  151. vq->call_ctx = NULL;
  152. vq->call = NULL;
  153. vq->log_ctx = NULL;
  154. vq->upend_idx = 0;
  155. vq->done_idx = 0;
  156. vq->ubufs = NULL;
  157. }
  158. static int vhost_worker(void *data)
  159. {
  160. struct vhost_dev *dev = data;
  161. struct vhost_work *work = NULL;
  162. unsigned uninitialized_var(seq);
  163. mm_segment_t oldfs = get_fs();
  164. set_fs(USER_DS);
  165. use_mm(dev->mm);
  166. for (;;) {
  167. /* mb paired w/ kthread_stop */
  168. set_current_state(TASK_INTERRUPTIBLE);
  169. spin_lock_irq(&dev->work_lock);
  170. if (work) {
  171. work->done_seq = seq;
  172. if (work->flushing)
  173. wake_up_all(&work->done);
  174. }
  175. if (kthread_should_stop()) {
  176. spin_unlock_irq(&dev->work_lock);
  177. __set_current_state(TASK_RUNNING);
  178. break;
  179. }
  180. if (!list_empty(&dev->work_list)) {
  181. work = list_first_entry(&dev->work_list,
  182. struct vhost_work, node);
  183. list_del_init(&work->node);
  184. seq = work->queue_seq;
  185. } else
  186. work = NULL;
  187. spin_unlock_irq(&dev->work_lock);
  188. if (work) {
  189. __set_current_state(TASK_RUNNING);
  190. work->fn(work);
  191. if (need_resched())
  192. schedule();
  193. } else
  194. schedule();
  195. }
  196. unuse_mm(dev->mm);
  197. set_fs(oldfs);
  198. return 0;
  199. }
  200. static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
  201. {
  202. kfree(vq->indirect);
  203. vq->indirect = NULL;
  204. kfree(vq->log);
  205. vq->log = NULL;
  206. kfree(vq->heads);
  207. vq->heads = NULL;
  208. kfree(vq->ubuf_info);
  209. vq->ubuf_info = NULL;
  210. }
  211. void vhost_enable_zcopy(int vq)
  212. {
  213. vhost_zcopy_mask |= 0x1 << vq;
  214. }
  215. /* Helper to allocate iovec buffers for all vqs. */
  216. static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
  217. {
  218. int i;
  219. bool zcopy;
  220. for (i = 0; i < dev->nvqs; ++i) {
  221. dev->vqs[i].indirect = kmalloc(sizeof *dev->vqs[i].indirect *
  222. UIO_MAXIOV, GFP_KERNEL);
  223. dev->vqs[i].log = kmalloc(sizeof *dev->vqs[i].log * UIO_MAXIOV,
  224. GFP_KERNEL);
  225. dev->vqs[i].heads = kmalloc(sizeof *dev->vqs[i].heads *
  226. UIO_MAXIOV, GFP_KERNEL);
  227. zcopy = vhost_zcopy_mask & (0x1 << i);
  228. if (zcopy)
  229. dev->vqs[i].ubuf_info =
  230. kmalloc(sizeof *dev->vqs[i].ubuf_info *
  231. UIO_MAXIOV, GFP_KERNEL);
  232. if (!dev->vqs[i].indirect || !dev->vqs[i].log ||
  233. !dev->vqs[i].heads ||
  234. (zcopy && !dev->vqs[i].ubuf_info))
  235. goto err_nomem;
  236. }
  237. return 0;
  238. err_nomem:
  239. for (; i >= 0; --i)
  240. vhost_vq_free_iovecs(&dev->vqs[i]);
  241. return -ENOMEM;
  242. }
  243. static void vhost_dev_free_iovecs(struct vhost_dev *dev)
  244. {
  245. int i;
  246. for (i = 0; i < dev->nvqs; ++i)
  247. vhost_vq_free_iovecs(&dev->vqs[i]);
  248. }
  249. long vhost_dev_init(struct vhost_dev *dev,
  250. struct vhost_virtqueue *vqs, int nvqs)
  251. {
  252. int i;
  253. dev->vqs = vqs;
  254. dev->nvqs = nvqs;
  255. mutex_init(&dev->mutex);
  256. dev->log_ctx = NULL;
  257. dev->log_file = NULL;
  258. dev->memory = NULL;
  259. dev->mm = NULL;
  260. spin_lock_init(&dev->work_lock);
  261. INIT_LIST_HEAD(&dev->work_list);
  262. dev->worker = NULL;
  263. for (i = 0; i < dev->nvqs; ++i) {
  264. dev->vqs[i].log = NULL;
  265. dev->vqs[i].indirect = NULL;
  266. dev->vqs[i].heads = NULL;
  267. dev->vqs[i].ubuf_info = NULL;
  268. dev->vqs[i].dev = dev;
  269. mutex_init(&dev->vqs[i].mutex);
  270. vhost_vq_reset(dev, dev->vqs + i);
  271. if (dev->vqs[i].handle_kick)
  272. vhost_poll_init(&dev->vqs[i].poll,
  273. dev->vqs[i].handle_kick, POLLIN, dev);
  274. }
  275. return 0;
  276. }
  277. /* Caller should have device mutex */
  278. long vhost_dev_check_owner(struct vhost_dev *dev)
  279. {
  280. /* Are you the owner? If not, I don't think you mean to do that */
  281. return dev->mm == current->mm ? 0 : -EPERM;
  282. }
  283. struct vhost_attach_cgroups_struct {
  284. struct vhost_work work;
  285. struct task_struct *owner;
  286. int ret;
  287. };
  288. static void vhost_attach_cgroups_work(struct vhost_work *work)
  289. {
  290. struct vhost_attach_cgroups_struct *s;
  291. s = container_of(work, struct vhost_attach_cgroups_struct, work);
  292. s->ret = cgroup_attach_task_all(s->owner, current);
  293. }
  294. static int vhost_attach_cgroups(struct vhost_dev *dev)
  295. {
  296. struct vhost_attach_cgroups_struct attach;
  297. attach.owner = current;
  298. vhost_work_init(&attach.work, vhost_attach_cgroups_work);
  299. vhost_work_queue(dev, &attach.work);
  300. vhost_work_flush(dev, &attach.work);
  301. return attach.ret;
  302. }
  303. /* Caller should have device mutex */
  304. static long vhost_dev_set_owner(struct vhost_dev *dev)
  305. {
  306. struct task_struct *worker;
  307. int err;
  308. /* Is there an owner already? */
  309. if (dev->mm) {
  310. err = -EBUSY;
  311. goto err_mm;
  312. }
  313. /* No owner, become one */
  314. dev->mm = get_task_mm(current);
  315. worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
  316. if (IS_ERR(worker)) {
  317. err = PTR_ERR(worker);
  318. goto err_worker;
  319. }
  320. dev->worker = worker;
  321. wake_up_process(worker); /* avoid contributing to loadavg */
  322. err = vhost_attach_cgroups(dev);
  323. if (err)
  324. goto err_cgroup;
  325. err = vhost_dev_alloc_iovecs(dev);
  326. if (err)
  327. goto err_cgroup;
  328. return 0;
  329. err_cgroup:
  330. kthread_stop(worker);
  331. dev->worker = NULL;
  332. err_worker:
  333. if (dev->mm)
  334. mmput(dev->mm);
  335. dev->mm = NULL;
  336. err_mm:
  337. return err;
  338. }
  339. /* Caller should have device mutex */
  340. long vhost_dev_reset_owner(struct vhost_dev *dev)
  341. {
  342. struct vhost_memory *memory;
  343. /* Restore memory to default empty mapping. */
  344. memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
  345. if (!memory)
  346. return -ENOMEM;
  347. vhost_dev_cleanup(dev, true);
  348. memory->nregions = 0;
  349. RCU_INIT_POINTER(dev->memory, memory);
  350. return 0;
  351. }
  352. void vhost_dev_stop(struct vhost_dev *dev)
  353. {
  354. int i;
  355. for (i = 0; i < dev->nvqs; ++i) {
  356. if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
  357. vhost_poll_stop(&dev->vqs[i].poll);
  358. vhost_poll_flush(&dev->vqs[i].poll);
  359. }
  360. }
  361. }
  362. /* Caller should have device mutex if and only if locked is set */
  363. void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
  364. {
  365. int i;
  366. for (i = 0; i < dev->nvqs; ++i) {
  367. if (dev->vqs[i].error_ctx)
  368. eventfd_ctx_put(dev->vqs[i].error_ctx);
  369. if (dev->vqs[i].error)
  370. fput(dev->vqs[i].error);
  371. if (dev->vqs[i].kick)
  372. fput(dev->vqs[i].kick);
  373. if (dev->vqs[i].call_ctx)
  374. eventfd_ctx_put(dev->vqs[i].call_ctx);
  375. if (dev->vqs[i].call)
  376. fput(dev->vqs[i].call);
  377. vhost_vq_reset(dev, dev->vqs + i);
  378. }
  379. vhost_dev_free_iovecs(dev);
  380. if (dev->log_ctx)
  381. eventfd_ctx_put(dev->log_ctx);
  382. dev->log_ctx = NULL;
  383. if (dev->log_file)
  384. fput(dev->log_file);
  385. dev->log_file = NULL;
  386. /* No one will access memory at this point */
  387. kfree(rcu_dereference_protected(dev->memory,
  388. locked ==
  389. lockdep_is_held(&dev->mutex)));
  390. RCU_INIT_POINTER(dev->memory, NULL);
  391. WARN_ON(!list_empty(&dev->work_list));
  392. if (dev->worker) {
  393. kthread_stop(dev->worker);
  394. dev->worker = NULL;
  395. }
  396. if (dev->mm)
  397. mmput(dev->mm);
  398. dev->mm = NULL;
  399. }
  400. static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
  401. {
  402. u64 a = addr / VHOST_PAGE_SIZE / 8;
  403. /* Make sure 64 bit math will not overflow. */
  404. if (a > ULONG_MAX - (unsigned long)log_base ||
  405. a + (unsigned long)log_base > ULONG_MAX)
  406. return 0;
  407. return access_ok(VERIFY_WRITE, log_base + a,
  408. (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
  409. }
  410. /* Caller should have vq mutex and device mutex. */
  411. static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
  412. int log_all)
  413. {
  414. int i;
  415. if (!mem)
  416. return 0;
  417. for (i = 0; i < mem->nregions; ++i) {
  418. struct vhost_memory_region *m = mem->regions + i;
  419. unsigned long a = m->userspace_addr;
  420. if (m->memory_size > ULONG_MAX)
  421. return 0;
  422. else if (!access_ok(VERIFY_WRITE, (void __user *)a,
  423. m->memory_size))
  424. return 0;
  425. else if (log_all && !log_access_ok(log_base,
  426. m->guest_phys_addr,
  427. m->memory_size))
  428. return 0;
  429. }
  430. return 1;
  431. }
  432. /* Can we switch to this memory table? */
  433. /* Caller should have device mutex but not vq mutex */
  434. static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
  435. int log_all)
  436. {
  437. int i;
  438. for (i = 0; i < d->nvqs; ++i) {
  439. int ok;
  440. mutex_lock(&d->vqs[i].mutex);
  441. /* If ring is inactive, will check when it's enabled. */
  442. if (d->vqs[i].private_data)
  443. ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
  444. log_all);
  445. else
  446. ok = 1;
  447. mutex_unlock(&d->vqs[i].mutex);
  448. if (!ok)
  449. return 0;
  450. }
  451. return 1;
  452. }
  453. static int vq_access_ok(struct vhost_dev *d, unsigned int num,
  454. struct vring_desc __user *desc,
  455. struct vring_avail __user *avail,
  456. struct vring_used __user *used)
  457. {
  458. size_t s = vhost_has_feature(d, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
  459. return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
  460. access_ok(VERIFY_READ, avail,
  461. sizeof *avail + num * sizeof *avail->ring + s) &&
  462. access_ok(VERIFY_WRITE, used,
  463. sizeof *used + num * sizeof *used->ring + s);
  464. }
  465. /* Can we log writes? */
  466. /* Caller should have device mutex but not vq mutex */
  467. int vhost_log_access_ok(struct vhost_dev *dev)
  468. {
  469. struct vhost_memory *mp;
  470. mp = rcu_dereference_protected(dev->memory,
  471. lockdep_is_held(&dev->mutex));
  472. return memory_access_ok(dev, mp, 1);
  473. }
  474. /* Verify access for write logging. */
  475. /* Caller should have vq mutex and device mutex */
  476. static int vq_log_access_ok(struct vhost_dev *d, struct vhost_virtqueue *vq,
  477. void __user *log_base)
  478. {
  479. struct vhost_memory *mp;
  480. size_t s = vhost_has_feature(d, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
  481. mp = rcu_dereference_protected(vq->dev->memory,
  482. lockdep_is_held(&vq->mutex));
  483. return vq_memory_access_ok(log_base, mp,
  484. vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
  485. (!vq->log_used || log_access_ok(log_base, vq->log_addr,
  486. sizeof *vq->used +
  487. vq->num * sizeof *vq->used->ring + s));
  488. }
  489. /* Can we start vq? */
  490. /* Caller should have vq mutex and device mutex */
  491. int vhost_vq_access_ok(struct vhost_virtqueue *vq)
  492. {
  493. return vq_access_ok(vq->dev, vq->num, vq->desc, vq->avail, vq->used) &&
  494. vq_log_access_ok(vq->dev, vq, vq->log_base);
  495. }
  496. static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
  497. {
  498. struct vhost_memory mem, *newmem, *oldmem;
  499. unsigned long size = offsetof(struct vhost_memory, regions);
  500. if (copy_from_user(&mem, m, size))
  501. return -EFAULT;
  502. if (mem.padding)
  503. return -EOPNOTSUPP;
  504. if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
  505. return -E2BIG;
  506. newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
  507. if (!newmem)
  508. return -ENOMEM;
  509. memcpy(newmem, &mem, size);
  510. if (copy_from_user(newmem->regions, m->regions,
  511. mem.nregions * sizeof *m->regions)) {
  512. kfree(newmem);
  513. return -EFAULT;
  514. }
  515. if (!memory_access_ok(d, newmem,
  516. vhost_has_feature(d, VHOST_F_LOG_ALL))) {
  517. kfree(newmem);
  518. return -EFAULT;
  519. }
  520. oldmem = rcu_dereference_protected(d->memory,
  521. lockdep_is_held(&d->mutex));
  522. rcu_assign_pointer(d->memory, newmem);
  523. synchronize_rcu();
  524. kfree(oldmem);
  525. return 0;
  526. }
  527. static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
  528. {
  529. struct file *eventfp, *filep = NULL;
  530. bool pollstart = false, pollstop = false;
  531. struct eventfd_ctx *ctx = NULL;
  532. u32 __user *idxp = argp;
  533. struct vhost_virtqueue *vq;
  534. struct vhost_vring_state s;
  535. struct vhost_vring_file f;
  536. struct vhost_vring_addr a;
  537. u32 idx;
  538. long r;
  539. r = get_user(idx, idxp);
  540. if (r < 0)
  541. return r;
  542. if (idx >= d->nvqs)
  543. return -ENOBUFS;
  544. vq = d->vqs + idx;
  545. mutex_lock(&vq->mutex);
  546. switch (ioctl) {
  547. case VHOST_SET_VRING_NUM:
  548. /* Resizing ring with an active backend?
  549. * You don't want to do that. */
  550. if (vq->private_data) {
  551. r = -EBUSY;
  552. break;
  553. }
  554. if (copy_from_user(&s, argp, sizeof s)) {
  555. r = -EFAULT;
  556. break;
  557. }
  558. if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
  559. r = -EINVAL;
  560. break;
  561. }
  562. vq->num = s.num;
  563. break;
  564. case VHOST_SET_VRING_BASE:
  565. /* Moving base with an active backend?
  566. * You don't want to do that. */
  567. if (vq->private_data) {
  568. r = -EBUSY;
  569. break;
  570. }
  571. if (copy_from_user(&s, argp, sizeof s)) {
  572. r = -EFAULT;
  573. break;
  574. }
  575. if (s.num > 0xffff) {
  576. r = -EINVAL;
  577. break;
  578. }
  579. vq->last_avail_idx = s.num;
  580. /* Forget the cached index value. */
  581. vq->avail_idx = vq->last_avail_idx;
  582. break;
  583. case VHOST_GET_VRING_BASE:
  584. s.index = idx;
  585. s.num = vq->last_avail_idx;
  586. if (copy_to_user(argp, &s, sizeof s))
  587. r = -EFAULT;
  588. break;
  589. case VHOST_SET_VRING_ADDR:
  590. if (copy_from_user(&a, argp, sizeof a)) {
  591. r = -EFAULT;
  592. break;
  593. }
  594. if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
  595. r = -EOPNOTSUPP;
  596. break;
  597. }
  598. /* For 32bit, verify that the top 32bits of the user
  599. data are set to zero. */
  600. if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
  601. (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
  602. (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
  603. r = -EFAULT;
  604. break;
  605. }
  606. if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
  607. (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
  608. (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
  609. r = -EINVAL;
  610. break;
  611. }
  612. /* We only verify access here if backend is configured.
  613. * If it is not, we don't as size might not have been setup.
  614. * We will verify when backend is configured. */
  615. if (vq->private_data) {
  616. if (!vq_access_ok(d, vq->num,
  617. (void __user *)(unsigned long)a.desc_user_addr,
  618. (void __user *)(unsigned long)a.avail_user_addr,
  619. (void __user *)(unsigned long)a.used_user_addr)) {
  620. r = -EINVAL;
  621. break;
  622. }
  623. /* Also validate log access for used ring if enabled. */
  624. if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
  625. !log_access_ok(vq->log_base, a.log_guest_addr,
  626. sizeof *vq->used +
  627. vq->num * sizeof *vq->used->ring)) {
  628. r = -EINVAL;
  629. break;
  630. }
  631. }
  632. vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
  633. vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
  634. vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
  635. vq->log_addr = a.log_guest_addr;
  636. vq->used = (void __user *)(unsigned long)a.used_user_addr;
  637. break;
  638. case VHOST_SET_VRING_KICK:
  639. if (copy_from_user(&f, argp, sizeof f)) {
  640. r = -EFAULT;
  641. break;
  642. }
  643. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  644. if (IS_ERR(eventfp)) {
  645. r = PTR_ERR(eventfp);
  646. break;
  647. }
  648. if (eventfp != vq->kick) {
  649. pollstop = (filep = vq->kick) != NULL;
  650. pollstart = (vq->kick = eventfp) != NULL;
  651. } else
  652. filep = eventfp;
  653. break;
  654. case VHOST_SET_VRING_CALL:
  655. if (copy_from_user(&f, argp, sizeof f)) {
  656. r = -EFAULT;
  657. break;
  658. }
  659. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  660. if (IS_ERR(eventfp)) {
  661. r = PTR_ERR(eventfp);
  662. break;
  663. }
  664. if (eventfp != vq->call) {
  665. filep = vq->call;
  666. ctx = vq->call_ctx;
  667. vq->call = eventfp;
  668. vq->call_ctx = eventfp ?
  669. eventfd_ctx_fileget(eventfp) : NULL;
  670. } else
  671. filep = eventfp;
  672. break;
  673. case VHOST_SET_VRING_ERR:
  674. if (copy_from_user(&f, argp, sizeof f)) {
  675. r = -EFAULT;
  676. break;
  677. }
  678. eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
  679. if (IS_ERR(eventfp)) {
  680. r = PTR_ERR(eventfp);
  681. break;
  682. }
  683. if (eventfp != vq->error) {
  684. filep = vq->error;
  685. vq->error = eventfp;
  686. ctx = vq->error_ctx;
  687. vq->error_ctx = eventfp ?
  688. eventfd_ctx_fileget(eventfp) : NULL;
  689. } else
  690. filep = eventfp;
  691. break;
  692. default:
  693. r = -ENOIOCTLCMD;
  694. }
  695. if (pollstop && vq->handle_kick)
  696. vhost_poll_stop(&vq->poll);
  697. if (ctx)
  698. eventfd_ctx_put(ctx);
  699. if (filep)
  700. fput(filep);
  701. if (pollstart && vq->handle_kick)
  702. vhost_poll_start(&vq->poll, vq->kick);
  703. mutex_unlock(&vq->mutex);
  704. if (pollstop && vq->handle_kick)
  705. vhost_poll_flush(&vq->poll);
  706. return r;
  707. }
  708. /* Caller must have device mutex */
  709. long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
  710. {
  711. void __user *argp = (void __user *)arg;
  712. struct file *eventfp, *filep = NULL;
  713. struct eventfd_ctx *ctx = NULL;
  714. u64 p;
  715. long r;
  716. int i, fd;
  717. /* If you are not the owner, you can become one */
  718. if (ioctl == VHOST_SET_OWNER) {
  719. r = vhost_dev_set_owner(d);
  720. goto done;
  721. }
  722. /* You must be the owner to do anything else */
  723. r = vhost_dev_check_owner(d);
  724. if (r)
  725. goto done;
  726. switch (ioctl) {
  727. case VHOST_SET_MEM_TABLE:
  728. r = vhost_set_memory(d, argp);
  729. break;
  730. case VHOST_SET_LOG_BASE:
  731. if (copy_from_user(&p, argp, sizeof p)) {
  732. r = -EFAULT;
  733. break;
  734. }
  735. if ((u64)(unsigned long)p != p) {
  736. r = -EFAULT;
  737. break;
  738. }
  739. for (i = 0; i < d->nvqs; ++i) {
  740. struct vhost_virtqueue *vq;
  741. void __user *base = (void __user *)(unsigned long)p;
  742. vq = d->vqs + i;
  743. mutex_lock(&vq->mutex);
  744. /* If ring is inactive, will check when it's enabled. */
  745. if (vq->private_data && !vq_log_access_ok(d, vq, base))
  746. r = -EFAULT;
  747. else
  748. vq->log_base = base;
  749. mutex_unlock(&vq->mutex);
  750. }
  751. break;
  752. case VHOST_SET_LOG_FD:
  753. r = get_user(fd, (int __user *)argp);
  754. if (r < 0)
  755. break;
  756. eventfp = fd == -1 ? NULL : eventfd_fget(fd);
  757. if (IS_ERR(eventfp)) {
  758. r = PTR_ERR(eventfp);
  759. break;
  760. }
  761. if (eventfp != d->log_file) {
  762. filep = d->log_file;
  763. ctx = d->log_ctx;
  764. d->log_ctx = eventfp ?
  765. eventfd_ctx_fileget(eventfp) : NULL;
  766. } else
  767. filep = eventfp;
  768. for (i = 0; i < d->nvqs; ++i) {
  769. mutex_lock(&d->vqs[i].mutex);
  770. d->vqs[i].log_ctx = d->log_ctx;
  771. mutex_unlock(&d->vqs[i].mutex);
  772. }
  773. if (ctx)
  774. eventfd_ctx_put(ctx);
  775. if (filep)
  776. fput(filep);
  777. break;
  778. default:
  779. r = vhost_set_vring(d, ioctl, argp);
  780. break;
  781. }
  782. done:
  783. return r;
  784. }
  785. static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
  786. __u64 addr, __u32 len)
  787. {
  788. struct vhost_memory_region *reg;
  789. int i;
  790. /* linear search is not brilliant, but we really have on the order of 6
  791. * regions in practice */
  792. for (i = 0; i < mem->nregions; ++i) {
  793. reg = mem->regions + i;
  794. if (reg->guest_phys_addr <= addr &&
  795. reg->guest_phys_addr + reg->memory_size - 1 >= addr)
  796. return reg;
  797. }
  798. return NULL;
  799. }
  800. /* TODO: This is really inefficient. We need something like get_user()
  801. * (instruction directly accesses the data, with an exception table entry
  802. * returning -EFAULT). See Documentation/x86/exception-tables.txt.
  803. */
  804. static int set_bit_to_user(int nr, void __user *addr)
  805. {
  806. unsigned long log = (unsigned long)addr;
  807. struct page *page;
  808. void *base;
  809. int bit = nr + (log % PAGE_SIZE) * 8;
  810. int r;
  811. r = get_user_pages_fast(log, 1, 1, &page);
  812. if (r < 0)
  813. return r;
  814. BUG_ON(r != 1);
  815. base = kmap_atomic(page);
  816. set_bit(bit, base);
  817. kunmap_atomic(base);
  818. set_page_dirty_lock(page);
  819. put_page(page);
  820. return 0;
  821. }
  822. static int log_write(void __user *log_base,
  823. u64 write_address, u64 write_length)
  824. {
  825. u64 write_page = write_address / VHOST_PAGE_SIZE;
  826. int r;
  827. if (!write_length)
  828. return 0;
  829. write_length += write_address % VHOST_PAGE_SIZE;
  830. for (;;) {
  831. u64 base = (u64)(unsigned long)log_base;
  832. u64 log = base + write_page / 8;
  833. int bit = write_page % 8;
  834. if ((u64)(unsigned long)log != log)
  835. return -EFAULT;
  836. r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
  837. if (r < 0)
  838. return r;
  839. if (write_length <= VHOST_PAGE_SIZE)
  840. break;
  841. write_length -= VHOST_PAGE_SIZE;
  842. write_page += 1;
  843. }
  844. return r;
  845. }
  846. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  847. unsigned int log_num, u64 len)
  848. {
  849. int i, r;
  850. /* Make sure data written is seen before log. */
  851. smp_wmb();
  852. for (i = 0; i < log_num; ++i) {
  853. u64 l = min(log[i].len, len);
  854. r = log_write(vq->log_base, log[i].addr, l);
  855. if (r < 0)
  856. return r;
  857. len -= l;
  858. if (!len) {
  859. if (vq->log_ctx)
  860. eventfd_signal(vq->log_ctx, 1);
  861. return 0;
  862. }
  863. }
  864. /* Length written exceeds what we have stored. This is a bug. */
  865. BUG();
  866. return 0;
  867. }
  868. static int vhost_update_used_flags(struct vhost_virtqueue *vq)
  869. {
  870. void __user *used;
  871. if (__put_user(vq->used_flags, &vq->used->flags) < 0)
  872. return -EFAULT;
  873. if (unlikely(vq->log_used)) {
  874. /* Make sure the flag is seen before log. */
  875. smp_wmb();
  876. /* Log used flag write. */
  877. used = &vq->used->flags;
  878. log_write(vq->log_base, vq->log_addr +
  879. (used - (void __user *)vq->used),
  880. sizeof vq->used->flags);
  881. if (vq->log_ctx)
  882. eventfd_signal(vq->log_ctx, 1);
  883. }
  884. return 0;
  885. }
  886. static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
  887. {
  888. if (__put_user(vq->avail_idx, vhost_avail_event(vq)))
  889. return -EFAULT;
  890. if (unlikely(vq->log_used)) {
  891. void __user *used;
  892. /* Make sure the event is seen before log. */
  893. smp_wmb();
  894. /* Log avail event write */
  895. used = vhost_avail_event(vq);
  896. log_write(vq->log_base, vq->log_addr +
  897. (used - (void __user *)vq->used),
  898. sizeof *vhost_avail_event(vq));
  899. if (vq->log_ctx)
  900. eventfd_signal(vq->log_ctx, 1);
  901. }
  902. return 0;
  903. }
  904. int vhost_init_used(struct vhost_virtqueue *vq)
  905. {
  906. int r;
  907. if (!vq->private_data)
  908. return 0;
  909. r = vhost_update_used_flags(vq);
  910. if (r)
  911. return r;
  912. vq->signalled_used_valid = false;
  913. return get_user(vq->last_used_idx, &vq->used->idx);
  914. }
  915. static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
  916. struct iovec iov[], int iov_size)
  917. {
  918. const struct vhost_memory_region *reg;
  919. struct vhost_memory *mem;
  920. struct iovec *_iov;
  921. u64 s = 0;
  922. int ret = 0;
  923. rcu_read_lock();
  924. mem = rcu_dereference(dev->memory);
  925. while ((u64)len > s) {
  926. u64 size;
  927. if (unlikely(ret >= iov_size)) {
  928. ret = -ENOBUFS;
  929. break;
  930. }
  931. reg = find_region(mem, addr, len);
  932. if (unlikely(!reg)) {
  933. ret = -EFAULT;
  934. break;
  935. }
  936. _iov = iov + ret;
  937. size = reg->memory_size - addr + reg->guest_phys_addr;
  938. _iov->iov_len = min((u64)len - s, size);
  939. _iov->iov_base = (void __user *)(unsigned long)
  940. (reg->userspace_addr + addr - reg->guest_phys_addr);
  941. s += size;
  942. addr += size;
  943. ++ret;
  944. }
  945. rcu_read_unlock();
  946. return ret;
  947. }
  948. /* Each buffer in the virtqueues is actually a chain of descriptors. This
  949. * function returns the next descriptor in the chain,
  950. * or -1U if we're at the end. */
  951. static unsigned next_desc(struct vring_desc *desc)
  952. {
  953. unsigned int next;
  954. /* If this descriptor says it doesn't chain, we're done. */
  955. if (!(desc->flags & VRING_DESC_F_NEXT))
  956. return -1U;
  957. /* Check they're not leading us off end of descriptors. */
  958. next = desc->next;
  959. /* Make sure compiler knows to grab that: we don't want it changing! */
  960. /* We will use the result as an index in an array, so most
  961. * architectures only need a compiler barrier here. */
  962. read_barrier_depends();
  963. return next;
  964. }
  965. static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
  966. struct iovec iov[], unsigned int iov_size,
  967. unsigned int *out_num, unsigned int *in_num,
  968. struct vhost_log *log, unsigned int *log_num,
  969. struct vring_desc *indirect)
  970. {
  971. struct vring_desc desc;
  972. unsigned int i = 0, count, found = 0;
  973. int ret;
  974. /* Sanity check */
  975. if (unlikely(indirect->len % sizeof desc)) {
  976. vq_err(vq, "Invalid length in indirect descriptor: "
  977. "len 0x%llx not multiple of 0x%zx\n",
  978. (unsigned long long)indirect->len,
  979. sizeof desc);
  980. return -EINVAL;
  981. }
  982. ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
  983. UIO_MAXIOV);
  984. if (unlikely(ret < 0)) {
  985. vq_err(vq, "Translation failure %d in indirect.\n", ret);
  986. return ret;
  987. }
  988. /* We will use the result as an address to read from, so most
  989. * architectures only need a compiler barrier here. */
  990. read_barrier_depends();
  991. count = indirect->len / sizeof desc;
  992. /* Buffers are chained via a 16 bit next field, so
  993. * we can have at most 2^16 of these. */
  994. if (unlikely(count > USHRT_MAX + 1)) {
  995. vq_err(vq, "Indirect buffer length too big: %d\n",
  996. indirect->len);
  997. return -E2BIG;
  998. }
  999. do {
  1000. unsigned iov_count = *in_num + *out_num;
  1001. if (unlikely(++found > count)) {
  1002. vq_err(vq, "Loop detected: last one at %u "
  1003. "indirect size %u\n",
  1004. i, count);
  1005. return -EINVAL;
  1006. }
  1007. if (unlikely(memcpy_fromiovec((unsigned char *)&desc,
  1008. vq->indirect, sizeof desc))) {
  1009. vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
  1010. i, (size_t)indirect->addr + i * sizeof desc);
  1011. return -EINVAL;
  1012. }
  1013. if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
  1014. vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
  1015. i, (size_t)indirect->addr + i * sizeof desc);
  1016. return -EINVAL;
  1017. }
  1018. ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
  1019. iov_size - iov_count);
  1020. if (unlikely(ret < 0)) {
  1021. vq_err(vq, "Translation failure %d indirect idx %d\n",
  1022. ret, i);
  1023. return ret;
  1024. }
  1025. /* If this is an input descriptor, increment that count. */
  1026. if (desc.flags & VRING_DESC_F_WRITE) {
  1027. *in_num += ret;
  1028. if (unlikely(log)) {
  1029. log[*log_num].addr = desc.addr;
  1030. log[*log_num].len = desc.len;
  1031. ++*log_num;
  1032. }
  1033. } else {
  1034. /* If it's an output descriptor, they're all supposed
  1035. * to come before any input descriptors. */
  1036. if (unlikely(*in_num)) {
  1037. vq_err(vq, "Indirect descriptor "
  1038. "has out after in: idx %d\n", i);
  1039. return -EINVAL;
  1040. }
  1041. *out_num += ret;
  1042. }
  1043. } while ((i = next_desc(&desc)) != -1);
  1044. return 0;
  1045. }
  1046. /* This looks in the virtqueue and for the first available buffer, and converts
  1047. * it to an iovec for convenient access. Since descriptors consist of some
  1048. * number of output then some number of input descriptors, it's actually two
  1049. * iovecs, but we pack them into one and note how many of each there were.
  1050. *
  1051. * This function returns the descriptor number found, or vq->num (which is
  1052. * never a valid descriptor number) if none was found. A negative code is
  1053. * returned on error. */
  1054. int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
  1055. struct iovec iov[], unsigned int iov_size,
  1056. unsigned int *out_num, unsigned int *in_num,
  1057. struct vhost_log *log, unsigned int *log_num)
  1058. {
  1059. struct vring_desc desc;
  1060. unsigned int i, head, found = 0;
  1061. u16 last_avail_idx;
  1062. int ret;
  1063. /* Check it isn't doing very strange things with descriptor numbers. */
  1064. last_avail_idx = vq->last_avail_idx;
  1065. if (unlikely(__get_user(vq->avail_idx, &vq->avail->idx))) {
  1066. vq_err(vq, "Failed to access avail idx at %p\n",
  1067. &vq->avail->idx);
  1068. return -EFAULT;
  1069. }
  1070. if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
  1071. vq_err(vq, "Guest moved used index from %u to %u",
  1072. last_avail_idx, vq->avail_idx);
  1073. return -EFAULT;
  1074. }
  1075. /* If there's nothing new since last we looked, return invalid. */
  1076. if (vq->avail_idx == last_avail_idx)
  1077. return vq->num;
  1078. /* Only get avail ring entries after they have been exposed by guest. */
  1079. smp_rmb();
  1080. /* Grab the next descriptor number they're advertising, and increment
  1081. * the index we've seen. */
  1082. if (unlikely(__get_user(head,
  1083. &vq->avail->ring[last_avail_idx % vq->num]))) {
  1084. vq_err(vq, "Failed to read head: idx %d address %p\n",
  1085. last_avail_idx,
  1086. &vq->avail->ring[last_avail_idx % vq->num]);
  1087. return -EFAULT;
  1088. }
  1089. /* If their number is silly, that's an error. */
  1090. if (unlikely(head >= vq->num)) {
  1091. vq_err(vq, "Guest says index %u > %u is available",
  1092. head, vq->num);
  1093. return -EINVAL;
  1094. }
  1095. /* When we start there are none of either input nor output. */
  1096. *out_num = *in_num = 0;
  1097. if (unlikely(log))
  1098. *log_num = 0;
  1099. i = head;
  1100. do {
  1101. unsigned iov_count = *in_num + *out_num;
  1102. if (unlikely(i >= vq->num)) {
  1103. vq_err(vq, "Desc index is %u > %u, head = %u",
  1104. i, vq->num, head);
  1105. return -EINVAL;
  1106. }
  1107. if (unlikely(++found > vq->num)) {
  1108. vq_err(vq, "Loop detected: last one at %u "
  1109. "vq size %u head %u\n",
  1110. i, vq->num, head);
  1111. return -EINVAL;
  1112. }
  1113. ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
  1114. if (unlikely(ret)) {
  1115. vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
  1116. i, vq->desc + i);
  1117. return -EFAULT;
  1118. }
  1119. if (desc.flags & VRING_DESC_F_INDIRECT) {
  1120. ret = get_indirect(dev, vq, iov, iov_size,
  1121. out_num, in_num,
  1122. log, log_num, &desc);
  1123. if (unlikely(ret < 0)) {
  1124. vq_err(vq, "Failure detected "
  1125. "in indirect descriptor at idx %d\n", i);
  1126. return ret;
  1127. }
  1128. continue;
  1129. }
  1130. ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
  1131. iov_size - iov_count);
  1132. if (unlikely(ret < 0)) {
  1133. vq_err(vq, "Translation failure %d descriptor idx %d\n",
  1134. ret, i);
  1135. return ret;
  1136. }
  1137. if (desc.flags & VRING_DESC_F_WRITE) {
  1138. /* If this is an input descriptor,
  1139. * increment that count. */
  1140. *in_num += ret;
  1141. if (unlikely(log)) {
  1142. log[*log_num].addr = desc.addr;
  1143. log[*log_num].len = desc.len;
  1144. ++*log_num;
  1145. }
  1146. } else {
  1147. /* If it's an output descriptor, they're all supposed
  1148. * to come before any input descriptors. */
  1149. if (unlikely(*in_num)) {
  1150. vq_err(vq, "Descriptor has out after in: "
  1151. "idx %d\n", i);
  1152. return -EINVAL;
  1153. }
  1154. *out_num += ret;
  1155. }
  1156. } while ((i = next_desc(&desc)) != -1);
  1157. /* On success, increment avail index. */
  1158. vq->last_avail_idx++;
  1159. /* Assume notifications from guest are disabled at this point,
  1160. * if they aren't we would need to update avail_event index. */
  1161. BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
  1162. return head;
  1163. }
  1164. /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
  1165. void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
  1166. {
  1167. vq->last_avail_idx -= n;
  1168. }
  1169. /* After we've used one of their buffers, we tell them about it. We'll then
  1170. * want to notify the guest, using eventfd. */
  1171. int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
  1172. {
  1173. struct vring_used_elem __user *used;
  1174. /* The virtqueue contains a ring of used buffers. Get a pointer to the
  1175. * next entry in that used ring. */
  1176. used = &vq->used->ring[vq->last_used_idx % vq->num];
  1177. if (__put_user(head, &used->id)) {
  1178. vq_err(vq, "Failed to write used id");
  1179. return -EFAULT;
  1180. }
  1181. if (__put_user(len, &used->len)) {
  1182. vq_err(vq, "Failed to write used len");
  1183. return -EFAULT;
  1184. }
  1185. /* Make sure buffer is written before we update index. */
  1186. smp_wmb();
  1187. if (__put_user(vq->last_used_idx + 1, &vq->used->idx)) {
  1188. vq_err(vq, "Failed to increment used idx");
  1189. return -EFAULT;
  1190. }
  1191. if (unlikely(vq->log_used)) {
  1192. /* Make sure data is seen before log. */
  1193. smp_wmb();
  1194. /* Log used ring entry write. */
  1195. log_write(vq->log_base,
  1196. vq->log_addr +
  1197. ((void __user *)used - (void __user *)vq->used),
  1198. sizeof *used);
  1199. /* Log used index update. */
  1200. log_write(vq->log_base,
  1201. vq->log_addr + offsetof(struct vring_used, idx),
  1202. sizeof vq->used->idx);
  1203. if (vq->log_ctx)
  1204. eventfd_signal(vq->log_ctx, 1);
  1205. }
  1206. vq->last_used_idx++;
  1207. /* If the driver never bothers to signal in a very long while,
  1208. * used index might wrap around. If that happens, invalidate
  1209. * signalled_used index we stored. TODO: make sure driver
  1210. * signals at least once in 2^16 and remove this. */
  1211. if (unlikely(vq->last_used_idx == vq->signalled_used))
  1212. vq->signalled_used_valid = false;
  1213. return 0;
  1214. }
  1215. static int __vhost_add_used_n(struct vhost_virtqueue *vq,
  1216. struct vring_used_elem *heads,
  1217. unsigned count)
  1218. {
  1219. struct vring_used_elem __user *used;
  1220. u16 old, new;
  1221. int start;
  1222. start = vq->last_used_idx % vq->num;
  1223. used = vq->used->ring + start;
  1224. if (__copy_to_user(used, heads, count * sizeof *used)) {
  1225. vq_err(vq, "Failed to write used");
  1226. return -EFAULT;
  1227. }
  1228. if (unlikely(vq->log_used)) {
  1229. /* Make sure data is seen before log. */
  1230. smp_wmb();
  1231. /* Log used ring entry write. */
  1232. log_write(vq->log_base,
  1233. vq->log_addr +
  1234. ((void __user *)used - (void __user *)vq->used),
  1235. count * sizeof *used);
  1236. }
  1237. old = vq->last_used_idx;
  1238. new = (vq->last_used_idx += count);
  1239. /* If the driver never bothers to signal in a very long while,
  1240. * used index might wrap around. If that happens, invalidate
  1241. * signalled_used index we stored. TODO: make sure driver
  1242. * signals at least once in 2^16 and remove this. */
  1243. if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
  1244. vq->signalled_used_valid = false;
  1245. return 0;
  1246. }
  1247. /* After we've used one of their buffers, we tell them about it. We'll then
  1248. * want to notify the guest, using eventfd. */
  1249. int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
  1250. unsigned count)
  1251. {
  1252. int start, n, r;
  1253. start = vq->last_used_idx % vq->num;
  1254. n = vq->num - start;
  1255. if (n < count) {
  1256. r = __vhost_add_used_n(vq, heads, n);
  1257. if (r < 0)
  1258. return r;
  1259. heads += n;
  1260. count -= n;
  1261. }
  1262. r = __vhost_add_used_n(vq, heads, count);
  1263. /* Make sure buffer is written before we update index. */
  1264. smp_wmb();
  1265. if (put_user(vq->last_used_idx, &vq->used->idx)) {
  1266. vq_err(vq, "Failed to increment used idx");
  1267. return -EFAULT;
  1268. }
  1269. if (unlikely(vq->log_used)) {
  1270. /* Log used index update. */
  1271. log_write(vq->log_base,
  1272. vq->log_addr + offsetof(struct vring_used, idx),
  1273. sizeof vq->used->idx);
  1274. if (vq->log_ctx)
  1275. eventfd_signal(vq->log_ctx, 1);
  1276. }
  1277. return r;
  1278. }
  1279. static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1280. {
  1281. __u16 old, new, event;
  1282. bool v;
  1283. /* Flush out used index updates. This is paired
  1284. * with the barrier that the Guest executes when enabling
  1285. * interrupts. */
  1286. smp_mb();
  1287. if (vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
  1288. unlikely(vq->avail_idx == vq->last_avail_idx))
  1289. return true;
  1290. if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
  1291. __u16 flags;
  1292. if (__get_user(flags, &vq->avail->flags)) {
  1293. vq_err(vq, "Failed to get flags");
  1294. return true;
  1295. }
  1296. return !(flags & VRING_AVAIL_F_NO_INTERRUPT);
  1297. }
  1298. old = vq->signalled_used;
  1299. v = vq->signalled_used_valid;
  1300. new = vq->signalled_used = vq->last_used_idx;
  1301. vq->signalled_used_valid = true;
  1302. if (unlikely(!v))
  1303. return true;
  1304. if (get_user(event, vhost_used_event(vq))) {
  1305. vq_err(vq, "Failed to get used event idx");
  1306. return true;
  1307. }
  1308. return vring_need_event(event, new, old);
  1309. }
  1310. /* This actually signals the guest, using eventfd. */
  1311. void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1312. {
  1313. /* Signal the Guest tell them we used something up. */
  1314. if (vq->call_ctx && vhost_notify(dev, vq))
  1315. eventfd_signal(vq->call_ctx, 1);
  1316. }
  1317. /* And here's the combo meal deal. Supersize me! */
  1318. void vhost_add_used_and_signal(struct vhost_dev *dev,
  1319. struct vhost_virtqueue *vq,
  1320. unsigned int head, int len)
  1321. {
  1322. vhost_add_used(vq, head, len);
  1323. vhost_signal(dev, vq);
  1324. }
  1325. /* multi-buffer version of vhost_add_used_and_signal */
  1326. void vhost_add_used_and_signal_n(struct vhost_dev *dev,
  1327. struct vhost_virtqueue *vq,
  1328. struct vring_used_elem *heads, unsigned count)
  1329. {
  1330. vhost_add_used_n(vq, heads, count);
  1331. vhost_signal(dev, vq);
  1332. }
  1333. /* OK, now we need to know about added descriptors. */
  1334. bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1335. {
  1336. u16 avail_idx;
  1337. int r;
  1338. if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
  1339. return false;
  1340. vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
  1341. if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
  1342. r = vhost_update_used_flags(vq);
  1343. if (r) {
  1344. vq_err(vq, "Failed to enable notification at %p: %d\n",
  1345. &vq->used->flags, r);
  1346. return false;
  1347. }
  1348. } else {
  1349. r = vhost_update_avail_event(vq, vq->avail_idx);
  1350. if (r) {
  1351. vq_err(vq, "Failed to update avail event index at %p: %d\n",
  1352. vhost_avail_event(vq), r);
  1353. return false;
  1354. }
  1355. }
  1356. /* They could have slipped one in as we were doing that: make
  1357. * sure it's written, then check again. */
  1358. smp_mb();
  1359. r = __get_user(avail_idx, &vq->avail->idx);
  1360. if (r) {
  1361. vq_err(vq, "Failed to check avail idx at %p: %d\n",
  1362. &vq->avail->idx, r);
  1363. return false;
  1364. }
  1365. return avail_idx != vq->avail_idx;
  1366. }
  1367. /* We don't need to be notified again. */
  1368. void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
  1369. {
  1370. int r;
  1371. if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
  1372. return;
  1373. vq->used_flags |= VRING_USED_F_NO_NOTIFY;
  1374. if (!vhost_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) {
  1375. r = vhost_update_used_flags(vq);
  1376. if (r)
  1377. vq_err(vq, "Failed to enable notification at %p: %d\n",
  1378. &vq->used->flags, r);
  1379. }
  1380. }
  1381. static void vhost_zerocopy_done_signal(struct kref *kref)
  1382. {
  1383. struct vhost_ubuf_ref *ubufs = container_of(kref, struct vhost_ubuf_ref,
  1384. kref);
  1385. wake_up(&ubufs->wait);
  1386. }
  1387. struct vhost_ubuf_ref *vhost_ubuf_alloc(struct vhost_virtqueue *vq,
  1388. bool zcopy)
  1389. {
  1390. struct vhost_ubuf_ref *ubufs;
  1391. /* No zero copy backend? Nothing to count. */
  1392. if (!zcopy)
  1393. return NULL;
  1394. ubufs = kmalloc(sizeof *ubufs, GFP_KERNEL);
  1395. if (!ubufs)
  1396. return ERR_PTR(-ENOMEM);
  1397. kref_init(&ubufs->kref);
  1398. init_waitqueue_head(&ubufs->wait);
  1399. ubufs->vq = vq;
  1400. return ubufs;
  1401. }
  1402. void vhost_ubuf_put(struct vhost_ubuf_ref *ubufs)
  1403. {
  1404. kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
  1405. }
  1406. void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *ubufs)
  1407. {
  1408. kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
  1409. wait_event(ubufs->wait, !atomic_read(&ubufs->kref.refcount));
  1410. kfree(ubufs);
  1411. }