Skip to contentSkip to conversation

mm: remove page_swap_entry()

PATCH In review Tal Zussman 4 participants 23 messages Sep 8 – Sep 14

Subsystems (6)
Memory management arm64 THP MM swap +2 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover mm: remove page_swap_entry()

A folio in the swap cache occupies folio_nr_pages() contiguous swap entries starting at folio->swap, so a page's swap entry is just folio->swap plus the page's index in the folio. The swap entry is folio state, but the only helper for it is page-based:
page_swap_entry() takes a page and recomputes the folio its callers already hold. Several callers avoid it by open-coding the arithmetic on folio->swap instead.

Add folio_swap_entry() and folio_page_swap_entry(), convert all users, and remove page_swap_entry(), with a few other cleanups along the way.

---
Changes in v2:
- Keep the swap entry computation in __split_folio_to_order() open-coded
and add a comment, per Zi Yan.
- Reduce the zswap patch to using folio_swap_entry(), as the folio
conversion landed separately.
- Link to v1: https://patch.msgid.link/[email protected]

---
Tal Zussman (8):
mm/swap: add folio_swap_entry() and folio_page_swap_entry() mm/huge_memory: add a comment to the open-coded swap entry mm/rmap: use folio_page_swap_entry() in ttu_anon_swapbacked_folio() mm/zswap: use folio_swap_entry() in zswap_store_page() mm/swapfile: use folio_page_swap_entry()
arm64: mte: make mte_save_tags() and mte_restore_tags() static
arm64: mte: pass the swap entry to mte_save_tags() mm/swap: remove page_swap_entry()

 arch/arm64/include/asm/mte.h |  2 --
 arch/arm64/mm/mteswap.c      | 32 ++++++++++++++------------------
 include/linux/swap.h         | 30 +++++++++++++++++++++++++++---
 mm/huge_memory.c             |  4 ++++
 mm/rmap.c                    |  2 +-
 mm/swapfile.c                |  4 ++--
 mm/zswap.c                   |  3 +--
 7 files changed, 49 insertions(+), 28 deletions(-)

---
base-commit: 9d3243fc689fef444f87e0a703b4c99653137e1b
change-id: 20260807-folio_swap_entry-e53d68610de0

Best regards,

Signature · 1 line
Tal Zussman <[email protected]>
#1 lore raw

PATCH v2 1/8 mm/swap: add folio_swap_entry() and folio_page_swap_entry()

A folio in the swap cache occupies folio_nr_pages() contiguous swap entries starting at folio->swap, so a page's swap entry is just folio->swap plus the page's index in the folio. page_swap_entry() hides this behind a compound_head() call, and callers that already have the folio sometimes open-code the arithmetic instead.

Add folio_swap_entry(), which takes a folio and a page index, and folio_page_swap_entry() for callers that have the page.

---
 include/linux/swap.h | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
include/linux/swap.h +33 -0
index fc290e29e4a9..1d979e76e78a 100644
@@ -272,6 +272,39 @@ struct swap_info_struct {
 	const struct swap_ops *ops;
 };
 
+/**
+ * folio_swap_entry - Return the swap entry for a page within a folio.
+ * @folio: The folio.
+ * @idx: The index of the page within the folio.
+ *
+ * A folio in the swap cache occupies folio_nr_pages() contiguous swap
+ * entries starting at folio->swap. The caller must ensure the folio is
+ * in the swap cache and that @idx is within the folio.
+ */
+static inline
+swp_entry_t folio_swap_entry(const struct folio *folio, unsigned long idx)
+{
+	swp_entry_t entry = folio->swap;
+
+	VM_WARN_ON_ONCE_FOLIO(idx >= folio_nr_pages(folio), folio);
+	entry.val += idx;
+	return entry;
+}
+
+/**
+ * folio_page_swap_entry - Return the swap entry of a page in a folio.
+ * @folio: The folio containing @page.
+ * @page: A page within @folio.
+ *
+ * The caller must ensure the folio is in the swap cache and that @page
+ * is part of @folio.
+ */
+static inline swp_entry_t folio_page_swap_entry(const struct folio *folio,
+		const struct page *page)
+{
+	return folio_swap_entry(folio, folio_page_idx(folio, page));
+}
+
 static inline swp_entry_t page_swap_entry(struct page *page)
 {
 	struct folio *folio = page_folio(page);
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH v2 1/8] mm/swap: add folio_swap_entry() and folio_page_swap_entry()

