Re: [PATCH] nilfs2: prevent stale B-tree node pages from lingering in page cache

From: Viacheslav Dubeyko

Date: Tue Dec 01 2026 - 13:09:43 EST


On Tue, 2026-12-01 at 18:17 +0900, Ryusuke Konishi wrote:
> When the block size differs from the page size, writing to files
> leaves
> unreferenced, stale B-tree node pages (folios) lingering in the B-
> tree
> node page cache until they are evicted by memory reclaim.
>
> This is a design flaw stemming from the original interface of
> nilfs_bmap_propagate().  For files other than the DAT, propagating
> dirty
> state to parent B-tree nodes updates the virtual block numbers of
> those
> nodes.  Because B-tree node buffers are indexed in the page cache by
> their virtual block number, a node buffer is relocated to a different
> page offset when its virtual block number changes.  When the block
> size
> is smaller than the page size, nilfs_btree_propagate() allocates a
> new
> buffer head at the new location, copies the node data, and releases
> the
> old buffer head via nilfs_btnode_commit_change_key().  However, the
> callers up to nilfs_segctor_apply_buffers() hold a reference to the
> original buffer head throughout the call chain, causing page
> invalidation of the old buffer's folio to always fail even when it
> was
> the last valid buffer on that folio.
>
> Fix this issue by transferring ownership of the buffer head reference
> along the propagation call path:
>
> - Change nilfs_bmap_propagate() and the bop_propagate operation to
>   consume the caller's reference to the input buffer head, and add an
>   output argument out_bh to return a reference to the resulting
> buffer
>   head (the replacement if relocated, or the original otherwise) when
>   needed.
>
> - Pass the buffer head reference through
> nilfs_segctor_apply_buffers(),
>   the segment constructor collect() callbacks,
> nilfs_bmap_propagate(),
>   and nilfs_{btree,direct}_propagate() instead of retaining it in the
>   callers, allowing nilfs_btnode_commit_change_key() to actively free
>   and invalidate the old buffer.
>
> - Remove the redundant buffer head argument passed down to internal
>   propagation helpers in btree.c, keeping reference handoff and
> release
>   consolidated in nilfs_btree_propagate() rather than leaking extra
>   buffer references into lower-level routines.
>
> Fixes: 17c76b0104e4 ("nilfs2: B-tree based block mapping")
> Signed-off-by: Ryusuke Konishi <konishi.ryusuke@xxxxxxxxx>
> ---
> Hi Viacheslav,
>
> Could you please apply this for the next cycle?
>
> This fixes an issue on filesystems with small block sizes (smaller
> than the page size) where stale B-tree node buffers and their
> containing
> folios remain in the B-tree node page cache every time a B-tree node
> is
> updated, until memory reclaim eventually evicts them.
>
> Thanks,
> Ryusuke Konishi
>
>  fs/nilfs2/bmap.c    | 19 +++++++++++-----
>  fs/nilfs2/bmap.h    |  6 +++--
>  fs/nilfs2/btree.c   | 54 +++++++++++++++++++++++++++++++------------
> --
>  fs/nilfs2/direct.c  | 21 ++++++++++++------
>  fs/nilfs2/segment.c | 29 ++++++++++++++++--------
>  5 files changed, 89 insertions(+), 40 deletions(-)
>
> diff --git a/fs/nilfs2/bmap.c b/fs/nilfs2/bmap.c
> index 83f6ea30cc8b..5a3596e0aedf 100644
> --- a/fs/nilfs2/bmap.c
> +++ b/fs/nilfs2/bmap.c
> @@ -310,23 +310,30 @@ void nilfs_bmap_clear(struct nilfs_bmap *bmap)
>  
>  /**
>   * nilfs_bmap_propagate - propagate dirty state
> - * @bmap: bmap
> - * @bh: buffer head
> + * @bmap:   bmap
> + * @bh:     buffer head at the point of origin for propagation
> + * @out_bh: location to store the up-to-date buffer head (out)
>   *
> - * Description: nilfs_bmap_propagate() marks the buffers that
> directly or
> - * indirectly refer to the block specified by @bh dirty.
> + * nilfs_bmap_propagate() marks as dirty any buffers that either
> directly or
> + * indirectly refer to the block of the buffer head @bh.  The block
> may be
> + * relocated in the page cache due to an update of its virtual block
> number.
> + * Regardless of success or failure, the caller's reference to @bh
> is dropped.
> + * On success, if @out_bh is not %NULL, a pointer to the latest
> buffer head
> + * (the new one if relocated, or @bh otherwise) is returned via
> @out_bh with
> + * a reference.
>   *
>   * Return: 0 on success, or one of the following negative error
> codes on
>   * failure:
>   * * %-EIO - I/O error (including metadata corruption).
>   * * %-ENOMEM - Insufficient memory available.
>   */
> -int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head
> *bh)
> +int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head
> *bh,
> + struct buffer_head **out_bh)
>  {
>   int ret;
>  
>   down_write(&bmap->b_sem);
> - ret = bmap->b_ops->bop_propagate(bmap, bh);
> + ret = bmap->b_ops->bop_propagate(bmap, bh, out_bh);
>   up_write(&bmap->b_sem);
>  
>   return nilfs_bmap_convert_error(bmap, __func__, ret);
> diff --git a/fs/nilfs2/bmap.h b/fs/nilfs2/bmap.h
> index 6493217b1a7f..26dd15a968f9 100644
> --- a/fs/nilfs2/bmap.h
> +++ b/fs/nilfs2/bmap.h
> @@ -67,7 +67,8 @@ struct nilfs_bmap_operations {
>   int (*bop_delete)(struct nilfs_bmap *bmap, __u64 key, bool
> deform);
>   void (*bop_clear)(struct nilfs_bmap *);
>  
> - int (*bop_propagate)(struct nilfs_bmap *, struct buffer_head
> *);
> + int (*bop_propagate)(struct nilfs_bmap *bmap, struct
> buffer_head *bh,
> +      struct buffer_head **out_bh);
>   void (*bop_lookup_dirty_buffers)(struct nilfs_bmap *,
>   struct list_head *);
>  
> @@ -167,7 +168,8 @@ int nilfs_bmap_seek_key(struct nilfs_bmap *bmap,
> __u64 start, __u64 *keyp);
>  int nilfs_bmap_last_key(struct nilfs_bmap *bmap, __u64 *keyp);
>  int nilfs_bmap_truncate(struct nilfs_bmap *bmap, __u64 key);
>  void nilfs_bmap_clear(struct nilfs_bmap *);
> -int nilfs_bmap_propagate(struct nilfs_bmap *, struct buffer_head *);
> +int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head
> *bh,
> + struct buffer_head **out_bh);
>  void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *, struct
> list_head *);
>  int nilfs_bmap_assign(struct nilfs_bmap *, struct buffer_head **,
>         unsigned long, union nilfs_binfo *);
> diff --git a/fs/nilfs2/btree.c b/fs/nilfs2/btree.c
> index 8c12b78c0e9d..552ae7199592 100644
> --- a/fs/nilfs2/btree.c
> +++ b/fs/nilfs2/btree.c
> @@ -1949,8 +1949,7 @@ int nilfs_btree_convert_and_insert(struct
> nilfs_bmap *btree,
>  
>  static int nilfs_btree_propagate_p(struct nilfs_bmap *btree,
>      struct nilfs_btree_path *path,
> -    int level,
> -    struct buffer_head *bh)
> +    int level)
>  {
>   while ((++level < nilfs_btree_height(btree) - 1) &&
>          !buffer_dirty(path[level].bp_bh))
> @@ -2068,7 +2067,6 @@ static int
> nilfs_btree_prepare_propagate_v(struct nilfs_bmap *btree,
>  static void nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree,
>      struct nilfs_btree_path
> *path,
>      int minlevel, int
> maxlevel,
> -    struct buffer_head *bh,
>      struct inode *dat)
>  {
>   int level;
> @@ -2082,7 +2080,7 @@ static void
> nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree,
>  
>  static int nilfs_btree_propagate_v(struct nilfs_bmap *btree,
>      struct nilfs_btree_path *path,
> -    int level, struct buffer_head
> *bh)
> +    int level)
>  {
>   int maxlevel = 0, ret;
>   struct nilfs_btree_node *parent;
> @@ -2090,8 +2088,6 @@ static int nilfs_btree_propagate_v(struct
> nilfs_bmap *btree,
>   __u64 ptr;
>   int ncmax;
>  
> - get_bh(bh);
> - path[level].bp_bh = bh;
>   ret = nilfs_btree_prepare_propagate_v(btree, path, level,
> &maxlevel,
>         dat);
>   if (ret < 0)
> @@ -2107,16 +2103,15 @@ static int nilfs_btree_propagate_v(struct
> nilfs_bmap *btree,
>   goto out;
>   }
>  
> - nilfs_btree_commit_propagate_v(btree, path, level, maxlevel,
> bh, dat);
> + nilfs_btree_commit_propagate_v(btree, path, level, maxlevel,
> dat);
>  
>   out:
> - brelse(path[level].bp_bh);
> - path[level].bp_bh = NULL;
>   return ret;
>  }
>  
>  static int nilfs_btree_propagate(struct nilfs_bmap *btree,
> - struct buffer_head *bh)
> + struct buffer_head *bh,
> + struct buffer_head **out_bh)
>  {
>   struct nilfs_btree_path *path;
>   struct nilfs_btree_node *node;
> @@ -2126,8 +2121,10 @@ static int nilfs_btree_propagate(struct
> nilfs_bmap *btree,
>   WARN_ON(!buffer_dirty(bh));
>  
>   path = nilfs_btree_alloc_path();
> - if (path == NULL)
> + if (unlikely(!path)) {
> + brelse(bh);
>   return -ENOMEM;
> + }
>  
>   if (buffer_nilfs_node(bh)) {
>   node = (struct nilfs_btree_node *)bh->b_data;
> @@ -2150,20 +2147,45 @@ static int nilfs_btree_propagate(struct
> nilfs_bmap *btree,
>   goto out;
>   }
>  
> - ret = NILFS_BMAP_USE_VBN(btree) ?
> - nilfs_btree_propagate_v(btree, path, level, bh) :
> - nilfs_btree_propagate_p(btree, path, level, bh);
> + if (NILFS_BMAP_USE_VBN(btree)) {
> + /*
> + * Lend the buffer reference to path[level].bp_bh
> during the
> + * nilfs_btree_propagate_v() call and retrieve it
> for output
> + * afterward, as the buffer may be replaced due to
> an update
> + * for its virtual block number.
> + */
> + path[level].bp_bh = bh;
> +
> + ret = nilfs_btree_propagate_v(btree, path, level);
> +
> + bh = path[level].bp_bh;
> + path[level].bp_bh = NULL;
> + } else {
> + ret = nilfs_btree_propagate_p(btree, path, level);
> + }
>  
>   out:
>   nilfs_btree_free_path(path);
>  
> + if (out_bh && likely(!ret))
> + *out_bh = bh;
> + else
> + brelse(bh);
>   return ret;
>  }
>  
>  static int nilfs_btree_propagate_gc(struct nilfs_bmap *btree,
> -     struct buffer_head *bh)
> +     struct buffer_head *bh,
> +     struct buffer_head **out_bh)
>  {
> - return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), bh-
> >b_blocknr);
> + int ret = nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree),
> +        bh->b_blocknr);
> +
> + if (out_bh && likely(!ret))
> + *out_bh = bh;
> + else
> + brelse(bh);
> + return ret;
>  }
>  
>  static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree,
> diff --git a/fs/nilfs2/direct.c b/fs/nilfs2/direct.c
> index 18a700db76ed..60211ee9aebe 100644
> --- a/fs/nilfs2/direct.c
> +++ b/fs/nilfs2/direct.c
> @@ -259,35 +259,37 @@ int nilfs_direct_delete_and_convert(struct
> nilfs_bmap *bmap,
>  }
>  
>  static int nilfs_direct_propagate(struct nilfs_bmap *bmap,
> -   struct buffer_head *bh)
> +   struct buffer_head *bh,
> +   struct buffer_head **out_bh)
>  {
>   struct nilfs_palloc_req oldreq, newreq;
>   struct inode *dat;
>   __u64 key;
>   __u64 ptr;
> - int ret;
> + int ret = 0;
>  
>   if (!NILFS_BMAP_USE_VBN(bmap))
> - return 0;
> + goto out;
>  
> + ret = -EINVAL;
>   if (WARN_ON_ONCE(buffer_nilfs_node(bh)))
> - return -EINVAL;
> + goto out;
>  
>   dat = nilfs_bmap_get_dat(bmap);
>   key = nilfs_bmap_data_get_key(bmap, bh);
>   if (WARN_ON_ONCE(key > NILFS_DIRECT_KEY_MAX))
> - return -EINVAL;
> + goto out;
>  
>   ptr = nilfs_direct_get_ptr(bmap, key);
>   if (ptr == NILFS_BMAP_INVALID_PTR)
> - return -EINVAL;
> + goto out;
>  
>   if (!buffer_nilfs_volatile(bh)) {
>   oldreq.pr_entry_nr = ptr;
>   newreq.pr_entry_nr = ptr;
>   ret = nilfs_dat_prepare_update(dat, &oldreq,
> &newreq);
>   if (ret < 0)
> - return ret;
> + goto out;
>   nilfs_dat_commit_update(dat, &oldreq, &newreq,
>   bmap->b_ptr_type ==
> NILFS_BMAP_PTR_VS);
>   set_buffer_nilfs_volatile(bh);
> @@ -295,6 +297,11 @@ static int nilfs_direct_propagate(struct
> nilfs_bmap *bmap,
>   } else
>   ret = nilfs_dat_mark_dirty(dat, ptr);
>  
> +out:
> + if (out_bh && likely(!ret))
> + *out_bh = bh;
> + else
> + brelse(bh);
>   return ret;
>  }
>  
> diff --git a/fs/nilfs2/segment.c b/fs/nilfs2/segment.c
> index 5765959617fe..34016804af14 100644
> --- a/fs/nilfs2/segment.c
> +++ b/fs/nilfs2/segment.c
> @@ -586,16 +586,18 @@ static int nilfs_segctor_add_file_block(struct
> nilfs_sc_info *sci,
>  static int nilfs_collect_file_data(struct nilfs_sc_info *sci,
>      struct buffer_head *bh, struct
> inode *inode)
>  {
> + struct buffer_head *out_bh;
>   int err;
>  
> - err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
> + err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh,
> &out_bh);
>   if (err < 0)
>   return err;
>  
> - err = nilfs_segctor_add_file_block(sci, bh, inode,
> + err = nilfs_segctor_add_file_block(sci, out_bh, inode,
>      sizeof(struct
> nilfs_binfo_v));
>   if (!err)
>   sci->sc_datablk_cnt++;
> + brelse(out_bh);
>   return err;
>  }
>  
> @@ -603,15 +605,19 @@ static int nilfs_collect_file_node(struct
> nilfs_sc_info *sci,
>      struct buffer_head *bh,
>      struct inode *inode)
>  {
> - return nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
> + return nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh,
> NULL);
>  }
>  
>  static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci,
>      struct buffer_head *bh,
>      struct inode *inode)
>  {
> + int err;
> +
>   WARN_ON(!buffer_dirty(bh));
> - return nilfs_segctor_add_file_block(sci, bh, inode,
> sizeof(__le64));
> + err = nilfs_segctor_add_file_block(sci, bh, inode,
> sizeof(__le64));
> + brelse(bh);
> + return err;
>  }
>  
>  static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci,
> @@ -643,24 +649,30 @@ static const struct nilfs_sc_operations
> nilfs_sc_file_ops = {
>  static int nilfs_collect_dat_data(struct nilfs_sc_info *sci,
>     struct buffer_head *bh, struct
> inode *inode)
>  {
> + struct buffer_head *out_bh;
>   int err;
>  
> - err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
> + err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh,
> &out_bh);
>   if (err < 0)
>   return err;
>  
> - err = nilfs_segctor_add_file_block(sci, bh, inode,
> sizeof(__le64));
> + err = nilfs_segctor_add_file_block(sci, out_bh, inode,
> sizeof(__le64));
>   if (!err)
>   sci->sc_datablk_cnt++;
> + brelse(out_bh);
>   return err;
>  }
>  
>  static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci,
>     struct buffer_head *bh, struct
> inode *inode)
>  {
> + int err;
> +
>   WARN_ON(!buffer_dirty(bh));
> - return nilfs_segctor_add_file_block(sci, bh, inode,
> -     sizeof(struct
> nilfs_binfo_dat));
> + err = nilfs_segctor_add_file_block(sci, bh, inode,
> +    sizeof(struct
> nilfs_binfo_dat));
> + brelse(bh);
> + return err;
>  }
>  
>  static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci,
> @@ -1008,7 +1020,6 @@ static int nilfs_segctor_apply_buffers(struct
> nilfs_sc_info *sci,
>   list_for_each_entry_safe(bh, n, listp,
> b_assoc_buffers) {
>   list_del_init(&bh->b_assoc_buffers);
>   err = collect(sci, bh, inode);
> - brelse(bh);
>   if (unlikely(err))
>   goto dispose_buffers;
>   }

Applied.

Thanks,
Slava.