vhost.c 41 KB

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