Quoted 49 lines · Tal Zussman

A folio in the swap cache occupies folio_nr_pages() contiguous swap entries starting at folio->swap, so a page's swap entry is just folio->swap plus the page's index in the folio. page_swap_entry() hides this behind a compound_head() call, and callers that already have the folio sometimes open-code the arithmetic instead.

Add folio_swap_entry(), which takes a folio and a page index, and folio_page_swap_entry() for callers that have the page.

---
 include/linux/swap.h | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
include/linux/swap.h +26 -0
index fc290e29e4a9..1d979e76e78a 100644
@@ -272,6 +272,39 @@ struct swap_info_struct {
 	const struct swap_ops *ops;
 };
 
+/**
+ * folio_swap_entry - Return the swap entry for a page within a folio.
+ * @folio: The folio.
+ * @idx: The index of the page within the folio.
+ *
+ * A folio in the swap cache occupies folio_nr_pages() contiguous swap
+ * entries starting at folio->swap. The caller must ensure the folio is
+ * in the swap cache and that @idx is within the folio.
+ */
+static inline
+swp_entry_t folio_swap_entry(const struct folio *folio, unsigned long idx)
+{
+	swp_entry_t entry = folio->swap;
+
+	VM_WARN_ON_ONCE_FOLIO(idx >= folio_nr_pages(folio), folio);
+	entry.val += idx;
+	return entry;
+}
+
+/**
+ * folio_page_swap_entry - Return the swap entry of a page in a folio.
+ * @folio: The folio containing @page.
+ * @page: A page within @folio.
+ *
+ * The caller must ensure the folio is in the swap cache and that @page
+ * is part of @folio.

include/linux/swap.h struct swap_info_struct { · line 310

Quoted 10 lines
include/linux/swap.h +7 -0
+ */
+static inline swp_entry_t folio_page_swap_entry(const struct folio *folio,
+		const struct page *page)
+{
+	return folio_swap_entry(folio, folio_page_idx(folio, page));
+}
+
 static inline swp_entry_t page_swap_entry(struct page *page)
 {
 	struct folio *folio = page_folio(page);

"Return the swap entry for a page within a folio" vs. "Return the swap entry of a page in a folio."

yet only of the variants has a "page" in the name :)

A bit confusing.

Not immediately sure how it could be done cleaner. The minority of cases seem to use folio_swap_entry.

Signature · 3 lines
Cheers,

David
#3 lore raw

Re: [PATCH v2 1/8] mm/swap: add folio_swap_entry() and folio_page_swap_entry()

Quoted 64 lines · David Hildenbrand (Arm)
Quoted 49 lines · Tal Zussman

A folio in the swap cache occupies folio_nr_pages() contiguous swap entries starting at folio->swap, so a page's swap entry is just folio->swap plus the page's index in the folio. page_swap_entry() hides this behind a compound_head() call, and callers that already have the folio sometimes open-code the arithmetic instead.

Add folio_swap_entry(), which takes a folio and a page index, and folio_page_swap_entry() for callers that have the page.

---
 include/linux/swap.h | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
include/linux/swap.h +26 -0
index fc290e29e4a9..1d979e76e78a 100644
@@ -272,6 +272,39 @@ struct swap_info_struct {
 	const struct swap_ops *ops;
 };
 
+/**
+ * folio_swap_entry - Return the swap entry for a page within a folio.
+ * @folio: The folio.
+ * @idx: The index of the page within the folio.
+ *
+ * A folio in the swap cache occupies folio_nr_pages() contiguous swap
+ * entries starting at folio->swap. The caller must ensure the folio is
+ * in the swap cache and that @idx is within the folio.
+ */
+static inline
+swp_entry_t folio_swap_entry(const struct folio *folio, unsigned long idx)
+{
+	swp_entry_t entry = folio->swap;
+
+	VM_WARN_ON_ONCE_FOLIO(idx >= folio_nr_pages(folio), folio);
+	entry.val += idx;
+	return entry;
+}
+
+/**
+ * folio_page_swap_entry - Return the swap entry of a page in a folio.
+ * @folio: The folio containing @page.
+ * @page: A page within @folio.
+ *
+ * The caller must ensure the folio is in the swap cache and that @page
+ * is part of @folio.

include/linux/swap.h struct swap_info_struct { · line 310

Quoted 10 lines
include/linux/swap.h +7 -0
+ */
+static inline swp_entry_t folio_page_swap_entry(const struct folio *folio,
+		const struct page *page)
+{
+	return folio_swap_entry(folio, folio_page_idx(folio, page));
+}
+
 static inline swp_entry_t page_swap_entry(struct page *page)
 {
 	struct folio *folio = page_folio(page);

"Return the swap entry for a page within a folio" vs. "Return the swap entry of a page in a folio."

Quoted 7 lines

yet only of the variants has a "page" in the name :)

