Skip to content

Commit 5accdf8

Browse files
jankaraAl Viro
authored andcommitted
fs: Improve filesystem freezing handling
vfs_check_frozen() tests are racy since the filesystem can be frozen just after the test is performed. Thus in write paths we can end up marking some pages or inodes dirty even though the file system is already frozen. This creates problems with flusher thread hanging on frozen filesystem. Another problem is that exclusion between ->page_mkwrite() and filesystem freezing has been handled by setting page dirty and then verifying s_frozen. This guaranteed that either the freezing code sees the faulted page, writes it, and writeprotects it again or we see s_frozen set and bail out of page fault. This works to protect from page being marked writeable while filesystem freezing is running but has an unpleasant artefact of leaving dirty (although unmodified and writeprotected) pages on frozen filesystem resulting in similar problems with flusher thread as the first problem. This patch aims at providing exclusion between write paths and filesystem freezing. We implement a writer-freeze read-write semaphore in the superblock. Actually, there are three such semaphores because of lock ranking reasons - one for page fault handlers (->page_mkwrite), one for all other writers, and one of internal filesystem purposes (used e.g. to track running transactions). Write paths which should block freezing (e.g. directory operations, ->aio_write(), ->page_mkwrite) hold reader side of the semaphore. Code freezing the filesystem takes the writer side. Only that we don't really want to bounce cachelines of the semaphores between CPUs for each write happening. So we implement the reader side of the semaphore as a per-cpu counter and the writer side is implemented using s_writers.frozen superblock field. [AV: microoptimize sb_start_write(); we want it fast in normal case] BugLink: https://bugs.launchpad.net/bugs/897421 Tested-by: Kamal Mostafa <kamal@canonical.com> Tested-by: Peter M. Petrakis <peter.petrakis@canonical.com> Tested-by: Dann Frazier <dann.frazier@canonical.com> Tested-by: Massimo Morana <massimo.morana@canonical.com> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent d87aae2 commit 5accdf8

2 files changed

Lines changed: 373 additions & 28 deletions

File tree

‎fs/super.c‎

Lines changed: 230 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,19 @@
3333
#include <linux/rculist_bl.h>
3434
#include <linux/cleancache.h>
3535
#include <linux/fsnotify.h>
36+
#include <linux/lockdep.h>
3637
#include "internal.h"
3738

3839

3940
LIST_HEAD(super_blocks);
4041
DEFINE_SPINLOCK(sb_lock);
4142

43+
static char *sb_writers_name[SB_FREEZE_LEVELS] = {
44+
"sb_writers",
45+
"sb_pagefaults",
46+
"sb_internal",
47+
};
48+
4249
/*
4350
* One thing we have to be careful of with a per-sb shrinker is that we don't
4451
* drop the last active reference to the superblock from within the shrinker.
@@ -102,6 +109,35 @@ static int prune_super(struct shrinker *shrink, struct shrink_control *sc)
102109
return total_objects;
103110
}
104111

112+
static int init_sb_writers(struct super_block *s, struct file_system_type *type)
113+
{
114+
int err;
115+
int i;
116+
117+
for (i = 0; i < SB_FREEZE_LEVELS; i++) {
118+
err = percpu_counter_init(&s->s_writers.counter[i], 0);
119+
if (err < 0)
120+
goto err_out;
121+
lockdep_init_map(&s->s_writers.lock_map[i], sb_writers_name[i],
122+
&type->s_writers_key[i], 0);
123+
}
124+
init_waitqueue_head(&s->s_writers.wait);
125+
init_waitqueue_head(&s->s_writers.wait_unfrozen);
126+
return 0;
127+
err_out:
128+
while (--i >= 0)
129+
percpu_counter_destroy(&s->s_writers.counter[i]);
130+
return err;
131+
}
132+
133+
static void destroy_sb_writers(struct super_block *s)
134+
{
135+
int i;
136+
137+
for (i = 0; i < SB_FREEZE_LEVELS; i++)
138+
percpu_counter_destroy(&s->s_writers.counter[i]);
139+
}
140+
105141
/**
106142
* alloc_super - create new superblock
107143
* @type: filesystem type superblock should belong to
@@ -117,18 +153,19 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags)
117153

118154
if (s) {
119155
if (security_sb_alloc(s)) {
156+
/*
157+
* We cannot call security_sb_free() without
158+
* security_sb_alloc() succeeding. So bail out manually
159+
*/
120160
kfree(s);
121161
s = NULL;
122162
goto out;
123163
}
124164
#ifdef CONFIG_SMP
125165
s->s_files = alloc_percpu(struct list_head);
126-
if (!s->s_files) {
127-
security_sb_free(s);
128-
kfree(s);
129-
s = NULL;
130-
goto out;
131-
} else {
166+
if (!s->s_files)
167+
goto err_out;
168+
else {
132169
int i;
133170

134171
for_each_possible_cpu(i)
@@ -137,6 +174,8 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags)
137174
#else
138175
INIT_LIST_HEAD(&s->s_files);
139176
#endif
177+
if (init_sb_writers(s, type))
178+
goto err_out;
140179
s->s_flags = flags;
141180
s->s_bdi = &default_backing_dev_info;
142181
INIT_HLIST_NODE(&s->s_instances);
@@ -190,6 +229,16 @@ static struct super_block *alloc_super(struct file_system_type *type, int flags)
190229
}
191230
out:
192231
return s;
232+
err_out:
233+
security_sb_free(s);
234+
#ifdef CONFIG_SMP
235+
if (s->s_files)
236+
free_percpu(s->s_files);
237+
#endif
238+
destroy_sb_writers(s);
239+
kfree(s);
240+
s = NULL;
241+
goto out;
193242
}
194243

