All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Btrfs: fix race between enabling quotas and subvolume creation
@ 2018-11-19 16:20 fdmanana
  2018-11-20  0:33 ` Qu Wenruo
  2018-11-21 16:02 ` David Sterba
  0 siblings, 2 replies; 3+ messages in thread
From: fdmanana @ 2018-11-19 16:20 UTC (permalink / raw)
  To: linux-btrfs

From: Filipe Manana <fdmanana@suse.com>

We have a race between enabling quotas end subvolume creation that cause
subvolume creation to fail with -EINVAL, and the following diagram shows
how it happens:

              CPU 0                                          CPU 1

 btrfs_ioctl()
  btrfs_ioctl_quota_ctl()
   btrfs_quota_enable()
    mutex_lock(fs_info->qgroup_ioctl_lock)

                                                  btrfs_ioctl()
                                                   create_subvol()
                                                    btrfs_qgroup_inherit()
                                                     -> save fs_info->quota_root
                                                        into quota_root
                                                     -> stores a NULL value
                                                     -> tries to lock the mutex
                                                        qgroup_ioctl_lock
                                                        -> blocks waiting for
                                                           the task at CPU0

   -> sets BTRFS_FS_QUOTA_ENABLED in fs_info
   -> sets quota_root in fs_info->quota_root
      (non-NULL value)

   mutex_unlock(fs_info->qgroup_ioctl_lock)

                                                     -> checks quota enabled
                                                        flag is set
                                                     -> returns -EINVAL because
                                                        fs_info->quota_root was
                                                        NULL before it acquired
                                                        the mutex
                                                        qgroup_ioctl_lock
                                                   -> ioctl returns -EINVAL

Returning -EINVAL to user space will be confusing if all the arguments
passed to the subvolume creation ioctl were valid.

Fix it by grabbing the value from fs_info->quota_root after acquiring
the mutex.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/qgroup.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
index ae1358253b7b..0bdf28499790 100644
--- a/fs/btrfs/qgroup.c
+++ b/fs/btrfs/qgroup.c
@@ -2250,7 +2250,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
 	int i;
 	u64 *i_qgroups;
 	struct btrfs_fs_info *fs_info = trans->fs_info;
-	struct btrfs_root *quota_root = fs_info->quota_root;
+	struct btrfs_root *quota_root;
 	struct btrfs_qgroup *srcgroup;
 	struct btrfs_qgroup *dstgroup;
 	u32 level_size = 0;
@@ -2260,6 +2260,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
 	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
 		goto out;
 
+	quota_root = fs_info->quota_root;
 	if (!quota_root) {
 		ret = -EINVAL;
 		goto out;
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] Btrfs: fix race between enabling quotas and subvolume creation
  2018-11-19 16:20 [PATCH] Btrfs: fix race between enabling quotas and subvolume creation fdmanana
@ 2018-11-20  0:33 ` Qu Wenruo
  2018-11-21 16:02 ` David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: Qu Wenruo @ 2018-11-20  0:33 UTC (permalink / raw)
  To: fdmanana, linux-btrfs


