Skip to contentSkip to conversation

nilfs2: prevent stale B-tree node pages from lingering in page cache

PATCH New Ryusuke Konishi 1 participant 1 message Dec 1 NILFS2 filesystem

lore
Share & tools
lore

Ryusuke Konishi

author maintainer
#0 lore raw

PATCH 1/1 nilfs2: prevent stale B-tree node pages from lingering in page cache

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")

---
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(-)
fs/nilfs2/bmap.c +13 -6
index 83f6ea30cc8b..5a3596e0aedf 100644
@@ -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);
fs/nilfs2/bmap.h +4 -2
index 6493217b1a7f..26dd15a968f9 100644
@@ -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 *);
fs/nilfs2/btree.c +38 -16

112 lines not on this page · raw message

fs/nilfs2/direct.c +14 -7
index 18a700db76ed..60211ee9aebe 100644
@@ -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;
 }
 
fs/nilfs2/segment.c +20 -9
index 5765959617fe..34016804af14 100644
@@ -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;
 		}
Signature · 1 line
2.53.0