195244
/**
@@ -203,6 +252,7 @@ static inline void destroy_super(struct super_block *s)
203252
#ifdef CONFIG_SMP
204253
free_percpu(s->s_files);
205254
#endif
255+
destroy_sb_writers(s);
206256
security_sb_free(s);
207257
WARN_ON(!list_empty(&s->s_mounts));
208258
kfree(s->s_subtype);
@@ -651,10 +701,11 @@ struct super_block *get_super_thawed(struct block_device *bdev)
651701
{
652702
while (1) {
653703
struct super_block *s = get_super(bdev);
654-
if (!s || s->s_frozen == SB_UNFROZEN)
704+
if (!s || s->s_writers.frozen == SB_UNFROZEN)
655705
return s;
656706
up_read(&s->s_umount);
657-
vfs_check_frozen(s, SB_FREEZE_WRITE);
707+
wait_event(s->s_writers.wait_unfrozen,
708+
s->s_writers.frozen == SB_UNFROZEN);
658709
put_super(s);
659710
}
660711
}
@@ -732,7 +783,7 @@ int do_remount_sb(struct super_block *sb, int flags, void *data, int force)
732783
int retval;
733784
int remount_ro;
734785

735-
if (sb->s_frozen != SB_UNFROZEN)
786+
if (sb->s_writers.frozen != SB_UNFROZEN)
736787
return -EBUSY;
737788

738789
#ifdef CONFIG_BLOCK
@@ -1163,21 +1214,160 @@ mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
11631214
return ERR_PTR(error);
11641215
}
11651216

1217+
/*
1218+
* This is an internal function, please use sb_end_{write,pagefault,intwrite}
1219+
* instead.
1220+
*/
1221+
void __sb_end_write(struct super_block *sb, int level)
1222+
{
1223+
percpu_counter_dec(&sb->s_writers.counter[level-1]);
1224+
/*
1225+
* Make sure s_writers are updated before we wake up waiters in
1226+
* freeze_super().
1227+
*/
1228+
smp_mb();
1229+
if (waitqueue_active(&sb->s_writers.wait))
1230+
wake_up(&sb->s_writers.wait);
1231+
rwsem_release(&sb->s_writers.lock_map[level-1], 1, _RET_IP_);
1232+
}
1233+
EXPORT_SYMBOL(__sb_end_write);
1234+
1235+
#ifdef CONFIG_LOCKDEP
1236+
/*
1237+
* We want lockdep to tell us about possible deadlocks with freezing but
1238+
* it's it bit tricky to properly instrument it. Getting a freeze protection
1239+
* works as getting a read lock but there are subtle problems. XFS for example
1240+
* gets freeze protection on internal level twice in some cases, which is OK
1241+
* only because we already hold a freeze protection also on higher level. Due
1242+
* to these cases we have to tell lockdep we are doing trylock when we
1243+
* already hold a freeze protection for a higher freeze level.
1244+
*/
1245+
static void acquire_freeze_lock(struct super_block *sb, int level, bool trylock,
1246+
unsigned long ip)
1247+
{
1248+
int i;
1249+
1250+
if (!trylock) {
1251+
for (i = 0; i < level - 1; i++)
1252+
if (lock_is_held(&sb->s_writers.lock_map[i])) {
1253+
trylock = true;
1254+
break;
1255+
}
1256+
}
1257+
rwsem_acquire_read(&sb->s_writers.lock_map[level-1], 0, trylock, ip);
1258+
}
1259+
#endif
1260+
1261+
/*
1262+
* This is an internal function, please use sb_start_{write,pagefault,intwrite}
1263+
* instead.
1264+
*/
1265+
int __sb_start_write(struct super_block *sb, int level, bool wait)
1266+
{
1267+
retry:
1268+
if (unlikely(sb->s_writers.frozen >= level)) {
1269+
if (!wait)
1270+
return 0;
1271+
wait_event(sb->s_writers.wait_unfrozen,
1272+
sb->s_writers.frozen < level);
1273+
}
1274+
1275+
#ifdef CONFIG_LOCKDEP
1276+
acquire_freeze_lock(sb, level, !wait, _RET_IP_);
1277+
#endif
1278+
percpu_counter_inc(&sb->s_writers.counter[level-1]);
1279+
/*
1280+
* Make sure counter is updated before we check for frozen.
1281+
* freeze_super() first sets frozen and then checks the counter.
1282+
*/
1283+
smp_mb();
1284+
if (unlikely(sb->s_writers.frozen >= level)) {
1285+
__sb_end_write(sb, level);
1286+
goto retry;
1287+
}
1288+
return 1;
1289+
}
1290+
EXPORT_SYMBOL(__sb_start_write);
1291+
1292+
/**
1293+
* sb_wait_write - wait until all writers to given file system finish
1294+
* @sb: the super for which we wait
1295+
* @level: type of writers we wait for (normal vs page fault)
1296+
*
1297+
* This function waits until there are no writers of given type to given file
1298+
* system. Caller of this function should make sure there can be no new writers
1299+
* of type @level before calling this function. Otherwise this function can
1300+
* livelock.
1301+
*/
1302+
static void sb_wait_write(struct super_block *sb, int level)
1303+
{
1304+
s64 writers;
1305+
1306+
/*
1307+
* We just cycle-through lockdep here so that it does not complain
1308+
* about returning with lock to userspace
1309+
*/
1310+
rwsem_acquire(&sb->s_writers.lock_map[level-1], 0, 0, _THIS_IP_);
1311+
rwsem_release(&sb->s_writers.lock_map[level-1], 1, _THIS_IP_);
1312+
1313+
do {
1314+
DEFINE_WAIT(wait);
1315+
1316+
/*
1317+
* We use a barrier in prepare_to_wait() to separate setting
1318+
* of frozen and checking of the counter
1319+
*/
1320+
prepare_to_wait(&sb->s_writers.wait, &wait,
1321+
TASK_UNINTERRUPTIBLE);
1322+
1323+
writers = percpu_counter_sum(&sb->s_writers.counter[level-1]);
1324+
if (writers)
1325+
schedule();
1326+
1327+
finish_wait(&sb->s_writers.wait, &wait);
1328+
} while (writers);
1329+
}
1330+
11661331
/**
11671332
* freeze_super - lock the filesystem and force it into a consistent state
11681333
* @sb: the super to lock
11691334
*
11701335
* Syncs the super to make sure the filesystem is consistent and calls the fs's
11711336
* freeze_fs. Subsequent calls to this without first thawing the fs will return
11721337
* -EBUSY.
1338+
*
1339+
* During this function, sb->s_writers.frozen goes through these values:
1340+
*
1341+
* SB_UNFROZEN: File system is normal, all writes progress as usual.
1342+
*
1343+
* SB_FREEZE_WRITE: The file system is in the process of being frozen. New
1344+
* writes should be blocked, though page faults are still allowed. We wait for
1345+
* all writes to complete and then proceed to the next stage.
1346+
*
1347+
* SB_FREEZE_PAGEFAULT: Freezing continues. Now also page faults are blocked
1348+
* but internal fs threads can still modify the filesystem (although they
1349+
* should not dirty new pages or inodes), writeback can run etc. After waiting
1350+
* for all running page faults we sync the filesystem which will clean all
1351+
* dirty pages and inodes (no new dirty pages or inodes can be created when
1352+
* sync is running).
1353+
*
1354+
* SB_FREEZE_FS: The file system is frozen. Now all internal sources of fs
1355+
* modification are blocked (e.g. XFS preallocation truncation on inode
1356+
* reclaim). This is usually implemented by blocking new transactions for
1357+
* filesystems that have them and need this additional guard. After all
1358+
* internal writers are finished we call ->freeze_fs() to finish filesystem
1359+
* freezing. Then we transition to SB_FREEZE_COMPLETE state. This state is
1360+
* mostly auxiliary for filesystems to verify they do not modify frozen fs.
1361+
*
1362+
* sb->s_writers.frozen is protected by sb->s_umount.
11731363
*/
11741364
int freeze_super(struct super_block *sb)
11751365
{
11761366
int ret;
11771367

11781368
atomic_inc(&sb->s_active);
11791369
down_write(&sb->s_umount);
1180-
if (sb->s_frozen) {
1370+
if (sb->s_writers.frozen != SB_UNFROZEN) {
11811371
deactivate_locked_super(sb);
11821372
return -EBUSY;
11831373
}
@@ -1188,33 +1378,53 @@ int freeze_super(struct super_block *sb)
11881378
}
11891379

11901380
if (sb->s_flags & MS_RDONLY) {
1191-
sb->s_frozen = SB_FREEZE_TRANS;
1192-
smp_wmb();
1381+
/* Nothing to do really... */
1382+
sb->s_writers.frozen = SB_FREEZE_COMPLETE;
11931383
up_write(&sb->s_umount);
11941384
return 0;
11951385
}
11961386

1197-
sb->s_frozen = SB_FREEZE_WRITE;
1387+
/* From now on, no new normal writers can start */
1388+
sb->s_writers.frozen = SB_FREEZE_WRITE;
1389+
smp_wmb();
1390+
1391+
/* Release s_umount to preserve sb_start_write -> s_umount ordering */
1392+
up_write(&sb->s_umount);
1393+
1394+
sb_wait_write(sb, SB_FREEZE_WRITE);
1395+
1396+
/* Now we go and block page faults... */
1397+
down_write(&sb->s_umount);
1398+
sb->s_writers.frozen = SB_FREEZE_PAGEFAULT;
11981399
smp_wmb();
11991400

1401+
sb_wait_write(sb, SB_FREEZE_PAGEFAULT);
1402+
1403+
/* All writers are done so after syncing there won't be dirty data */
12001404
sync_filesystem(sb);
12011405

1202-
sb->s_frozen = SB_FREEZE_TRANS;
1406+
/* Now wait for internal filesystem counter */
1407+
sb->s_writers.frozen = SB_FREEZE_FS;
12031408
smp_wmb();
1409+
sb_wait_write(sb, SB_FREEZE_FS);
12041410

1205-
sync_blockdev(sb->s_bdev);
12061411
if (sb->s_op->freeze_fs) {
12071412
ret = sb->s_op->freeze_fs(sb);
12081413
if (ret) {
12091414
printk(KERN_ERR
12101415
"VFS:Filesystem freeze failed\n");
1211-
sb->s_frozen = SB_UNFROZEN;
1416+
sb->s_writers.frozen = SB_UNFROZEN;
12121417
smp_wmb();
1213-
wake_up(&sb->s_wait_unfrozen);
1418+
wake_up(&sb->s_writers.wait_unfrozen);
12141419
deactivate_locked_super(sb);
12151420
return ret;
12161421
}
12171422
}
1423+
/*
1424+
* This is just for debugging purposes so that fs can warn if it
1425+
* sees write activity when frozen is set to SB_FREEZE_COMPLETE.
1426+
*/
1427+
sb->s_writers.frozen = SB_FREEZE_COMPLETE;
12181428
up_write(&sb->s_umount);
12191429
return 0;
12201430
}
@@ -1231,7 +1441,7 @@ int thaw_super(struct super_block *sb)
12311441
int error;
12321442

12331443
down_write(&sb->s_umount);
1234-
if (sb->s_frozen == SB_UNFROZEN) {
1444+
if (sb->s_writers.frozen == SB_UNFROZEN) {
12351445
up_write(&sb->s_umount);
12361446
return -EINVAL;
12371447
}
@@ -1244,16 +1454,15 @@ int thaw_super(struct super_block *sb)
12441454
if (error) {
12451455
printk(KERN_ERR
12461456
"VFS:Filesystem thaw failed\n");
1247-
sb->s_frozen = SB_FREEZE_TRANS;
12481457
up_write(&sb->s_umount);
12491458
return error;
12501459
}
12511460
}
12521461

12531462
out:
1254-
sb->s_frozen = SB_UNFROZEN;
1463+
sb->s_writers.frozen = SB_UNFROZEN;
12551464
smp_wmb();
1256-
wake_up(&sb->s_wait_unfrozen);
1465+
wake_up(&sb->s_writers.wait_unfrozen);
12571466
deactivate_locked_super(sb);
12581467

12591468
return 0;

0 commit comments

Comments
 (0)