mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Viacheslav Dubeyko <slava@dubeyko.com>
To: Ryusuke Konishi <konishi.ryusuke@gmail.com>,
	Hengyu Liang <hengyul@cs.unc.edu>
Cc: Dave Hansen <dave.hansen@linux.intel.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-nilfs@vger.kernel.org,  linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH v2] nilfs2: count new blocks that a short copy leaves dirty
Date: Fri, 27 Nov 2026 10:13:57 -0800	[thread overview]
Message-ID: <f6dc488e8bfc7a536bcc8ae227ab5013d29691ad.camel@dubeyko.com> (raw)
In-Reply-To: <CAKFNMo=7jdKJ=yZ5PMUfDk3GPW=KFD2GrmgqZJzN9z7q4p+sRg@mail.gmail.com>

On Fri, 2026-11-27 at 22:50 +0900, Ryusuke Konishi wrote:
> On Fri, Nov 27, 2026 at 6:54 PM Hengyu Liang wrote:
> > 
> > nilfs_write_end() adds only the clean buffers of the copied range
> > to
> > the count of dirty blocks.  After a short copy, however,
> > block_write_end() also zero-fills and dirties the newly allocated
> > buffers of the part that was not copied, which is the whole range
> > when
> > the folio is not uptodate.  The write is then retried, finds these
> > buffers dirty, and does not count them either.  This skews the free
> > space calculation: nilfs_near_disk_full() does not stop the writer
> > in
> > time, and the reserved segments are consumed.
> > 
> > This has been the case since nilfs2 was merged, but it used to take
> > an
> > unusual short copy, for example from a source buffer that runs into
> > an
> > unreadable page.  Commit 665575cff098 ("filemap: move prefaulting
> > out
> > of hot write path") made it much easier to hit:
> > generic_perform_write()
> > now faults in the source buffer only after a copy into the page
> > cache
> > has made no progress, so a write from memory that is not present
> > yet
> > first calls ->write_end() with copied == 0 for each page.  As of
> > now, a
> > write from a fresh anonymous mapping is accepted far beyond the
> > free
> > space, fsync() fails with ENOSPC, and the file is gone after a
> > remount,
> > together with the other changes made since the last log was
> > written.
> > On a volume shared with another user, that user's fsync() fails
> > with
> > ENOSPC too, and the files and directories that user created after
> > the
> > write are lost the same way.
> > 
> > The issue can be reproduced on a 160 MiB disk with this program:
> > 
> >     #include <errno.h>
> >     #include <fcntl.h>
> >     #include <stdio.h>
> >     #include <stdlib.h>
> >     #include <string.h>
> >     #include <sys/mman.h>
> >     #include <unistd.h>
> > 
> >     int main(int argc, char *argv[])
> >     {
> >             size_t len = atol(argv[2]) << 20;
> >             /* fresh anonymous memory: none of its pages is present
> > yet */
> >             char *buf = mmap(NULL, len, PROT_READ,
> >                              MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> >             int fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC,
> > 0644);
> > 
> >             printf("write: %zd\n", write(fd, buf, len));
> >             printf("fsync: %s\n", fsync(fd) ? strerror(errno) :
> > "ok");
> >             return 0;
> >     }
> > 
> > and these commands:
> > 
> >     mkfs.nilfs2 -q /dev/vda
> >     mount -t nilfs2 /dev/vda /mnt
> >     ./cold /mnt/f 256
> >     umount /mnt
> >     mount -t nilfs2 /dev/vda /mnt
> >     wc -c /mnt/f
> > 
> > Before commit 665575cff098 ("filemap: move prefaulting out of hot
> > write
> > path"), the result is:
> > 
> >     write: 67518464
> >     fsync: ok
> >     67518464 /mnt/f
> > 
> > After that commit, the result is:
> > 
> >     write: 268435456
> >     fsync: No space left on device
> >     wc: /mnt/f: No such file or directory
> > 
> > With this change, the result is:
> > 
> >     write: 70217728
> >     fsync: ok
> >     70217728 /mnt/f
> > 
> > Count the buffers that block_write_end() is going to dirty,
> > including
> > the zero-filled new ones, before calling generic_write_end().
> > 
> > Fixes: 05fe58fdc10d ("nilfs2: inode operations")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Hengyu Liang <hengyul@cs.unc.edu>
> > ---
> > I found this while comparing the behavior of different kernel
> > versions.
> > I used an LLM to analyze the problem and to draft the patch and
> > this
> > message, and I analyzed the result myself.
> > 
> > v2: as Ryusuke asked, the Fixes tag now names the original
> > implementation, the stable Cc has no version, and the message says
> > the
> > bug is old and 665575cff098 made it easier to hit. No code change.
> > v1:
> > https://lore.kernel.org/linux-nilfs/20261122130247.2069007-1-hengyul@cs.unc.edu/
> > 
> > Tested on nilfs2 for-next with KASAN and lockdep, also with the
> > short
> > copy from Ryusuke's reply (1 KiB blocks, source running into a
> > PROT_NONE page): the dirty block count is right, and a nearly full
> > volume returns ENOSPC instead of accepting more than the free
> > space.
> 
> Hi Viacheslav,
> 
> This v2 patch looks good to me.  Could you please apply it directly
> to
> your tree?
> 
> Acked-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
> 
> Thanks,
> Ryusuke Konishi
> 
> > 
> >  fs/nilfs2/inode.c |  3 +--
> >  fs/nilfs2/page.c  | 41 +++++++++++++++++++++++++++++++++++++++++
> >  fs/nilfs2/page.h  |  4 ++++
> >  3 files changed, 46 insertions(+), 2 deletions(-)
> > 
> > diff --git a/fs/nilfs2/inode.c b/fs/nilfs2/inode.c
> > index b217eee57c36..dd0bed24972a 100644
> > --- a/fs/nilfs2/inode.c
> > +++ b/fs/nilfs2/inode.c
> > @@ -249,8 +249,7 @@ static int nilfs_write_end(const struct kiocb
> > *iocb,
> >         unsigned int nr_dirty;
> >         int err;
> > 
> > -       nr_dirty = nilfs_page_count_clean_buffers(folio, start,
> > -                                                 start + copied);
> > +       nr_dirty = nilfs_page_count_buffers_to_dirty(folio, start,
> > len, copied);
> >         copied = generic_write_end(iocb, mapping, pos, len, copied,
> > folio,
> >                                    fsdata);
> >         nilfs_set_file_dirty(inode, nr_dirty);
> > diff --git a/fs/nilfs2/page.c b/fs/nilfs2/page.c
> > index 857926da9f21..0dec7b5b27c4 100644
> > --- a/fs/nilfs2/page.c
> > +++ b/fs/nilfs2/page.c
> > @@ -481,6 +481,47 @@ unsigned int
> > nilfs_page_count_clean_buffers(struct folio *folio,
> >         return nc;
> >  }
> > 
> > +/**
> > + * nilfs_page_count_buffers_to_dirty - count buffers a write will
> > dirty
> > + * @folio:  folio being written to
> > + * @from:   offset of the write in @folio
> > + * @len:    number of bytes to be written
> > + * @copied: number of bytes copied into @folio
> > + *
> > + * Count the clean buffers of @folio that block_write_end() is
> > going to mark
> > + * dirty: those overlapping the copied range and, after a short
> > copy, the
> > + * newly allocated ones in the rest of the range, which it zero-
> > fills.  As
> > + * there, a short copy into a folio that is not uptodate counts as
> > a
> > + * zero-length write.
> > + *
> > + * Return: Number of buffers that the write changes from clean to
> > dirty.
> > + */
> > +unsigned int nilfs_page_count_buffers_to_dirty(struct folio
> > *folio,
> > +                                              unsigned int from,
> > +                                              unsigned int len,
> > +                                              unsigned int copied)
> > +{
> > +       unsigned int to = from + len, end = from + copied;
> > +       unsigned int block_start, block_end;
> > +       struct buffer_head *bh, *head;
> > +       unsigned int nc = 0;
> > +
> > +       if (copied < len && !folio_test_uptodate(folio))
> > +               end = from;
> > +
> > +       for (bh = head = folio_buffers(folio), block_start = 0;
> > +            bh != head || !block_start;
> > +            block_start = block_end, bh = bh->b_this_page) {
> > +               block_end = block_start + bh->b_size;
> > +               if (buffer_dirty(bh))
> > +                       continue;
> > +               if ((block_end > from && block_start < end) ||
> > +                   (buffer_new(bh) && block_end > end &&
> > block_start < to))
> > +                       nc++;
> > +       }
> > +       return nc;
> > +}
> > +
> >  /*
> >   * NILFS2 needs clear_page_dirty() in the following two cases:
> >   *
> > diff --git a/fs/nilfs2/page.h b/fs/nilfs2/page.h
> > index c3ba3468af5c..4ccd740966ec 100644
> > --- a/fs/nilfs2/page.h
> > +++ b/fs/nilfs2/page.h
> > @@ -45,6 +45,10 @@ void nilfs_clear_folio_dirty(struct folio
> > *folio, bool force);
> >  void nilfs_clear_dirty_pages(struct address_space *mapping, bool
> > force);
> >  unsigned int nilfs_page_count_clean_buffers(struct folio *folio,
> >                 unsigned int from, unsigned int to);
> > +unsigned int nilfs_page_count_buffers_to_dirty(struct folio
> > *folio,
> > +                                              unsigned int from,
> > +                                              unsigned int len,
> > +                                              unsigned int
> > copied);
> >  unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
> >                                             sector_t start_blk,
> >                                             sector_t *blkoff);
> > 
> > base-commit: 8fef1a9dec7bf58a29bc8a7ee6909ab629401cd3
> > --
> > 2.53.0
> > 

Applied.

Thanks,
Slava.

      reply	other threads:[~2026-11-27 18:14 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  9:54 Hengyu Liang
2026-11-27 13:50 ` Ryusuke Konishi
2026-11-27 18:13   ` Viacheslav Dubeyko [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=f6dc488e8bfc7a536bcc8ae227ab5013d29691ad.camel@dubeyko.com \
    --to=slava@dubeyko.com \
    --cc=akpm@linux-foundation.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=hengyul@cs.unc.edu \
    --cc=konishi.ryusuke@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nilfs@vger.kernel.org \
    --cc=stable@vger.kernel.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®