[-- Attachment #1.1: Type: text/plain, Size: 3293 bytes --]



On 2018/11/20 上午12:20, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> We have a race between enabling quotas end subvolume creation that cause
> subvolume creation to fail with -EINVAL, and the following diagram shows
> how it happens:
> 
>               CPU 0                                          CPU 1
> 
>  btrfs_ioctl()
>   btrfs_ioctl_quota_ctl()
>    btrfs_quota_enable()
>     mutex_lock(fs_info->qgroup_ioctl_lock)
> 
>                                                   btrfs_ioctl()
>                                                    create_subvol()
>                                                     btrfs_qgroup_inherit()
>                                                      -> save fs_info->quota_root
>                                                         into quota_root
>                                                      -> stores a NULL value
>                                                      -> tries to lock the mutex
>                                                         qgroup_ioctl_lock
>                                                         -> blocks waiting for
>                                                            the task at CPU0
> 
>    -> sets BTRFS_FS_QUOTA_ENABLED in fs_info
>    -> sets quota_root in fs_info->quota_root
>       (non-NULL value)
> 
>    mutex_unlock(fs_info->qgroup_ioctl_lock)
> 
>                                                      -> checks quota enabled
>                                                         flag is set
>                                                      -> returns -EINVAL because
>                                                         fs_info->quota_root was
>                                                         NULL before it acquired
>                                                         the mutex
>                                                         qgroup_ioctl_lock
>                                                    -> ioctl returns -EINVAL
> 
> Returning -EINVAL to user space will be confusing if all the arguments
> passed to the subvolume creation ioctl were valid.
> 
> Fix it by grabbing the value from fs_info->quota_root after acquiring
> the mutex.
> 
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

Reviewed-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu

> ---
>  fs/btrfs/qgroup.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
> index ae1358253b7b..0bdf28499790 100644
> --- a/fs/btrfs/qgroup.c
> +++ b/fs/btrfs/qgroup.c
> @@ -2250,7 +2250,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
>  	int i;
>  	u64 *i_qgroups;
>  	struct btrfs_fs_info *fs_info = trans->fs_info;
> -	struct btrfs_root *quota_root = fs_info->quota_root;
> +	struct btrfs_root *quota_root;
>  	struct btrfs_qgroup *srcgroup;
>  	struct btrfs_qgroup *dstgroup;
>  	u32 level_size = 0;
> @@ -2260,6 +2260,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
>  	if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
>  		goto out;
>  
> +	quota_root = fs_info->quota_root;
>  	if (!quota_root) {
>  		ret = -EINVAL;
>  		goto out;
> 


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Btrfs: fix race between enabling quotas and subvolume creation
  2018-11-19 16:20 [PATCH] Btrfs: fix race between enabling quotas and subvolume creation fdmanana
  2018-11-20  0:33 ` Qu Wenruo
@ 2018-11-21 16:02 ` David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: David Sterba @ 2018-11-21 16:02 UTC (permalink / raw)
  To: fdmanana; +Cc: linux-btrfs

On Mon, Nov 19, 2018 at 04:20:34PM +0000, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> We have a race between enabling quotas end subvolume creation that cause
> subvolume creation to fail with -EINVAL, and the following diagram shows
> how it happens:
> 
>               CPU 0                                          CPU 1
> 
>  btrfs_ioctl()
>   btrfs_ioctl_quota_ctl()
>    btrfs_quota_enable()
>     mutex_lock(fs_info->qgroup_ioctl_lock)
> 
>                                                   btrfs_ioctl()
>                                                    create_subvol()
>                                                     btrfs_qgroup_inherit()
>                                                      -> save fs_info->quota_root
>                                                         into quota_root
>                                                      -> stores a NULL value
>                                                      -> tries to lock the mutex
>                                                         qgroup_ioctl_lock
>                                                         -> blocks waiting for
>                                                            the task at CPU0
> 
>    -> sets BTRFS_FS_QUOTA_ENABLED in fs_info
>    -> sets quota_root in fs_info->quota_root
>       (non-NULL value)
> 
>    mutex_unlock(fs_info->qgroup_ioctl_lock)
> 
>                                                      -> checks quota enabled
>                                                         flag is set
>                                                      -> returns -EINVAL because
>                                                         fs_info->quota_root was
>                                                         NULL before it acquired
>                                                         the mutex
>                                                         qgroup_ioctl_lock
>                                                    -> ioctl returns -EINVAL
> 
> Returning -EINVAL to user space will be confusing if all the arguments
> passed to the subvolume creation ioctl were valid.
> 
> Fix it by grabbing the value from fs_info->quota_root after acquiring
> the mutex.
> 
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

Reviewed-by: David Sterba <dsterba@suse.com>

Added to misc-next, thanks.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-11-21 16:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-19 16:20 [PATCH] Btrfs: fix race between enabling quotas and subvolume creation fdmanana
2018-11-20  0:33 ` Qu Wenruo
2018-11-21 16:02 ` David Sterba

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.