A bit confusing.

Not immediately sure how it could be done cleaner. The minority of cases seem to use folio_swap_entry.

Yeah... I considered naming them folio_swap_entry() and folio_swap_entry_idx() (or a variant of that) but I liked the page version more. I can update the comments to try to differentiate between the two a little more if that would help. How's "Return the swap entry at an index within a folio"?

Quoted 5 lines
Signature · 3 lines
Cheers,

David
#4 lore raw

Re: [PATCH v2 1/8] mm/swap: add folio_swap_entry() and folio_page_swap_entry()

Quoted 69 lines · Tal Zussman
Quoted 68 lines · David Hildenbrand (Arm)
Quoted 49 lines · Tal Zussman

A folio in the swap cache occupies folio_nr_pages() contiguous swap entries starting at folio->swap, so a page's swap entry is just folio->swap plus the page's index in the folio. page_swap_entry() hides this behind a compound_head() call, and callers that already have the folio sometimes open-code the arithmetic instead.

Add folio_swap_entry(), which takes a folio and a page index, and folio_page_swap_entry() for callers that have the page.

---
 include/linux/swap.h | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
include/linux/swap.h +26 -0
index fc290e29e4a9..1d979e76e78a 100644
@@ -272,6 +272,39 @@ struct swap_info_struct {
 	const struct swap_ops *ops;
 };
 
+/**
+ * folio_swap_entry - Return the swap entry for a page within a folio.
+ * @folio: The folio.
+ * @idx: The index of the page within the folio.
+ *
+ * A folio in the swap cache occupies folio_nr_pages() contiguous swap
+ * entries starting at folio->swap. The caller must ensure the folio is
+ * in the swap cache and that @idx is within the folio.
+ */
+static inline
+swp_entry_t folio_swap_entry(const struct folio *folio, unsigned long idx)
+{
+	swp_entry_t entry = folio->swap;
+
+	VM_WARN_ON_ONCE_FOLIO(idx >= folio_nr_pages(folio), folio);
+	entry.val += idx;
+	return entry;
+}
+
+/**
+ * folio_page_swap_entry - Return the swap entry of a page in a folio.
+ * @folio: The folio containing @page.
+ * @page: A page within @folio.
+ *
+ * The caller must ensure the folio is in the swap cache and that @page
+ * is part of @folio.

include/linux/swap.h struct swap_info_struct { · line 310

Quoted 10 lines
include/linux/swap.h +7 -0
+ */
+static inline swp_entry_t folio_page_swap_entry(const struct folio *folio,
+		const struct page *page)
+{
+	return folio_swap_entry(folio, folio_page_idx(folio, page));
+}
+
 static inline swp_entry_t page_swap_entry(struct page *page)
 {
 	struct folio *folio = page_folio(page);

"Return the swap entry for a page within a folio" vs. "Return the swap entry of a page in a folio."

yet only of the variants has a "page" in the name :)

A bit confusing.

Quoted 9 lines
Quoted 3 lines

Not immediately sure how it could be done cleaner. The minority of cases seem to use folio_swap_entry.

Yeah... I considered naming them folio_swap_entry() and folio_swap_entry_idx() (or a variant of that) but I liked the page version more. I can update the comments to try to differentiate between the two a little more if that would help. How's "Return the swap entry at an index within a folio"?

I guess we should call it "page index" in the doc.

Signature · 3 lines
Cheers,

David
#5 lore raw

Re: [PATCH v2 1/8] mm/swap: add folio_swap_entry() and folio_page_swap_entry()

Andrew, could you please apply this as a fix patch?

-- >8 --
Subject: [PATCH] mm/swap: adjust the folio_swap_entry() kerneldoc summary

---
 include/linux/swap.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/swap.h +1 -1
