Skip to content

Commit 86b74eb

Browse files
Hou TaoMiklos Szeredi
authored andcommitted
virtiofs: use GFP_NOFS when enqueuing request through kworker
When invoking virtio_fs_enqueue_req() through kworker, both the allocation of the sg array and the bounce buffer still use GFP_ATOMIC. Considering the size of the sg array may be greater than PAGE_SIZE, use GFP_NOFS instead of GFP_ATOMIC to lower the possibility of memory allocation failure and to avoid unnecessarily depleting the atomic reserves. GFP_NOFS is not passed to virtio_fs_enqueue_req() directly, GFP_KERNEL and memalloc_nofs_{save|restore} helpers are used instead. It may seem OK to pass GFP_NOFS to virtio_fs_enqueue_req() as well when queuing the request for the first time, but this is not the case. The reason is that fuse_request_queue_background() may call ->queue_request_and_unlock() while holding fc->bg_lock, which is a spin-lock. Therefore, still use GFP_ATOMIC for it. Signed-off-by: Hou Tao <[email protected]> Reviewed-by: Jingbo Xu <[email protected]> Signed-off-by: Miklos Szeredi <[email protected]>
1 parent 4174867 commit 86b74eb

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

fs/fuse/virtio_fs.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ struct virtio_fs_req_work {
9797
};
9898

9999
static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
100-
struct fuse_req *req, bool in_flight);
100+
struct fuse_req *req, bool in_flight,
101+
gfp_t gfp);
101102

102103
static const struct constant_table dax_param_enums[] = {
103104
{"always", FUSE_DAX_ALWAYS },
@@ -575,6 +576,8 @@ static void virtio_fs_request_dispatch_work(struct work_struct *work)
575576

576577
/* Dispatch pending requests */
577578
while (1) {
579+
unsigned int flags;
580+
578581
spin_lock(&fsvq->lock);
579582
req = list_first_entry_or_null(&fsvq->queued_reqs,
580583
struct fuse_req, list);
@@ -585,7 +588,9 @@ static void virtio_fs_request_dispatch_work(struct work_struct *work)
585588
list_del_init(&req->list);
586589
spin_unlock(&fsvq->lock);
587590

588-
ret = virtio_fs_enqueue_req(fsvq, req, true);
591+
flags = memalloc_nofs_save();
592+
ret = virtio_fs_enqueue_req(fsvq, req, true, GFP_KERNEL);
593+
memalloc_nofs_restore(flags);
589594
if (ret < 0) {
590595
if (ret == -ENOSPC) {
591596
spin_lock(&fsvq->lock);
@@ -686,7 +691,7 @@ static void virtio_fs_hiprio_dispatch_work(struct work_struct *work)
686691
}
687692

688693
/* Allocate and copy args into req->argbuf */
689-
static int copy_args_to_argbuf(struct fuse_req *req)
694+
static int copy_args_to_argbuf(struct fuse_req *req, gfp_t gfp)
690695
{
691696
struct fuse_args *args = req->args;
692697
unsigned int offset = 0;
@@ -700,7 +705,7 @@ static int copy_args_to_argbuf(struct fuse_req *req)
700705
len = fuse_len_args(num_in, (struct fuse_arg *) args->in_args) +
701706
fuse_len_args(num_out, args->out_args);
702707

703-
req->argbuf = kmalloc(len, GFP_ATOMIC);
708+
req->argbuf = kmalloc(len, gfp);
704709
if (!req->argbuf)
705710
return -ENOMEM;
706711

@@ -1366,7 +1371,8 @@ static unsigned int sg_init_fuse_args(struct scatterlist *sg,
13661371

13671372
/* Add a request to a virtqueue and kick the device */
13681373
static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
1369-
struct fuse_req *req, bool in_flight)
1374+
struct fuse_req *req, bool in_flight,
1375+
gfp_t gfp)
13701376
{
13711377
/* requests need at least 4 elements */
13721378
struct scatterlist *stack_sgs[6];
@@ -1387,16 +1393,16 @@ static int virtio_fs_enqueue_req(struct virtio_fs_vq *fsvq,
13871393
/* Does the sglist fit on the stack? */
13881394
total_sgs = sg_count_fuse_req(req);
13891395
if (total_sgs > ARRAY_SIZE(stack_sgs)) {
1390-
sgs = kmalloc_array(total_sgs, sizeof(sgs[0]), GFP_ATOMIC);
1391-
sg = kmalloc_array(total_sgs, sizeof(sg[0]), GFP_ATOMIC);
1396+
sgs = kmalloc_array(total_sgs, sizeof(sgs[0]), gfp);
1397+
sg = kmalloc_array(total_sgs, sizeof(sg[0]), gfp);
13921398
if (!sgs || !sg) {
13931399
ret = -ENOMEM;
13941400
goto out;
13951401
}
13961402
}
13971403

13981404
/* Use a bounce buffer since stack args cannot be mapped */
1399-
ret = copy_args_to_argbuf(req);
1405+
ret = copy_args_to_argbuf(req, gfp);
14001406
if (ret < 0)
14011407
goto out;
14021408

@@ -1490,7 +1496,7 @@ static void virtio_fs_send_req(struct fuse_iqueue *fiq, struct fuse_req *req)
14901496
queue_id);
14911497

14921498
fsvq = &fs->vqs[queue_id];
1493-
ret = virtio_fs_enqueue_req(fsvq, req, false);
1499+
ret = virtio_fs_enqueue_req(fsvq, req, false, GFP_ATOMIC);
14941500
if (ret < 0) {
14951501
if (ret == -ENOSPC) {
14961502
/*

0 commit comments

Comments
 (0)