Linux filesystem development
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: Christoph Hellwig <hch@lst.de>, jack@suse.cz
Cc: viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org
Subject: Re: [PATCH] fs: open the block device after allocation the super_block
Date: Tue, 15 Aug 2023 16:43:07 +0200	[thread overview]
Message-ID: <20230815-zeitdokument-quintessenz-7b75f29456a7@brauner> (raw)
In-Reply-To: <20230724175145.201318-1-hch@lst.de>

>  		up_write(&s->s_umount);
> -		blkdev_put(bdev, fs_type);
> +		error = setup_bdev_super(s, flags, NULL);
>  		down_write(&s->s_umount);

So I've been looking through the branches to see what's ready for v6.6
and what needs some more time. While doing so I went over this again and
realized that we have an issue here.

While it looks like dropping s_umount here and calling
setup_bdev_super() is fine I think it isn't. Consider two processes
racing to create the same mount:

P1                                                                    P2
vfs_get_tree()                                                        vfs_get_tree()
-> get_tree() == get_tree_bdev()                                      -> get_tree() == get_tree_bdev()
   -> sget_fc()                                                          -> sget_fc()
        // allocate new sb; no matching sb found
      -> sb_p1 = alloc_super()
      -> hlist_add_head(&s->s_instances, &s->s_type->fs_supers)
      -> spin_unlock(&sb_lock)                                              
      // yield s_umount to avoid deadlocks
   -> up_write(&sb->s_umount)
                                                                            -> spin_lock(&sb_lock)
                                                                               // find sb_p1
                                                                               if (test(old, fc))
                                                                                       goto share_extant_sb;
      // Assume P1 sleeps on bdev_lock or open_mutex
      // in blkdev_get_by_dev().
   -> setup_bdev_super()
   -> down_write(&sb->s_umount)

Now P2 jumps to the share_extant_sb label and calls:

grab_super(sb_p1)
-> spin_unlock(&sb_lock)
-> down_write(&s->s_umount)

Since s_umount is unlocked P2 doesn't go to sleep and instead immediately
goes to retry by jumping to the "retry" label. If P1 is still sleeping
on a a bdev mutex the same thing happens again.

So if you have a range of processes P{1,n} that all try to mount the
same device you're hammering endlessly on sb_lock without ever going to
sleep like we used to. The same problem exists for all iterate_supers()
and user_get_dev() variants afaict. So that needs fixing unless I'm
wrong. grab_super() and other variants need to sleep until
setup_bdev_super() is done and not busy loop. Am I wrong here?

  parent reply	other threads:[~2023-08-15 14:44 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-24 17:51 [PATCH] fs: open the block device after allocation the super_block Christoph Hellwig
2023-07-25 12:35 ` Christian Brauner
2023-07-25 16:32   ` Christian Brauner
2023-07-26 12:51     ` Christoph Hellwig
2023-07-26 12:57       ` Christian Brauner
2023-07-25 15:53 ` Christian Brauner
2023-08-15 14:43 ` Christian Brauner [this message]
2023-08-16  7:29   ` Christian Brauner
2023-08-16 21:39     ` Jan Kara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230815-zeitdokument-quintessenz-7b75f29456a7@brauner \
    --to=brauner@kernel.org \
    --cc=hch@lst.de \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox