Re: [PATCH v3 02/16] mm: introduce leaf entry type and use to simplify leaf entry logic

Lorenzo Stoakes
2025-11-13T15:03:09Z

Hi Andrew,

Please apply the attached fix-patch.

This ensures that we do not accidentally conflict with any valid swap
entry. We can do so without occupying any additional swap (softleaf) type.

Cheers, Lorenzo

----8<----
From 78439310eded5db10692c3e8d0d322bdd6409eff Mon Sep 17 00:00:00 2001
From: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Date: Thu, 13 Nov 2025 14:20:34 +0000
Subject: [PATCH] mm: avoid any possible collision between swap entries,
SOFTLEAF_NONE

The way swap entries are encoded varies by architecture. For x86-64 for
instance, the encoded swap offset is the one's complement of the specified
swap offset.

As a result, device 0, offset 0 would be encoded as 0..01..10b.

This means it is possible to specify a PTE entry that is both device 0,
offset 0 and something that will be identified as a swap entry rather than
a pte_none() entry.

For other architectures, the encoding may preclude such entries being
valid.

The softleaf implementation currently depends on a 0..0b entry being
uniquely identifiable as a none entry.

This is therefore not a safe assumption, so let's fix that.

PTE markers unconditionally occupy a softleaf type, and currently use only
3 bits of the offset field to encode their type with no further information
recorded.

It is therefore no issue at all to add an additional marker type
designating the field as a none entry.

We also make the none checks more canonical by adjusting softleaf_is_none()
to reference softleaf_mk_none().

By doing so we avoid any possible collision with swap file entries while
taking up no further meaningful resource.

Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
---
include/linux/leafops.h | 8 +++++---
include/linux/swap.h | 1 +
include/linux/swapops.h | 6 +++++-
3 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/include/linux/leafops.h b/include/linux/leafops.h
index cff9d94fd5d1..74fd95b55e9c 100644
--- a/include/linux/leafops.h
+++ b/include/linux/leafops.h
@@ -40,7 +40,8 @@ enum softleaf_type {
*/
static inline softleaf_t softleaf_mk_none(void)
{
- return ((softleaf_t) { 0 });
+ /* Uniquely identifies none entry. */
+ return make_pte_marker_entry(PTE_MARKER_SOFTLEAF_NONE);
}

/**
@@ -72,7 +73,7 @@ static inline softleaf_t softleaf_from_pte(pte_t pte)
*/
static inline bool softleaf_is_none(softleaf_t entry)
{
- return entry.val == 0;
+ return entry.val == softleaf_mk_none().val;
}

/**
@@ -199,7 +200,8 @@ static inline bool softleaf_is_hwpoison(softleaf_t entry)
*/
static inline bool softleaf_is_marker(softleaf_t entry)
{
- return softleaf_type(entry) == SOFTLEAF_MARKER;
+ return softleaf_type(entry) == SOFTLEAF_MARKER &&
+ !softleaf_is_none(entry);
}

/**
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 38ca3df68716..e5abea55448b 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -112,6 +112,7 @@ static inline int current_is_kswapd(void)
#define SWP_HWPOISON_NUM 0
#endif

+/* Leave a type reserved for softleaf none. */
#define MAX_SWAPFILES \
((1 << MAX_SWAPFILES_SHIFT) - SWP_DEVICE_NUM - \
SWP_MIGRATION_NUM - SWP_HWPOISON_NUM - \
diff --git a/include/linux/swapops.h b/include/linux/swapops.h
index 0a4b3f51ecf5..04e74716a845 100644
--- a/include/linux/swapops.h
+++ b/include/linux/swapops.h
@@ -419,7 +419,11 @@ typedef unsigned long pte_marker;
* PROT_NONE, rather than if they were a memory hole or equivalent.
*/
#define PTE_MARKER_GUARD BIT(2)
-#define PTE_MARKER_MASK (BIT(3) - 1)
+
+/* Internal use by the softleaf implementation to represent 'none' entries. */
+#define PTE_MARKER_SOFTLEAF_NONE BIT(3)
+
+#define PTE_MARKER_MASK (BIT(4) - 1)

static inline swp_entry_t make_pte_marker_entry(pte_marker marker)
{
--
2.51.0