Skip to contentSkip to conversation

jfs: implement migrate_folio for jfs_metapage_aops

PATCH Superseded Shivank Garg 2 participants 4 messages Apr 13, 2025 – Apr 14, 2025

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 jfs: implement migrate_folio for jfs_metapage_aops

Add the missing migrate_folio operation to jfs_metapage_aops to fix warnings during memory compaction. These warnings were introduced by commit 7ee3647243e5 ("migrate: Remove call to ->writepage") which added explicit warnings when filesystems don't implement migrate_folio.

System reports following warnings:
jfs_metapage_aops does not implement migrate_folio

  WARNING: CPU: 0 PID: 6870 at mm/migrate.c:955 fallback_migrate_folio mm/migrate.c:953 [inline]
  WARNING: CPU: 0 PID: 6870 at mm/migrate.c:955 move_to_new_folio+0x70e/0x840 mm/migrate.c:1007

Implement the migrate_folio handler by delegating to the filemap_migrate_folio() function.

---
 fs/jfs/jfs_metapage.c | 1 +
 1 file changed, 1 insertion(+)
fs/jfs/jfs_metapage.c +1 -0
index df575a873ec6..868ab7d16ab3 100644
@@ -570,6 +570,7 @@ const struct address_space_operations jfs_metapage_aops = {
 	.release_folio	= metapage_release_folio,
 	.invalidate_folio = metapage_invalidate_folio,
 	.dirty_folio	= filemap_dirty_folio,
+	.migrate_folio	= filemap_migrate_folio,
 };
 
 struct metapage *__get_metapage(struct inode *inode, unsigned long lblock,
Signature · 1 line
2.34.1
#1 lore raw

fs/jfs/jfs_metapage.c const struct address_space_operations jfs_metapage_aops = { · line 574

Quoted 7 lines · Shivank Garg

+++ b/fs/jfs/jfs_metapage.c

fs/jfs/jfs_metapage.c +1 -0
@@ -570,6 +570,7 @@ const struct address_space_operations jfs_metapage_aops = {
 	.release_folio	= metapage_release_folio,
 	.invalidate_folio = metapage_invalidate_folio,
 	.dirty_folio	= filemap_dirty_folio,
+	.migrate_folio	= filemap_migrate_folio,
 };

Ooh, damn, I think we're going to need more than this ;-(

static inline struct metapage *folio_to_mp(struct folio *folio, int offset)

{
        return folio->private;
}

struct metapage {
...
void *data; /* Data pointer */

So we're going to need something like buffer_migrate_folio(), but specialised to jfs metadata. And when we come up with it, that'll be Fixes: 35474d52c605

#2 lore raw
Quoted 16 lines · Matthew Wilcox

fs/jfs/jfs_metapage.c const struct address_space_operations jfs_metapage_aops = { · line 574

Quoted 7 lines · Shivank Garg

+++ b/fs/jfs/jfs_metapage.c

fs/jfs/jfs_metapage.c +1 -0
@@ -570,6 +570,7 @@ const struct address_space_operations jfs_metapage_aops = {
 	.release_folio	= metapage_release_folio,
 	.invalidate_folio = metapage_invalidate_folio,
 	.dirty_folio	= filemap_dirty_folio,
+	.migrate_folio	= filemap_migrate_folio,
 };

Ooh, damn, I think we're going to need more than this ;-(

static inline struct metapage *folio_to_mp(struct folio *folio, int offset)

{
        return folio->private;
}
Quoted 7 lines

struct metapage {
...
void *data; /* Data pointer */

So we're going to need something like buffer_migrate_folio(), but specialised to jfs metadata. And when we come up with it, that'll be Fixes: 35474d52c605

Thanks for the pointers. I missed the private data aspect earlier. I think it's doable - IIUC, I'll need to
update mp->folio and mp->data to point to the dst folio info. I'll work on a patch for it.

Best Regards,
Shivank

#3 lore raw
In reply to Matthew Wilcox
Quoted 16 lines · Matthew Wilcox

Review const struct address_space_operations jfs_metapage_aops = { · line 574

Quoted 7 lines · Shivank Garg

+++ b/fs/jfs/jfs_metapage.c

(quoted diff) +1 -0
@@ -570,6 +570,7 @@ const struct address_space_operations jfs_metapage_aops = {
 	.release_folio	= metapage_release_folio,
 	.invalidate_folio = metapage_invalidate_folio,
 	.dirty_folio	= filemap_dirty_folio,
+	.migrate_folio	= filemap_migrate_folio,
 };

Ooh, damn, I think we're going to need more than this ;-(

static inline struct metapage *folio_to_mp(struct folio *folio, int offset)

{
        return folio->private;
}
Quoted 7 lines

struct metapage {
...
void *data; /* Data pointer */

So we're going to need something like buffer_migrate_folio(), but specialised to jfs metadata. And when we come up with it, that'll be Fixes: 35474d52c605

I'm able to come up with a revised patch but testing seem tricky. I'm not sure how to trigger the metapage migration.

From 0cd57d102a07f453d4a722215ce8ae34ff81c0a1 Mon Sep 17 00:00:00 2001
From: Shivank Garg <[email protected]>
Date: Sun, 13 Apr 2025 16:40:53 +0000
Subject: [PATCH] jfs: implement migrate_folio for jfs_metapage_aops

Add the missing migrate_folio operation to jfs_metapage_aops to fix warnings during memory compaction. These warnings were introduced by commit 7ee3647243e5 ("migrate: Remove call to ->writepage") which added explicit warnings when filesystems don't implement migrate_folio.

System reports following warnings:
jfs_metapage_aops does not implement migrate_folio

  WARNING: CPU: 0 PID: 6870 at mm/migrate.c:955 fallback_migrate_folio mm/migrate.c:953 [inline]
  WARNING: CPU: 0 PID: 6870 at mm/migrate.c:955 move_to_new_folio+0x70e/0x840 mm/migrate.c:1007

Implement metapage_migrate_folio which handles both single and multiple metapages per page configurations.

Fixes 35474d52c605 ("jfs: Convert metapage_writepage to metapage_write_folio")
Reported-by [email protected]
---
 fs/jfs/jfs_metapage.c   | 94 +++++++++++++++++++++++++++++++++++++++++
 include/linux/migrate.h |  1 +
 mm/migrate.c            |  3 +-
 3 files changed, 97 insertions(+), 1 deletion(-)
fs/jfs/jfs_metapage.c +94 -0
index df575a873ec6..50759a846e22 100644
@@ -15,6 +15,7 @@
 #include <linux/mempool.h>
 #include <linux/seq_file.h>
 #include <linux/writeback.h>
+#include <linux/migrate.h>
 #include "jfs_incore.h"
 #include "jfs_superblock.h"
 #include "jfs_filsys.h"
@@ -151,6 +152,54 @@ static inline void dec_io(struct folio *folio, blk_status_t status,
 		handler(folio, anchor->status);
 }
 
+static int __metapage_migrate_folio(struct address_space *mapping, struct folio *dst,
+				    struct folio *src, enum migrate_mode mode)
+{
+	struct meta_anchor *src_anchor = src->private;
+	struct metapage *mps[MPS_PER_PAGE] = {0};
+	struct metapage *mp;
+	int i, rc;
+
+	for (i = 0; i < MPS_PER_PAGE; i++) {
+		mp = src_anchor->mp[i];
+		if (mp && metapage_locked(mp))
+			return -EAGAIN;
+	}
+
+	rc = filemap_migrate_folio(mapping, dst, src, mode);
+	if (rc != MIGRATEPAGE_SUCCESS)
+		return rc;
+
+	for (i = 0; i < MPS_PER_PAGE; i++) {
+		mp = src_anchor->mp[i];
+		if (!mp)
+			continue;
+		if (unlikely(insert_metapage(dst, mp))) {
+			/* If error, roll-back previosly inserted pages */
+			for (int j = 0 ; j < i; j++) {
+				if (mps[j])
+					remove_metapage(dst, mps[j]);
+			}
+			return -EAGAIN;
+		}
+		mps[i] = mp;
+	}
+
+	/* Update the metapage and remove it from src */
+	for (int i = 0; i < MPS_PER_PAGE; i++) {
+		mp = mps[i];
+		if (mp) {
+			int page_offset = mp->data - folio_address(src);
+
+			mp->data = folio_address(dst) + page_offset;
+			mp->folio = dst;
+			remove_metapage(src, mp);
+		}
+	}
+
+	return MIGRATEPAGE_SUCCESS;
+}
+
 #else
 static inline struct metapage *folio_to_mp(struct folio *folio, int offset)
 {
@@ -175,6 +224,32 @@ static inline void remove_metapage(struct folio *folio, struct metapage *mp)
 #define inc_io(folio) do {} while(0)
 #define dec_io(folio, status, handler) handler(folio, status)
 
+static int __metapage_migrate_folio(struct address_space *mapping, struct folio *dst,
+				    struct folio *src, enum migrate_mode mode)
+{
+	struct metapage *mp;
+	int page_offset;
+	int rc;
+
+	mp = folio_to_mp(src, 0);
+	if (mp && metapage_locked(mp))
+		return -EAGAIN;
+
+	rc = filemap_migrate_folio(mapping, dst, src, mode);
+	if (rc != MIGRATEPAGE_SUCCESS)
+		return rc;
+
+	if (unlikely(insert_metapage(src, mp)))
+		return -EAGAIN;
+
+	page_offset = mp->data - folio_address(src);
+	mp->data = folio_address(dst) + page_offset;
+	mp->folio = dst;
+	remove_metapage(src, mp);
+
+	return MIGRATEPAGE_SUCCESS;
+}
+
 #endif
 
 static inline struct metapage *alloc_metapage(gfp_t gfp_mask)
@@ -554,6 +629,24 @@ static bool metapage_release_folio(struct folio *folio, gfp_t gfp_mask)
 	return ret;
 }
 
+/**
+ * metapage_migrate_folio - Migration function for JFS metapages
+ */
+static int metapage_migrate_folio(struct address_space *mapping, struct folio *dst,
+				  struct folio *src, enum migrate_mode mode)
+{
+	int expected_count;
+
+	if (!src->private)
+		return filemap_migrate_folio(mapping, dst, src, mode);
+
+	/* Check whether page does not have extra refs before we do more work */
+	expected_count = folio_expected_refs(mapping, src);
+	if (folio_ref_count(src) != expected_count)
+		return -EAGAIN;
+	return __metapage_migrate_folio(mapping, dst, src, mode);
+}
+
 static void metapage_invalidate_folio(struct folio *folio, size_t offset,
 				    size_t length)
 {
@@ -570,6 +663,7 @@ const struct address_space_operations jfs_metapage_aops = {
 	.release_folio	= metapage_release_folio,
 	.invalidate_folio = metapage_invalidate_folio,
 	.dirty_folio	= filemap_dirty_folio,
+	.migrate_folio	= metapage_migrate_folio,
 };
 
 struct metapage *__get_metapage(struct inode *inode, unsigned long lblock,
include/linux/migrate.h +1 -0
index aaa2114498d6..cb31c5b1eb6a 100644
@@ -60,6 +60,7 @@ struct movable_operations {
 /* Defined in mm/debug.c: */
 extern const char *migrate_reason_names[MR_TYPES];
 
+int folio_expected_refs(struct address_space *mapping, struct folio *folio);
 #ifdef CONFIG_MIGRATION
 
 void putback_movable_pages(struct list_head *l);
mm/migrate.c +2 -1
index 6e2488e5dbe4..0f01b8a87dec 100644
@@ -445,7 +445,7 @@ void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
 }
 #endif
 
-static int folio_expected_refs(struct address_space *mapping,
+int folio_expected_refs(struct address_space *mapping,
 		struct folio *folio)
 {
 	int refs = 1;
@@ -458,6 +458,7 @@ static int folio_expected_refs(struct address_space *mapping,
 
 	return refs;
 }
+EXPORT_SYMBOL_GPL(folio_expected_refs);
 
 /*
  * Replace the folio in the mapping.
Signature · 1 line
2.34.1