Linux EXT4 FS development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Christian Brauner" <brauner@kernel.org>, "Jan Kara" <jack@suse.com>
Cc: "Al Viro" <viro@zeniv.linux.org.uk>,
	linux-ext4@vger.kernel.org, linux-mm@kvack.org,
	linux-efi@vger.kernel.org, linux-mtd@lists.infradead.org,
	dsterba@suse.cz, tytso@mit.edu, "Mark Fasheh" <mark@fasheh.com>,
	"Joel Becker" <jlbec@evilplan.org>,
	"Joseph Qi" <joseph.qi@linux.alibaba.com>
Subject: Re: [PATCH v3 30/33] ubifs: don't set SB_RDONLY after errors
Date: Sat, 14 Nov 2026 12:03:52 +0000	[thread overview]
Message-ID: <sashiko-outbox-214481@kernel.org> (raw)
In-Reply-To: <20261113-work-fs-sb_rdonly-v3-30-0be92bb73709@kernel.org>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] ubifs: false space accounting error in ubifs_remount_ro()
  after error
- [Medium] ubifs: ubifs_fsync() trips !c->ro_mount assert after error
  remount
- [Medium] ubifs: race between in-flight writeback and
  ubifs_remount_ro() after error
--

commit fda6fbd307537dfd99ef75b3610022c5af4a9353
Author: Christian Brauner <brauner@kernel.org>

ubifs: don't set SB_RDONLY after errors

This commit switches ubifs_ro_mode() from setting SB_RDONLY directly on
s_flags to calling sb_set_emergency_ro(), allows remounting read-only after
an error, and drops dirty folios and inodes during writeback once
c->ro_error is set.

> diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c
> index 38972786817e7..2d71274e89ad0 100644
> --- a/fs/ubifs/super.c
> +++ b/fs/ubifs/super.c
[ ... ]
> @@ -1839,19 +1846,26 @@ static void ubifs_remount_ro(struct ubifs_info *c)
>  
>  	dbg_save_space_info(c);
>  
> -	for (i = 0; i < c->jhead_cnt; i++) {
> -		err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
> +	/* Like ubifs_put_super(), don't write anything after an error. */
> +	if (!c->ro_error) {
> +		for (i = 0; i < c->jhead_cnt; i++) {
> +			err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
> +			if (err)
> +				ubifs_ro_mode(c, err);
> +		}
> +
> +		c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
> +		c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
> +		c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
> +		err = ubifs_write_master(c);
>  		if (err)
>  			ubifs_ro_mode(c, err);
> +	} else {
> +		/* Syncing the write-buffers would have canceled them. */
> +		for (i = 0; i < c->jhead_cnt; i++)
> +			hrtimer_cancel(&c->jheads[i].wbuf.timer);
>  	}

[Severity: Medium]
Can an in-flight ubifs_fsync() trip the !c->ro_mount assertion in
ubifs_wbuf_sync_nolock() when racing with a read-only remount after an
error?

When c->ro_error is set, skipping ubifs_wbuf_sync() leaves wbuf->used and
wbuf->inodes[] populated while ubifs_remount_ro() sets c->ro_mount = 1.
Because vfs_fsync_range() does not hold s_umount or mnt_want_write(), an
fsync() on an inode whose node is already buffered in a wbuf (with no dirty
folios and ui->dirty == 0) can interleave with ubifs_remount_ro():

CPU0                              CPU1
ubifs_fsync()
  if (c->ro_mount)
    return 0;
  file_write_and_wait_range()
  ubifs_write_inode() -> 0
                                  ubifs_remount_ro()
                                    hrtimer_cancel(&wbuf->timer)
                                    c->ro_mount = 1
  ubifs_sync_wbufs_by_inode()
    ubifs_wbuf_sync_nolock(wbuf)
      ubifs_assert(c, !c->ro_media && !c->ro_mount)
        <-- assertion failure

In ubifs_wbuf_sync_nolock(), the !c->ro_mount assertion runs before the
if (c->ro_error) return -EROFS check, causing a warning and stack dump (or
a panic when mounted with assert=panic).

> -
> -	c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
> -	c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
> -	c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
> -	err = ubifs_write_master(c);
> -	if (err)
> -		ubifs_ro_mode(c, err);
> -
>  	vfree(c->orph_buf);
>  	c->orph_buf = NULL;
>  	kfree(c->write_reserve_buf);

[Severity: Medium]
Can ubifs_remount_ro() free c->write_reserve_buf and set c->ro_mount = 1
while an in-flight do_writepage() is still running?

Once ubifs_ro_mode() calls sb_set_emergency_ro(), sync_filesystem(sb) in
ubifs_reconfigure() returns immediately at sb_rdonly_any(sb) without
calling sync_inodes_sb() or waiting for in-flight writeback.  A writeback
caller that does not take mnt_want_write() or s_umount (such as
sync_file_range()) can pass the unlocked c->ro_error check in
do_writepage() just before an error occurs and then race with
ubifs_remount_ro():

CPU0                              CPU1
do_writepage()
  if (unlikely(c->ro_error))
  ubifs_jnl_write_data()
                                  ubifs_ro_mode()
                                    c->ro_error = 1
                                    sb_set_emergency_ro(sb)
                                  ubifs_reconfigure()
                                    sync_filesystem(sb) -> 0
                                    ubifs_remount_ro()
                                      kfree(c->write_reserve_buf)
                                      c->write_reserve_buf = NULL
                                      c->ro_mount = 1
    mutex_lock(&c->write_reserve_mutex)
    data = c->write_reserve_buf
    data->ch.node_type = ...
      <-- NULL dereference or use-after-free

Even when kmalloc() succeeds in ubifs_jnl_write_data(), make_reservation()
subsequently calls reserve_space(), tripping
ubifs_assert(c, !c->ro_media && !c->ro_mount).

> @@ -1977,10 +1991,7 @@ static int ubifs_reconfigure(struct fs_context *fc)
>  		if (err)
>  			return err;
>  	} else if (!c->ro_mount && (fc->sb_flags & SB_RDONLY)) {
> -		if (c->ro_error) {
> -			ubifs_msg(c, "cannot re-mount R/O due to prior errors");
> -			return -EROFS;
> -		}
> +		/* After an error ubifs_remount_ro() can't write but still works. */
>  		ubifs_remount_ro(c);
>  	}

