Skip to contentSkip to conversation

mm/mempolicy: shared policy limits for tmpfs/shmem/guest_memfd

PATCH Applied David Hildenbrand (Arm) 4 participants 13 messages Nov 13 – Nov 17

Subsystems (4)
lore
Share & tools
lore

David Hildenbrand (Arm)

author maintainer
#0 lore raw

cover mm/mempolicy: shared policy limits for tmpfs/shmem/guest_memfd

For some reason we thought it would be a good idea to let users create as many shared policy ranges for shmem/tmpfs as they want. And guest_memfd inherited that handling.

Fortunately, at least for guest_memfd we know that we shouldn't really have more than one shared policy right now in practice.

While we now at least charge these allocations to the memcg, users can still allocate possibly many, and cause trouble, for example, by having long list walks under spinlocks.

Instead of trying to improve things, let's just stop the user from doing stupid things.

Essentially: limit the number of shared policy ranges per inode to 1024 as default. Let the admin increase that limit. 1024 is likely still
plenty: who maps a shmem/tmpfs/guest_memfd file and configures that many mempolicies on it through separate VMAs?

Maybe we should set the limit even lower ... but 1024 feels at least like a start where no user case should be impacted.

Patch #2 can be considered a fix, but I am not tagging it like that. It's been that way forever ... should we? The impact is primarily just wasting kernel memory and doing a lot of expensive operation under locks.

Patch #1 prepares for that by merging adjacent policies if they are equal, to reduce the number of policies further if some user space actually does something questionable on sub-ranges.

This is not super urgent but something that should be addressed.

---
David Hildenbrand (Arm) (2):
mm/mempolicy: merge adjacent shared policy ranges mm/mempolicy: limit shared policy ranges per inode

 Documentation/admin-guide/sysctl/vm.rst | 18 ++++++++
 include/linux/mempolicy.h               |  1 +
 mm/mempolicy.c                          | 74 +++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+)

---

base-commit: c6e0cad6c9b4180bb229db06e5b062c5ce0723bd

change-id: 20261113-mempolicy_limit-6f043870d04b

--

Cheers,

David

author maintainer
#1 lore raw

PATCH 1/2 mm/mempolicy: merge adjacent shared policy ranges

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.

---
 mm/mempolicy.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
mm/mempolicy.c +17 -0
index 06b90903e8794..183bc3bd077cd 100644
@@ -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) {
Signature · 1 line
2.43.0
reviewer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] mm/mempolicy: merge adjacent shared policy ranges

Quoted 5 lines · David Hildenbrand (Arm)

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.

Quoted 7 lines

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.

Hi David,

Independent of 2/2 I think this is a really great change. Thank you!

Quoted 40 lines

This change will make it easier to cap the number of shared policy ranges per inode.

---
 mm/mempolicy.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
mm/mempolicy.c +17 -0
index 06b90903e8794..183bc3bd077cd 100644
@@ -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) {
Signature · 2 lines
-- 
2.43.0
maintainer Tested-by Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] mm/mempolicy: merge adjacent shared policy ranges

In reply to David Hildenbrand (Arm)
Quoted 10 lines · David Hildenbrand (Arm)

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.

Quoted 7 lines

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.

Reproduced some bad effects by setting a policy on every other page shm policy across millions of pages - then setting a bunch of pages individually to the same policy.

Base: merge never occurs, 1 range per page.
Patch: They all collapse back to a single range.

I was also able to reproduce a "BUG: soft lockup" when a file is freed after creating millions of shm policies (absolutely not realistic).

I think this commit is probably fine to backport, but I agree it doesn't seem worth it.

Have some thoughts about the limit in patch 2, but otherwise

Signature · 1 line
~Gregory
reviewer Reviewed-by on v1 1/2
#4 lore raw

Re: [PATCH 1/2] mm/mempolicy: merge adjacent shared policy ranges

In reply to David Hildenbrand (Arm)
Quoted 13 lines · David Hildenbrand (Arm)

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.

Quoted 8 lines

This change will make it easier to cap the number of shared policy ranges per inode.