index 78974da6810e..43155e122b5c 100644
@@ -273,7 +273,7 @@ struct swap_info_struct {
 };
 
 /**
- * folio_swap_entry - Return the swap entry for a page within a folio.
+ * folio_swap_entry - Return the swap entry at a page index within a folio.
  * @folio: The folio.
  * @idx: The index of the page within the folio.
  *
Signature · 1 line
2.39.5
maintainer Acked-by
#6 lore raw

Re: [PATCH v2 1/8] mm/swap: add folio_swap_entry() and folio_page_swap_entry()

Quoted 15 lines · Tal Zussman

Andrew, could you please apply this as a fix patch?

-- >8 --
Subject: [PATCH] mm/swap: adjust the folio_swap_entry() kerneldoc summary

---
 include/linux/swap.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/swap.h +0 -0
index 78974da6810e..43155e122b5c 100644

include/linux/swap.h struct swap_info_struct { · line 279

Quoted 8 lines
include/linux/swap.h +1 -1
@@ -273,7 +273,7 @@ struct swap_info_struct {
 };
 
 /**
- * folio_swap_entry - Return the swap entry for a page within a folio.
+ * folio_swap_entry - Return the swap entry at a page index within a folio.
  * @folio: The folio.
  * @idx: The index of the page within the folio.
  *
Signature · 3 lines
Cheers,

David
#7 lore raw

PATCH v2 2/8 mm/huge_memory: add a comment to the open-coded swap entry

In reply to Tal Zussman

The swap entry of each new folio in __split_folio_to_order() is computed by hand from folio->swap rather than with folio_swap_entry(), because the folio's page count is not valid while it is being split. Add a comment explaining this.

---
 mm/huge_memory.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/huge_memory.c +4 -0
index d1ce061601bc..1fabdcbd8c8f 100644
@@ -3763,6 +3763,10 @@ static void __split_folio_to_order(struct folio *folio, int old_order,
 		 */
 		VM_WARN_ON_ONCE_PAGE(new_folio->private, new_head);
 
+		/*
+		 * Not all folio fields are valid during a split, so open-code
+		 * the swap entry rather than using folio_swap_entry().
+		 */
 		if (folio_test_swapcache(folio))
 			new_folio->swap.val = folio->swap.val + i;
 
Signature · 1 line
2.39.5
reviewer Reviewed-by on v2 2/8
#8 lore raw

Re: [PATCH v2 2/8] mm/huge_memory: add a comment to the open-coded swap entry

Quoted 15 lines · Tal Zussman

The swap entry of each new folio in __split_folio_to_order() is computed by hand from folio->swap rather than with folio_swap_entry(), because the folio's page count is not valid while it is being split. Add a comment explaining this.

---
 mm/huge_memory.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/huge_memory.c +0 -0
index d1ce061601bc..1fabdcbd8c8f 100644