[Severity: Medium]
Should ubifs_remount_ro() skip dbg_save_space_info() and
dbg_check_space_info() when c->ro_error is set, as ubifs_put_super() and
the taken_empty_lebs check in ubifs_reconfigure() do?

Because sync_filesystem() is a no-op once sb_set_emergency_ro() is set,
dirty folios and dirty inodes remain pending during ubifs_remount_ro().
dbg_save_space_info() snapshots ubifs_get_free_space_nolock(), which
includes c->bi.data_growth and c->bi.dd_growth.  If a concurrent writeback
or folio release drops a dirty folio via do_writepage() or a dirty inode
via ubifs_write_inode() before dbg_check_space_info() runs at the end of
ubifs_remount_ro(), releasing that page or inode budget changes the
reported free space and causes dbg_check_space_info() to log a false "free
space changed" error and call dump_stack().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261113-work-fs-sb_rdonly-v3-0-0be92bb73709@kernel.org?part=30

  parent reply	other threads:[~2026-11-14 12:03 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 11:58 [PATCH v3 00/33] fs: don't set SB_RDONLY after filesystem errors Christian Brauner
2026-11-13 11:58 ` [PATCH v3 01/33] jfs: free uncommitted anonymous tlocks when evicting an inode Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:58 ` [PATCH v3 02/33] jfs: use jfs_symlink_inode_operations in jfs_iget() Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:58 ` [PATCH v3 03/33] ecryptfs: don't leak the inode when a lookup can't open the lower file Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 04/33] ecryptfs: fall back to a read-only " Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 05/33] fs: add a generic emergency read-only state Christian Brauner
2026-11-13 14:53   ` Jan Kara
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 06/33] ext2: don't set SB_RDONLY after filesystem errors Christian Brauner
2026-11-13 14:54   ` Jan Kara
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 07/33] jfs: " Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 08/33] nilfs2: " Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 09/33] fat: " Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 10/33] exfat: keep the current mount options on remount Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 11/33] exfat: don't set SB_RDONLY after filesystem errors Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 12/33] exfat: use the generic emergency read-only state Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 13/33] hfs: don't set SB_RDONLY after MDB write errors Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 14/33] hfs: use the generic emergency read-only state Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 15/33] hfsplus: don't set SB_RDONLY when creating the attributes tree Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 16/33] affs: don't set SB_RDONLY after filesystem errors Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 17/33] ufs: " Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 18/33] hpfs: " Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 19/33] ntfs: " Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 20/33] ntfs: use the generic emergency read-only state Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 21/33] ocfs2: keep the current mount options on remount Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 22/33] ocfs2: don't set SB_RDONLY after filesystem errors Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 23/33] zonefs: don't set SB_RDONLY after I/O errors Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 24/33] zonefs: use the generic emergency read-only state Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 25/33] btrfs: check BTRFS_FS_STATE_RO instead of SB_RDONLY at runtime Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 26/33] btrfs: don't set SB_RDONLY after filesystem errors Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 27/33] gfs2: wait for a pending withdraw before remounting read-only Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 28/33] gfs2: don't set SB_RDONLY on withdraw Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 29/33] efivarfs: stop using SB_RDONLY for read-only efivar ops Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 30/33] ubifs: don't set SB_RDONLY after errors Christian Brauner
2026-11-14  2:17   ` Zhihao Cheng
2026-11-14 12:03   ` sashiko-bot [this message]
2026-11-13 11:59 ` [PATCH v3 31/33] ext4: allow remounting read-only in the emergency state Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 32/33] ext4: use the generic emergency read-only state Christian Brauner
2026-11-14 12:03   ` sashiko-bot
2026-11-13 11:59 ` [PATCH v3 33/33] xfs: allow remounting read-only after a shutdown Christian Brauner
2026-11-14 12:03   ` sashiko-bot

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=sashiko-outbox-214481@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=brauner@kernel.org \
    --cc=dsterba@suse.cz \
    --cc=jack@suse.com \
    --cc=jlbec@evilplan.org \
    --cc=joseph.qi@linux.alibaba.com \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=mark@fasheh.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tytso@mit.edu \
    --cc=viro@zeniv.linux.org.uk \
    /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