Skip to content

Commit 5a8bee6

Browse files
ebiedermMiklos Szeredi
authored andcommitted
fuse: in fuse_flush only wait if someone wants the return code
If a fuse filesystem is mounted inside a container, there is a problem during pid namespace destruction. The scenario is: 1. task (a thread in the fuse server, with a fuse file open) starts exiting, does exit_signals(), goes into fuse_flush() -> wait 2. fuse daemon gets killed, tries to wake everyone up 3. task from 1 is stuck because complete_signal() doesn't wake it up, since it has PF_EXITING. The result is that the thread will never be woken up, and pid namespace destruction will block indefinitely. To add insult to injury, nobody is waiting for these return codes, since the pid namespace is being destroyed. To fix this, let's not block on flush operations when the current task has PF_EXITING. This does change the semantics slightly: the wait here is for posix locks to be unlocked, so the task will exit before things are unlocked. To quote Miklos: "remote" posix locks are almost never used due to problems like this, so I think it's safe to do this. Signed-off-by: "Eric W. Biederman" <[email protected]> Signed-off-by: Tycho Andersen <[email protected]> Link: https://lore.kernel.org/all/YrShFXRLtRt6T%2Fj+@risky/ Tested-by: Tycho Andersen <[email protected]> Signed-off-by: Miklos Szeredi <[email protected]>
1 parent 8ed7cb3 commit 5a8bee6

File tree

1 file changed

+63
-26
lines changed

1 file changed

+63
-26
lines changed

fs/fuse/file.c

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/falloc.h>
1919
#include <linux/uio.h>
2020
#include <linux/fs.h>
21+
#include <linux/file.h>
2122

2223
static int fuse_send_open(struct fuse_mount *fm, u64 nodeid,
2324
unsigned int open_flags, int opcode,
@@ -477,48 +478,36 @@ static void fuse_sync_writes(struct inode *inode)
477478
fuse_release_nowrite(inode);
478479
}
479480

480-
static int fuse_flush(struct file *file, fl_owner_t id)
481-
{
482-
struct inode *inode = file_inode(file);
483-
struct fuse_mount *fm = get_fuse_mount(inode);
484-
struct fuse_file *ff = file->private_data;
481+
struct fuse_flush_args {
482+
struct fuse_args args;
485483
struct fuse_flush_in inarg;
486-
FUSE_ARGS(args);
487-
int err;
488-
489-
if (fuse_is_bad(inode))
490-
return -EIO;
484+
struct work_struct work;
485+
struct file *file;
486+
};
491487

492-
if (ff->open_flags & FOPEN_NOFLUSH && !fm->fc->writeback_cache)
493-
return 0;
488+
static int fuse_do_flush(struct fuse_flush_args *fa)
489+
{
490+
int err;
491+
struct inode *inode = file_inode(fa->file);
492+
struct fuse_mount *fm = get_fuse_mount(inode);
494493

495494
err = write_inode_now(inode, 1);
496495
if (err)
497-
return err;
496+
goto out;
498497

499498
inode_lock(inode);
500499
fuse_sync_writes(inode);
501500
inode_unlock(inode);
502501

503-
err = filemap_check_errors(file->f_mapping);
502+
err = filemap_check_errors(fa->file->f_mapping);
504503
if (err)
505-
return err;
504+
goto out;
506505

507506
err = 0;
508507
if (fm->fc->no_flush)
509508
goto inval_attr_out;
510509

511-
memset(&inarg, 0, sizeof(inarg));
512-
inarg.fh = ff->fh;
513-
inarg.lock_owner = fuse_lock_owner_id(fm->fc, id);
514-
args.opcode = FUSE_FLUSH;
515-
args.nodeid = get_node_id(inode);
516-
args.in_numargs = 1;
517-
args.in_args[0].size = sizeof(inarg);
518-
args.in_args[0].value = &inarg;
519-
args.force = true;
520-
521-
err = fuse_simple_request(fm, &args);
510+
err = fuse_simple_request(fm, &fa->args);
522511
if (err == -ENOSYS) {
523512
fm->fc->no_flush = 1;
524513
err = 0;
@@ -531,9 +520,57 @@ static int fuse_flush(struct file *file, fl_owner_t id)
531520
*/
532521
if (!err && fm->fc->writeback_cache)
533522
fuse_invalidate_attr_mask(inode, STATX_BLOCKS);
523+
524+
out:
525+
fput(fa->file);
526+
kfree(fa);
534527
return err;
535528
}
536529

530+
static void fuse_flush_async(struct work_struct *work)
531+
{
532+
struct fuse_flush_args *fa = container_of(work, typeof(*fa), work);
533+
534+
fuse_do_flush(fa);
535+
}
536+
537+
static int fuse_flush(struct file *file, fl_owner_t id)
538+
{
539+
struct fuse_flush_args *fa;
540+
struct inode *inode = file_inode(file);
541+
struct fuse_mount *fm = get_fuse_mount(inode);
542+
struct fuse_file *ff = file->private_data;
543+
544+
if (fuse_is_bad(inode))
545+
return -EIO;
546+
547+
if (ff->open_flags & FOPEN_NOFLUSH && !fm->fc->writeback_cache)
548+
return 0;
549+
550+
fa = kzalloc(sizeof(*fa), GFP_KERNEL);
551+
if (!fa)
552+
return -ENOMEM;
553+
554+
fa->inarg.fh = ff->fh;
555+
fa->inarg.lock_owner = fuse_lock_owner_id(fm->fc, id);
556+
fa->args.opcode = FUSE_FLUSH;
557+
fa->args.nodeid = get_node_id(inode);
558+
fa->args.in_numargs = 1;
559+
fa->args.in_args[0].size = sizeof(fa->inarg);
560+
fa->args.in_args[0].value = &fa->inarg;
561+
fa->args.force = true;
562+
fa->file = get_file(file);
563+
564+
/* Don't wait if the task is exiting */
565+
if (current->flags & PF_EXITING) {
566+
INIT_WORK(&fa->work, fuse_flush_async);
567+
schedule_work(&fa->work);
568+
return 0;
569+
}
570+
571+
return fuse_do_flush(fa);
572+
}
573+
537574
int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
538575
int datasync, int opcode)
539576
{

0 commit comments

Comments
 (0)