diff options
Diffstat (limited to 'net/9p/trans_virtio.c')
| -rw-r--r-- | net/9p/trans_virtio.c | 87 |
1 files changed, 57 insertions, 30 deletions
diff --git a/net/9p/trans_virtio.c b/net/9p/trans_virtio.c index 9b550ed9c711..244e70742183 100644 --- a/net/9p/trans_virtio.c +++ b/net/9p/trans_virtio.c | |||
| @@ -43,6 +43,7 @@ | |||
| 43 | #include <net/9p/client.h> | 43 | #include <net/9p/client.h> |
| 44 | #include <net/9p/transport.h> | 44 | #include <net/9p/transport.h> |
| 45 | #include <linux/scatterlist.h> | 45 | #include <linux/scatterlist.h> |
| 46 | #include <linux/swap.h> | ||
| 46 | #include <linux/virtio.h> | 47 | #include <linux/virtio.h> |
| 47 | #include <linux/virtio_9p.h> | 48 | #include <linux/virtio_9p.h> |
| 48 | #include "trans_common.h" | 49 | #include "trans_common.h" |
| @@ -51,6 +52,8 @@ | |||
| 51 | 52 | ||
| 52 | /* a single mutex to manage channel initialization and attachment */ | 53 | /* a single mutex to manage channel initialization and attachment */ |
| 53 | static DEFINE_MUTEX(virtio_9p_lock); | 54 | static DEFINE_MUTEX(virtio_9p_lock); |
| 55 | static DECLARE_WAIT_QUEUE_HEAD(vp_wq); | ||
| 56 | static atomic_t vp_pinned = ATOMIC_INIT(0); | ||
| 54 | 57 | ||
| 55 | /** | 58 | /** |
| 56 | * struct virtio_chan - per-instance transport information | 59 | * struct virtio_chan - per-instance transport information |
| @@ -78,7 +81,10 @@ struct virtio_chan { | |||
| 78 | struct virtqueue *vq; | 81 | struct virtqueue *vq; |
| 79 | int ring_bufs_avail; | 82 | int ring_bufs_avail; |
| 80 | wait_queue_head_t *vc_wq; | 83 | wait_queue_head_t *vc_wq; |
| 81 | 84 | /* This is global limit. Since we don't have a global structure, | |
| 85 | * will be placing it in each channel. | ||
| 86 | */ | ||
| 87 | int p9_max_pages; | ||
| 82 | /* Scatterlist: can be too big for stack. */ | 88 | /* Scatterlist: can be too big for stack. */ |
| 83 | struct scatterlist sg[VIRTQUEUE_NUM]; | 89 | struct scatterlist sg[VIRTQUEUE_NUM]; |
| 84 | 90 | ||
| @@ -141,34 +147,36 @@ static void req_done(struct virtqueue *vq) | |||
| 141 | 147 | ||
| 142 | P9_DPRINTK(P9_DEBUG_TRANS, ": request done\n"); | 148 | P9_DPRINTK(P9_DEBUG_TRANS, ": request done\n"); |
| 143 | 149 | ||
| 144 | do { | 150 | while (1) { |
| 145 | spin_lock_irqsave(&chan->lock, flags); | 151 | spin_lock_irqsave(&chan->lock, flags); |
| 146 | rc = virtqueue_get_buf(chan->vq, &len); | 152 | rc = virtqueue_get_buf(chan->vq, &len); |
| 147 | 153 | ||
| 148 | if (rc != NULL) { | 154 | if (rc == NULL) { |
| 149 | if (!chan->ring_bufs_avail) { | ||
| 150 | chan->ring_bufs_avail = 1; | ||
| 151 | wake_up(chan->vc_wq); | ||
| 152 | } | ||
| 153 | spin_unlock_irqrestore(&chan->lock, flags); | ||
| 154 | P9_DPRINTK(P9_DEBUG_TRANS, ": rc %p\n", rc); | ||
| 155 | P9_DPRINTK(P9_DEBUG_TRANS, ": lookup tag %d\n", | ||
| 156 | rc->tag); | ||
| 157 | req = p9_tag_lookup(chan->client, rc->tag); | ||
| 158 | req->status = REQ_STATUS_RCVD; | ||
| 159 | if (req->tc->private) { | ||
| 160 | struct trans_rpage_info *rp = req->tc->private; | ||
| 161 | /*Release pages */ | ||
| 162 | p9_release_req_pages(rp); | ||
| 163 | if (rp->rp_alloc) | ||
| 164 | kfree(rp); | ||
| 165 | req->tc->private = NULL; | ||
| 166 | } | ||
| 167 | p9_client_cb(chan->client, req); | ||
| 168 | } else { | ||
| 169 | spin_unlock_irqrestore(&chan->lock, flags); | 155 | spin_unlock_irqrestore(&chan->lock, flags); |
| 156 | break; | ||
| 157 | } | ||
| 158 | |||
| 159 | chan->ring_bufs_avail = 1; | ||
| 160 | spin_unlock_irqrestore(&chan->lock, flags); | ||
| 161 | /* Wakeup if anyone waiting for VirtIO ring space. */ | ||
| 162 | wake_up(chan->vc_wq); | ||
| 163 | P9_DPRINTK(P9_DEBUG_TRANS, ": rc %p\n", rc); | ||
| 164 | P9_DPRINTK(P9_DEBUG_TRANS, ": lookup tag %d\n", rc->tag); | ||
| 165 | req = p9_tag_lookup(chan->client, rc->tag); | ||
| 166 | if (req->tc->private) { | ||
| 167 | struct trans_rpage_info *rp = req->tc->private; | ||
| 168 | int p = rp->rp_nr_pages; | ||
| 169 | /*Release pages */ | ||
| 170 | p9_release_req_pages(rp); | ||
| 171 | atomic_sub(p, &vp_pinned); | ||
| 172 | wake_up(&vp_wq); | ||
| 173 | if (rp->rp_alloc) | ||
| 174 | kfree(rp); | ||
| 175 | req->tc->private = NULL; | ||
| 170 | } | 176 | } |
| 171 | } while (rc != NULL); | 177 | req->status = REQ_STATUS_RCVD; |
| 178 | p9_client_cb(chan->client, req); | ||
| 179 | } | ||
| 172 | } | 180 | } |
| 173 | 181 | ||
| 174 | /** | 182 | /** |
| @@ -263,7 +271,6 @@ p9_virtio_request(struct p9_client *client, struct p9_req_t *req) | |||
| 263 | 271 | ||
| 264 | P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request\n"); | 272 | P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request\n"); |
| 265 | 273 | ||
| 266 | req_retry: | ||
| 267 | req->status = REQ_STATUS_SENT; | 274 | req->status = REQ_STATUS_SENT; |
| 268 | 275 | ||
| 269 | if (req->tc->pbuf_size && (req->tc->pubuf && P9_IS_USER_CONTEXT)) { | 276 | if (req->tc->pbuf_size && (req->tc->pubuf && P9_IS_USER_CONTEXT)) { |
| @@ -271,6 +278,14 @@ req_retry: | |||
| 271 | int rpinfo_size = sizeof(struct trans_rpage_info) + | 278 | int rpinfo_size = sizeof(struct trans_rpage_info) + |
| 272 | sizeof(struct page *) * nr_pages; | 279 | sizeof(struct page *) * nr_pages; |
| 273 | 280 | ||
| 281 | if (atomic_read(&vp_pinned) >= chan->p9_max_pages) { | ||
| 282 | err = wait_event_interruptible(vp_wq, | ||
| 283 | atomic_read(&vp_pinned) < chan->p9_max_pages); | ||
| 284 | if (err == -ERESTARTSYS) | ||
| 285 | return err; | ||
| 286 | P9_DPRINTK(P9_DEBUG_TRANS, "9p: May gup pages now.\n"); | ||
| 287 | } | ||
| 288 | |||
| 274 | if (rpinfo_size <= (req->tc->capacity - req->tc->size)) { | 289 | if (rpinfo_size <= (req->tc->capacity - req->tc->size)) { |
| 275 | /* We can use sdata */ | 290 | /* We can use sdata */ |
| 276 | req->tc->private = req->tc->sdata + req->tc->size; | 291 | req->tc->private = req->tc->sdata + req->tc->size; |
| @@ -293,9 +308,12 @@ req_retry: | |||
| 293 | if (rpinfo->rp_alloc) | 308 | if (rpinfo->rp_alloc) |
| 294 | kfree(rpinfo); | 309 | kfree(rpinfo); |
| 295 | return err; | 310 | return err; |
| 311 | } else { | ||
| 312 | atomic_add(rpinfo->rp_nr_pages, &vp_pinned); | ||
| 296 | } | 313 | } |
| 297 | } | 314 | } |
| 298 | 315 | ||
| 316 | req_retry_pinned: | ||
| 299 | spin_lock_irqsave(&chan->lock, flags); | 317 | spin_lock_irqsave(&chan->lock, flags); |
| 300 | 318 | ||
| 301 | /* Handle out VirtIO ring buffers */ | 319 | /* Handle out VirtIO ring buffers */ |
| @@ -308,8 +326,11 @@ req_retry: | |||
| 308 | outp = pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM, | 326 | outp = pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM, |
| 309 | pdata_off, rpinfo->rp_data, pdata_len); | 327 | pdata_off, rpinfo->rp_data, pdata_len); |
| 310 | } else { | 328 | } else { |
| 311 | char *pbuf = req->tc->pubuf ? req->tc->pubuf : | 329 | char *pbuf; |
| 312 | req->tc->pkbuf; | 330 | if (req->tc->pubuf) |
| 331 | pbuf = (__force char *) req->tc->pubuf; | ||
| 332 | else | ||
| 333 | pbuf = req->tc->pkbuf; | ||
| 313 | outp = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM, pbuf, | 334 | outp = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM, pbuf, |
| 314 | req->tc->pbuf_size); | 335 | req->tc->pbuf_size); |
| 315 | } | 336 | } |
| @@ -334,8 +355,12 @@ req_retry: | |||
| 334 | in = pack_sg_list_p(chan->sg, out+inp, VIRTQUEUE_NUM, | 355 | in = pack_sg_list_p(chan->sg, out+inp, VIRTQUEUE_NUM, |
| 335 | pdata_off, rpinfo->rp_data, pdata_len); | 356 | pdata_off, rpinfo->rp_data, pdata_len); |
| 336 | } else { | 357 | } else { |
| 337 | char *pbuf = req->tc->pubuf ? req->tc->pubuf : | 358 | char *pbuf; |
| 338 | req->tc->pkbuf; | 359 | if (req->tc->pubuf) |
| 360 | pbuf = (__force char *) req->tc->pubuf; | ||
| 361 | else | ||
| 362 | pbuf = req->tc->pkbuf; | ||
| 363 | |||
| 339 | in = pack_sg_list(chan->sg, out+inp, VIRTQUEUE_NUM, | 364 | in = pack_sg_list(chan->sg, out+inp, VIRTQUEUE_NUM, |
| 340 | pbuf, req->tc->pbuf_size); | 365 | pbuf, req->tc->pbuf_size); |
| 341 | } | 366 | } |
| @@ -356,7 +381,7 @@ req_retry: | |||
| 356 | return err; | 381 | return err; |
| 357 | 382 | ||
| 358 | P9_DPRINTK(P9_DEBUG_TRANS, "9p:Retry virtio request\n"); | 383 | P9_DPRINTK(P9_DEBUG_TRANS, "9p:Retry virtio request\n"); |
| 359 | goto req_retry; | 384 | goto req_retry_pinned; |
| 360 | } else { | 385 | } else { |
| 361 | spin_unlock_irqrestore(&chan->lock, flags); | 386 | spin_unlock_irqrestore(&chan->lock, flags); |
| 362 | P9_DPRINTK(P9_DEBUG_TRANS, | 387 | P9_DPRINTK(P9_DEBUG_TRANS, |
| @@ -453,6 +478,8 @@ static int p9_virtio_probe(struct virtio_device *vdev) | |||
| 453 | } | 478 | } |
| 454 | init_waitqueue_head(chan->vc_wq); | 479 | init_waitqueue_head(chan->vc_wq); |
| 455 | chan->ring_bufs_avail = 1; | 480 | chan->ring_bufs_avail = 1; |
| 481 | /* Ceiling limit to avoid denial of service attacks */ | ||
| 482 | chan->p9_max_pages = nr_free_buffer_pages()/4; | ||
| 456 | 483 | ||
| 457 | mutex_lock(&virtio_9p_lock); | 484 | mutex_lock(&virtio_9p_lock); |
| 458 | list_add_tail(&chan->chan_list, &virtio_chan_list); | 485 | list_add_tail(&chan->chan_list, &virtio_chan_list); |
