Allocate struct fuse_dev when opening the device. This means that unlike before, ->private_data is always set to a valid pointer. The use of USE_DEV_SYNC_INIT magic pointer for the private_data is now replaced with a simple bool sync_init member. If sync INIT is not set, I/O on the device returns error before mount. Keep this behavior by checking for the ->fc member. If fud->fc is set, the mount has succeeded. Testing this used READ_ONCE(file->private_data) and smp_mb() to try and provide the necessary semantics. Switch this to smp_store_release() and smp_load_acquire(). Setting fud->fc is protected by fuse_mutex, this is unchanged. Will need this later so the /dev/fuse open file reference is not held during FSCONFIG_CMD_CREATE. Signed-off-by: Miklos Szeredi --- fs/fuse/dev.c | 47 +++++++++++++++++--------------------------- fs/fuse/fuse_dev_i.h | 33 +++++++++++++++++++++++-------- fs/fuse/fuse_i.h | 5 ++--- fs/fuse/inode.c | 35 +++++++++++---------------------- fs/fuse/virtio_fs.c | 2 -- 5 files changed, 56 insertions(+), 66 deletions(-) diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c index 2c16b94357d5..4795e3d01b75 100644 --- a/fs/fuse/dev.c +++ b/fs/fuse/dev.c @@ -1542,32 +1542,24 @@ static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file, static int fuse_dev_open(struct inode *inode, struct file *file) { - /* - * The fuse device's file's private_data is used to hold - * the fuse_conn(ection) when it is mounted, and is used to - * keep track of whether the file has been mounted already. - */ - file->private_data = NULL; + struct fuse_dev *fud = fuse_dev_alloc(); + + if (!fud) + return -ENOMEM; + + file->private_data = fud; return 0; } struct fuse_dev *fuse_get_dev(struct file *file) { - struct fuse_dev *fud = __fuse_get_dev(file); + struct fuse_dev *fud = fuse_file_to_fud(file); int err; - if (likely(fud)) - return fud; - - err = wait_event_interruptible(fuse_dev_waitq, - READ_ONCE(file->private_data) != FUSE_DEV_SYNC_INIT); + err = wait_event_interruptible(fuse_dev_waitq, fuse_dev_fc_get(fud)); if (err) return ERR_PTR(err); - fud = __fuse_get_dev(file); - if (!fud) - return ERR_PTR(-EPERM); - return fud; } @@ -2534,10 +2526,10 @@ void fuse_wait_aborted(struct fuse_conn *fc) int fuse_dev_release(struct inode *inode, struct file *file) { - struct fuse_dev *fud = __fuse_get_dev(file); + struct fuse_dev *fud = fuse_file_to_fud(file); + struct fuse_conn *fc = fuse_dev_fc_get(fud); - if (fud) { - struct fuse_conn *fc = fud->fc; + if (fc) { struct fuse_pqueue *fpq = &fud->pq; LIST_HEAD(to_end); unsigned int i; @@ -2555,8 +2547,8 @@ int fuse_dev_release(struct inode *inode, struct file *file) WARN_ON(fc->iq.fasync != NULL); fuse_abort_conn(fc); } - fuse_dev_free(fud); } + fuse_dev_free(fud); return 0; } EXPORT_SYMBOL_GPL(fuse_dev_release); @@ -2574,16 +2566,12 @@ static int fuse_dev_fasync(int fd, struct file *file, int on) static int fuse_device_clone(struct fuse_conn *fc, struct file *new) { - struct fuse_dev *fud; + struct fuse_dev *new_fud = fuse_file_to_fud(new); - if (__fuse_get_dev(new)) + if (fuse_dev_fc_get(new_fud)) return -EINVAL; - fud = fuse_dev_alloc_install(fc); - if (!fud) - return -ENOMEM; - - new->private_data = fud; + fuse_dev_install(new_fud, fc); atomic_inc(&fc->dev_count); return 0; @@ -2657,10 +2645,11 @@ static long fuse_dev_ioctl_backing_close(struct file *file, __u32 __user *argp) static long fuse_dev_ioctl_sync_init(struct file *file) { int err = -EINVAL; + struct fuse_dev *fud = fuse_file_to_fud(file); mutex_lock(&fuse_mutex); - if (!__fuse_get_dev(file)) { - WRITE_ONCE(file->private_data, FUSE_DEV_SYNC_INIT); + if (!fuse_dev_fc_get(fud)) { + fud->sync_init = true; err = 0; } mutex_unlock(&fuse_mutex); diff --git a/fs/fuse/fuse_dev_i.h b/fs/fuse/fuse_dev_i.h index 134bf44aff0d..522b2012cd1f 100644 --- a/fs/fuse/fuse_dev_i.h +++ b/fs/fuse/fuse_dev_i.h @@ -39,18 +39,35 @@ struct fuse_copy_state { } ring; }; -#define FUSE_DEV_SYNC_INIT ((struct fuse_dev *) 1) -#define FUSE_DEV_PTR_MASK (~1UL) +/* + * Lockless access is OK, because fud->fc is set once during mount and is valid + * until the file is released. + */ +static inline struct fuse_conn *fuse_dev_fc_get(struct fuse_dev *fud) +{ + /* Pairs with smp_store_release() in fuse_dev_fc_set() */ + return smp_load_acquire(&fud->fc); +} + +static inline void fuse_dev_fc_set(struct fuse_dev *fud, struct fuse_conn *fc) +{ + /* Pairs with smp_load_acquire() in fuse_dev_fc_get() */ + smp_store_release(&fud->fc, fc); +} + +static inline struct fuse_dev *fuse_file_to_fud(struct file *file) +{ + return file->private_data; +} static inline struct fuse_dev *__fuse_get_dev(struct file *file) { - /* - * Lockless access is OK, because file->private data is set - * once during mount and is valid until the file is released. - */ - struct fuse_dev *fud = READ_ONCE(file->private_data); + struct fuse_dev *fud = fuse_file_to_fud(file); + + if (!fuse_dev_fc_get(fud)) + return NULL; - return (typeof(fud)) ((unsigned long) fud & FUSE_DEV_PTR_MASK); + return fud; } struct fuse_dev *fuse_get_dev(struct file *file); diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 7f16049387d1..739220d96b6f 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -576,6 +576,8 @@ struct fuse_pqueue { * Fuse device instance */ struct fuse_dev { + bool sync_init; + /** Fuse connection for this device */ struct fuse_conn *fc; @@ -622,9 +624,6 @@ struct fuse_fs_context { /* DAX device, may be NULL */ struct dax_device *dax_dev; - - /* fuse_dev pointer to fill in, should contain NULL on entry */ - void **fudptr; }; struct fuse_sync_bucket { diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c index e57b8af06be9..53663846db36 100644 --- a/fs/fuse/inode.c +++ b/fs/fuse/inode.c @@ -1637,7 +1637,7 @@ EXPORT_SYMBOL_GPL(fuse_dev_alloc); void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc) { - fud->fc = fuse_conn_get(fc); + fuse_dev_fc_set(fud, fuse_conn_get(fc)); spin_lock(&fc->lock); list_add_tail(&fud->entry, &fc->devices); spin_unlock(&fc->lock); @@ -1659,7 +1659,7 @@ EXPORT_SYMBOL_GPL(fuse_dev_alloc_install); void fuse_dev_free(struct fuse_dev *fud) { - struct fuse_conn *fc = fud->fc; + struct fuse_conn *fc = fuse_dev_fc_get(fud); if (fc) { spin_lock(&fc->lock); @@ -1822,7 +1822,7 @@ EXPORT_SYMBOL_GPL(fuse_init_fs_context_submount); int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) { - struct fuse_dev *fud = NULL; + struct fuse_dev *fud = ctx->file ? fuse_file_to_fud(ctx->file) : NULL; struct fuse_mount *fm = get_fuse_mount_super(sb); struct fuse_conn *fc = fm->fc; struct inode *root; @@ -1856,18 +1856,11 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) goto err; } - if (ctx->fudptr) { - err = -ENOMEM; - fud = fuse_dev_alloc_install(fc); - if (!fud) - goto err_free_dax; - } - fc->dev = sb->s_dev; fm->sb = sb; err = fuse_bdi_init(fc, sb); if (err) - goto err_dev_free; + goto err_free_dax; /* Handle umasking inside the fuse code */ if (sb->s_flags & SB_POSIXACL) @@ -1889,15 +1882,15 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) set_default_d_op(sb, &fuse_dentry_operations); root_dentry = d_make_root(root); if (!root_dentry) - goto err_dev_free; + goto err_free_dax; mutex_lock(&fuse_mutex); err = -EINVAL; - if (ctx->fudptr && *ctx->fudptr) { - if (*ctx->fudptr == FUSE_DEV_SYNC_INIT) - fc->sync_init = 1; - else + if (fud) { + if (fuse_dev_fc_get(fud)) goto err_unlock; + if (fud->sync_init) + fc->sync_init = 1; } err = fuse_ctl_add_conn(fc); @@ -1906,8 +1899,8 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) list_add_tail(&fc->entry, &fuse_conn_list); sb->s_root = root_dentry; - if (ctx->fudptr) { - *ctx->fudptr = fud; + if (fud) { + fuse_dev_install(fud, fc); wake_up_all(&fuse_dev_waitq); } mutex_unlock(&fuse_mutex); @@ -1916,9 +1909,6 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) err_unlock: mutex_unlock(&fuse_mutex); dput(root_dentry); - err_dev_free: - if (fud) - fuse_dev_free(fud); err_free_dax: if (IS_ENABLED(CONFIG_FUSE_DAX)) fuse_dax_conn_free(fc); @@ -1944,13 +1934,10 @@ static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc) if ((ctx->file->f_op != &fuse_dev_operations) || (ctx->file->f_cred->user_ns != sb->s_user_ns)) return -EINVAL; - ctx->fudptr = &ctx->file->private_data; err = fuse_fill_super_common(sb, ctx); if (err) return err; - /* file->private_data shall be visible on all CPUs after this */ - smp_mb(); fm = get_fuse_mount_super(sb); diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c index 2f7485ffac52..f685916754ad 100644 --- a/fs/fuse/virtio_fs.c +++ b/fs/fuse/virtio_fs.c @@ -1590,8 +1590,6 @@ static int virtio_fs_fill_super(struct super_block *sb, struct fs_context *fsc) goto err_free_fuse_devs; } - /* virtiofs allocates and installs its own fuse devices */ - ctx->fudptr = NULL; if (ctx->dax_mode != FUSE_DAX_NEVER) { if (ctx->dax_mode == FUSE_DAX_ALWAYS && !fs->dax_dev) { err = -EINVAL; -- 2.53.0 This will make it possible to grab the fuse_dev and subsequently release the file that it came from. In the above case, fud->fc will be set to FUSE_DEV_FC_DISCONNECTED to indicate that this is no longer a functional device. When trying to assign an fc to such a disconnected fuse_dev, the fc is set to the disconnected state. Use atomic operations xchg() and cmpxchg() to prevent races. Signed-off-by: Miklos Szeredi --- fs/fuse/cuse.c | 2 +- fs/fuse/dev.c | 9 +++++++-- fs/fuse/fuse_dev_i.h | 11 ++++------- fs/fuse/fuse_i.h | 6 ++++-- fs/fuse/inode.c | 43 +++++++++++++++++++++++++++++++++++-------- fs/fuse/virtio_fs.c | 2 +- 6 files changed, 52 insertions(+), 21 deletions(-) diff --git a/fs/fuse/cuse.c b/fs/fuse/cuse.c index dfcb98a654d8..174333633471 100644 --- a/fs/fuse/cuse.c +++ b/fs/fuse/cuse.c @@ -527,7 +527,7 @@ static int cuse_channel_open(struct inode *inode, struct file *file) cc->fc.initialized = 1; rc = cuse_send_init(cc); if (rc) { - fuse_dev_free(fud); + fuse_dev_put(fud); return rc; } file->private_data = fud; diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c index 4795e3d01b75..3adf6bd38c9b 100644 --- a/fs/fuse/dev.c +++ b/fs/fuse/dev.c @@ -2527,7 +2527,8 @@ void fuse_wait_aborted(struct fuse_conn *fc) int fuse_dev_release(struct inode *inode, struct file *file) { struct fuse_dev *fud = fuse_file_to_fud(file); - struct fuse_conn *fc = fuse_dev_fc_get(fud); + /* Pairs with cmpxchg() in fuse_dev_install() */ + struct fuse_conn *fc = xchg(&fud->fc, FUSE_DEV_FC_DISCONNECTED); if (fc) { struct fuse_pqueue *fpq = &fud->pq; @@ -2547,8 +2548,12 @@ int fuse_dev_release(struct inode *inode, struct file *file) WARN_ON(fc->iq.fasync != NULL); fuse_abort_conn(fc); } + spin_lock(&fc->lock); + list_del(&fud->entry); + spin_unlock(&fc->lock); + fuse_conn_put(fc); } - fuse_dev_free(fud); + fuse_dev_put(fud); return 0; } EXPORT_SYMBOL_GPL(fuse_dev_release); diff --git a/fs/fuse/fuse_dev_i.h b/fs/fuse/fuse_dev_i.h index 522b2012cd1f..9eae14ace73c 100644 --- a/fs/fuse/fuse_dev_i.h +++ b/fs/fuse/fuse_dev_i.h @@ -39,22 +39,19 @@ struct fuse_copy_state { } ring; }; +/* fud->fc gets assigned to this value when /dev/fuse is closed */ +#define FUSE_DEV_FC_DISCONNECTED ((struct fuse_conn *) 1) + /* * Lockless access is OK, because fud->fc is set once during mount and is valid * until the file is released. */ static inline struct fuse_conn *fuse_dev_fc_get(struct fuse_dev *fud) { - /* Pairs with smp_store_release() in fuse_dev_fc_set() */ + /* Pairs with xchg() in fuse_dev_install() */ return smp_load_acquire(&fud->fc); } -static inline void fuse_dev_fc_set(struct fuse_dev *fud, struct fuse_conn *fc) -{ - /* Pairs with smp_load_acquire() in fuse_dev_fc_get() */ - smp_store_release(&fud->fc, fc); -} - static inline struct fuse_dev *fuse_file_to_fud(struct file *file) { return file->private_data; diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 739220d96b6f..66ec92f06497 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -576,6 +576,8 @@ struct fuse_pqueue { * Fuse device instance */ struct fuse_dev { + refcount_t ref; + bool sync_init; /** Fuse connection for this device */ @@ -1341,8 +1343,8 @@ void fuse_conn_put(struct fuse_conn *fc); struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc); struct fuse_dev *fuse_dev_alloc(void); -void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc); -void fuse_dev_free(struct fuse_dev *fud); +bool fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc); +void fuse_dev_put(struct fuse_dev *fud); int fuse_send_init(struct fuse_mount *fm); /** diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c index 53663846db36..ba845092e7f2 100644 --- a/fs/fuse/inode.c +++ b/fs/fuse/inode.c @@ -1622,6 +1622,7 @@ struct fuse_dev *fuse_dev_alloc(void) if (!fud) return NULL; + refcount_set(&fud->ref, 1); pq = kzalloc_objs(struct list_head, FUSE_PQ_HASH_SIZE); if (!pq) { kfree(fud); @@ -1635,12 +1636,32 @@ struct fuse_dev *fuse_dev_alloc(void) } EXPORT_SYMBOL_GPL(fuse_dev_alloc); -void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc) +bool fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc) { - fuse_dev_fc_set(fud, fuse_conn_get(fc)); + struct fuse_conn *old_fc; + + fuse_conn_get(fc); spin_lock(&fc->lock); + /* + * Pairs with: + * - xchg() in fuse_dev_release() + * - smp_load_acquire() in fuse_dev_fc_get() + */ + old_fc = cmpxchg(&fud->fc, NULL, fc); + if (old_fc) { + /* + * failed to set fud->fc because + * - it was already set to a different fc + * - it was set to disconneted + */ + spin_unlock(&fc->lock); + fuse_conn_put(fc); + return false; + } list_add_tail(&fud->entry, &fc->devices); spin_unlock(&fc->lock); + + return true; } EXPORT_SYMBOL_GPL(fuse_dev_install); @@ -1657,11 +1678,15 @@ struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc) } EXPORT_SYMBOL_GPL(fuse_dev_alloc_install); -void fuse_dev_free(struct fuse_dev *fud) +void fuse_dev_put(struct fuse_dev *fud) { - struct fuse_conn *fc = fuse_dev_fc_get(fud); + struct fuse_conn *fc; - if (fc) { + if (!refcount_dec_and_test(&fud->ref)) + return; + + fc = fuse_dev_fc_get(fud); + if (fc && fc != FUSE_DEV_FC_DISCONNECTED) { spin_lock(&fc->lock); list_del(&fud->entry); spin_unlock(&fc->lock); @@ -1671,7 +1696,7 @@ void fuse_dev_free(struct fuse_dev *fud) kfree(fud->pq.processing); kfree(fud); } -EXPORT_SYMBOL_GPL(fuse_dev_free); +EXPORT_SYMBOL_GPL(fuse_dev_put); static void fuse_fill_attr_from_inode(struct fuse_attr *attr, const struct fuse_inode *fi) @@ -1900,8 +1925,10 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) list_add_tail(&fc->entry, &fuse_conn_list); sb->s_root = root_dentry; if (fud) { - fuse_dev_install(fud, fc); - wake_up_all(&fuse_dev_waitq); + if (!fuse_dev_install(fud, fc)) + fc->connected = 0; /* device file got closed */ + else + wake_up_all(&fuse_dev_waitq); } mutex_unlock(&fuse_mutex); return 0; diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c index f685916754ad..12300651a0f1 100644 --- a/fs/fuse/virtio_fs.c +++ b/fs/fuse/virtio_fs.c @@ -486,7 +486,7 @@ static void virtio_fs_free_devs(struct virtio_fs *fs) if (!fsvq->fud) continue; - fuse_dev_free(fsvq->fud); + fuse_dev_put(fsvq->fud); fsvq->fud = NULL; } } -- 2.53.0 With the new mount API the sequence of syscalls would be: fs_fd = fsopen("fuse", 0); snprintf(opt, sizeof(opt), "%i", devfd); fsconfig(fs_fd, FSCONFIG_SET_STRING, "fd", opt, 0); /* ... */ fsconfig(fs_fd, FSCONFIG_CMD_CREATE, 0, 0, 0); Current mount code just stores the value of devfd in the fs_context and uses it in during FSCONFIG_CMD_CREATE. This is not very elegant, but there's a bigger problem: when sync init is used and the server exits for some reason (error, crash) while processing FUSE_INIT, the filesystem creation will hang. The reason is that while all other threads will exit, the mounting thread (or process) will keep the device fd open, which will prevent an abort from happening. This is a regression from the async mount case, where the mount was done first, and the FUSE_INIT processing afterwards, in which case there's no such recursive syscall keeping the fd open. The solution is twofold: a) use unshare(CLONE_FILES) in the mounting thread and close the device fd after fsconfig(fs_fd, FSCONFIG_SET_STRING, "fd", ...) b) only reference the fuse_dev from fs_context not the device file itself Signed-off-by: Miklos Szeredi --- fs/fuse/fuse_i.h | 4 +--- fs/fuse/inode.c | 54 +++++++++++++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 66ec92f06497..b77b384b0385 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -603,13 +603,11 @@ static inline bool fuse_is_inode_dax_mode(enum fuse_dax_mode mode) } struct fuse_fs_context { - int fd; - struct file *file; + struct fuse_dev *fud; unsigned int rootmode; kuid_t user_id; kgid_t group_id; bool is_bdev:1; - bool fd_present:1; bool rootmode_present:1; bool user_id_present:1; bool group_id_present:1; diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c index ba845092e7f2..45abcfec03a4 100644 --- a/fs/fuse/inode.c +++ b/fs/fuse/inode.c @@ -800,6 +800,26 @@ static const struct fs_parameter_spec fuse_fs_parameters[] = { {} }; +static int fuse_opt_fd(struct fs_context *fsc, int fd) +{ + struct file *file __free(fput) = fget(fd); + struct fuse_fs_context *ctx = fsc->fs_private; + + if (file->f_op != &fuse_dev_operations) + return invalfc(fsc, "fd is not a fuse device"); + /* + * Require mount to happen from the same user namespace which + * opened /dev/fuse to prevent potential attacks. + */ + if (file->f_cred->user_ns != fsc->user_ns) + return invalfc(fsc, "wrong user namespace for fuse device"); + + ctx->fud = file->private_data; + refcount_inc(&ctx->fud->ref); + + return 0; +} + static int fuse_parse_param(struct fs_context *fsc, struct fs_parameter *param) { struct fs_parse_result result; @@ -839,9 +859,7 @@ static int fuse_parse_param(struct fs_context *fsc, struct fs_parameter *param) return 0; case OPT_FD: - ctx->fd = result.uint_32; - ctx->fd_present = true; - break; + return fuse_opt_fd(fsc, result.uint_32); case OPT_ROOTMODE: if (!fuse_valid_type(result.uint_32)) @@ -904,6 +922,8 @@ static void fuse_free_fsc(struct fs_context *fsc) struct fuse_fs_context *ctx = fsc->fs_private; if (ctx) { + if (ctx->fud) + fuse_dev_put(ctx->fud); kfree(ctx->subtype); kfree(ctx); } @@ -1847,7 +1867,7 @@ EXPORT_SYMBOL_GPL(fuse_init_fs_context_submount); int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) { - struct fuse_dev *fud = ctx->file ? fuse_file_to_fud(ctx->file) : NULL; + struct fuse_dev *fud = ctx->fud; struct fuse_mount *fm = get_fuse_mount_super(sb); struct fuse_conn *fc = fm->fc; struct inode *root; @@ -1950,18 +1970,10 @@ static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc) struct fuse_mount *fm; int err; - if (!ctx->file || !ctx->rootmode_present || + if (!ctx->fud || !ctx->rootmode_present || !ctx->user_id_present || !ctx->group_id_present) return -EINVAL; - /* - * Require mount to happen from the same user namespace which - * opened /dev/fuse to prevent potential attacks. - */ - if ((ctx->file->f_op != &fuse_dev_operations) || - (ctx->file->f_cred->user_ns != sb->s_user_ns)) - return -EINVAL; - err = fuse_fill_super_common(sb, ctx); if (err) return err; @@ -1989,8 +2001,7 @@ static int fuse_test_super(struct super_block *sb, struct fs_context *fsc) static int fuse_get_tree(struct fs_context *fsc) { struct fuse_fs_context *ctx = fsc->fs_private; - struct fuse_dev *fud; - struct fuse_conn *fc; + struct fuse_conn *fc, *key; struct fuse_mount *fm; struct super_block *sb; int err; @@ -2010,9 +2021,6 @@ static int fuse_get_tree(struct fs_context *fsc) fsc->s_fs_info = fm; - if (ctx->fd_present) - ctx->file = fget(ctx->fd); - if (IS_ENABLED(CONFIG_BLOCK) && ctx->is_bdev) { err = get_tree_bdev(fsc, fuse_fill_super); goto out; @@ -2022,16 +2030,16 @@ static int fuse_get_tree(struct fs_context *fsc) * (found by device name), normal fuse mounts can't */ err = -EINVAL; - if (!ctx->file) + if (!ctx->fud) goto out; /* * Allow creating a fuse mount with an already initialized fuse * connection */ - fud = __fuse_get_dev(ctx->file); - if (ctx->file->f_op == &fuse_dev_operations && fud) { - fsc->sget_key = fud->fc; + key = fuse_dev_fc_get(ctx->fud); + if (key) { + fsc->sget_key = key; sb = sget_fc(fsc, fuse_test_super, fuse_set_no_super); err = PTR_ERR_OR_ZERO(sb); if (!IS_ERR(sb)) @@ -2042,8 +2050,6 @@ static int fuse_get_tree(struct fs_context *fsc) out: if (fsc->s_fs_info) fuse_mount_destroy(fm); - if (ctx->file) - fput(ctx->file); return err; } -- 2.53.0 - fuse_mutex is not needed for device cloning, because fuse_dev_install() uses cmpxcg() to set fud->fc, which prevents races between clone/mount or clone/clone. This makes the logic simpler - Drop fc->dev_count. This is only used to check in release if the device is the last clone, but checking list_empty(&fc->devices) is equivalent after removing the released device from the list. Removing the fuse_dev before calling fuse_abort_conn() is okay, since the processing and io lists are now empty for this device. Signed-off-by: Miklos Szeredi --- fs/fuse/dev.c | 44 ++++++++++++++++---------------------------- fs/fuse/fuse_i.h | 3 --- fs/fuse/inode.c | 1 - 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c index 3adf6bd38c9b..18cc844cf290 100644 --- a/fs/fuse/dev.c +++ b/fs/fuse/dev.c @@ -2543,14 +2543,15 @@ int fuse_dev_release(struct inode *inode, struct file *file) fuse_dev_end_requests(&to_end); + spin_lock(&fc->lock); + list_del(&fud->entry); + spin_unlock(&fc->lock); + /* Are we the last open device? */ - if (atomic_dec_and_test(&fc->dev_count)) { + if (list_empty(&fc->devices)) { WARN_ON(fc->iq.fasync != NULL); fuse_abort_conn(fc); } - spin_lock(&fc->lock); - list_del(&fud->entry); - spin_unlock(&fc->lock); fuse_conn_put(fc); } fuse_dev_put(fud); @@ -2569,24 +2570,10 @@ static int fuse_dev_fasync(int fd, struct file *file, int on) return fasync_helper(fd, file, on, &fud->fc->iq.fasync); } -static int fuse_device_clone(struct fuse_conn *fc, struct file *new) -{ - struct fuse_dev *new_fud = fuse_file_to_fud(new); - - if (fuse_dev_fc_get(new_fud)) - return -EINVAL; - - fuse_dev_install(new_fud, fc); - atomic_inc(&fc->dev_count); - - return 0; -} - static long fuse_dev_ioctl_clone(struct file *file, __u32 __user *argp) { - int res; int oldfd; - struct fuse_dev *fud = NULL; + struct fuse_dev *fud, *new_fud; if (get_user(oldfd, argp)) return -EFAULT; @@ -2599,17 +2586,18 @@ static long fuse_dev_ioctl_clone(struct file *file, __u32 __user *argp) * Check against file->f_op because CUSE * uses the same ioctl handler. */ - if (fd_file(f)->f_op == file->f_op) - fud = __fuse_get_dev(fd_file(f)); + if (fd_file(f)->f_op != file->f_op) + return -EINVAL; - res = -EINVAL; - if (fud) { - mutex_lock(&fuse_mutex); - res = fuse_device_clone(fud->fc, file); - mutex_unlock(&fuse_mutex); - } + fud = __fuse_get_dev(fd_file(f)); + if (!fud) + return -EINVAL; - return res; + new_fud = fuse_file_to_fud(file); + if (!fuse_dev_install(new_fud, fud->fc)) + return -EINVAL; + + return 0; } static long fuse_dev_ioctl_backing_open(struct file *file, diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index b77b384b0385..92576e28f8ac 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -647,9 +647,6 @@ struct fuse_conn { /** Refcount */ refcount_t count; - /** Number of fuse_dev's */ - atomic_t dev_count; - /** Current epoch for up-to-date dentries */ atomic_t epoch; diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c index 45abcfec03a4..e42356d60f7a 100644 --- a/fs/fuse/inode.c +++ b/fs/fuse/inode.c @@ -995,7 +995,6 @@ void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm, spin_lock_init(&fc->bg_lock); init_rwsem(&fc->killsb); refcount_set(&fc->count, 1); - atomic_set(&fc->dev_count, 1); atomic_set(&fc->epoch, 1); INIT_WORK(&fc->epoch_work, fuse_epoch_work); init_waitqueue_head(&fc->blocked_waitq); -- 2.53.0 Prior to this patchset, fuse_dev (containing fuse_pqueue) was allocated on mount. But now fuse_dev is allocated when opening /dev/fuse, even though the queues are not needed at that time. Delay allocation of the pqueue (4k worth of list_head) just before mounting or cloning a device. Signed-off-by: Miklos Szeredi --- fs/fuse/dev.c | 9 ++++++-- fs/fuse/dev_uring.c | 2 +- fs/fuse/fuse_i.h | 5 +++-- fs/fuse/inode.c | 51 +++++++++++++++++++++++++++++---------------- fs/fuse/virtio_fs.c | 4 ++-- 5 files changed, 46 insertions(+), 25 deletions(-) diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c index 18cc844cf290..4d11750addbe 100644 --- a/fs/fuse/dev.c +++ b/fs/fuse/dev.c @@ -1542,7 +1542,7 @@ static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file, static int fuse_dev_open(struct inode *inode, struct file *file) { - struct fuse_dev *fud = fuse_dev_alloc(); + struct fuse_dev *fud = fuse_dev_alloc(false); if (!fud) return -ENOMEM; @@ -2574,6 +2574,7 @@ static long fuse_dev_ioctl_clone(struct file *file, __u32 __user *argp) { int oldfd; struct fuse_dev *fud, *new_fud; + struct list_head *pq; if (get_user(oldfd, argp)) return -EFAULT; @@ -2593,8 +2594,12 @@ static long fuse_dev_ioctl_clone(struct file *file, __u32 __user *argp) if (!fud) return -EINVAL; + pq = fuse_pqueue_alloc(); + if (!pq) + return -ENOMEM; + new_fud = fuse_file_to_fud(file); - if (!fuse_dev_install(new_fud, fud->fc)) + if (!fuse_dev_install(new_fud, fud->fc, pq)) return -EINVAL; return 0; diff --git a/fs/fuse/dev_uring.c b/fs/fuse/dev_uring.c index 7b9822e8837b..fb4f21c871fb 100644 --- a/fs/fuse/dev_uring.c +++ b/fs/fuse/dev_uring.c @@ -277,7 +277,7 @@ static struct fuse_ring_queue *fuse_uring_create_queue(struct fuse_ring *ring, queue = kzalloc_obj(*queue, GFP_KERNEL_ACCOUNT); if (!queue) return NULL; - pq = kzalloc_objs(struct list_head, FUSE_PQ_HASH_SIZE); + pq = fuse_pqueue_alloc(); if (!pq) { kfree(queue); return NULL; diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 92576e28f8ac..eaeda2f097dc 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -1337,8 +1337,9 @@ void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm, void fuse_conn_put(struct fuse_conn *fc); struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc); -struct fuse_dev *fuse_dev_alloc(void); -bool fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc); +struct list_head *fuse_pqueue_alloc(void); +struct fuse_dev *fuse_dev_alloc(bool alloc_pq); +bool fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc, struct list_head *pq); void fuse_dev_put(struct fuse_dev *fud); int fuse_send_init(struct fuse_mount *fm); diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c index e42356d60f7a..f951f8d69e47 100644 --- a/fs/fuse/inode.c +++ b/fs/fuse/inode.c @@ -977,15 +977,22 @@ static void fuse_iqueue_init(struct fuse_iqueue *fiq, void fuse_pqueue_init(struct fuse_pqueue *fpq) { - unsigned int i; - spin_lock_init(&fpq->lock); - for (i = 0; i < FUSE_PQ_HASH_SIZE; i++) - INIT_LIST_HEAD(&fpq->processing[i]); INIT_LIST_HEAD(&fpq->io); fpq->connected = 1; } +struct list_head *fuse_pqueue_alloc(void) +{ + struct list_head *pq = kzalloc_objs(struct list_head, FUSE_PQ_HASH_SIZE); + + if (pq) { + for (int i = 0; i < FUSE_PQ_HASH_SIZE; i++) + INIT_LIST_HEAD(&pq[i]); + } + return pq; +} + void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm, struct user_namespace *user_ns, const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv) @@ -1632,33 +1639,38 @@ static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb) return 0; } -struct fuse_dev *fuse_dev_alloc(void) +struct fuse_dev *fuse_dev_alloc(bool alloc_pq) { struct fuse_dev *fud; - struct list_head *pq; + struct list_head *pq __free(kfree) = NULL; + + if (alloc_pq) { + pq = fuse_pqueue_alloc(); + if (!pq) + return NULL; + } fud = kzalloc_obj(struct fuse_dev); if (!fud) return NULL; refcount_set(&fud->ref, 1); - pq = kzalloc_objs(struct list_head, FUSE_PQ_HASH_SIZE); - if (!pq) { - kfree(fud); - return NULL; - } - - fud->pq.processing = pq; + fud->pq.processing = no_free_ptr(pq); fuse_pqueue_init(&fud->pq); return fud; } EXPORT_SYMBOL_GPL(fuse_dev_alloc); -bool fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc) +bool fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc, struct list_head *pq) { struct fuse_conn *old_fc; + if (!pq) + WARN_ON(!fud->pq.processing); + else if (cmpxchg(&fud->pq.processing, NULL, pq)) + kfree(pq); + fuse_conn_get(fc); spin_lock(&fc->lock); /* @@ -1686,13 +1698,12 @@ EXPORT_SYMBOL_GPL(fuse_dev_install); struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc) { - struct fuse_dev *fud; + struct fuse_dev *fud = fuse_dev_alloc(true); - fud = fuse_dev_alloc(); if (!fud) return NULL; - fuse_dev_install(fud, fc); + fuse_dev_install(fud, fc, NULL); return fud; } EXPORT_SYMBOL_GPL(fuse_dev_alloc_install); @@ -1869,10 +1880,14 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) struct fuse_dev *fud = ctx->fud; struct fuse_mount *fm = get_fuse_mount_super(sb); struct fuse_conn *fc = fm->fc; + struct list_head *pq __free(kfree) = fuse_pqueue_alloc(); struct inode *root; struct dentry *root_dentry; int err; + if (!pq) + return -ENOMEM; + err = -EINVAL; if (sb->s_flags & SB_MANDLOCK) goto err; @@ -1944,7 +1959,7 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) list_add_tail(&fc->entry, &fuse_conn_list); sb->s_root = root_dentry; if (fud) { - if (!fuse_dev_install(fud, fc)) + if (!fuse_dev_install(fud, fc, no_free_ptr(pq))) fc->connected = 0; /* device file got closed */ else wake_up_all(&fuse_dev_waitq); diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c index 12300651a0f1..cc6426992ecd 100644 --- a/fs/fuse/virtio_fs.c +++ b/fs/fuse/virtio_fs.c @@ -1585,7 +1585,7 @@ static int virtio_fs_fill_super(struct super_block *sb, struct fs_context *fsc) for (i = 0; i < fs->nvqs; i++) { struct virtio_fs_vq *fsvq = &fs->vqs[i]; - fsvq->fud = fuse_dev_alloc(); + fsvq->fud = fuse_dev_alloc(true); if (!fsvq->fud) goto err_free_fuse_devs; } @@ -1606,7 +1606,7 @@ static int virtio_fs_fill_super(struct super_block *sb, struct fs_context *fsc) for (i = 0; i < fs->nvqs; i++) { struct virtio_fs_vq *fsvq = &fs->vqs[i]; - fuse_dev_install(fsvq->fud, fc); + fuse_dev_install(fsvq->fud, fc, NULL); } /* Previous unmount will stop all queues. Start these again */ -- 2.53.0 This is not only cleaner to use in userspace (no need to sprintf the fd to a string) but also allows userspace to detect that the devfd can be closed after the fsconfig call. Signed-off-by: Miklos Szeredi --- fs/fuse/inode.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c index f951f8d69e47..5608ed5ba000 100644 --- a/fs/fuse/inode.c +++ b/fs/fuse/inode.c @@ -788,7 +788,7 @@ enum { static const struct fs_parameter_spec fuse_fs_parameters[] = { fsparam_string ("source", OPT_SOURCE), - fsparam_u32 ("fd", OPT_FD), + fsparam_fd ("fd", OPT_FD), fsparam_u32oct ("rootmode", OPT_ROOTMODE), fsparam_uid ("user_id", OPT_USER_ID), fsparam_gid ("group_id", OPT_GROUP_ID), @@ -800,9 +800,8 @@ static const struct fs_parameter_spec fuse_fs_parameters[] = { {} }; -static int fuse_opt_fd(struct fs_context *fsc, int fd) +static int fuse_opt_fd(struct fs_context *fsc, struct file *file) { - struct file *file __free(fput) = fget(fd); struct fuse_fs_context *ctx = fsc->fs_private; if (file->f_op != &fuse_dev_operations) @@ -859,7 +858,11 @@ static int fuse_parse_param(struct fs_context *fsc, struct fs_parameter *param) return 0; case OPT_FD: - return fuse_opt_fd(fsc, result.uint_32); + if (param->type == fs_value_is_file) + return fuse_opt_fd(fsc, param->file); + + struct file *file __free(fput) = fget(result.uint_32); + return fuse_opt_fd(fsc, file); case OPT_ROOTMODE: if (!fuse_valid_type(result.uint_32)) -- 2.53.0