From: Wang Jianchao <jianchao.wan9@gmail.com>
To: tytso@mit.edu, adilger.kernel@dilger.ca
Cc: jack@suse.cz, guoqing.jiang@linux.dev,
linux-kernel@vger.kernel.org, linux-ext4@vger.kernel.org
Subject: [PATCH V4 5/5] ext4: flush background discard kwork when retry allocation
Date: Mon, 30 Aug 2021 15:52:46 +0800 [thread overview]
Message-ID: <20210830075246.12516-6-jianchao.wan9@gmail.com> (raw)
In-Reply-To: <20210830075246.12516-1-jianchao.wan9@gmail.com>
From: Wang Jianchao <wangjianchao@kuaishou.com>
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>
---
fs/ext4/balloc.c | 8 +++++++-
fs/ext4/ext4.h | 1 +
fs/ext4/mballoc.c | 7 +++++--
3 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/fs/ext4/balloc.c b/fs/ext4/balloc.c
index 9dc6e74b265c..a0fb0c4bdc7c 100644
--- a/fs/ext4/balloc.c
+++ b/fs/ext4/balloc.c
@@ -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,
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 6b678b968d84..d71dcac3b97f 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -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;
diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 259822fc0ae9..c0dea52a7124 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -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;
--
2.17.1
prev parent reply other threads:[~2021-08-30 7:54 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-30 7:52 [PATCH V4 0/5] ext4: get discard out of jbd2 commit kthread Wang Jianchao
2021-08-30 7:52 ` [PATCH V4 1/5] ext4: remove the 'group' parameter of ext4_trim_extent Wang Jianchao
2021-08-30 7:52 ` [PATCH V4 2/5] ext4: add new helper interface ext4_try_to_trim_range() Wang Jianchao
2021-08-30 7:52 ` [PATCH V4 3/5] ext4: remove the repeated comment of ext4_trim_all_free Wang Jianchao
2021-08-30 7:52 ` [PATCH V4 4/5] ext4: get discard out of jbd2 commit kthread contex Wang Jianchao
2021-08-31 4:00 ` Theodore Ts'o
2021-08-30 7:52 ` Wang Jianchao [this message]
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=20210830075246.12516-6-jianchao.wan9@gmail.com \
--to=jianchao.wan9@gmail.com \
--cc=adilger.kernel@dilger.ca \
--cc=guoqing.jiang@linux.dev \
--cc=jack@suse.cz \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=tytso@mit.edu \
/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
all inboxes | Powered by JetHome®