---
 mm/mempolicy.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

Makes sense to me.

Best Regards,
Yan, Zi

author maintainer
#5 lore raw

PATCH 2/2 mm/mempolicy: limit shared policy ranges per inode

In reply to David Hildenbrand (Arm)

Shared NUMA policy ranges can accumulate on an inode after its mappings are removed. Currently, these nodes are effectively unbounded (even though the allcoations are charged).

An unbounded tree can cause long rwlock hold times when replacing or freeing policies. Instead of trying to fix that, it sounds more like having many nodes is actually only a scenario for processes trying to cause trouble. Most use cases will probably only use a handful of them, not millions.

Add vm.max_shared_policy_count (inspired by vm.max_map_count), defaulting to 1024 ranges per inode. Reject updates with -ENOMEM if they would increase the count beyond the limit, taking merging, splitting etc. into account.

---
 Documentation/admin-guide/sysctl/vm.rst | 18 +++++++++++
 include/linux/mempolicy.h               |  1 +
 mm/mempolicy.c                          | 57 +++++++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+)
Documentation/admin-guide/sysctl/vm.rst +18 -0
index 3cb0138e850c1..8ef63d933821b 100644
@@ -41,6 +41,7 @@ Currently, these files are in /proc/sys/vm:
 - legacy_va_layout
 - lowmem_reserve_ratio
 - max_map_count
+- max_shared_policy_count
 - mem_profiling         (only if CONFIG_MEM_ALLOC_PROFILING=y)
 - memory_failure_early_kill
 - memory_failure_recovery
@@ -473,6 +474,23 @@ e.g., up to one or two maps per allocation.
 The default value is 65530.
 
 
+max_shared_policy_count
+=======================
+
+The maximum number of shared NUMA policy ranges stored on a single inode.
+This applies to tmpfs files, shared anonymous mappings, shmem-backed memfds
+and System V shared memory segments, and guest_memfds. The policies are
+shared by all mappings of the same inode and remain after all mappings are
+removed.
+
+An update that would increase the number of ranges beyond this limit fails
+with -ENOMEM; this includes splitting of existing ranges. Lowering the
+limit will not remove existing policies. Updates that preserve or reduce
+the count remain allowed even if it exceeds the limit.
+
+The default value is 1024. The minimum value is 1.
+
+
 mem_profiling
 ==============
 
include/linux/mempolicy.h +1 -0
index cd8f944209ca4..828e7de6dee73 100644
@@ -113,6 +113,7 @@ static inline bool mpol_equal(struct mempolicy *a, struct mempolicy *b)
 struct shared_policy {
 	struct rb_root root;
 	rwlock_t lock;
+	unsigned int nr_nodes;
 };
 struct sp_node {
 	struct rb_node nd;
mm/mempolicy.c +57 -0

99 lines not on this page · raw message

Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH 2/2] mm/mempolicy: limit shared policy ranges per inode

Quoted 10 lines · David Hildenbrand (Arm)

Shared NUMA policy ranges can accumulate on an inode after its mappings are removed. Currently, these nodes are effectively unbounded (even though the allcoations are charged).

An unbounded tree can cause long rwlock hold times when replacing or freeing policies. Instead of trying to fix that, it sounds more like having many nodes is actually only a scenario for processes trying to cause trouble. Most use cases will probably only use a handful of them, not millions.

Quoted 6 lines

Add vm.max_shared_policy_count (inspired by vm.max_map_count), defaulting to 1024 ranges per inode. Reject updates with -ENOMEM if they would increase the count beyond the limit, taking merging, splitting etc. into account.

This arguably breaks userspace.

A single mapper that tries to set >1024 ranges would start getting -ENOMEMs unexpectedly with this change.

Multi-task mappers were previously unlimited, but that seems like more of a nasty unintended corner case - not a real feature.

A single process is already limited on the number of vmas/policies based on max_map_count, so it seems natural to just use max_map_count directly, but I took a brief look at some distros and most seem to ship a raised max_map_count (>1 million) by default).

