Skip to contentSkip to conversation

Fix deadlock with suspend & quotas

PATCH In review Jan Kara 2 participants 5 messages Feb 10, 2012 – Feb 13, 2012

Subsystems (2)
lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

v2 cover Fix deadlock with suspend & quotas

Hello Al,

This is my attempt to fix deadlock in quota code when filesystem is
suspended and someone tries to modify quota e.g. via Q_SETQUOTA command. I added a VFS function to get a superblock reference and return only after the filesystem is thawed.

Changes since v1:
* wait for frozen fs really only when requested
* updated some comments & names

Honza

author reviewer
#1 lore raw

PATCH v2 1/2 vfs: Provide function to get superblock and wait for it to thaw

In quota code we need to find a superblock corresponding to a device and wait for superblock to be unfrozen. However this waiting has to happen without s_umount semaphore because that is required for superblock to thaw. So provide a function in VFS for this to keep dances with s_umount where they belong.

---
 fs/super.c         |   50 ++++++++++++++++++++++++++++++++++++++------------
 include/linux/fs.h |    1 +
 2 files changed, 39 insertions(+), 12 deletions(-)
fs/super.c +38 -12
index 6015c02..1f657d6 100644
@@ -593,15 +593,8 @@ void iterate_supers_type(struct file_system_type *type,
 
 EXPORT_SYMBOL(iterate_supers_type);
 
-/**
- *	get_super - get the superblock of a device
- *	@bdev: device to get the superblock for
- *	
- *	Scans the superblock list and finds the superblock of the file system
- *	mounted on the device given. %NULL is returned if no match is found.
- */
-
-struct super_block *get_super(struct block_device *bdev)
+static struct super_block *__get_super(struct block_device *bdev,
+				       bool wait_thaw)
 {
 	struct super_block *sb;
 
@@ -618,9 +611,13 @@ rescan:
 			spin_unlock(&sb_lock);
 			down_read(&sb->s_umount);
 			/* still alive? */
-			if (sb->s_root && (sb->s_flags & MS_BORN))
-				return sb;
-			up_read(&sb->s_umount);
+			if (sb->s_root && (sb->s_flags & MS_BORN)) {
+				if (!wait_thaw || sb->s_frozen == SB_UNFROZEN)
+					return sb;
+				up_read(&sb->s_umount);
+				vfs_check_frozen(sb, SB_FREEZE_WRITE);
+			} else
+				up_read(&sb->s_umount);
 			/* nope, got unmounted */
 			spin_lock(&sb_lock);
 			__put_super(sb);
@@ -631,9 +628,38 @@ rescan:
 	return NULL;
 }
 
+/**
+ *	get_super - get the superblock of a device
+ *	@bdev: device to get the superblock for
+ *
+ *	Scans the superblock list and finds the superblock of the file system
+ *	mounted on the device given. %NULL is returned if no match is found.
+ *	Note that the returned superblock may be frozen.
+ */
+
+struct super_block *get_super(struct block_device *bdev)
+{
+	return __get_super(bdev, false);
+}
 EXPORT_SYMBOL(get_super);
 
 /**
+ *	get_super_thawed - get thawed superblock of a device
+ *	@bdev: device to get the superblock for
+ *
+ *	Scans the superblock list and finds the superblock of the file system
+ *	mounted on the device. The superblock is returned once it is thawed
+ *	(or immediately if it was not frozen). %NULL is returned if no match
+ *	is found.
+ */
+
+struct super_block *get_super_thawed(struct block_device *bdev)
+{
+	return __get_super(bdev, true);
+}
+EXPORT_SYMBOL(get_super_thawed);
+
+/**
  * get_active_super - get an active reference to the superblock of a device
  * @bdev: device to get the superblock for
  *
include/linux/fs.h +1 -0
index 386da09..69cd5bb 100644
@@ -2496,6 +2496,7 @@ extern void get_filesystem(struct file_system_type *fs);
 extern void put_filesystem(struct file_system_type *fs);
 extern struct file_system_type *get_fs_type(const char *name);
 extern struct super_block *get_super(struct block_device *);
+extern struct super_block *get_super_thawed(struct block_device *);
 extern struct super_block *get_active_super(struct block_device *bdev);
 extern void drop_super(struct super_block *sb);
 extern void iterate_supers(void (*)(struct super_block *, void *), void *);
Signature · 1 line
1.7.1
maintainer
#2 lore raw

Re: [PATCH 1/2] vfs: Provide function to get superblock and wait for it to thaw

Quoted 4 lines · Jan Kara

In quota code we need to find a superblock corresponding to a device and wait for superblock to be unfrozen. However this waiting has to happen without s_umount semaphore because that is required for superblock to thaw. So provide a function in VFS for this to keep dances with s_umount where they belong.

Eww... All it takes is
struct super_block *get_super_thawed(struct block_device *bdev)

{
        while (1) {
                struct super_block *s = get_super(bdev);
                if (!s || s->s_frozen == SB_UNFROZEN)
                        return s;
		up_read(&s->s_umount);
		vfs_check_frozen(s, SB_FREEZE_WRITE);
		put_super(s);
        }
}

and there's no need of extra arguments, etc. whatsoever. Both patches applied, with implementation of get_super_thawed() done as above.

author reviewer
#3 lore raw

Re: [PATCH 1/2] vfs: Provide function to get superblock and wait for it to thaw

Quoted 18 lines · Al Viro
Quoted 4 lines · Jan Kara

In quota code we need to find a superblock corresponding to a device and wait for superblock to be unfrozen. However this waiting has to happen without s_umount semaphore because that is required for superblock to thaw. So provide a function in VFS for this to keep dances with s_umount where they belong.

Eww... All it takes is
struct super_block *get_super_thawed(struct block_device *bdev)

{
        while (1) {
                struct super_block *s = get_super(bdev);
                if (!s || s->s_frozen == SB_UNFROZEN)
                        return s;
		up_read(&s->s_umount);
		vfs_check_frozen(s, SB_FREEZE_WRITE);
		put_super(s);
        }
}
Quoted 2 lines

and there's no need of extra arguments, etc. whatsoever. Both patches applied, with implementation of get_super_thawed() done as above.

Yeah, this looks better. Thanks!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#4 lore raw

PATCH v2 2/2 quota: Fix deadlock with suspend and quotas

In reply to Jan Kara

This script causes a kernel deadlock:
set -e
DEVICE=/dev/vg1/linear
lvchange -ay $DEVICE
mkfs.ext3 $DEVICE
mount -t ext3 -o usrquota,grpquota $DEVICE /mnt/test quotacheck -gu /mnt/test
umount /mnt/test
mount -t ext3 -o usrquota,grpquota $DEVICE /mnt/test quotaon /mnt/test
dmsetup suspend $DEVICE
setquota -u root 1 2 3 4 /mnt/test &
sleep 1
dmsetup resume $DEVICE

setquota acquired semaphore s_umount for read and then tried to perform a transaction (and waits because the device is suspended). dmsetup resume tries to acquire s_umount for write before resuming the device (and waits for setquota).

Fix the deadlock by grabbing a thawed superblock for quota commands which need it.

---
 fs/quota/quota.c |   24 +++++++++++++++++++++---
 1 files changed, 21 insertions(+), 3 deletions(-)
fs/quota/quota.c +21 -3
index 7898cd6..fc2c438 100644
@@ -292,11 +292,26 @@ static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
 	}
 }
 
+/* Return 1 if 'cmd' will block on frozen filesystem */
+static int quotactl_cmd_write(int cmd)
+{
+	switch (cmd) {
+	case Q_GETFMT:
+	case Q_GETINFO:
+	case Q_SYNC:
+	case Q_XGETQSTAT:
+	case Q_XGETQUOTA:
+	case Q_XQUOTASYNC:
+		return 0;
+	}
+	return 1;
+}
+
 /*
  * look up a superblock on which quota ops will be performed
  * - use the name of a block device to find the superblock thereon
  */
-static struct super_block *quotactl_block(const char __user *special)
+static struct super_block *quotactl_block(const char __user *special, int cmd)
 {
 #ifdef CONFIG_BLOCK
 	struct block_device *bdev;
@@ -309,7 +324,10 @@ static struct super_block *quotactl_block(const char __user *special)
 	putname(tmp);
 	if (IS_ERR(bdev))
 		return ERR_CAST(bdev);
-	sb = get_super(bdev);
+	if (quotactl_cmd_write(cmd))
+		sb = get_super_thawed(bdev);
+	else
+		sb = get_super(bdev);
 	bdput(bdev);
 	if (!sb)
 		return ERR_PTR(-ENODEV);
@@ -361,7 +379,7 @@ SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
 			pathp = &path;
 	}
 
-	sb = quotactl_block(special);
+	sb = quotactl_block(special, cmds);
 	if (IS_ERR(sb)) {
 		ret = PTR_ERR(sb);
 		goto out;
Signature · 1 line
1.7.1