All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Scott <dave@recoil.org>
To: linux-erofs@lists.ozlabs.org
Cc: dave.scott@docker.com, dave@recoil.org
Subject: [PATCH] erofs-utils: mkfs: fix CPU spin using --tar=f when stdin is closed
Date: Mon, 2 Mar 2026 19:01:07 +0000	[thread overview]
Message-ID: <aaXec59gbj8fIXai@beast> (raw)

On my Mac I saw a CPU spin which looked like this:
```
Call graph:
    2192 Thread_132504   DispatchQueue_1: com.apple.main-thread  (serial)
      2192 start  (in dyld) + 6992  [0x185bcbda4]
        2192 main  (in mkfs.erofs) + 7916  [0x10253a6d0]
          2192 tarerofs_parse_tar  (in mkfs.erofs) + 5492  [0x102551d48]
            2187 tarerofs_write_file_data  (in mkfs.erofs) + 140  [0x102551fe0]
            + 2187 write  (in libsystem_kernel.dylib) + 8  [0x185f47834]
            4 tarerofs_write_file_data  (in mkfs.erofs) + 116  [0x102551fc8]
            + 4 erofs_iostream_read  (in mkfs.erofs) + 16,36,...  [0x10254fa28,0x10254fa3c,...]
            1 tarerofs_write_file_data  (in mkfs.erofs) + 140  [0x102551fe0]
```

The input stream was closed prematurely, so the reads returned 0 (EOF),
which wasn't considered an error.

Treat return of 0 (EOF) as an error.

Reproduce by:
```
dd if=/dev/zero bs=1024 count=4 2>/dev/null > /tmp/testfile
COPYFILE_DISABLE=1 tar cf - -C /tmp testfile | head -c 2048 > /tmp/truncated.tar
./mkfs/mkfs.erofs --tar=f output.erofs < /tmp/truncated.tar
```
Before the patch this will hang, after it should fail as expected.

(COPYFILE_DISABLE tells mac to avoid putting extra stuff in the tar)

Closes: https://github.com/erofs/erofs-utils/issues/43
Signed-off-by: David Scott <dave@recoil.org>
---
 lib/tar.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/lib/tar.c b/lib/tar.c
index 178f843..57c6fee 100644
--- a/lib/tar.c
+++ b/lib/tar.c
@@ -638,8 +638,11 @@ static int tarerofs_write_uncompressed_file(struct erofs_inode *inode,
 
 	for (pos = 0; pos < inode->i_size; pos += ret) {
 		ret = erofs_iostream_read(&tar->ios, &buf, inode->i_size - pos);
-		if (ret < 0)
+		if (ret <= 0) {
+			if (!ret)
+				ret = -EIO;
 			break;
+		}
 		if (erofs_dev_write(sbi, buf,
 				    erofs_pos(sbi, inode->u.i_blkaddr) + pos,
 				    ret)) {
@@ -649,6 +652,8 @@ static int tarerofs_write_uncompressed_file(struct erofs_inode *inode,
 	}
 	inode->idata_size = 0;
 	inode->datasource = EROFS_INODE_DATA_SOURCE_NONE;
+	if (ret < 0)
+		return ret;
 	return 0;
 }
 
@@ -673,8 +678,11 @@ static int tarerofs_write_file_data(struct erofs_inode *inode,
 
 	for (j = inode->i_size; j; ) {
 		nread = erofs_iostream_read(&tar->ios, &buf, j);
-		if (nread < 0)
+		if (nread <= 0) {
+			if (!nread)
+				nread = -EIO;
 			break;
+		}
 		if (pwrite(fd, buf, nread, off) != nread) {
 			nread = -EIO;
 			break;
@@ -684,6 +692,8 @@ static int tarerofs_write_file_data(struct erofs_inode *inode,
 	}
 	erofs_diskbuf_commit(inode->i_diskbuf, inode->i_size);
 	inode->datasource = EROFS_INODE_DATA_SOURCE_DISKBUF;
+	if (nread < 0)
+		return nread;
 	return 0;
 }
 
-- 
2.43.0



             reply	other threads:[~2026-03-02 19:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-02 19:01 David Scott [this message]
2026-03-03  1:41 ` [PATCH] erofs-utils: mkfs: fix CPU spin using --tar=f when stdin is closed Gao Xiang

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=aaXec59gbj8fIXai@beast \
    --to=dave@recoil.org \
    --cc=dave.scott@docker.com \
    --cc=linux-erofs@lists.ozlabs.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.