ext4: flush background discard kwork when retry allocation
authorWang Jianchao <wangjianchao@kuaishou.com>
Mon, 30 Aug 2021 07:52:46 +0000 (15:52 +0800)
committerTheodore Ts'o <tytso@mit.edu>
Tue, 31 Aug 2021 03:35:53 +0000 (23:35 -0400)
The background discard kwork tries to mark blocks used and issue
discard. This can make filesystem suffer from NOSPC error, xfstest
generic/371 can fail due to it. Fix it by flushing discard kwork
in ext4_should_retry_alloc. At the same time, give up discard at
the moment.

Signed-off-by: Wang Jianchao <wangjianchao@kuaishou.com>
Link: https://lore.kernel.org/r/20210830075246.12516-6-jianchao.wan9@gmail.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/balloc.c
fs/ext4/ext4.h
fs/ext4/mballoc.c

index 9dc6e74..a0fb0c4 100644 (file)
@@ -652,8 +652,14 @@ int ext4_should_retry_alloc(struct super_block *sb, int *retries)
         * possible we just missed a transaction commit that did so
         */
        smp_mb();
-       if (sbi->s_mb_free_pending == 0)
+       if (sbi->s_mb_free_pending == 0) {
+               if (test_opt(sb, DISCARD)) {
+                       atomic_inc(&sbi->s_retry_alloc_pending);
+                       flush_work(&sbi->s_discard_work);
+                       atomic_dec(&sbi->s_retry_alloc_pending);
+               }
                return ext4_has_free_clusters(sbi, 1, 0);
+       }
 
        /*
         * it's possible we've just missed a transaction commit here,
index 6b678b9..d71dcac 100644 (file)
@@ -1538,6 +1538,7 @@ struct ext4_sb_info {
                                                   after commit completed */
        struct list_head s_discard_list;
        struct work_struct s_discard_work;
+       atomic_t s_retry_alloc_pending;
        struct rb_root s_mb_avg_fragment_size_root;
        rwlock_t s_mb_rb_lock;
        struct list_head *s_mb_largest_free_orders;
index 907b357..34670cb 100644 (file)
@@ -3331,9 +3331,11 @@ static void ext4_discard_work(struct work_struct *work)
        load_grp = UINT_MAX;
        list_for_each_entry_safe(fd, nfd, &discard_list, efd_list) {
                /*
-                * If filesystem is umounting or no memory, give up the discard
+                * If filesystem is umounting or no memory or suffering
+                * from no space, give up the discard
                 */
-               if ((sb->s_flags & SB_ACTIVE) && !err) {
+               if ((sb->s_flags & SB_ACTIVE) && !err &&
+                   !atomic_read(&sbi->s_retry_alloc_pending)) {
                        grp = fd->efd_group;
                        if (grp != load_grp) {
                                if (load_grp != UINT_MAX)
@@ -3431,6 +3433,7 @@ int ext4_mb_init(struct super_block *sb)
        INIT_LIST_HEAD(&sbi->s_freed_data_list);
        INIT_LIST_HEAD(&sbi->s_discard_list);
        INIT_WORK(&sbi->s_discard_work, ext4_discard_work);
+       atomic_set(&sbi->s_retry_alloc_pending, 0);
 
        sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
        sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;