mm/huge_memory.c static void __split_folio_to_order(struct folio *folio, int old_order, · line 3772

Quoted 10 lines
mm/huge_memory.c +4 -0
@@ -3763,6 +3763,10 @@ static void __split_folio_to_order(struct folio *folio, int old_order,
 		 */
 		VM_WARN_ON_ONCE_PAGE(new_folio->private, new_head);
 
+		/*
+		 * Not all folio fields are valid during a split, so open-code
+		 * the swap entry rather than using folio_swap_entry().
+		 */
 		if (folio_test_swapcache(folio))
 			new_folio->swap.val = folio->swap.val + i;
 

LGTM.

Signature · 2 lines
Best Regards,
Yan, Zi
maintainer Acked-by on v2 2/8
#9 lore raw

Re: [PATCH v2 2/8] mm/huge_memory: add a comment to the open-coded swap entry

In reply to Tal Zussman
Quoted 16 lines · Tal Zussman

The swap entry of each new folio in __split_folio_to_order() is computed by hand from folio->swap rather than with folio_swap_entry(), because the folio's page count is not valid while it is being split. Add a comment explaining this.

---
 mm/huge_memory.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/huge_memory.c +0 -0
index d1ce061601bc..1fabdcbd8c8f 100644

mm/huge_memory.c static void __split_folio_to_order(struct folio *folio, int old_order, · line 3772

Quoted 10 lines
mm/huge_memory.c +4 -0
@@ -3763,6 +3763,10 @@ static void __split_folio_to_order(struct folio *folio, int old_order,
 		 */
 		VM_WARN_ON_ONCE_PAGE(new_folio->private, new_head);
 
+		/*
+		 * Not all folio fields are valid during a split, so open-code
+		 * the swap entry rather than using folio_swap_entry().
+		 */
 		if (folio_test_swapcache(folio))
 			new_folio->swap.val = folio->swap.val + i;
 
Signature · 3 lines
Cheers,

David
#10 lore raw

PATCH v2 3/8 mm/rmap: use folio_page_swap_entry() in ttu_anon_swapbacked_folio()

In reply to Tal Zussman

We already have the folio here, so use folio_page_swap_entry() instead of going through page_swap_entry(). This saves a call to compound_head().

No functional change.

---
 mm/rmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/rmap.c +1 -1
index 5fefe5b060b1..193e1264b631 100644
@@ -2147,7 +2147,7 @@ static bool ttu_anon_swapbacked_folio(struct vm_area_struct *vma,
 {
 	const bool anon_exclusive = folio_test_anon(folio) &&
 				    PageAnonExclusive(page);
-	swp_entry_t entry = page_swap_entry(page);
+	swp_entry_t entry = folio_page_swap_entry(folio, page);
 	struct mm_struct *mm = vma->vm_mm;
 
 	if (folio_dup_swap(folio, page) < 0)
Signature · 1 line
2.39.5
maintainer Acked-by on v2 3/8
#11 lore raw

Re: [PATCH v2 3/8] mm/rmap: use folio_page_swap_entry() in ttu_anon_swapbacked_folio()

Quoted 8 lines · Tal Zussman

We already have the folio here, so use folio_page_swap_entry() instead of going through page_swap_entry(). This saves a call to compound_head().

No functional change.

---

Signature · 3 lines
Cheers,

David
#12 lore raw

PATCH v2 4/8 mm/zswap: use folio_swap_entry() in zswap_store_page()

In reply to Tal Zussman

zswap_store_page() open-codes the swap entry computation from folio->swap and the page index. Use folio_swap_entry() instead.

No functional change.

---
 mm/zswap.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/zswap.c +1 -2
index 5d0d8bd72193..c27e22d067e3 100644
@@ -1398,8 +1398,7 @@ static bool zswap_store_page(struct folio *folio, long index,
 			     struct obj_cgroup *objcg,
 			     struct zswap_pool *pool)
 {
-	swp_entry_t page_swpentry = swp_entry(swp_type(folio->swap),
-					      swp_offset(folio->swap) + index);
+	swp_entry_t page_swpentry = folio_swap_entry(folio, index);
 	struct zswap_entry *entry, *old;
 
 	/* allocate entry */
Signature · 1 line
2.39.5
maintainer Acked-by on v2 4/8
#13 lore raw

Re: [PATCH v2 4/8] mm/zswap: use folio_swap_entry() in zswap_store_page()

Quoted 16 lines · Tal Zussman

zswap_store_page() open-codes the swap entry computation from folio->swap and the page index. Use folio_swap_entry() instead.

No functional change.

---
 mm/zswap.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/zswap.c +0 -0
index 5d0d8bd72193..c27e22d067e3 100644

mm/zswap.c static bool zswap_store_page(struct folio *folio, long index, · line 1404

Quoted 9 lines
mm/zswap.c +1 -2
@@ -1398,8 +1398,7 @@ static bool zswap_store_page(struct folio *folio, long index,
 			     struct obj_cgroup *objcg,
 			     struct zswap_pool *pool)
 {
-	swp_entry_t page_swpentry = swp_entry(swp_type(folio->swap),
-					      swp_offset(folio->swap) + index);
+	swp_entry_t page_swpentry = folio_swap_entry(folio, index);
 	struct zswap_entry *entry, *old;
 
 	/* allocate entry */
Signature · 3 lines
Cheers,

David
#14 lore raw

PATCH v2 5/8 mm/swapfile: use folio_page_swap_entry()

In reply to Tal Zussman

folio_dup_swap() and folio_put_swap() open-code the swap entry computation from folio->swap and folio_page_idx(). Use folio_page_swap_entry() instead.

No functional change.

---
 mm/swapfile.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/swapfile.c +2 -2
index 869a918b18e8..0a3a3b2218c7 100644
@@ -1822,7 +1822,7 @@ int folio_dup_swap(struct folio *folio, struct page *page)
 	VM_WARN_ON_FOLIO(!folio_test_swapcache(folio), folio);
 
 	if (page) {
-		entry.val += folio_page_idx(folio, page);
+		entry = folio_page_swap_entry(folio, page);
 		nr_pages = 1;
 	}
 
@@ -1849,7 +1849,7 @@ void folio_put_swap(struct folio *folio, struct page *page)
 	VM_WARN_ON_FOLIO(!folio_test_swapcache(folio), folio);
 
 	if (page) {
-		entry.val += folio_page_idx(folio, page);
+		entry = folio_page_swap_entry(folio, page);
 		nr_pages = 1;
 	}
 
Signature · 1 line
2.39.5
#16 lore raw

PATCH v2 6/8 arm64: mte: make mte_save_tags() and mte_restore_tags() static

In reply to Tal Zussman

mte_save_tags() and mte_restore_tags() are only used by arch_prepare_to_swap() and arch_swap_restore() in mteswap.c. Make them static and remove their declarations from mte.h.

No functional change.

---
 arch/arm64/include/asm/mte.h | 2 --
 arch/arm64/mm/mteswap.c      | 4 ++--
 2 files changed, 2 insertions(+), 4 deletions(-)
arch/arm64/include/asm/mte.h +0 -2
index 7f7b97e09996..83f3b05fc784 100644
@@ -23,9 +23,7 @@ unsigned long mte_copy_tags_from_user(void *to, const void __user *from,
 				      unsigned long n);
 unsigned long mte_copy_tags_to_user(void __user *to, void *from,
 				    unsigned long n);
-int mte_save_tags(struct page *page);
 void mte_save_page_tags(const void *page_addr, void *tag_storage);
-void mte_restore_tags(swp_entry_t entry, struct page *page);
 void mte_restore_page_tags(void *page_addr, const void *tag_storage);
 void mte_invalidate_tags(int type, pgoff_t offset);
 void mte_invalidate_tags_area(int type);
arch/arm64/mm/mteswap.c +2 -2
index 63e8d72f202a..3c54afc8b758 100644
@@ -20,7 +20,7 @@ void mte_free_tag_storage(char *storage)
 	kfree(storage);
 }
 
-int mte_save_tags(struct page *page)
+static int mte_save_tags(struct page *page)
 {
 	void *tag_storage, *ret;
 
@@ -47,7 +47,7 @@ int mte_save_tags(struct page *page)
 	return 0;
 }
 
-void mte_restore_tags(swp_entry_t entry, struct page *page)
+static void mte_restore_tags(swp_entry_t entry, struct page *page)
 {
 	void *tags = xa_load(&mte_pages, entry.val);
 
Signature · 1 line
2.39.5
maintainer Acked-by on v2 6/8
#17 lore raw

Re: [PATCH v2 6/8] arm64: mte: make mte_save_tags() and mte_restore_tags() static

Quoted 8 lines · Tal Zussman

mte_save_tags() and mte_restore_tags() are only used by arch_prepare_to_swap() and arch_swap_restore() in mteswap.c. Make them static and remove their declarations from mte.h.

No functional change.

---

Signature · 3 lines
Cheers,

David
#18 lore raw

PATCH v2 7/8 arm64: mte: pass the swap entry to mte_save_tags()

In reply to Tal Zussman

arch_prepare_to_swap() derives a page from the folio only for mte_save_tags() to recompute the folio's swap entry from that page. Pass the entry in directly with folio_swap_entry(), matching mte_restore_tags(), and move the page_mte_tagged() check into the caller so we only compute the entry for tagged pages.

__mte_invalidate_tags() loses its only user, so remove it and call mte_invalidate_tags() directly in the error path. This removes two calls to compound_head().

No functional change.

---
 arch/arm64/mm/mteswap.c | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)
arch/arm64/mm/mteswap.c +13 -17
index 3c54afc8b758..f64202b69309 100644
@@ -20,22 +20,17 @@ void mte_free_tag_storage(char *storage)
 	kfree(storage);
 }
 
-static int mte_save_tags(struct page *page)
+static int mte_save_tags(swp_entry_t entry, struct page *page)
 {
 	void *tag_storage, *ret;
 
-	if (!page_mte_tagged(page))
-		return 0;
-
 	tag_storage = mte_allocate_tag_storage();
 	if (!tag_storage)
 		return -ENOMEM;
 
 	mte_save_page_tags(page_address(page), tag_storage);
 
-	/* lookup the swap entry.val from the page */
-	ret = xa_store(&mte_pages, page_swap_entry(page).val, tag_storage,
-		       GFP_KERNEL);
+	ret = xa_store(&mte_pages, entry.val, tag_storage, GFP_KERNEL);
 	if (WARN(xa_is_err(ret), "Failed to store MTE tags")) {
 		mte_free_tag_storage(tag_storage);
 		return xa_err(ret);
@@ -68,13 +63,6 @@ void mte_invalidate_tags(int type, pgoff_t offset)
 	mte_free_tag_storage(tags);
 }
 
-static inline void __mte_invalidate_tags(struct page *page)
-{
-	swp_entry_t entry = page_swap_entry(page);
-
-	mte_invalidate_tags(swp_type(entry), swp_offset(entry));
-}
-
 void mte_invalidate_tags_area(int type)
 {
 	swp_entry_t entry = swp_entry(type, 0);
@@ -102,15 +90,23 @@ int arch_prepare_to_swap(struct folio *folio)
 	nr = folio_nr_pages(folio);
 
 	for (i = 0; i < nr; i++) {
-		err = mte_save_tags(folio_page(folio, i));
+		struct page *page = folio_page(folio, i);
+
+		if (!page_mte_tagged(page))
+			continue;
+
+		err = mte_save_tags(folio_swap_entry(folio, i), page);
 		if (err)
 			goto out;
 	}
 	return 0;
 
 out:
-	while (i--)
-		__mte_invalidate_tags(folio_page(folio, i));
+	while (i--) {
+		swp_entry_t swap = folio_swap_entry(folio, i);
+
+		mte_invalidate_tags(swp_type(swap), swp_offset(swap));
+	}
 	return err;
 }
 
Signature · 1 line
2.39.5
maintainer Acked-by on v2 7/8
#19 lore raw

Re: [PATCH v2 7/8] arm64: mte: pass the swap entry to mte_save_tags()

Quoted 6 lines · Tal Zussman

arch_prepare_to_swap() derives a page from the folio only for mte_save_tags() to recompute the folio's swap entry from that page. Pass the entry in directly with folio_swap_entry(), matching mte_restore_tags(), and move the page_mte_tagged() check into the caller so we only compute the entry for tagged pages.

Quoted 8 lines

__mte_invalidate_tags() loses its only user, so remove it and call mte_invalidate_tags() directly in the error path. This removes two calls to compound_head().

No functional change.

---

Signature · 3 lines
Cheers,

David
#20 lore raw

PATCH v2 8/8 mm/swap: remove page_swap_entry()

In reply to Tal Zussman

All callers have been converted to folio_swap_entry() and folio_page_swap_entry(), so remove it.

---
 include/linux/swap.h | 9 ---------
 1 file changed, 9 deletions(-)
include/linux/swap.h +0 -9
index 1d979e76e78a..8032a0f3c319 100644
@@ -305,15 +305,6 @@ static inline swp_entry_t folio_page_swap_entry(const struct folio *folio,
 	return folio_swap_entry(folio, folio_page_idx(folio, page));
 }
 
-static inline swp_entry_t page_swap_entry(struct page *page)
-{
-	struct folio *folio = page_folio(page);
-	swp_entry_t entry = folio->swap;
-
-	entry.val += folio_page_idx(folio, page);
-	return entry;
-}
-
 /* linux/mm/page_alloc.c */
 extern unsigned long totalreserve_pages;
 
Signature · 1 line
2.39.5
maintainer
#22 lore raw
In reply to Tal Zussman
Quoted 10 lines · On Tue, 08 Sep 2026 11:16:30 -0400 Tal Zussman <[email protected]> wrote:

A folio in the swap cache occupies folio_nr_pages() contiguous swap entries starting at folio->swap, so a page's swap entry is just folio->swap plus the page's index in the folio. The swap entry is folio state, but the only helper for it is page-based:
page_swap_entry() takes a page and recomputes the folio its callers already hold. Several callers avoid it by open-coding the arithmetic on folio->swap instead.

Add folio_swap_entry() and folio_page_swap_entry(), convert all users, and remove page_swap_entry(), with a few other cleanups along the way.

Thanks, I'll queue this for testing and further review.