mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: laishangzhen <laishangzhen@163.com>
To: Matthew Wilcox <willy@infradead.org>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	laishangzhen <laishangzhen@163.com>
Subject: [PATCH] mm: Removing the card during write
Date: Mon, 28 Jul 2025 07:13:06 -0700	[thread overview]
Message-ID: <20250728141306.23451-1-laishangzhen@163.com> (raw)

operations blocks the I/O process
when during the process
Issue encountered:
When formatting an SD card to ext4 using mkfs.ext4,
if the card is ejected during the process,
the formatting process blocks at
balance_dirty_pages_ratelimited.

task:mkfs.ext4       state:D stack:
 0 pid:  900 ppid:   889 flags:0x00000000
 (__schedule)
 (schedule) from
 (schedule_timeout)
 (io_schedule_timeout)
 (balance_dirty_pages_ratelimited)
 (generic_perform_write)
 (__generic_file_write_iter)
 (blkdev_write_iter)
 (vfs_write)
 (ksys_pwrite64)
 Exception stack(0xc2283fa8 to 0xc2283ff0)

Modification approach:
After card ejection,
the dev in the bdi (backing_dev_info) is
released in del_gendisk.
Within generic_perform_write,
if writeback is supported,
it directly checks whether bdi->dev is NULL to determine
if the card has been ejected.
Once ejected, it becomes unnecessary to proceed
with page allocation and data writing.

Signed-off-by: laishangzhen <laishangzhen@163.com>
---
 mm/filemap.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mm/filemap.c b/mm/filemap.c
index 6af6d8f29..5d6ba9f02 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -4087,6 +4087,8 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)
 	struct address_space *mapping = file->f_mapping;
 	const struct address_space_operations *a_ops = mapping->a_ops;
 	size_t chunk = mapping_max_folio_size(mapping);
+	struct inode *inode = mapping->host;
+	struct backing_dev_info *bdi = inode_to_bdi(inode);
 	long status = 0;
 	ssize_t written = 0;
 
@@ -4101,6 +4103,12 @@ ssize_t generic_perform_write(struct kiocb *iocb, struct iov_iter *i)
 retry:
 		offset = pos & (chunk - 1);
 		bytes = min(chunk - offset, bytes);
+		if (bdi->capabilities & BDI_CAP_WRITEBACK) {
+			if (bdi->dev == NULL) {
+				status = -ENODEV;
+				break;
+			}
+		}
 		balance_dirty_pages_ratelimited(mapping);
 
 		if (fatal_signal_pending(current)) {
-- 
2.25.1


             reply	other threads:[~2025-07-28 14:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-28 14:13 laishangzhen [this message]
2025-07-28 14:42 ` Matthew Wilcox

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=20250728141306.23451-1-laishangzhen@163.com \
    --to=laishangzhen@163.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=willy@infradead.org \
    /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®