Reimagining Memory Cgroup (memcg_ext)
Subsystems (3)
Over the last couple of weeks, I have been brainstorming on how I would go about redesigning memcg, taking inspiration from sched_ext and bpfoom, with a focus on existing challenges and issues. This proposal outlines the high-level direction. Followup emails and patch series will cover and brainstorm the mechanisms (of course BPF) to achieve these goals.
Memory cgroups provide memory accounting and the ability to control memory usage of workloads through two categories of limits. Throttling limits (memory.max and memory.high) cap memory consumption. Protection limits (memory.min and memory.low) shield a workload's memory from reclaim under external memory pressure.
Challenges
----------
- Workload owners rarely know their actual memory requirements, leading to overprovisioned limits, lower utilization, and higher infrastructure costs.
- Throttling limit enforcement is synchronous in the allocating task's context, which can stall latency-sensitive threads.
- The stalled thread may hold shared locks, causing priority inversion -- all waiters are blocked regardless of their priority.
- Enforcement is indiscriminate -- there is no way to distinguish a performance-critical or latency-critical allocator from a latency-tolerant one.
- Protection limits assume static working sets size, forcing owners to either overprovision or build complex userspace infrastructure to dynamically adjust them.
Feature Wishlist
----------------
Here is the list of features and capabilities I want to enable in the redesigned memcg limit enforcement world.
Per-Memcg Background Reclaim
In the new memcg world, with the goal of (mostly) eliminating direct synchronous reclaim for limit enforcement, provide per-memcg background reclaimers which can scale across CPUs with the allocation rate.
Lock-Aware Throttling
The ability to avoid throttling an allocating task that is holding locks, to prevent priority inversion. In Meta's fleet, we have observed lock holders stuck in memcg reclaim, blocking all waiters regardless of their priority or criticality.
Thread-Level Throttling Control
Workloads should be able to indicate at the thread level which threads can be synchronously throttled and which cannot. For example, while experimenting with sched_ext, we drastically improved the performance of AI training workloads by prioritizing threads interacting with the GPU. Similarly, applications can identify the threads or thread pools on their performance-critical paths and the memcg enforcement mechanism should not throttle them.
Combined Memory and Swap Limits
Some users (Google actually) need the ability to enforce limits based on combined memory and swap usage, similar to cgroup v1's memsw limit, providing a ceiling on total memory commitment rather than treating memory and swap independently.
Dynamic Protection Limits
Rather than static protection limits, the kernel should support defining protection based on the actual working set of the workload, leveraging signals such as working set estimation, PSI, refault rates, or a combination thereof to automatically adapt to the workload's current memory needs.
Shared Memory Semantics
With more flexibility in limit enforcement, the kernel should be able to account for memory shared between workloads (cgroups) during enforcement. Today, enforcement only looks at each workload's memory usage independently. Sensible shared memory semantics would allow the enforcer to consider cross-cgroup sharing when making reclaim and throttling decisions.
Memory Tiering
With a flexible limit enforcement mechanism, the kernel can balance memory usage of different workloads across memory tiers based on their performance requirements. Tier accounting and hotness tracking are orthogonal, but the decisions of when and how to balance memory between tiers should be handled by the enforcer.
Collaborative Load Shedding
Many workloads communicate with an external entity for load balancing and rely on their own usage metrics like RSS or memory pressure to signal whether they can accept more or less work. This is guesswork. Instead of the workload guessing, the limit enforcer -- which is actually managing the workload's memory usage -- should be able to communicate available headroom or request the workload to shed load or reduce memory usage. This collaborative load shedding mechanism would allow workloads to make informed decisions rather than reacting to coarse signals.
Cross-Subsystem Collaboration
Finally, the limit enforcement mechanism should collaborate with the CPU scheduler and other subsystems that can release memory. For example, dirty memory is not reclaimable and the memory subsystem wakes up flushers to trigger writeback. However, flushers need CPU to run -- asking the CPU scheduler to prioritize them ensures the kernel does not lack reclaimable memory under stressful conditions. Similarly, some subsystems free memory through workqueues or RCU callbacks. While this may seem orthogonal to limit enforcement, we can definitely take advantage by having visibility into these situations.
Putting It All Together
-----------------------
To illustrate the end goal, here is an example of the scenario I want to enable. Suppose there is an AI agent controlling the resources of a host. I should be able to provide the following policy and everything should work out of the box:
Policy: "keep system-level memory utilization below 95 percent; avoid priority inversions by not throttling allocators holding locks; trim each workload's usage to its working set without regressing its relevant performance metrics; collaborate with workloads on load shedding and memory trimming decisions; and under extreme memory pressure, collaborate with the OOM killer and the central job scheduler to kill and clean up a workload."
Initially I added this example for fun, but from [1] it seems like there is a real need to enable such capabilities.
Quoted 120 lines · Shakeel Butt
Over the last couple of weeks, I have been brainstorming on how I would go about redesigning memcg, taking inspiration from
sched_extand bpfoom, with a focus on existing challenges and issues. This proposal outlines the high-level direction. Followup emails and patch series will cover and brainstorm the mechanisms (of course BPF) to achieve these goals.Memory cgroups provide memory accounting and the ability to control memory usage of workloads through two categories of limits. Throttling limits (memory.max and memory.high) cap memory consumption. Protection limits (memory.min and memory.low) shield a workload's memory from reclaim under external memory pressure.
Challenges
----------- Workload owners rarely know their actual memory requirements, leading to overprovisioned limits, lower utilization, and higher infrastructure costs.
- Throttling limit enforcement is synchronous in the allocating task's context, which can stall latency-sensitive threads.
- The stalled thread may hold shared locks, causing priority inversion -- all waiters are blocked regardless of their priority.
- Enforcement is indiscriminate -- there is no way to distinguish a performance-critical or latency-critical allocator from a latency-tolerant one.
- Protection limits assume static working sets size, forcing owners to either overprovision or build complex userspace infrastructure to dynamically adjust them.
Feature Wishlist
----------------Here is the list of features and capabilities I want to enable in the redesigned memcg limit enforcement world.
Per-Memcg Background Reclaim
In the new memcg world, with the goal of (mostly) eliminating direct synchronous reclaim for limit enforcement, provide per-memcg background reclaimers which can scale across CPUs with the allocation rate.
Lock-Aware Throttling
The ability to avoid throttling an allocating task that is holding locks, to prevent priority inversion. In Meta's fleet, we have observed lock holders stuck in memcg reclaim, blocking all waiters regardless of their priority or criticality.
Thread-Level Throttling Control
Workloads should be able to indicate at the thread level which threads can be synchronously throttled and which cannot. For example, while experimenting with
sched_ext, we drastically improved the performance of AI training workloads by prioritizing threads interacting with the GPU. Similarly, applications can identify the threads or thread pools on their performance-critical paths and the memcg enforcement mechanism should not throttle them.Combined Memory and Swap Limits
Some users (Google actually) need the ability to enforce limits based on combined memory and swap usage, similar to cgroup v1's memsw limit, providing a ceiling on total memory commitment rather than treating memory and swap independently.
Dynamic Protection Limits
Rather than static protection limits, the kernel should support defining protection based on the actual working set of the workload, leveraging signals such as working set estimation, PSI, refault rates, or a combination thereof to automatically adapt to the workload's current memory needs.
Shared Memory Semantics
With more flexibility in limit enforcement, the kernel should be able to account for memory shared between workloads (cgroups) during enforcement. Today, enforcement only looks at each workload's memory usage independently. Sensible shared memory semantics would allow the enforcer to consider cross-cgroup sharing when making reclaim and throttling decisions.
Memory Tiering
With a flexible limit enforcement mechanism, the kernel can balance memory usage of different workloads across memory tiers based on their performance requirements. Tier accounting and hotness tracking are orthogonal, but the decisions of when and how to balance memory between tiers should be handled by the enforcer.
Collaborative Load Shedding
Many workloads communicate with an external entity for load balancing and rely on their own usage metrics like RSS or memory pressure to signal whether they can accept more or less work. This is guesswork. Instead of the workload guessing, the limit enforcer -- which is actually managing the workload's memory usage -- should be able to communicate available headroom or request the workload to shed load or reduce memory usage. This collaborative load shedding mechanism would allow workloads to make informed decisions rather than reacting to coarse signals.
Cross-Subsystem Collaboration
Finally, the limit enforcement mechanism should collaborate with the CPU scheduler and other subsystems that can release memory. For example, dirty memory is not reclaimable and the memory subsystem wakes up flushers to trigger writeback. However, flushers need CPU to run -- asking the CPU scheduler to prioritize them ensures the kernel does not lack reclaimable memory under stressful conditions. Similarly, some subsystems free memory through workqueues or RCU callbacks. While this may seem orthogonal to limit enforcement, we can definitely take advantage by having visibility into these situations.
Putting It All Together
-----------------------To illustrate the end goal, here is an example of the scenario I want to enable. Suppose there is an AI agent controlling the resources of a host. I should be able to provide the following policy and everything should work out of the box:
Quoted 6 lines
Policy: "keep system-level memory utilization below 95 percent; avoid priority inversions by not throttling allocators holding locks; trim each workload's usage to its working set without regressing its relevant performance metrics; collaborate with workloads on load shedding and memory trimming decisions; and under extreme memory pressure, collaborate with the OOM killer and the central job scheduler to kill and clean up a workload."
This is fantastic, thanks Shakeel!
I'd be very interested to discuss it! I suggested a somewhat similar/related topic to the bpf track (bpf use cases in mm), we might think of joining them.
Thanks!
Quoted 2 lines · Roman Gushchin
Shakeel Butt <[email protected]> writes:
[...]
Quoted 7 lines
This is fantastic, thanks Shakeel!
I'd be very interested to discuss it! I suggested a somewhat similar/related topic to the bpf track (bpf use cases in mm), we might think of joining them.
Awesome, we can request to have a joint session between MM and BPF to discuss these topics. I know Emil and Tal are also interested in upstreaming cached_ext. So, we have multiple topics in the intersection of MM and BPF.
Quoted 35 lines · Shakeel Butt
Over the last couple of weeks, I have been brainstorming on how I would go about redesigning memcg, taking inspiration from
sched_extand bpfoom, with a focus on existing challenges and issues. This proposal outlines the high-level direction. Followup emails and patch series will cover and brainstorm the mechanisms (of course BPF) to achieve these goals.Memory cgroups provide memory accounting and the ability to control memory usage of workloads through two categories of limits. Throttling limits (memory.max and memory.high) cap memory consumption. Protection limits (memory.min and memory.low) shield a workload's memory from reclaim under external memory pressure.
Challenges
----------- Workload owners rarely know their actual memory requirements, leading to overprovisioned limits, lower utilization, and higher infrastructure costs.
- Throttling limit enforcement is synchronous in the allocating task's context, which can stall latency-sensitive threads.
- The stalled thread may hold shared locks, causing priority inversion -- all waiters are blocked regardless of their priority.
- Enforcement is indiscriminate -- there is no way to distinguish a performance-critical or latency-critical allocator from a latency-tolerant one.
- Protection limits assume static working sets size, forcing owners to either overprovision or build complex userspace infrastructure to dynamically adjust them.
Feature Wishlist
----------------
Quoted 8 lines
Here is the list of features and capabilities I want to enable in the redesigned memcg limit enforcement world.
Per-Memcg Background Reclaim
In the new memcg world, with the goal of (mostly) eliminating direct synchronous reclaim for limit enforcement, provide per-memcg background reclaimers which can scale across CPUs with the allocation rate.
This sounds like a very useful approach. I have a few questions I'm thinking through:
How would you approach implementing this background reclaim? I'm imagining something like asynchronous memory.reclaim operations - is that in line with your thinking?
And regarding cold page identification - do you have a preferred approach? I'm curious what the most practical way would be to accurately identify which pages to reclaim.
Would be great to hear your perspective.
Quoted 2 lines · Jiayuan Chen
On 3/8/26 2:24 AM, Shakeel Butt wrote:
[...]
Quoted 7 lines
Quoted 6 lines
Per-Memcg Background Reclaim
In the new memcg world, with the goal of (mostly) eliminating direct synchronous reclaim for limit enforcement, provide per-memcg background reclaimers which can scale across CPUs with the allocation rate.
Quoted 6 lines
This sounds like a very useful approach. I have a few questions I'm thinking through:
How would you approach implementing this background reclaim? I'm imagining something like asynchronous memory.reclaim operations - is that in line with your thinking?
Yes something similar. I still need to figure out the details of the mechanism but it will be calling try_to_free_mem_cgroup_pages(). More specifically the context need more thought because we need to account the CPU consumption of those background reclaimers to corresponding cgroup. Will we be using BPF workqueues or something else, need more investigation.
Quoted 4 lines
And regarding cold page identification - do you have a preferred approach? I'm curious what the most practical way would be to accurately identify which pages to reclaim.
That's orthogonal and is the job of the reclaim mechanism which can traditional LRU or MGLRU.
Quoted 1 line
Quoted 39 lines · Shakeel Butt
Over the last couple of weeks, I have been brainstorming on how I would go about redesigning memcg, taking inspiration from
sched_extand bpfoom, with a focus on existing challenges and issues. This proposal outlines the high-level direction. Followup emails and patch series will cover and brainstorm the mechanisms (of course BPF) to achieve these goals.Memory cgroups provide memory accounting and the ability to control memory usage of workloads through two categories of limits. Throttling limits (memory.max and memory.high) cap memory consumption. Protection limits (memory.min and memory.low) shield a workload's memory from reclaim under external memory pressure.
Challenges
----------- Workload owners rarely know their actual memory requirements, leading to overprovisioned limits, lower utilization, and higher infrastructure costs.
- Throttling limit enforcement is synchronous in the allocating task's context, which can stall latency-sensitive threads.
- The stalled thread may hold shared locks, causing priority inversion -- all waiters are blocked regardless of their priority.
- Enforcement is indiscriminate -- there is no way to distinguish a performance-critical or latency-critical allocator from a latency-tolerant one.
- Protection limits assume static working sets size, forcing owners to either overprovision or build complex userspace infrastructure to dynamically adjust them.
Feature Wishlist
----------------
Quoted 8 lines
Here is the list of features and capabilities I want to enable in the redesigned memcg limit enforcement world.
Per-Memcg Background Reclaim
In the new memcg world, with the goal of (mostly) eliminating direct synchronous reclaim for limit enforcement, provide per-memcg background reclaimers which can scale across CPUs with the allocation rate.
Hi Shakeel,
I'm quite interested in this. Internally, we privately maintain a set of code to implement asynchronous reclamation, but we're also trying to discard these private codes as much as possible. Therefore, we want to implement a similar asynchronous reclamation mechanism in user space through the memory.reclaim mechanism. However, currently there's a lack of suitable policy notification mechanisms to trigger user threads to proactively reclaim in advance.
Quoted 7 lines
Lock-Aware Throttling
The ability to avoid throttling an allocating task that is holding locks, to prevent priority inversion. In Meta's fleet, we have observed lock holders stuck in memcg reclaim, blocking all waiters regardless of their priority or criticality.
This is a real problem we encountered, especially with the jbd handler resources of the ext4 file system. Our current attempt is to defer memory reclamation until returning to user space, in order to solve various priority inversion issues caused by the jbd handler. Therefore, I would be interested to discuss this topic.
Muchun,
Thanks.
Quoted 81 lines
Thread-Level Throttling Control
Workloads should be able to indicate at the thread level which threads can be synchronously throttled and which cannot. For example, while experimenting with
sched_ext, we drastically improved the performance of AI training workloads by prioritizing threads interacting with the GPU. Similarly, applications can identify the threads or thread pools on their performance-critical paths and the memcg enforcement mechanism should not throttle them.Combined Memory and Swap Limits
Some users (Google actually) need the ability to enforce limits based on combined memory and swap usage, similar to cgroup v1's memsw limit, providing a ceiling on total memory commitment rather than treating memory and swap independently.
Dynamic Protection Limits
Rather than static protection limits, the kernel should support defining protection based on the actual working set of the workload, leveraging signals such as working set estimation, PSI, refault rates, or a combination thereof to automatically adapt to the workload's current memory needs.
Shared Memory Semantics
With more flexibility in limit enforcement, the kernel should be able to account for memory shared between workloads (cgroups) during enforcement. Today, enforcement only looks at each workload's memory usage independently. Sensible shared memory semantics would allow the enforcer to consider cross-cgroup sharing when making reclaim and throttling decisions.
Memory Tiering
With a flexible limit enforcement mechanism, the kernel can balance memory usage of different workloads across memory tiers based on their performance requirements. Tier accounting and hotness tracking are orthogonal, but the decisions of when and how to balance memory between tiers should be handled by the enforcer.
Collaborative Load Shedding
Many workloads communicate with an external entity for load balancing and rely on their own usage metrics like RSS or memory pressure to signal whether they can accept more or less work. This is guesswork. Instead of the workload guessing, the limit enforcer -- which is actually managing the workload's memory usage -- should be able to communicate available headroom or request the workload to shed load or reduce memory usage. This collaborative load shedding mechanism would allow workloads to make informed decisions rather than reacting to coarse signals.
Cross-Subsystem Collaboration
Finally, the limit enforcement mechanism should collaborate with the CPU scheduler and other subsystems that can release memory. For example, dirty memory is not reclaimable and the memory subsystem wakes up flushers to trigger writeback. However, flushers need CPU to run -- asking the CPU scheduler to prioritize them ensures the kernel does not lack reclaimable memory under stressful conditions. Similarly, some subsystems free memory through workqueues or RCU callbacks. While this may seem orthogonal to limit enforcement, we can definitely take advantage by having visibility into these situations.
Putting It All Together
-----------------------To illustrate the end goal, here is an example of the scenario I want to enable. Suppose there is an AI agent controlling the resources of a host. I should be able to provide the following policy and everything should work out of the box:
Policy: "keep system-level memory utilization below 95 percent; avoid priority inversions by not throttling allocators holding locks; trim each workload's usage to its working set without regressing its relevant performance metrics; collaborate with workloads on load shedding and memory trimming decisions; and under extreme memory pressure, collaborate with the OOM killer and the central job scheduler to kill and clean up a workload."
Initially I added this example for fun, but from [1] it seems like there is a real need to enable such capabilities.
Quoted 4 lines · Muchun Song
Quoted 4 lines · Shakeel Butt
[...]
Quoted 9 lines
Quoted 6 lines
Per-Memcg Background Reclaim
In the new memcg world, with the goal of (mostly) eliminating direct synchronous reclaim for limit enforcement, provide per-memcg background reclaimers which can scale across CPUs with the allocation rate.
Hi Shakeel,
Quoted 7 lines
I'm quite interested in this. Internally, we privately maintain a set of code to implement asynchronous reclamation, but we're also trying to discard these private codes as much as possible. Therefore, we want to implement a similar asynchronous reclamation mechanism in user space through the memory.reclaim mechanism. However, currently there's a lack of suitable policy notification mechanisms to trigger user threads to proactively reclaim in advance.
Cool, can you please share what "suitable policy notification mechanisms" you need for your use-case? This will give me more data on the comparison between memory.reclaim and the proposed approach.
Quoted 9 lines
Quoted 8 lines
Lock-Aware Throttling
The ability to avoid throttling an allocating task that is holding locks, to prevent priority inversion. In Meta's fleet, we have observed lock holders stuck in memcg reclaim, blocking all waiters regardless of their priority or criticality.
Quoted 5 lines
This is a real problem we encountered, especially with the jbd handler resources of the ext4 file system. Our current attempt is to defer memory reclamation until returning to user space, in order to solve various priority inversion issues caused by the jbd handler. Therefore, I would be interested to discuss this topic.
Awesome, do you use memory.max and memory.high both and defer the reclaim for both? Are you deferring all the reclaims or just the ones where the charging process has the lock? (I need to look what jbd handler is).
Quoted 29 lines · Shakeel Butt
Quoted 4 lines · Muchun Song
Quoted 4 lines · Shakeel Butt
[...]
Quoted 9 lines
Quoted 6 lines
Per-Memcg Background Reclaim
In the new memcg world, with the goal of (mostly) eliminating direct synchronous reclaim for limit enforcement, provide per-memcg background reclaimers which can scale across CPUs with the allocation rate.
Hi Shakeel,
Quoted 7 lines
I'm quite interested in this. Internally, we privately maintain a set of code to implement asynchronous reclamation, but we're also trying to discard these private codes as much as possible. Therefore, we want to implement a similar asynchronous reclamation mechanism in user space through the memory.reclaim mechanism. However, currently there's a lack of suitable policy notification mechanisms to trigger user threads to proactively reclaim in advance.
Quoted 3 lines
Cool, can you please share what "suitable policy notification mechanisms" you need for your use-case? This will give me more data on the comparison between memory.reclaim and the proposed approach.
If we expect the proactive reclamation to be triggered when the current memcg's memory usage reaches a certain point, we have to continuously read memory.current to determine whether it has reached our set watermark value to trigger asynchronous reclamation. Perhaps we need an event that can notify user-space threads when the current memory usage reaches a specific watermark value. Currently, the events supported by memory.events may lack the capability for custom watermarks.
Quoted 17 lines
Quoted 11 lines
Quoted 10 lines
Lock-Aware Throttling
The ability to avoid throttling an allocating task that is holding locks, to prevent priority inversion. In Meta's fleet, we have observed lock holders stuck in memcg reclaim, blocking all waiters regardless of their priority or criticality.
Quoted 5 lines
This is a real problem we encountered, especially with the jbd handler resources of the ext4 file system. Our current attempt is to defer memory reclamation until returning to user space, in order to solve various priority inversion issues caused by the jbd handler. Therefore, I would be interested to discuss this topic.
Quoted 4 lines
Awesome, do you use memory.max and memory.high both and defer the reclaim for both? Are you deferring all the reclaims or just the ones where the charging process has the lock? (I need to look what jbd handler is).
We do not use memory.high, although it supports deferring memory reclamation to user-space, it also attempts to throttle memory allocation speed, which introduces significant latency. In our application's case, we would rather accept an OOM under such circumstances. We previously attempted to address the priority inversion issue caused by the jbd handler separately (which we frequently encounter since we use the ext4 file system), and you can refer to this [1]. Of course, this solution lacks generality, as it requires calling new interfaces for various lock resources. Therefore, we internally have a more aggressive idea: defer all reclamation triggered by kernel-space memory allocation until just before returning to user-space. This should resolve the vast majority of priority inversion problems. The only potential issue introduced is that kernel-space memory usage may briefly exceed memory.max.
[1] https://lore.kernel.org/linux-mm/[email protected]/#r
Muchun,
Thanks.
Quoted 37 lines
Quoted 33 lines · Shakeel Butt
Quoted 3 lines · Muchun Song
On Mar 8, 2026, at 02:24, Shakeel Butt <[email protected]> wrote:
[...]
Per-Memcg Background Reclaim
In the new memcg world, with the goal of (mostly) eliminating direct synchronous reclaim for limit enforcement, provide per-memcg background reclaimers which can scale across CPUs with the allocation rate.
Quoted 11 lines
Hi Shakeel,
I'm quite interested in this. Internally, we privately maintain a set of code to implement asynchronous reclamation, but we're also trying to discard these private codes as much as possible. Therefore, we want to implement a similar asynchronous reclamation mechanism in user space through the memory.reclaim mechanism. However, currently there's a lack of suitable policy notification mechanisms to trigger user threads to proactively reclaim in advance.
Quoted 4 lines
Cool, can you please share what "suitable policy notification mechanisms" you need for your use-case? This will give me more data on the comparison between memory.reclaim and the proposed approach.
Quoted 7 lines
If we expect the proactive reclamation to be triggered when the current memcg's memory usage reaches a certain point, we have to continuously read memory.current to determine whether it has reached our set watermark value to trigger asynchronous reclamation. Perhaps we need an event that can notify user-space threads when the current memory usage reaches a specific watermark value. Currently, the events supported by memory.events may lack the capability for custom watermarks.
I agree. Even with BPF controlling proactive reclamation, I believe there needs to be an event reflecting capacity changes to signal when to stop.
Otherwise, the reclamation volume per batch would have to be set very low, leading to frequent BPF triggers and poor efficiency.
Best,
Hui
Quoted 43 lines
Quoted 21 lines
Quoted 5 lines
Lock-Aware Throttling
The ability to avoid throttling an allocating task that is holding locks, to prevent priority inversion. In Meta's fleet, we have observed lock holders stuck in memcg reclaim, blocking all waiters regardless of their priority or criticality.
Quoted 7 lines
This is a real problem we encountered, especially with the jbd handler
resources of the ext4 file system. Our current attempt is to defer memory reclamation until returning to user space, in order to solve various priority inversion issues caused by the jbd handler. Therefore, I would be interested to discuss this topic.Quoted 4 lines
Awesome, do you use memory.max and memory.high both and defer the reclaim for both? Are you deferring all the reclaims or just the ones where the charging process has the lock? (I need to look what jbd handler is).
We do not use memory.high, although it supports deferring memory reclamation to user-space, it also attempts to throttle memory allocation speed, which introduces significant latency. In our application's case, we would rather accept an OOM under such circumstances. We previously attempted to address the priority inversion issue caused by the jbd handler separately (which we frequently encounter since we use the ext4 file system), and you can refer to this [1]. Of course, this solution lacks generality, as it requires calling new interfaces for various lock resources. Therefore, we internally have a more aggressive idea: defer all reclamation triggered by kernel-space memory allocation until just before returning to user-space. This should resolve the vast majority of priority inversion problems. The only potential issue introduced is that kernel-space memory usage may briefly exceed memory.max.
[1] https://lore.kernel.org/linux-mm/[email protected]/#r
Muchun,
Thanks.
Quoted 128 lines · Shakeel Butt
Over the last couple of weeks, I have been brainstorming on how I would go about redesigning memcg, taking inspiration from
sched_extand bpfoom, with a focus on existing challenges and issues. This proposal outlines the high-level direction. Followup emails and patch series will cover and brainstorm the mechanisms (of course BPF) to achieve these goals.Memory cgroups provide memory accounting and the ability to control memory usage of workloads through two categories of limits. Throttling limits (memory.max and memory.high) cap memory consumption. Protection limits (memory.min and memory.low) shield a workload's memory from reclaim under external memory pressure.
Challenges
----------- Workload owners rarely know their actual memory requirements, leading to overprovisioned limits, lower utilization, and higher infrastructure costs.
- Throttling limit enforcement is synchronous in the allocating task's context, which can stall latency-sensitive threads.
- The stalled thread may hold shared locks, causing priority inversion -- all waiters are blocked regardless of their priority.
- Enforcement is indiscriminate -- there is no way to distinguish a performance-critical or latency-critical allocator from a latency-tolerant one.
- Protection limits assume static working sets size, forcing owners to either overprovision or build complex userspace infrastructure to dynamically adjust them.
Feature Wishlist
----------------Here is the list of features and capabilities I want to enable in the redesigned memcg limit enforcement world.
Per-Memcg Background Reclaim
In the new memcg world, with the goal of (mostly) eliminating direct synchronous reclaim for limit enforcement, provide per-memcg background reclaimers which can scale across CPUs with the allocation rate.
Lock-Aware Throttling
The ability to avoid throttling an allocating task that is holding locks, to prevent priority inversion. In Meta's fleet, we have observed lock holders stuck in memcg reclaim, blocking all waiters regardless of their priority or criticality.
Thread-Level Throttling Control
Workloads should be able to indicate at the thread level which threads can be synchronously throttled and which cannot. For example, while experimenting with
sched_ext, we drastically improved the performance of AI training workloads by prioritizing threads interacting with the GPU. Similarly, applications can identify the threads or thread pools on their performance-critical paths and the memcg enforcement mechanism should not throttle them.Combined Memory and Swap Limits
Some users (Google actually) need the ability to enforce limits based on combined memory and swap usage, similar to cgroup v1's memsw limit, providing a ceiling on total memory commitment rather than treating memory and swap independently.
Dynamic Protection Limits
Rather than static protection limits, the kernel should support defining protection based on the actual working set of the workload, leveraging signals such as working set estimation, PSI, refault rates, or a combination thereof to automatically adapt to the workload's current memory needs.
Shared Memory Semantics
With more flexibility in limit enforcement, the kernel should be able to account for memory shared between workloads (cgroups) during enforcement. Today, enforcement only looks at each workload's memory usage independently. Sensible shared memory semantics would allow the enforcer to consider cross-cgroup sharing when making reclaim and throttling decisions.
Memory Tiering
With a flexible limit enforcement mechanism, the kernel can balance memory usage of different workloads across memory tiers based on their performance requirements. Tier accounting and hotness tracking are orthogonal, but the decisions of when and how to balance memory between tiers should be handled by the enforcer.
Collaborative Load Shedding
Many workloads communicate with an external entity for load balancing and rely on their own usage metrics like RSS or memory pressure to signal whether they can accept more or less work. This is guesswork. Instead of the workload guessing, the limit enforcer -- which is actually managing the workload's memory usage -- should be able to communicate available headroom or request the workload to shed load or reduce memory usage. This collaborative load shedding mechanism would allow workloads to make informed decisions rather than reacting to coarse signals.
Cross-Subsystem Collaboration
Finally, the limit enforcement mechanism should collaborate with the CPU scheduler and other subsystems that can release memory. For example, dirty memory is not reclaimable and the memory subsystem wakes up flushers to trigger writeback. However, flushers need CPU to run -- asking the CPU scheduler to prioritize them ensures the kernel does not lack reclaimable memory under stressful conditions. Similarly, some subsystems free memory through workqueues or RCU callbacks. While this may seem orthogonal to limit enforcement, we can definitely take advantage by having visibility into these situations.
Putting It All Together
-----------------------To illustrate the end goal, here is an example of the scenario I want to enable. Suppose there is an AI agent controlling the resources of a host. I should be able to provide the following policy and everything should work out of the box:
Policy: "keep system-level memory utilization below 95 percent; avoid priority inversions by not throttling allocators holding locks; trim each workload's usage to its working set without regressing its relevant performance metrics; collaborate with workloads on load shedding and memory trimming decisions; and under extreme memory pressure, collaborate with the OOM killer and the central job scheduler to kill and clean up a workload."
Quoted 5 lines
Initially I added this example for fun, but from [1] it seems like there is a real need to enable such capabilities.
Very interesting set of topics. A few more come to mind.
I've wondered about preallocating memory or guaranteeing access to physical memory for a job. Memcg has max limits and min protections, but no preallocation (i.e. no conceptual memcg free list). So if a job is configured with 1GB min workingset protection that only ensures 1GB won't be reclaimed, not that 1GB can be allocated in a reasonable amount of time. This isn't just a job startup problem: if a page is freed with MADV_DONTNEED a subsequent pgfault may require a lot of time to handle, even if usage is below min.
Initial allocation policies are controlled by mempolicy/cpuset. Should we continue to keep allocation policies and resource accounting separate? It's a little strange that memcg can (1) cap max usage of tier X memory, and (2) provide minimum protection for tier X usage, but has no influence on where memory is initially allocated?
Hi Greg,
Quoted 5 lines · Greg Thelen
Quoted 2 lines · Shakeel Butt
Very interesting set of topics. A few more come to mind.
Thanks.
Quoted 9 lines
I've wondered about preallocating memory or guaranteeing access to physical memory for a job. Memcg has max limits and min protections, but no preallocation (i.e. no conceptual memcg free list). So if a job is configured with 1GB min workingset protection that only ensures 1GB won't be reclaimed, not that 1GB can be allocated in a reasonable amount of time. This isn't just a job startup problem: if a page is freed with
MADV_DONTNEEDa subsequent pgfault may require a lot of time to handle, even if usage is below min.
This is indeed correct i.e. protection limits protect the workload from external reclaim but does not provide any gurantee on allocating memory in a reasonable cheap way (without triggering reclaim/compaction). This is one of the challenge to implement userspace oom-killer in an aggressively overcommitted environment.
However to me providing memory allocation guarantees is more of a system level feature and orthogonal to memcg. And I see your next para is about that :)
Anyways I think if we keep system memory utilization below some value and guarantee there is always some free memory (this can be done by having common ancestor of all workloads and ancestor has a limit or node controller maintains the condition that the sum of limits of all top level cgroups is below some percentage of total memory) then we might not need memcg free list or similar mechanisms (most of the time, I think).
Quoted 6 lines
Initial allocation policies are controlled by mempolicy/cpuset. Should we continue to keep allocation policies and resource accounting separate? It's a little strange that memcg can (1) cap max usage of tier X memory, and (2) provide minimum protection for tier X usage, but has no influence on where memory is initially allocated?
I think I understand your point but I think the implementation would be too messy. This is orthogonal to the proposal but I would say a good topic for LSFMMBPF if you want to lead the discussion.
Johannes Weiner
maintainerQuoted 12 lines · Shakeel Butt
Over the last couple of weeks, I have been brainstorming on how I would go about redesigning memcg, taking inspiration from
sched_extand bpfoom, with a focus on existing challenges and issues. This proposal outlines the high-level direction. Followup emails and patch series will cover and brainstorm the mechanisms (of course BPF) to achieve these goals.Memory cgroups provide memory accounting and the ability to control memory usage of workloads through two categories of limits. Throttling limits (memory.max and memory.high) cap memory consumption. Protection limits (memory.min and memory.low) shield a workload's memory from reclaim under external memory pressure.
Quoted 5 lines
Challenges
----------- Workload owners rarely know their actual memory requirements, leading to overprovisioned limits, lower utilization, and higher infrastructure costs.
Is this actually a challenge?
It appears to me proactive reclaim is fairly widespread at this point, giving workload owners, job schedulers, and capacity planners real-world, long-term profiles of memory usage.
Workload owners can use this to adjust their limits accordingly, of course, but even that is less relevant if schedulers and planners go off of the measured information. The limits become failsafes, no longer the declarative source of truth for memory size.
Quoted 17 lines
- Throttling limit enforcement is synchronous in the allocating task's context, which can stall latency-sensitive threads.
- The stalled thread may hold shared locks, causing priority inversion -- all waiters are blocked regardless of their priority.
- Enforcement is indiscriminate -- there is no way to distinguish a performance-critical or latency-critical allocator from a latency-tolerant one.
- Protection limits assume static working sets size, forcing owners to either overprovision or build complex userspace infrastructure to dynamically adjust them.
Feature Wishlist
----------------
Quoted 8 lines
Here is the list of features and capabilities I want to enable in the redesigned memcg limit enforcement world.
Per-Memcg Background Reclaim
In the new memcg world, with the goal of (mostly) eliminating direct synchronous reclaim for limit enforcement, provide per-memcg background reclaimers which can scale across CPUs with the allocation rate.
Meta has been carrying this patch for half a decade:
https://lore.kernel.org/linux-mm/[email protected]/
It sounds like others have carried similar patches.
The relevance of this, too, has somewhat faded with proactive reclaim. But I think it would still be worthwhile to have. The primary objection was a lack of attribution of the consumed CPU cycles.
Quoted 7 lines
Lock-Aware Throttling
The ability to avoid throttling an allocating task that is holding locks, to prevent priority inversion. In Meta's fleet, we have observed lock holders stuck in memcg reclaim, blocking all waiters regardless of their priority or criticality.
Quoted 8 lines
Thread-Level Throttling Control
Workloads should be able to indicate at the thread level which threads can be synchronously throttled and which cannot. For example, while experimenting with
sched_ext, we drastically improved the performance of AI training workloads by prioritizing threads interacting with the GPU. Similarly, applications can identify the threads or thread pools on their performance-critical paths and the memcg enforcement mechanism should not throttle them.
I'm struggling to envision this.
CPU and GPU are renewable resources where a bias in access time and scheduling delays over time is naturally compensated.
With memory access past the limit, though, such a bias adds up over time. How do you prevent high priority threads from runaway memory consumption that ends up OOMing the host?
Quoted 7 lines
Combined Memory and Swap Limits
Some users (Google actually) need the ability to enforce limits based on combined memory and swap usage, similar to cgroup v1's memsw limit, providing a ceiling on total memory commitment rather than treating memory and swap independently.
Quoted 6 lines
Dynamic Protection Limits
Rather than static protection limits, the kernel should support defining protection based on the actual working set of the workload, leveraging signals such as working set estimation, PSI, refault rates, or a combination thereof to automatically adapt to the workload's current memory needs.
This should be possible with today's interfaces of memory.reclaim, memory.pressure and memory.low, right?
Quoted 7 lines
Shared Memory Semantics
With more flexibility in limit enforcement, the kernel should be able to account for memory shared between workloads (cgroups) during enforcement. Today, enforcement only looks at each workload's memory usage independently. Sensible shared memory semantics would allow the enforcer to consider cross-cgroup sharing when making reclaim and throttling decisions.
My understanding is that this hasn't been a problem of implementation, but one of identifying reasonable, predictable semantics - how exactly the liability of shared resources are allocated to participating groups.
Quoted 21 lines
Memory Tiering
With a flexible limit enforcement mechanism, the kernel can balance memory usage of different workloads across memory tiers based on their performance requirements. Tier accounting and hotness tracking are orthogonal, but the decisions of when and how to balance memory between tiers should be handled by the enforcer.
Collaborative Load Shedding
Many workloads communicate with an external entity for load balancing and rely on their own usage metrics like RSS or memory pressure to signal whether they can accept more or less work. This is guesswork. Instead of the workload guessing, the limit enforcer -- which is actually managing the workload's memory usage -- should be able to communicate available headroom or request the workload to shed load or reduce memory usage. This collaborative load shedding mechanism would allow workloads to make informed decisions rather than reacting to coarse signals.
Cross-Subsystem Collaboration
Quoted 8 lines
Finally, the limit enforcement mechanism should collaborate with the CPU scheduler and other subsystems that can release memory. For example, dirty memory is not reclaimable and the memory subsystem wakes up flushers to trigger writeback. However, flushers need CPU to run -- asking the CPU scheduler to prioritize them ensures the kernel does not lack reclaimable memory under stressful conditions. Similarly, some subsystems free memory through workqueues or RCU callbacks. While this may seem orthogonal to limit enforcement, we can definitely take advantage by having visibility into these situations.
It sounds like the lock holder problem would also fit into this
category: Identifying critical lock holders and allowing them temporary access past the memory and CPU limits.
But as per above, I'm not sure if blank check exemptions are workable for memory. It makes sense for allocations in the reclaim path for example, because it doesn't leave us wondering who will pay for the excess through a deficit. It's less obvious for a path that is involved with further expansion of the cgroup's footprint.
Quoted 1 line · Johannes Weiner
On Sat, Mar 07, 2026 at 10:24:24AM -0800, Shakeel Butt wrote:
[...]
Quoted 6 lines
Quoted 3 lines
- Workload owners rarely know their actual memory requirements, leading to overprovisioned limits, lower utilization, and higher infrastructure costs.
Is this actually a challenge?
Quoted 8 lines
It appears to me proactive reclaim is fairly widespread at this point, giving workload owners, job schedulers, and capacity planners real-world, long-term profiles of memory usage.
Workload owners can use this to adjust their limits accordingly, of course, but even that is less relevant if schedulers and planners go off of the measured information. The limits become failsafes, no longer the declarative source of truth for memory size.
Yes for sophisticated users, this is a solved problem, particularly for workloads with consistent memory usage behavior. I think workloads with inconsistent or sporadic usage behavior is still a challenge.
Quoted 8 lines
Quoted 7 lines
Per-Memcg Background Reclaim
In the new memcg world, with the goal of (mostly) eliminating direct synchronous reclaim for limit enforcement, provide per-memcg background reclaimers which can scale across CPUs with the allocation rate.
Quoted 5 lines
Meta has been carrying this patch for half a decade:
https://lore.kernel.org/linux-mm/[email protected]/
It sounds like others have carried similar patches.
Yeah ByteDance has something similar too.
Quoted 21 lines
The relevance of this, too, has somewhat faded with proactive reclaim. But I think it would still be worthwhile to have. The primary objection was a lack of attribution of the consumed CPU cycles.
Quoted 7 lines
Lock-Aware Throttling
The ability to avoid throttling an allocating task that is holding locks, to prevent priority inversion. In Meta's fleet, we have observed lock holders stuck in memcg reclaim, blocking all waiters regardless of their priority or criticality.
Quoted 8 lines
Thread-Level Throttling Control
Workloads should be able to indicate at the thread level which threads can be synchronously throttled and which cannot. For example, while experimenting with
sched_ext, we drastically improved the performance of AI training workloads by prioritizing threads interacting with the GPU. Similarly, applications can identify the threads or thread pools on their performance-critical paths and the memcg enforcement mechanism should not throttle them.
Quoted 8 lines
I'm struggling to envision this.
CPU and GPU are renewable resources where a bias in access time and scheduling delays over time is naturally compensated.
With memory access past the limit, though, such a bias adds up over time. How do you prevent high priority threads from runaway memory consumption that ends up OOMing the host?
Oh don't consider this feature in isolation. In practice there definitely will be background reclaimers running here. The way I am envisioning the scenario for this feature is something like: At some usage threshold, we will start the background reclaimers, at the next threshold, we will start synchronously throttle the threads that are allowed by the workload and at next threshold point we may decide to just kill the workload.
Quoted 10 lines
Quoted 10 lines
Combined Memory and Swap Limits
Some users (Google actually) need the ability to enforce limits based on combined memory and swap usage, similar to cgroup v1's memsw limit, providing a ceiling on total memory commitment rather than treating memory and swap independently.
Dynamic Protection Limits
Quoted 7 lines
Quoted 4 lines
Rather than static protection limits, the kernel should support defining protection based on the actual working set of the workload, leveraging signals such as working set estimation, PSI, refault rates, or a combination thereof to automatically adapt to the workload's current memory needs.
This should be possible with today's interfaces of memory.reclaim, memory.pressure and memory.low, right?
Yes, node controller or workload can dynamically their protection limit based on psi or refaults or some other metrics.
Quoted 9 lines
Quoted 8 lines
Shared Memory Semantics
With more flexibility in limit enforcement, the kernel should be able to account for memory shared between workloads (cgroups) during enforcement. Today, enforcement only looks at each workload's memory usage independently. Sensible shared memory semantics would allow the enforcer to consider cross-cgroup sharing when making reclaim and throttling decisions.
Quoted 4 lines
My understanding is that this hasn't been a problem of implementation, but one of identifying reasonable, predictable semantics - how exactly the liability of shared resources are allocated to participating groups.
This particular feature is hand-wavy at the moment particulary due to lack of mechanism that tells how much memory is really shared.
The high level idea is when we know there is shared memory/fs between different workloads, during throttling decision, we can consider their memory usage excluding the shared usage. So, mainly their exclusive memory usage. Will this help or is useful, I need to brainstorm more.
Quoted 34 lines
Quoted 21 lines
Memory Tiering
With a flexible limit enforcement mechanism, the kernel can balance memory usage of different workloads across memory tiers based on their performance requirements. Tier accounting and hotness tracking are orthogonal, but the decisions of when and how to balance memory between tiers should be handled by the enforcer.
Collaborative Load Shedding
Many workloads communicate with an external entity for load balancing and rely on their own usage metrics like RSS or memory pressure to signal whether they can accept more or less work. This is guesswork. Instead of the workload guessing, the limit enforcer -- which is actually managing the workload's memory usage -- should be able to communicate available headroom or request the workload to shed load or reduce memory usage. This collaborative load shedding mechanism would allow workloads to make informed decisions rather than reacting to coarse signals.
Cross-Subsystem Collaboration
Quoted 8 lines
Finally, the limit enforcement mechanism should collaborate with the CPU scheduler and other subsystems that can release memory. For example, dirty memory is not reclaimable and the memory subsystem wakes up flushers to trigger writeback. However, flushers need CPU to run -- asking the CPU scheduler to prioritize them ensures the kernel does not lack reclaimable memory under stressful conditions. Similarly, some subsystems free memory through workqueues or RCU callbacks. While this may seem orthogonal to limit enforcement, we can definitely take advantage by having visibility into these situations.
It sounds like the lock holder problem would also fit into this
category: Identifying critical lock holders and allowing them temporary access past the memory and CPU limits.
Quoted 5 lines
But as per above, I'm not sure if blank check exemptions are workable for memory. It makes sense for allocations in the reclaim path for example, because it doesn't leave us wondering who will pay for the excess through a deficit. It's less obvious for a path that is involved with further expansion of the cgroup's footprint.
No need to have blank check. Same as above for the thread throttling, the lock holder not getting throttled will be, in practice, in the presense of background reclaimers and may get killed if going over board too much.
Thanks for taking a look and poking holes.
2026年3月8日 02:24, "Shakeel Butt" <[email protected] mailto:[email protected]?to=%22Shakeel%20Butt%22%20%3Cshakeel.butt%40linux.dev%3E > 写到:
Quoted 33 lines
Over the last couple of weeks, I have been brainstorming on how I would go about redesigning memcg, taking inspiration from
sched_extand bpfoom, with a focus on existing challenges and issues. This proposal outlines the high-level direction. Followup emails and patch series will cover and brainstorm the mechanisms (of course BPF) to achieve these goals.Memory cgroups provide memory accounting and the ability to control memory usage of workloads through two categories of limits. Throttling limits (memory.max and memory.high) cap memory consumption. Protection limits (memory.min and memory.low) shield a workload's memory from reclaim under external memory pressure.
Challenges
----------- Workload owners rarely know their actual memory requirements, leading to overprovisioned limits, lower utilization, and higher infrastructure costs.
- Throttling limit enforcement is synchronous in the allocating task's context, which can stall latency-sensitive threads.
- The stalled thread may hold shared locks, causing priority inversion -- all waiters are blocked regardless of their priority.
- Enforcement is indiscriminate -- there is no way to distinguish a performance-critical or latency-critical allocator from a latency-tolerant one.
- Protection limits assume static working sets size, forcing owners to either overprovision or build complex userspace infrastructure to dynamically adjust them.
Quoted 5 lines
Feature Wishlist
----------------Here is the list of features and capabilities I want to enable in the redesigned memcg limit enforcement world.
Thanks for summarizing and categorizing all of this.
Quoted 14 lines
Per-Memcg Background Reclaim
In the new memcg world, with the goal of (mostly) eliminating direct synchronous reclaim for limit enforcement, provide per-memcg background reclaimers which can scale across CPUs with the allocation rate.
Lock-Aware Throttling
The ability to avoid throttling an allocating task that is holding locks, to prevent priority inversion. In Meta's fleet, we have observed lock holders stuck in memcg reclaim, blocking all waiters regardless of their priority or criticality.
Quoted 8 lines
Thread-Level Throttling Control
Workloads should be able to indicate at the thread level which threads can be synchronously throttled and which cannot. For example, while experimenting with
sched_ext, we drastically improved the performance of AI training workloads by prioritizing threads interacting with the GPU. Similarly, applications can identify the threads or thread pools on their performance-critical paths and the memcg enforcement mechanism should not throttle them.
Does this mean that different threads within the same memcg can be selectively exempt from throttling control via BPF?
Quoted 8 lines
Combined Memory and Swap Limits
Some users (Google actually) need the ability to enforce limits based on combined memory and swap usage, similar to cgroup v1's memsw limit, providing a ceiling on total memory commitment rather than treating memory and swap independently.
Quoted 6 lines
Dynamic Protection Limits
Rather than static protection limits, the kernel should support defining protection based on the actual working set of the workload, leveraging signals such as working set estimation, PSI, refault rates, or a combination thereof to automatically adapt to the workload's current memory needs.
This part is what we are interesting now.
https://www.spinics.net/lists/kernel/msg6037006.html this is the RFC for it.
Best,
Hui
Quoted 59 lines
Shared Memory Semantics
With more flexibility in limit enforcement, the kernel should be able to account for memory shared between workloads (cgroups) during enforcement. Today, enforcement only looks at each workload's memory usage independently. Sensible shared memory semantics would allow the enforcer to consider cross-cgroup sharing when making reclaim and throttling decisions.
Memory Tiering
With a flexible limit enforcement mechanism, the kernel can balance memory usage of different workloads across memory tiers based on their performance requirements. Tier accounting and hotness tracking are orthogonal, but the decisions of when and how to balance memory between tiers should be handled by the enforcer.
Collaborative Load Shedding
Many workloads communicate with an external entity for load balancing and rely on their own usage metrics like RSS or memory pressure to signal whether they can accept more or less work. This is guesswork. Instead of the workload guessing, the limit enforcer -- which is actually managing the workload's memory usage -- should be able to communicate available headroom or request the workload to shed load or reduce memory usage. This collaborative load shedding mechanism would allow workloads to make informed decisions rather than reacting to coarse signals.
Cross-Subsystem Collaboration
Finally, the limit enforcement mechanism should collaborate with the CPU scheduler and other subsystems that can release memory. For example, dirty memory is not reclaimable and the memory subsystem wakes up flushers to trigger writeback. However, flushers need CPU to run -- asking the CPU scheduler to prioritize them ensures the kernel does not lack reclaimable memory under stressful conditions. Similarly, some subsystems free memory through workqueues or RCU callbacks. While this may seem orthogonal to limit enforcement, we can definitely take advantage by having visibility into these situations.
Putting It All Together
-----------------------To illustrate the end goal, here is an example of the scenario I want to enable. Suppose there is an AI agent controlling the resources of a host. I should be able to provide the following policy and everything should work out of the box:
Policy: "keep system-level memory utilization below 95 percent; avoid priority inversions by not throttling allocators holding locks; trim each workload's usage to its working set without regressing its relevant performance metrics; collaborate with workloads on load shedding and memory trimming decisions; and under extreme memory pressure, collaborate with the OOM killer and the central job scheduler to kill and clean up a workload."
Initially I added this example for fun, but from [1] it seems like there is a real need to enable such capabilities.
2026年3月8日 02:24, "Shakeel Butt" <[email protected] mailto:[email protected]?to=%22Shakeel%20Butt%22%20%3Cshakeel.butt%40linux.dev%3E > 写到:
Quoted 36 lines
Over the last couple of weeks, I have been brainstorming on how I would go about redesigning memcg, taking inspiration from
sched_extand bpfoom, with a focus on existing challenges and issues. This proposal outlines the high-level direction. Followup emails and patch series will cover and brainstorm the mechanisms (of course BPF) to achieve these goals.Memory cgroups provide memory accounting and the ability to control memory usage of workloads through two categories of limits. Throttling limits (memory.max and memory.high) cap memory consumption. Protection limits (memory.min and memory.low) shield a workload's memory from reclaim under external memory pressure.
Challenges
----------- Workload owners rarely know their actual memory requirements, leading to overprovisioned limits, lower utilization, and higher infrastructure costs.
- Throttling limit enforcement is synchronous in the allocating task's context, which can stall latency-sensitive threads.
- The stalled thread may hold shared locks, causing priority inversion -- all waiters are blocked regardless of their priority.
- Enforcement is indiscriminate -- there is no way to distinguish a performance-critical or latency-critical allocator from a latency-tolerant one.
- Protection limits assume static working sets size, forcing owners to either overprovision or build complex userspace infrastructure to dynamically adjust them.
Feature Wishlist
----------------
Quoted 8 lines
Here is the list of features and capabilities I want to enable in the redesigned memcg limit enforcement world.
Per-Memcg Background Reclaim
In the new memcg world, with the goal of (mostly) eliminating direct synchronous reclaim for limit enforcement, provide per-memcg background reclaimers which can scale across CPUs with the allocation rate.
I am aware that several companies maintain out-of-tree patches for asynchronous reclaim, though some have not yet attempted to upstream them.
Would it be feasible to introduce a generic memcg asynchronous reclaim framework into the upstream kernel, where eBPF is used to orchestrate and control the reclaim logic? In this model, the kernel's role would be to enforce "guardrails" for these operations—for instance, restricting a BPF program to initiating only one asynchronous reclaim pass at a time—to ensure system safety and predictability.
Best,
Hui
Quoted 89 lines
Lock-Aware Throttling
The ability to avoid throttling an allocating task that is holding locks, to prevent priority inversion. In Meta's fleet, we have observed lock holders stuck in memcg reclaim, blocking all waiters regardless of their priority or criticality.
Thread-Level Throttling Control
Workloads should be able to indicate at the thread level which threads can be synchronously throttled and which cannot. For example, while experimenting with
sched_ext, we drastically improved the performance of AI training workloads by prioritizing threads interacting with the GPU. Similarly, applications can identify the threads or thread pools on their performance-critical paths and the memcg enforcement mechanism should not throttle them.Combined Memory and Swap Limits
Some users (Google actually) need the ability to enforce limits based on combined memory and swap usage, similar to cgroup v1's memsw limit, providing a ceiling on total memory commitment rather than treating memory and swap independently.
Dynamic Protection Limits
Rather than static protection limits, the kernel should support defining protection based on the actual working set of the workload, leveraging signals such as working set estimation, PSI, refault rates, or a combination thereof to automatically adapt to the workload's current memory needs.
Shared Memory Semantics
With more flexibility in limit enforcement, the kernel should be able to account for memory shared between workloads (cgroups) during enforcement. Today, enforcement only looks at each workload's memory usage independently. Sensible shared memory semantics would allow the enforcer to consider cross-cgroup sharing when making reclaim and throttling decisions.
Memory Tiering
With a flexible limit enforcement mechanism, the kernel can balance memory usage of different workloads across memory tiers based on their performance requirements. Tier accounting and hotness tracking are orthogonal, but the decisions of when and how to balance memory between tiers should be handled by the enforcer.
Collaborative Load Shedding
Many workloads communicate with an external entity for load balancing and rely on their own usage metrics like RSS or memory pressure to signal whether they can accept more or less work. This is guesswork. Instead of the workload guessing, the limit enforcer -- which is actually managing the workload's memory usage -- should be able to communicate available headroom or request the workload to shed load or reduce memory usage. This collaborative load shedding mechanism would allow workloads to make informed decisions rather than reacting to coarse signals.
Cross-Subsystem Collaboration
Finally, the limit enforcement mechanism should collaborate with the CPU scheduler and other subsystems that can release memory. For example, dirty memory is not reclaimable and the memory subsystem wakes up flushers to trigger writeback. However, flushers need CPU to run -- asking the CPU scheduler to prioritize them ensures the kernel does not lack reclaimable memory under stressful conditions. Similarly, some subsystems free memory through workqueues or RCU callbacks. While this may seem orthogonal to limit enforcement, we can definitely take advantage by having visibility into these situations.
Putting It All Together
-----------------------To illustrate the end goal, here is an example of the scenario I want to enable. Suppose there is an AI agent controlling the resources of a host. I should be able to provide the following policy and everything should work out of the box:
Policy: "keep system-level memory utilization below 95 percent; avoid priority inversions by not throttling allocators holding locks; trim each workload's usage to its working set without regressing its relevant performance metrics; collaborate with workloads on load shedding and memory trimming decisions; and under extreme memory pressure, collaborate with the OOM killer and the central job scheduler to kill and clean up a workload."
Initially I added this example for fun, but from [1] it seems like there is a real need to enable such capabilities.
Quoted 82 lines · Shakeel Butt
Over the last couple of weeks, I have been brainstorming on how I would go about redesigning memcg, taking inspiration from
sched_extand bpfoom, with a focus on existing challenges and issues. This proposal outlines the high-level direction. Followup emails and patch series will cover and brainstorm the mechanisms (of course BPF) to achieve these goals.Memory cgroups provide memory accounting and the ability to control memory usage of workloads through two categories of limits. Throttling limits (memory.max and memory.high) cap memory consumption. Protection limits (memory.min and memory.low) shield a workload's memory from reclaim under external memory pressure.
Challenges
----------- Workload owners rarely know their actual memory requirements, leading to overprovisioned limits, lower utilization, and higher infrastructure costs.
- Throttling limit enforcement is synchronous in the allocating task's context, which can stall latency-sensitive threads.
- The stalled thread may hold shared locks, causing priority inversion -- all waiters are blocked regardless of their priority.
- Enforcement is indiscriminate -- there is no way to distinguish a performance-critical or latency-critical allocator from a latency-tolerant one.
- Protection limits assume static working sets size, forcing owners to either overprovision or build complex userspace infrastructure to dynamically adjust them.
Feature Wishlist
----------------Here is the list of features and capabilities I want to enable in the redesigned memcg limit enforcement world.
Per-Memcg Background Reclaim
In the new memcg world, with the goal of (mostly) eliminating direct synchronous reclaim for limit enforcement, provide per-memcg background reclaimers which can scale across CPUs with the allocation rate.
Lock-Aware Throttling
The ability to avoid throttling an allocating task that is holding locks, to prevent priority inversion. In Meta's fleet, we have observed lock holders stuck in memcg reclaim, blocking all waiters regardless of their priority or criticality.
Thread-Level Throttling Control
Workloads should be able to indicate at the thread level which threads can be synchronously throttled and which cannot. For example, while experimenting with
sched_ext, we drastically improved the performance of AI training workloads by prioritizing threads interacting with the GPU. Similarly, applications can identify the threads or thread pools on their performance-critical paths and the memcg enforcement mechanism should not throttle them.Combined Memory and Swap Limits
Some users (Google actually) need the ability to enforce limits based on combined memory and swap usage, similar to cgroup v1's memsw limit, providing a ceiling on total memory commitment rather than treating memory and swap independently.
Dynamic Protection Limits
Rather than static protection limits, the kernel should support defining protection based on the actual working set of the workload, leveraging signals such as working set estimation, PSI, refault rates, or a combination thereof to automatically adapt to the workload's current memory needs.
Shared Memory Semantics
With more flexibility in limit enforcement, the kernel should be able to account for memory shared between workloads (cgroups) during enforcement. Today, enforcement only looks at each workload's memory usage independently. Sensible shared memory semantics would allow the enforcer to consider cross-cgroup sharing when making reclaim and throttling decisions.
Quoted 7 lines
Memory Tiering
With a flexible limit enforcement mechanism, the kernel can balance memory usage of different workloads across memory tiers based on their performance requirements. Tier accounting and hotness tracking are orthogonal, but the decisions of when and how to balance memory between tiers should be handled by the enforcer.
Hi Shakeel
This looks like a good idea. I was thinking along similar lines, but wasn’t sure about the best way to implement it.
For memcg with memory tiering, the idea is that we set memory.high and memory.max as the maximum limits. Within memory.high, a certain percentage (x%) could be backed by higher-tier memory, with the remaining portion coming from lower-tier memory.
In this model, an application would get up to memory.high * x / 100 from higher-tier memory, and the rest from lower-tier memory.
Once the higher-tier usage reaches its limit, we would start demoting pages. If the lower-tier usage also reaches its limit, we would then start swapping out pages from lower tier.
What is your opinion on how memory tiering should be handled in memcg?
-Donet
Quoted 43 lines
Collaborative Load Shedding
Many workloads communicate with an external entity for load balancing and rely on their own usage metrics like RSS or memory pressure to signal whether they can accept more or less work. This is guesswork. Instead of the workload guessing, the limit enforcer -- which is actually managing the workload's memory usage -- should be able to communicate available headroom or request the workload to shed load or reduce memory usage. This collaborative load shedding mechanism would allow workloads to make informed decisions rather than reacting to coarse signals.
Cross-Subsystem Collaboration
Finally, the limit enforcement mechanism should collaborate with the CPU scheduler and other subsystems that can release memory. For example, dirty memory is not reclaimable and the memory subsystem wakes up flushers to trigger writeback. However, flushers need CPU to run -- asking the CPU scheduler to prioritize them ensures the kernel does not lack reclaimable memory under stressful conditions. Similarly, some subsystems free memory through workqueues or RCU callbacks. While this may seem orthogonal to limit enforcement, we can definitely take advantage by having visibility into these situations.
Putting It All Together
-----------------------To illustrate the end goal, here is an example of the scenario I want to enable. Suppose there is an AI agent controlling the resources of a host. I should be able to provide the following policy and everything should work out of the box:
Policy: "keep system-level memory utilization below 95 percent; avoid priority inversions by not throttling allocators holding locks; trim each workload's usage to its working set without regressing its relevant performance metrics; collaborate with workloads on load shedding and memory trimming decisions; and under extreme memory pressure, collaborate with the OOM killer and the central job scheduler to kill and clean up a workload."
Initially I added this example for fun, but from [1] it seems like there is a real need to enable such capabilities.