BACKPORT: drm/virtio: Use vmalloc for command buffer allocations.

Userspace requested command buffer allocations could be too large
to make as a contiguous allocation.  Use vmalloc if necessary to
satisfy those allocations.

Signed-off-by: David Riley <davidriley@chromium.org>
Link: http://patchwork.freedesktop.org/patch/msgid/20190911181403.40909-3-davidriley@chromium.org
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>

Bug: https://crbug.com/993452
Change-Id: I253a9eb22fdee4836d48abecbe836ee5369e6526
(cherry picked from commit e1218b8c0cc1f8108be67ba3783d63eb4a50d792)
[kaiyili: Resolved minor conflict in drivers/gpu/drm/virtio/virtgpu_vq.c ]
Signed-off-by: Kaiyi Li <kaiyili@google.com>
This commit is contained in:
David Riley 2019-09-10 13:06:51 -07:00 committed by Kaiyi Li
parent 7c9d656e58
commit 5ba052a725
2 changed files with 73 additions and 10 deletions

View file

@ -195,7 +195,7 @@ static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
if (ret) if (ret)
goto out_free; goto out_free;
buf = memdup_user(u64_to_user_ptr(exbuf->command), exbuf->size); buf = vmemdup_user(u64_to_user_ptr(exbuf->command), exbuf->size);
if (IS_ERR(buf)) { if (IS_ERR(buf)) {
ret = PTR_ERR(buf); ret = PTR_ERR(buf);
goto out_unresv; goto out_unresv;
@ -230,7 +230,7 @@ static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
return 0; return 0;
out_memdup: out_memdup:
kfree(buf); kvfree(buf);
out_unresv: out_unresv:
ttm_eu_backoff_reservation(&ticket, &validate_list); ttm_eu_backoff_reservation(&ticket, &validate_list);
out_free: out_free:

View file

@ -154,7 +154,7 @@ static void free_vbuf(struct virtio_gpu_device *vgdev,
{ {
if (vbuf->resp_size > MAX_INLINE_RESP_SIZE) if (vbuf->resp_size > MAX_INLINE_RESP_SIZE)
kfree(vbuf->resp_buf); kfree(vbuf->resp_buf);
kfree(vbuf->data_buf); kvfree(vbuf->data_buf);
kmem_cache_free(vgdev->vbufs, vbuf); kmem_cache_free(vgdev->vbufs, vbuf);
} }
@ -251,13 +251,54 @@ void virtio_gpu_dequeue_cursor_func(struct work_struct *work)
wake_up(&vgdev->cursorq.ack_queue); wake_up(&vgdev->cursorq.ack_queue);
} }
/* Create sg_table from a vmalloc'd buffer. */
static struct sg_table *vmalloc_to_sgt(char *data, uint32_t size, int *sg_ents)
{
int ret, s, i;
struct sg_table *sgt;
struct scatterlist *sg;
struct page *pg;
if (WARN_ON(!PAGE_ALIGNED(data)))
return NULL;
sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
if (!sgt)
return NULL;
*sg_ents = DIV_ROUND_UP(size, PAGE_SIZE);
ret = sg_alloc_table(sgt, *sg_ents, GFP_KERNEL);
if (ret) {
kfree(sgt);
return NULL;
}
for_each_sg(sgt->sgl, sg, *sg_ents, i) {
pg = vmalloc_to_page(data);
if (!pg) {
sg_free_table(sgt);
kfree(sgt);
return NULL;
}
s = min_t(int, PAGE_SIZE, size);
sg_set_page(sg, pg, s, 0);
size -= s;
data += s;
}
return sgt;
}
static int virtio_gpu_queue_ctrl_buffer_locked(struct virtio_gpu_device *vgdev, static int virtio_gpu_queue_ctrl_buffer_locked(struct virtio_gpu_device *vgdev,
struct virtio_gpu_vbuffer *vbuf) struct virtio_gpu_vbuffer *vbuf,
struct scatterlist *vout)
__releases(&vgdev->ctrlq.qlock) __releases(&vgdev->ctrlq.qlock)
__acquires(&vgdev->ctrlq.qlock) __acquires(&vgdev->ctrlq.qlock)
{ {
struct virtqueue *vq = vgdev->ctrlq.vq; struct virtqueue *vq = vgdev->ctrlq.vq;
struct scatterlist *sgs[3], vcmd, vout, vresp; struct scatterlist *sgs[3], vcmd, vresp;
int outcnt = 0, incnt = 0; int outcnt = 0, incnt = 0;
int ret; int ret;
@ -268,9 +309,8 @@ static int virtio_gpu_queue_ctrl_buffer_locked(struct virtio_gpu_device *vgdev,
sgs[outcnt + incnt] = &vcmd; sgs[outcnt + incnt] = &vcmd;
outcnt++; outcnt++;
if (vbuf->data_size) { if (vout) {
sg_init_one(&vout, vbuf->data_buf, vbuf->data_size); sgs[outcnt + incnt] = vout;
sgs[outcnt + incnt] = &vout;
outcnt++; outcnt++;
} }
@ -305,7 +345,24 @@ static int virtio_gpu_queue_fenced_ctrl_buffer(struct virtio_gpu_device *vgdev,
struct virtio_gpu_fence *fence) struct virtio_gpu_fence *fence)
{ {
struct virtqueue *vq = vgdev->ctrlq.vq; struct virtqueue *vq = vgdev->ctrlq.vq;
struct scatterlist *vout = NULL, sg;
struct sg_table *sgt = NULL;
int rc; int rc;
int outcnt = 0;
if (vbuf->data_size) {
if (is_vmalloc_addr(vbuf->data_buf)) {
sgt = vmalloc_to_sgt(vbuf->data_buf, vbuf->data_size,
&outcnt);
if (!sgt)
return -ENOMEM;
vout = sgt->sgl;
} else {
sg_init_one(&sg, vbuf->data_buf, vbuf->data_size);
vout = &sg;
outcnt = 1;
}
}
again: again:
spin_lock(&vgdev->ctrlq.qlock); spin_lock(&vgdev->ctrlq.qlock);
@ -318,7 +375,7 @@ again:
* to wait for free space, which can result in fence ids being * to wait for free space, which can result in fence ids being
* submitted out-of-order. * submitted out-of-order.
*/ */
if (vq->num_free < 3) { if (vq->num_free < 2 + outcnt) {
spin_unlock(&vgdev->ctrlq.qlock); spin_unlock(&vgdev->ctrlq.qlock);
wait_event(vgdev->ctrlq.ack_queue, vq->num_free >= 3); wait_event(vgdev->ctrlq.ack_queue, vq->num_free >= 3);
goto again; goto again;
@ -326,8 +383,14 @@ again:
if (hdr && fence) if (hdr && fence)
virtio_gpu_fence_emit(vgdev, hdr, fence); virtio_gpu_fence_emit(vgdev, hdr, fence);
rc = virtio_gpu_queue_ctrl_buffer_locked(vgdev, vbuf); rc = virtio_gpu_queue_ctrl_buffer_locked(vgdev, vbuf, vout);
spin_unlock(&vgdev->ctrlq.qlock); spin_unlock(&vgdev->ctrlq.qlock);
if (sgt) {
sg_free_table(sgt);
kfree(sgt);
}
return rc; return rc;
} }