提交 bbd603ef 编写于 作者: M Michael S. Tsirkin 提交者: Rusty Russell

virtio: add_buf_gfp

Add an add_buf variant that gets gfp parameter. Use that
to allocate indirect buffers.
Signed-off-by: NMichael S. Tsirkin <mst@redhat.com>
Signed-off-by: NRusty Russell <rusty@rustcorp.com.au>
上级 dc3f5e68
...@@ -110,13 +110,14 @@ struct vring_virtqueue ...@@ -110,13 +110,14 @@ struct vring_virtqueue
static int vring_add_indirect(struct vring_virtqueue *vq, static int vring_add_indirect(struct vring_virtqueue *vq,
struct scatterlist sg[], struct scatterlist sg[],
unsigned int out, unsigned int out,
unsigned int in) unsigned int in,
gfp_t gfp)
{ {
struct vring_desc *desc; struct vring_desc *desc;
unsigned head; unsigned head;
int i; int i;
desc = kmalloc((out + in) * sizeof(struct vring_desc), GFP_ATOMIC); desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
if (!desc) if (!desc)
return vq->vring.num; return vq->vring.num;
...@@ -155,11 +156,12 @@ static int vring_add_indirect(struct vring_virtqueue *vq, ...@@ -155,11 +156,12 @@ static int vring_add_indirect(struct vring_virtqueue *vq,
return head; return head;
} }
int virtqueue_add_buf(struct virtqueue *_vq, int virtqueue_add_buf_gfp(struct virtqueue *_vq,
struct scatterlist sg[], struct scatterlist sg[],
unsigned int out, unsigned int out,
unsigned int in, unsigned int in,
void *data) void *data,
gfp_t gfp)
{ {
struct vring_virtqueue *vq = to_vvq(_vq); struct vring_virtqueue *vq = to_vvq(_vq);
unsigned int i, avail, head, uninitialized_var(prev); unsigned int i, avail, head, uninitialized_var(prev);
...@@ -171,7 +173,7 @@ int virtqueue_add_buf(struct virtqueue *_vq, ...@@ -171,7 +173,7 @@ int virtqueue_add_buf(struct virtqueue *_vq,
/* If the host supports indirect descriptor tables, and we have multiple /* If the host supports indirect descriptor tables, and we have multiple
* buffers, then go indirect. FIXME: tune this threshold */ * buffers, then go indirect. FIXME: tune this threshold */
if (vq->indirect && (out + in) > 1 && vq->num_free) { if (vq->indirect && (out + in) > 1 && vq->num_free) {
head = vring_add_indirect(vq, sg, out, in); head = vring_add_indirect(vq, sg, out, in, gfp);
if (head != vq->vring.num) if (head != vq->vring.num)
goto add_head; goto add_head;
} }
...@@ -232,7 +234,7 @@ int virtqueue_add_buf(struct virtqueue *_vq, ...@@ -232,7 +234,7 @@ int virtqueue_add_buf(struct virtqueue *_vq,
return vq->num_free ? vq->vring.num : 0; return vq->num_free ? vq->vring.num : 0;
return vq->num_free; return vq->num_free;
} }
EXPORT_SYMBOL_GPL(virtqueue_add_buf); EXPORT_SYMBOL_GPL(virtqueue_add_buf_gfp);
void virtqueue_kick(struct virtqueue *_vq) void virtqueue_kick(struct virtqueue *_vq)
{ {
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <linux/device.h> #include <linux/device.h>
#include <linux/mod_devicetable.h> #include <linux/mod_devicetable.h>
#include <linux/gfp.h>
/** /**
* virtqueue - a queue to register buffers for sending or receiving. * virtqueue - a queue to register buffers for sending or receiving.
...@@ -32,6 +33,7 @@ struct virtqueue { ...@@ -32,6 +33,7 @@ struct virtqueue {
* out_num: the number of sg readable by other side * out_num: the number of sg readable by other side
* in_num: the number of sg which are writable (after readable ones) * in_num: the number of sg which are writable (after readable ones)
* data: the token identifying the buffer. * data: the token identifying the buffer.
* gfp: how to do memory allocations (if necessary).
* Returns remaining capacity of queue (sg segments) or a negative error. * Returns remaining capacity of queue (sg segments) or a negative error.
* virtqueue_kick: update after add_buf * virtqueue_kick: update after add_buf
* vq: the struct virtqueue * vq: the struct virtqueue
...@@ -60,11 +62,21 @@ struct virtqueue { ...@@ -60,11 +62,21 @@ struct virtqueue {
* All operations can be called in any context. * All operations can be called in any context.
*/ */
int virtqueue_add_buf(struct virtqueue *vq, int virtqueue_add_buf_gfp(struct virtqueue *vq,
struct scatterlist sg[], struct scatterlist sg[],
unsigned int out_num, unsigned int out_num,
unsigned int in_num, unsigned int in_num,
void *data); void *data,
gfp_t gfp);
static inline int virtqueue_add_buf(struct virtqueue *vq,
struct scatterlist sg[],
unsigned int out_num,
unsigned int in_num,
void *data)
{
return virtqueue_add_buf_gfp(vq, sg, out_num, in_num, data, GFP_ATOMIC);
}
void virtqueue_kick(struct virtqueue *vq); void virtqueue_kick(struct virtqueue *vq);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册