casaroli commented on code in PR #21408:
URL: https://github.com/apache/nuttx/pull/21408#discussion_r4473591572


##########
drivers/virtio/virtio-rng.c:
##########
@@ -160,23 +161,34 @@ static ssize_t virtio_rng_read(FAR struct file *filep, 
FAR char *buffer,
   cookie.len = 0;
   nxsem_init(&cookie.sem, 0, 0);
 
+  /* Map the user buffer so the device side can access it. On MMIO/PCI
+   * map returns the buffer itself and the device writes the entropy straight
+   * into it (zero copy); a transport that cannot reach it (rptun, or a user
+   * buffer with CONFIG_ARCH_ADDRENV) bounces it and unmap copies the result
+   * back below.
+   */
+
+  ret = virtio_map_buf(priv->vdev, buffer, buflen, VIRTIO_DMA_FROM_DEVICE,
+                       &mapped);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
   /* Add the input buffer to the virtqueue, and the cookie as the virtqueue
    * cookie. (virtqueue_get_buffer() will return cookie).
    */
 
   vb.len = buflen;
-  vb.buf = virtio_zalloc_buf(priv->vdev, buflen, 16);
-  if (vb.buf == NULL)
-    {
-      return -ENOMEM;
-    }
+  vb.buf = mapped;
 
   flags = spin_lock_irqsave(&priv->lock);
   ret = virtqueue_add_buffer(vq, &vb, 0, 1, &cookie);
   if (ret < 0)
     {
       spin_unlock_irqrestore(&priv->lock, flags);
-      virtio_free_buf(priv->vdev, vb.buf);
+      virtio_unmap_buf(priv->vdev, buffer, buflen, VIRTIO_DMA_FROM_DEVICE,

Review Comment:
   When `virtqueue_add_buffer()` fails, this unmap copies `buflen` bytes of a 
`kmm_malloc()` bounce that the device never wrote into the caller's buffer; 
virtio-blk now passes 0 on failure, and master frees the buffer here with no 
copy.
   
   Fix: pass 0 instead of `buflen`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to