[PATCH 1/2] mm/mempolicy: merge adjacent shared policy ranges
From: David Hildenbrand (Arm)
Date: Fri Nov 13 2026 - 16:36:42 EST
Shared policies can retain separate tree nodes for adjacent ranges with
identical policies. This can create unnecessary policy ranges especially
when individual VMAs cover individual file ranges, and user space sets
the policy on these VMAs independently.
Extend the existing replacement handling to merge ranges with equal policy
at either boundary. The existing replacement loop will then remove the old
nodes and installs a single node for the combined range. This also avoids
splitting an existing node when setting the same policy on a subrange.
Extending either boundary rules out splitting an existing node, so the
allocation retry can only occur with the originally requested range.
This change will make it easier to cap the number of shared policy
ranges per inode.
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
mm/mempolicy.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 06b90903e8794..183bc3bd077cd 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -3208,6 +3208,23 @@ static int shared_policy_replace(struct shared_policy *sp, pgoff_t start,
restart:
write_lock(&sp->lock);
+ if (new) {
+ /* Try to merge ranges with same policies. */
+ if (start) {
+ n = sp_lookup(sp, start - 1, start);
+ if (n && mpol_equal(n->policy, new->policy)) {
+ new->start = n->start;
+ start = new->start;
+ }
+ }
+ if (end != ULONG_MAX) {
+ n = sp_lookup(sp, end, end + 1);
+ if (n && mpol_equal(n->policy, new->policy)) {
+ new->end = n->end;
+ end = new->end;
+ }
+ }
+ }
n = sp_lookup(sp, start, end);
/* Take care of old policies in the same range. */
while (n && n->start < end) {
--
2.43.0