I tested that and even 1 million can cause a soft lockup.

Not sure there's an easy solution here, we might have to rework the shm policy locks.

Signature · 1 line
~Gregory
author maintainer
#7 lore raw

Re: [PATCH 2/2] mm/mempolicy: limit shared policy ranges per inode

Quoted 16 lines · Gregory Price (Meta)
Quoted 15 lines · David Hildenbrand (Arm)

Shared NUMA policy ranges can accumulate on an inode after its mappings are removed. Currently, these nodes are effectively unbounded (even though the allcoations are charged).

An unbounded tree can cause long rwlock hold times when replacing or freeing policies. Instead of trying to fix that, it sounds more like having many nodes is actually only a scenario for processes trying to cause trouble. Most use cases will probably only use a handful of them, not millions.

Add vm.max_shared_policy_count (inspired by vm.max_map_count), defaulting to 1024 ranges per inode. Reject updates with -ENOMEM if they would increase the count beyond the limit, taking merging, splitting etc. into account.

Quoted 4 lines
Quoted 1 line

This arguably breaks userspace.

Which is fine if there is no valid userspace that does something stupid.

Quoted 3 lines

A single mapper that tries to set >1024 ranges would start getting -ENOMEMs unexpectedly with this change.

Sure, that's expected. The admin can raise the default for these odd cases.

I did some digging and was not able to come up with any valid use case.

The only is that you have many VMAs for the same file mapping (mprotect etc) and then do a mind().

But that's precisely what patch #1 does.

Quoted 9 lines

Multi-task mappers were previously unlimited, but that seems like more of a nasty unintended corner case - not a real feature.

A single process is already limited on the number of vmas/policies based on max_map_count, so it seems natural to just use max_map_count directly, but I took a brief look at some distros and most seem to ship a raised max_map_count (>1 million) by default).

Quoted 4 lines

I tested that and even 1 million can cause a soft lockup.

Not sure there's an easy solution here, we might have to rework the shm policy locks.

Do you know a single valid use case that's not solved by patch #1 and would trigger that many policies?

Ai was not able to come up with anything for me.

Signature · 3 lines
Cheers,

David
#8 lore raw

Re: [PATCH 2/2] mm/mempolicy: limit shared policy ranges per inode

Quoted 12 lines · David Hildenbrand (Arm)
Quoted 11 lines · Gregory Price (Meta)

Multi-task mappers were previously unlimited, but that seems like more of a nasty unintended corner case - not a real feature.

A single process is already limited on the number of vmas/policies based on max_map_count, so it seems natural to just use max_map_count directly, but I took a brief look at some distros and most seem to ship a raised max_map_count (>1 million) by default).

I tested that and even 1 million can cause a soft lockup.

Quoted 7 lines
Quoted 2 lines

Not sure there's an easy solution here, we might have to rework the shm policy locks.

Do you know a single valid use case that's not solved by patch #1 and would trigger that many policies?

Ai was not able to come up with anything for me.

Just want to make sure the bases are covered.

If others are ok with this change, it doesn't look unreasonable to me.

Although I'd wonder whether just using max_map_count's limit instead of adding a new toggle would just be cleaner and avoid adding a new uapi.

Signature · 1 line
~Gregory
author maintainer
#9 lore raw

Re: [PATCH 2/2] mm/mempolicy: limit shared policy ranges per inode

Quoted 16 lines · Gregory Price (Meta)
Quoted 15 lines · David Hildenbrand (Arm)
Quoted 13 lines · Gregory Price (Meta)

Multi-task mappers were previously unlimited, but that seems like more of a nasty unintended corner case - not a real feature.

A single process is already limited on the number of vmas/policies based on max_map_count, so it seems natural to just use max_map_count directly, but I took a brief look at some distros and most seem to ship a raised max_map_count (>1 million) by default).

I tested that and even 1 million can cause a soft lockup.

Not sure there's an easy solution here, we might have to rework the shm policy locks.

Quoted 7 lines
Quoted 4 lines

Do you know a single valid use case that's not solved by patch #1 and would trigger that many policies?

Ai was not able to come up with anything for me.

Just want to make sure the bases are covered.

Yes, and I'd be very happy to learn about use cases.

Maybe we can let this stay in -next for a while before moving forward with this?

Quoted 4 lines

If others are ok with this change, it doesn't look unreasonable to me.

Although I'd wonder whether just using max_map_count's limit instead of adding a new toggle would just be cleaner and avoid adding a new uapi.

I considered it but discarded that. Having 1mil on Fedora et al is not particularly helpful. Both are related but, in practice, completely separate concepts.

Signature · 3 lines
Cheers,

David
maintainer Reviewed-by
#10 lore raw

Re: [PATCH 2/2] mm/mempolicy: limit shared policy ranges per inode

Quoted 20 lines · David Hildenbrand (Arm)
Quoted 19 lines · Gregory Price (Meta)
Quoted 18 lines · David Hildenbrand (Arm)
Quoted 9 lines · Gregory Price (Meta)

Multi-task mappers were previously unlimited, but that seems like more of a nasty unintended corner case - not a real feature.

A single process is already limited on the number of vmas/policies based on max_map_count, so it seems natural to just use max_map_count directly, but I took a brief look at some distros and most seem to ship a raised max_map_count (>1 million) by default).

Quoted 4 lines

I tested that and even 1 million can cause a soft lockup.

Not sure there's an easy solution here, we might have to rework the shm policy locks.

Do you know a single valid use case that's not solved by patch #1 and would trigger that many policies?

Quoted 9 lines
Quoted 4 lines
Quoted 1 line

Ai was not able to come up with anything for me.

Just want to make sure the bases are covered.

Yes, and I'd be very happy to learn about use cases.

Maybe we can let this stay in -next for a while before moving forward with this?

Seems reasonable, although just kind of delaying the inevitable.

I suppose the mitigation for it causing issues is backporting an increase to the default size.

Eh, if the concern here isn't a big deal, I'd just say ship it.

Signature · 1 line
~Gregory
author maintainer
#11 lore raw

Re: [PATCH 2/2] mm/mempolicy: limit shared policy ranges per inode

Quoted 11 lines · Gregory Price (Meta)
Quoted 9 lines · David Hildenbrand (Arm)
Quoted 3 lines · Gregory Price (Meta)

Just want to make sure the bases are covered.

Yes, and I'd be very happy to learn about use cases.

Maybe we can let this stay in -next for a while before moving forward with this?

Quoted 8 lines

Seems reasonable, although just kind of delaying the inevitable.

I suppose the mitigation for it causing issues is backporting an increase to the default size.

Eh, if the concern here isn't a big deal, I'd just say ship it.

Thanks, I'll wait a bit longer for feedback, especially from Hugh and Baolin; maybe they have some examples where we would actually end up using way more than 1024 disting shared policy ranges even after #1.

Signature · 3 lines
Cheers,

David
author maintainer Applied
#12 lore raw
In reply to David Hildenbrand (Arm)
Quoted 8 lines · David Hildenbrand (Arm)

For some reason we thought it would be a good idea to let users create as many shared policy ranges for shmem/tmpfs as they want. And guest_memfd inherited that handling.

Fortunately, at least for guest_memfd we know that we shouldn't really have more than one shared policy right now in practice.

[...]

Applied, thanks!

tree: mm/core.git
branch: mempolicy-7.5.shared_policy_limit

Applied patches will appear soon in mm-next, followed by linux-next.

Please report any outstanding bugs that were missed during review, so we can either drop the patch series or decide how to fix it up.

It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even though the patches have already been applied. If possible, patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. Further, this is not a guarantee that the patches will go upstream. If in doubt, please check the listed branch.

[1/2] mm/mempolicy: merge adjacent shared policy ranges

      https://git.kernel.org/mm/core/c/ca9e4df0
[2/2] mm/mempolicy: limit shared policy ranges per inode
      https://git.kernel.org/mm/core/c/8d93a9af
Signature · 3 lines
Cheers,

David