Skip to contentSkip to conversation

MGLRU on Android: Real-World Problems and Challenges

LSF/MM/BPF TOPIC wangzicheng 7 participants 14 messages Feb 24 – May 12 Memory management

lore
Share & tools
lore
#0 lore raw

Hi,

I previously sent a similar email which unfortunately had encoding issues. I'm resending a cleaned-up version here so it's easier to read and discuss.

MGLRU has been available on Android for about four years, but many OEM vendors still choose not to enable it in production. HONOR is a major Android OEM shipping tens of millions of devices per year, and we run MGLRU on all our devices across multiple kernel versions (5.15~6.12) and RAM configurations(4G~24G), backed by large-scale beta and field data. From this deployment, we have identified four concrete issues (Q1-Q4) and current workarounds, and would like to work with the community to design upstream solutions.
Also we would like to discuss MGLRU's future direction on Android.

Below is a short summary of what we see.

Q1: anon/file imbalance and drop in available memory Android apps workload show a persistent anon/file generational imbalance under MGLRU:
anon pages tend to stay in the youngest 2 generations; file pages are spread across multiple generations and over-reclaimed. Tuning swappiness to 200 and ANON_ONLY does not fully fix this. On a 16G media workload we see:
MGLRU: MemAvailable ~ 6060 MB
legacy: MemAvailable ~ 6982 MB (differs by ~1G)
Today we mitigate this via explicit memcg aging in Android userspace [1], which is a vendor-only workaround.

Q2: Hard to control reclaim amount and stopping conditions (memcg) For memcg reclaim it is hard to stop near a target reclaim amount:
kswapd can continue reclaiming even after watermarks are met (e.g. to satisfy higher-order or memcg allocations); reclaim via try_to_free_mem_cgroup_pages() lacks clear abort semantics and can overshoot the intended reclaim amount. We currently use OEM hooks [2] to early-exit or bypass reclaim under some conditions

Q3: High reclaim cost and long uninterruptible sleep on lower-end devices
On lower-end devices, reclaim cost and latency are harder to control:
throttle_direct_reclaim can make tasks wait for kswapd instead of doing direct reclaim;
sometimes the target generations in many memcgs have very few reclaimable
pages, so the CPU spends time scanning with little progress. We observe tasks staying in uninterruptible sleep in try_to_free_pages() We haven't find any proper ways to fix it.

Q4: Lack of global hot/cold + priority view with per-app memcg Android uses a per-app memcg model and foreground/background levels for resource control. root reclaim lacks a cross-memcg hot/cold and priority view;
foreground app file pages may be reclaimed and reloaded frequently, causing visible stalls;
We currently use a hook [3] to skip reclaim for foreground apps.

Discussion

- Vendor-only workarounds -> generic mechanisms (Q1-Q4) Our current fixes (userspace memcg aging [1], OEM reclaim hooks [2,3]) are Android/vendor-only—what parts should be turned into generic MGLRU/kernel mechanisms vs. kept as Android policy? We need guidance from community.

- How much control should MGLRU expose to Android? (Q1-Q3) For Q1/Q2, Android has strong fg/bg and priority semantics that the kernel does not see. Should MGLRU provide more explicit control points (e.g. anon-vs-file / generation steering,
"target amount + abort condition" memcg reclaim) so Android can safely trade complexity and risk for better performance and bounded reclaim latency (Q3)?

- MGLRU evolution without memcg LRU: global hot/cold & scanning (Q4) If memcg LRU will be removed [4], how should we maintain a cross-memcg global hot/cold view and per-app priority on Android?
Given that much of the power benefit seems to come from page-table scanning while generations are complex, is it reasonable to decouple page-scanning functionality from MGLRU and make it a seperate kernel configuration.

We are happy to share more detailed data and experiments and to help with PoCs and large-scale validation if there is interest in pursuing these directions.

Reference
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://android-review.googlesource.com/c/kernel/common/+/3866554
[3] https://android-review.googlesource.com/c/kernel/common/+/3870920
[4] https://lwn.net/Articles/1051882/

--
Best regards, and wishing you a prosperous Year of the Horse, Zicheng Wang

#1 lore raw
Quoted 82 lines · wangzicheng

Hi,

I previously sent a similar email which unfortunately had encoding issues. I'm resending a cleaned-up version here so it's easier to read and discuss.

MGLRU has been available on Android for about four years, but many OEM vendors still choose not to enable it in production. HONOR is a major Android OEM shipping tens of millions of devices per year, and we run MGLRU on all our devices across multiple kernel versions (5.15~6.12) and RAM configurations(4G~24G), backed by large-scale beta and field data. From this deployment, we have identified four concrete issues (Q1-Q4) and current workarounds, and would like to work with the community to design upstream solutions.
Also we would like to discuss MGLRU's future direction on Android.

Below is a short summary of what we see.

Q1: anon/file imbalance and drop in available memory Android apps workload show a persistent anon/file generational imbalance under MGLRU:
anon pages tend to stay in the youngest 2 generations; file pages are spread across multiple generations and over-reclaimed. Tuning swappiness to 200 and ANON_ONLY does not fully fix this. On a 16G media workload we see:
MGLRU: MemAvailable ~ 6060 MB
legacy: MemAvailable ~ 6982 MB (differs by ~1G)
Today we mitigate this via explicit memcg aging in Android userspace [1], which is a vendor-only workaround.

Q2: Hard to control reclaim amount and stopping conditions (memcg) For memcg reclaim it is hard to stop near a target reclaim amount:
kswapd can continue reclaiming even after watermarks are met (e.g. to satisfy higher-order or memcg allocations); reclaim via try_to_free_mem_cgroup_pages() lacks clear abort semantics and can overshoot the intended reclaim amount. We currently use OEM hooks [2] to early-exit or bypass reclaim under some conditions

Q3: High reclaim cost and long uninterruptible sleep on lower-end devices
On lower-end devices, reclaim cost and latency are harder to control:
throttle_direct_reclaim can make tasks wait for kswapd instead of doing direct reclaim;
sometimes the target generations in many memcgs have very few reclaimable
pages, so the CPU spends time scanning with little progress. We observe tasks staying in uninterruptible sleep in try_to_free_pages() We haven't find any proper ways to fix it.

Q4: Lack of global hot/cold + priority view with per-app memcg Android uses a per-app memcg model and foreground/background levels for resource control. root reclaim lacks a cross-memcg hot/cold and priority view;
foreground app file pages may be reclaimed and reloaded frequently, causing visible stalls;
We currently use a hook [3] to skip reclaim for foreground apps.

Discussion

- Vendor-only workarounds -> generic mechanisms (Q1-Q4) Our current fixes (userspace memcg aging [1], OEM reclaim hooks [2,3]) are Android/vendor-only—what parts should be turned into generic MGLRU/kernel mechanisms vs. kept as Android policy? We need guidance from community.

- How much control should MGLRU expose to Android? (Q1-Q3) For Q1/Q2, Android has strong fg/bg and priority semantics that the kernel does not see. Should MGLRU provide more explicit control points (e.g. anon-vs-file / generation steering,
"target amount + abort condition" memcg reclaim) so Android can safely trade complexity and risk for better performance and bounded reclaim latency (Q3)?

- MGLRU evolution without memcg LRU: global hot/cold & scanning (Q4) If memcg LRU will be removed [4], how should we maintain a cross-memcg global hot/cold view and per-app priority on Android?
Given that much of the power benefit seems to come from page-table scanning while generations are complex, is it reasonable to decouple page-scanning functionality from MGLRU and make it a seperate kernel configuration.

Quoted 3 lines

We are happy to share more detailed data and experiments and to help with PoCs and large-scale validation if there is interest in pursuing these directions.

For obvious reasons I'm interested in this discussion. We also notice some shortcomings of MGLRU and would like to collaborate on resolving them.
Thanks,
Suren.

Quoted 10 lines

Reference
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://android-review.googlesource.com/c/kernel/common/+/3866554
[3] https://android-review.googlesource.com/c/kernel/common/+/3870920
[4] https://lwn.net/Articles/1051882/

--
Best regards, and wishing you a prosperous Year of the Horse, Zicheng Wang

#2 lore raw
Quoted 11 lines
Quoted 4 lines

We are happy to share more detailed data and experiments and to help with PoCs and large-scale validation if there is interest in pursuing these directions.

For obvious reasons I'm interested in this discussion. We also notice some shortcomings of MGLRU and would like to collaborate on resolving them.
Thanks,
Suren.

Hi Suren,

thanks, we’d be very happy to work together on this.

Quoted 13 lines
Quoted 3 lines

Reference
[1] https://lore.kernel.org/linux-mm/20251128025315.3520689-1-

[email protected]/

Quoted 1 line

[2] https://android-

review.googlesource.com/c/kernel/common/+/3866554

Quoted 1 line

[3] https://android-

review.googlesource.com/c/kernel/common/+/3870920

Quoted 5 lines

[4] https://lwn.net/Articles/1051882/

--
Best regards, and wishing you a prosperous Year of the Horse, Zicheng Wang

#3 lore raw
Quoted 5 lines · wangzicheng
Quoted 5 lines
Quoted 5 lines

We are happy to share more detailed data and experiments and to help with PoCs and large-scale validation if there is interest in pursuing these directions.

Hi Zicheng,

I would be very interested in joining this discussion at LSF/MM as well.

I actually just replied to Kairui's MGLRU thread noting some similar observations we've made while evaluating MGLRU [1]. I think your findings in Q3 regarding high reclaim costs and tasks waiting in uninterruptible sleep corroborate the increased direct reclaim tail latencies we have been investigating on our end.

It will be valuable to discuss your large-scale field data and see how the workarounds you've implemented might inform the upstream discussions alongside Kairui's proposals.

[1] https://lore.kernel.org/all/CAC_TJvfi_5n2crGHbRVO0R8jdYcSCmgzUJPzhtSvWeCN6fCbQg@mail.gmail.com/#t

Thanks,
Kalesh

Quoted 24 lines
Quoted 7 lines

For obvious reasons I'm interested in this discussion. We also notice some shortcomings of MGLRU and would like to collaborate on resolving them.
Thanks,
Suren.

Hi Suren,

thanks, we’d be very happy to work together on this.

Quoted 13 lines
Quoted 3 lines

Reference
[1] https://lore.kernel.org/linux-mm/20251128025315.3520689-1-

[email protected]/

Quoted 1 line

[2] https://android-

review.googlesource.com/c/kernel/common/+/3866554

Quoted 1 line

[3] https://android-

review.googlesource.com/c/kernel/common/+/3870920

Quoted 5 lines

[4] https://lwn.net/Articles/1051882/

--
Best regards, and wishing you a prosperous Year of the Horse, Zicheng Wang

#4 lore raw
Quoted 19 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

We are happy to share more detailed data and experiments and to help with PoCs and large-scale validation if there is interest in pursuing these directions.

Hi Zicheng,

I would be very interested in joining this discussion at LSF/MM as well.

I actually just replied to Kairui's MGLRU thread noting some similar observations we've made while evaluating MGLRU [1]. I think your findings in Q3 regarding high reclaim costs and tasks waiting in uninterruptible sleep corroborate the increased direct reclaim tail latencies we have been investigating on our end.

It will be valuable to discuss your large-scale field data and see how the workarounds you've implemented might inform the upstream discussions alongside Kairui's proposals.

Quoted 7 lines

[1]
https://lore.kernel.org/all/CAC_TJvfi_5n2crGHbRVO0R8jdYcSCmgzUJPzhtSv
[email protected]/#t

Thanks,
Kalesh

Hi Kalesh,

Thanks for following up and for sharing your observations.
For Q3, we have only tried some approaches so far and don’t have a solid solution yet. We’d be happy to share our field data and some Perfetto traces. We believe it would be very helpful to discuss with you and Kairui at LSF/MM/BPF.

Best,
Zicheng

Quoted 24 lines
Quoted 24 lines
Quoted 7 lines

For obvious reasons I'm interested in this discussion. We also notice some shortcomings of MGLRU and would like to collaborate on resolving them.
Thanks,
Suren.

Hi Suren,

thanks, we’d be very happy to work together on this.

Quoted 13 lines
Quoted 3 lines

Reference
[1] https://lore.kernel.org/linux-mm/20251128025315.3520689-1-

[email protected]/

Quoted 1 line

[2] https://android-

review.googlesource.com/c/kernel/common/+/3866554

Quoted 1 line

[3] https://android-

review.googlesource.com/c/kernel/common/+/3870920

Quoted 5 lines

[4] https://lwn.net/Articles/1051882/

--
Best regards, and wishing you a prosperous Year of the Horse, Zicheng Wang

#5 lore raw
In reply to wangzicheng
Quoted 18 lines · wangzicheng

Hi,

I previously sent a similar email which unfortunately had encoding issues. I'm resending a cleaned-up version here so it's easier to read and discuss.

MGLRU has been available on Android for about four years, but many OEM vendors still choose not to enable it in production. HONOR is a major Android OEM shipping tens of millions of devices per year, and we run MGLRU on all our devices across multiple kernel versions (5.15~6.12) and RAM configurations(4G~24G), backed by large-scale beta and field data. From this deployment, we have identified four concrete issues (Q1-Q4) and current workarounds, and would like to work with the community to design upstream solutions.
Also we would like to discuss MGLRU's future direction on Android.

Below is a short summary of what we see.

Quoted 11 lines

Q1: anon/file imbalance and drop in available memory Android apps workload show a persistent anon/file generational imbalance under MGLRU:
anon pages tend to stay in the youngest 2 generations; file pages are spread across multiple generations and over-reclaimed. Tuning swappiness to 200 and ANON_ONLY does not fully fix this. On a 16G media workload we see:
MGLRU: MemAvailable ~ 6060 MB
legacy: MemAvailable ~ 6982 MB (differs by ~1G)
Today we mitigate this via explicit memcg aging in Android userspace [1], which is a vendor-only workaround.

One fundamental design of MGLRU is that file generations and anon generations catch up with each other when the generation gap reaches two or more. As a result, even if swappiness is set very high, its effect on aggressively reclaiming anonymous pages is much smaller than with the traditional LRU.

One workaround is to force old file folios to be promoted to relatively younger generations, but this could also cause problems by clustering file folios in the newer generations.

I wonder if anon and file generations can progress separately to some extent.

Quoted 5 lines

Q2: Hard to control reclaim amount and stopping conditions (memcg) For memcg reclaim it is hard to stop near a target reclaim amount:
kswapd can continue reclaiming even after watermarks are met (e.g. to satisfy higher-order or memcg allocations);

High-order is an interesting topic. Sometimes, vmscan over-reclaims to satisfy high-order allocations, reclaiming many zero-order pages even when free pages of the required order are sufficient[1]. You’ve revealed another aspect: high-order pages may already exist, but reclamation doesn’t push them out in time.

Quoted 5 lines

reclaim via try_to_free_mem_cgroup_pages() lacks clear abort semantics and can overshoot the intended reclaim amount. We currently use OEM hooks [2] to early-exit or bypass reclaim under some conditions

Quoted 10 lines

Q3: High reclaim cost and long uninterruptible sleep on lower-end devices
On lower-end devices, reclaim cost and latency are harder to control:
throttle_direct_reclaim can make tasks wait for kswapd instead of doing direct reclaim;
sometimes the target generations in many memcgs have very few reclaimable
pages, so the CPU spends time scanning with little progress. We observe tasks staying in uninterruptible sleep in try_to_free_pages() We haven't find any proper ways to fix it.

Have you identified the exact line of code where direct reclaim enters uninterruptible sleep? Is it waiting on a lock or something else?

Quoted 8 lines

Q4: Lack of global hot/cold + priority view with per-app memcg Android uses a per-app memcg model and foreground/background levels for resource control. root reclaim lacks a cross-memcg hot/cold and priority view;
foreground app file pages may be reclaimed and reloaded frequently, causing visible stalls;
We currently use a hook [3] to skip reclaim for foreground apps.

Interesting. This somehow reflects that the LRU lacks the user’s context, especially on Android systems—for example, which apps are in the foreground, which are in the background, and how long an app has been in the background.

But this is not specific to MGLRU; it can also be an issue for the active/inactive LRU?

Additionally, I’d like to add Q5 based on my observations:
Q5:
MGLRU places readahead folios in the newest generation. For example, if a page fault occurs at address 5, readahead fetches addresses 1–16, and all 16 folios are put in the youngest generation, even though many may not be needed. This can seriously impact reclamation performance, as these cold readahead folios occupy active slots.

See the code below and the checks performed by lru_gen_in_fault().

void folio_add_lru(struct folio *folio)

{
        ...
        /* see the comment in lru_gen_folio_seq() */
        if (lru_gen_enabled() && !folio_test_unevictable(folio) &&
            lru_gen_in_fault() && !(current->flags & PF_MEMALLOC))
                folio_set_active(folio);

        folio_batch_add_and_move(folio, lru_add);
}

EXPORT_SYMBOL(folio_add_lru);

I could have submitted a patchset to address this by initially marking only address 5 as active, and activating the other addresses later when they are actually mapped or accessed.

Quoted 25 lines

Discussion

- Vendor-only workarounds -> generic mechanisms (Q1-Q4) Our current fixes (userspace memcg aging [1], OEM reclaim hooks [2,3]) are Android/vendor-only—what parts should be turned into generic MGLRU/kernel mechanisms vs. kept as Android policy? We need guidance from community.

- How much control should MGLRU expose to Android? (Q1-Q3) For Q1/Q2, Android has strong fg/bg and priority semantics that the kernel does not see. Should MGLRU provide more explicit control points (e.g. anon-vs-file / generation steering,
"target amount + abort condition" memcg reclaim) so Android can safely trade complexity and risk for better performance and bounded reclaim latency (Q3)?

- MGLRU evolution without memcg LRU: global hot/cold & scanning (Q4) If memcg LRU will be removed [4], how should we maintain a cross-memcg global hot/cold view and per-app priority on Android?
Given that much of the power benefit seems to come from page-table scanning while generations are complex, is it reasonable to decouple page-scanning functionality from MGLRU and make it a seperate kernel configuration.

Quoted 3 lines

We are happy to share more detailed data and experiments and to help with PoCs and large-scale validation if there is interest in pursuing these directions.

This is very welcome.

[1] https://lore.kernel.org/linux-mm/[email protected]/

Best Regards
Barry

#6 lore raw
Quoted 39 lines
Quoted 18 lines · wangzicheng

Hi,

I previously sent a similar email which unfortunately had encoding issues. I'm resending a cleaned-up version here so it's easier to read and discuss.

MGLRU has been available on Android for about four years, but many OEM vendors still choose not to enable it in production. HONOR is a major Android OEM shipping tens of millions of devices per year, and we run MGLRU on all our devices across multiple kernel versions (5.15~6.12) and RAM configurations(4G~24G), backed by large-scale beta and field data. From this deployment, we have identified four concrete issues (Q1-Q4) and current workarounds, and would like to work with the community to design upstream solutions.
Also we would like to discuss MGLRU's future direction on Android.

Below is a short summary of what we see.

Quoted 11 lines

Q1: anon/file imbalance and drop in available memory Android apps workload show a persistent anon/file generational imbalance under MGLRU:
anon pages tend to stay in the youngest 2 generations; file pages are spread across multiple generations and over-reclaimed. Tuning swappiness to 200 and ANON_ONLY does not fully fix this. On a 16G media workload we see:
MGLRU: MemAvailable ~ 6060 MB
legacy: MemAvailable ~ 6982 MB (differs by ~1G)
Today we mitigate this via explicit memcg aging in Android userspace [1], which is a vendor-only workaround.

One fundamental design of MGLRU is that file generations and anon generations catch up with each other when the generation gap reaches two or more. As a result, even if swappiness is set very high, its effect on aggressively reclaiming anonymous pages is much smaller than with the traditional LRU.

Quoted 7 lines

One workaround is to force old file folios to be promoted to relatively younger generations, but this could also cause problems by clustering file folios in the newer generations.

I wonder if anon and file generations can progress separately to some extent.

Hi Barry,
Thanks for the detailed feedback and observations. We also feel this direction might be more reasonable.

Quoted 6 lines
Quoted 5 lines

Q2: Hard to control reclaim amount and stopping conditions (memcg) For memcg reclaim it is hard to stop near a target reclaim amount:
kswapd can continue reclaiming even after watermarks are met (e.g. to satisfy higher-order or memcg allocations);

Quoted 6 lines

High-order is an interesting topic. Sometimes, vmscan over-reclaims to satisfy high-order allocations, reclaiming many zero-order pages even when free pages of the required order are sufficient[1]. You’ve revealed another aspect: high-order pages may already exist, but reclamation doesn’t push them out in time.

Yes.

Quoted 16 lines
Quoted 5 lines

reclaim via try_to_free_mem_cgroup_pages() lacks clear abort semantics and can overshoot the intended reclaim amount. We currently use OEM hooks [2] to early-exit or bypass reclaim under some conditions

Quoted 10 lines

Q3: High reclaim cost and long uninterruptible sleep on lower-end devices
On lower-end devices, reclaim cost and latency are harder to control:
throttle_direct_reclaim can make tasks wait for kswapd instead of doing direct reclaim;
sometimes the target generations in many memcgs have very few reclaimable
pages, so the CPU spends time scanning with little progress. We observe tasks staying in uninterruptible sleep in try_to_free_pages() We haven't find any proper ways to fix it.

Quoted 3 lines

Have you identified the exact line of code where direct reclaim enters uninterruptible sleep? Is it waiting on a lock or something else?

Yes, we’ve identified the exact code locations where this happens:

in slow path

static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
nodemask_t *nodemask)
{
...

	if (!(gfp_mask & __GFP_FS))
		wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
			allow_direct_reclaim(pgdat), HZ);
	else
		/* Throttle until kswapd wakes the process */
		wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
			allow_direct_reclaim(pgdat));

...
}

in kswapd

static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order,
int highest_zoneidx)
{
...

	if (waitqueue_active(&pgdat->pfmemalloc_wait))
		wake_up_all(&pgdat->pfmemalloc_wait);

...
}

Quoted 9 lines
Quoted 8 lines

Q4: Lack of global hot/cold + priority view with per-app memcg Android uses a per-app memcg model and foreground/background levels for resource control. root reclaim lacks a cross-memcg hot/cold and priority view;
foreground app file pages may be reclaimed and reloaded frequently, causing visible stalls;
We currently use a hook [3] to skip reclaim for foreground apps.

Quoted 8 lines

Interesting. This somehow reflects that the LRU lacks the user’s context, especially on Android systems—for example, which apps are in the foreground, which are in the background, and how long an app has been in the background.

But this is not specific to MGLRU; it can also be an issue for the active/inactive LRU?

That's right, this affects both MGLRU and the traditional LRU.
We believe this comes from a semantic gap between the kernel and Android (e.g. fg/bg, per-app priorities), and this is one of the main topics we’d like to discuss. Additionally, even with MGLRU’s memcg-LRU, this issue is still not fully resolved in our workloads.

Quoted 21 lines

Additionally, I’d like to add Q5 based on my observations:
Q5:
MGLRU places readahead folios in the newest generation. For example, if a page fault occurs at address 5, readahead fetches addresses 1–16, and all 16 folios are put in the youngest generation, even though many may not be needed. This can seriously impact reclamation performance, as these cold readahead folios occupy active slots.

See the code below and the checks performed by lru_gen_in_fault().

void folio_add_lru(struct folio *folio)

{
        ...
        /* see the comment in lru_gen_folio_seq() */
        if (lru_gen_enabled() && !folio_test_unevictable(folio) &&
            lru_gen_in_fault() && !(current->flags & PF_MEMALLOC))
                folio_set_active(folio);

        folio_batch_add_and_move(folio, lru_add);
}
Quoted 6 lines

EXPORT_SYMBOL(folio_add_lru);

I could have submitted a patchset to address this by initially marking only address 5 as active, and activating the other addresses later when they are actually mapped or accessed.

This sounds very reasonable to us, and we look forward to discussing and evaluating this direction together.

Quoted 29 lines
Quoted 25 lines

Discussion

- Vendor-only workarounds -> generic mechanisms (Q1-Q4) Our current fixes (userspace memcg aging [1], OEM reclaim hooks [2,3]) are Android/vendor-only—what parts should be turned into generic MGLRU/kernel mechanisms vs. kept as Android policy? We need guidance from community.

- How much control should MGLRU expose to Android? (Q1-Q3) For Q1/Q2, Android has strong fg/bg and priority semantics that the kernel does not see. Should MGLRU provide more explicit control points (e.g. anon-vs-file / generation steering,
"target amount + abort condition" memcg reclaim) so Android can safely trade complexity and risk for better performance and bounded reclaim latency (Q3)?

- MGLRU evolution without memcg LRU: global hot/cold & scanning (Q4) If memcg LRU will be removed [4], how should we maintain a cross-memcg global hot/cold view and per-app priority on Android?
Given that much of the power benefit seems to come from page-table scanning while generations are complex, is it reasonable to decouple page-scanning functionality from MGLRU and make it a seperate kernel configuration.

Quoted 3 lines

We are happy to share more detailed data and experiments and to help with PoCs and large-scale validation if there is interest in pursuing these directions.

Quoted 7 lines

This is very welcome.

[1] https://lore.kernel.org/linux-mm/20251013101636.69220-1-
[email protected]/

Best Regards
Barry

Best regards,
Zicheng

#7 lore raw

On Wed, Feb 25, 2026 at 6:43 PM wangzicheng <[email protected]> wrote:
[...]

Quoted 39 lines
Quoted 16 lines
Quoted 5 lines

reclaim via try_to_free_mem_cgroup_pages() lacks clear abort semantics and can overshoot the intended reclaim amount. We currently use OEM hooks [2] to early-exit or bypass reclaim under some conditions

Quoted 10 lines

Q3: High reclaim cost and long uninterruptible sleep on lower-end devices
On lower-end devices, reclaim cost and latency are harder to control:
throttle_direct_reclaim can make tasks wait for kswapd instead of doing direct reclaim;
sometimes the target generations in many memcgs have very few reclaimable
pages, so the CPU spends time scanning with little progress. We observe tasks staying in uninterruptible sleep in try_to_free_pages() We haven't find any proper ways to fix it.

Quoted 3 lines

Have you identified the exact line of code where direct reclaim enters uninterruptible sleep? Is it waiting on a lock or something else?

Yes, we’ve identified the exact code locations where this happens:

in slow path

static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
nodemask_t *nodemask)
{
...

        if (!(gfp_mask & __GFP_FS))
                wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
                        allow_direct_reclaim(pgdat), HZ);
        else
                /* Throttle until kswapd wakes the process */
                wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
                        allow_direct_reclaim(pgdat));

...
}

in kswapd

Quoted 8 lines

static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order,
int highest_zoneidx)
{
...

        if (waitqueue_active(&pgdat->pfmemalloc_wait))
                wake_up_all(&pgdat->pfmemalloc_wait);

...
}

Thanks. I understand it could be problematic if throttling occurs, especially on threads related to user experience.

Quoted 15 lines
Quoted 15 lines
Quoted 9 lines

Q4: Lack of global hot/cold + priority view with per-app memcg Android uses a per-app memcg model and foreground/background levels for resource control. root reclaim lacks a cross-memcg hot/cold and priority view;
foreground app file pages may be reclaimed and reloaded frequently, causing visible stalls;
We currently use a hook [3] to skip reclaim for foreground apps.

Interesting. This somehow reflects that the LRU lacks the user’s context, especially on Android systems—for example, which apps are in the foreground, which are in the background, and how long an app has been in the background.

Quoted 8 lines
Quoted 3 lines

But this is not specific to MGLRU; it can also be an issue for the active/inactive LRU?

That's right, this affects both MGLRU and the traditional LRU.
We believe this comes from a semantic gap between the kernel and Android (e.g. fg/bg, per-app priorities), and this is one of the main topics we’d like to discuss. Additionally, even with MGLRU’s memcg-LRU, this issue is still not fully resolved in our workloads.

We might be able to leverage some existing infrastructure. MGLRU maintains an LRU of LRUs, and within this structure, it may be possible to adjust positions based on whether a cgroup is in the foreground or background. In other words, the LRU of LRUs could receive hints from userspace to influence a cgroup’s position.

Quoted 24 lines
Quoted 24 lines

Additionally, I’d like to add Q5 based on my observations:
Q5:
MGLRU places readahead folios in the newest generation. For example, if a page fault occurs at address 5, readahead fetches addresses 1–16, and all 16 folios are put in the youngest generation, even though many may not be needed. This can seriously impact reclamation performance, as these cold readahead folios occupy active slots.

See the code below and the checks performed by lru_gen_in_fault().

void folio_add_lru(struct folio *folio)

{
        ...
        /* see the comment in lru_gen_folio_seq() */
        if (lru_gen_enabled() && !folio_test_unevictable(folio) &&
            lru_gen_in_fault() && !(current->flags & PF_MEMALLOC))
                folio_set_active(folio);

        folio_batch_add_and_move(folio, lru_add);
}

EXPORT_SYMBOL(folio_add_lru);

Quoted 6 lines
Quoted 4 lines

I could have submitted a patchset to address this by initially marking only address 5 as active, and activating the other addresses later when they are actually mapped or accessed.

This sounds very reasonable to us, and we look forward to discussing and evaluating this direction together.

I sent an RFC today for Q5. I hope you can review, comment, and test it together:

https://lore.kernel.org/linux-mm/[email protected]/

Thanks
Barry

#8 lore raw
Quoted 49 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

reclaim via try_to_free_mem_cgroup_pages() lacks clear abort semantics and can overshoot the intended reclaim amount. We currently use OEM hooks [2] to early-exit or bypass reclaim under some conditions

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

Q3: High reclaim cost and long uninterruptible sleep on lower-end devices
On lower-end devices, reclaim cost and latency are harder to control:
throttle_direct_reclaim can make tasks wait for kswapd instead of doing direct reclaim;
sometimes the target generations in many memcgs have very few reclaimable
pages, so the CPU spends time scanning with little progress. We observe tasks staying in uninterruptible sleep in

try_to_free_pages()

Quoted 25 lines
Quoted 5 lines
Quoted 1 line

We haven't find any proper ways to fix it.

Have you identified the exact line of code where direct reclaim enters uninterruptible sleep? Is it waiting on a lock or something else?

Yes, we’ve identified the exact code locations where this happens:

in slow path

static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
nodemask_t *nodemask)
{
...

        if (!(gfp_mask & __GFP_FS))
                wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
                        allow_direct_reclaim(pgdat), HZ);
        else
                /* Throttle until kswapd wakes the process */
                wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
                        allow_direct_reclaim(pgdat));

...
}

in kswapd

Quoted 8 lines

static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order,
int highest_zoneidx)
{
...

        if (waitqueue_active(&pgdat->pfmemalloc_wait))
                wake_up_all(&pgdat->pfmemalloc_wait);

...
}

Quoted 3 lines

Thanks. I understand it could be problematic if throttling occurs, especially on threads related to user experience.

Thank you for the detailed following up.

For Q3, the throttling is dangerous for UX‑critical threads. Kalesh also shared similar observations about long direct reclaim tail latencies.

Quoted 25 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Q4: Lack of global hot/cold + priority view with per-app memcg Android uses a per-app memcg model and foreground/background

levels

Quoted 11 lines
Quoted 11 lines
Quoted 5 lines

for resource control. root reclaim lacks a cross-memcg hot/cold and priority view;
foreground app file pages may be reclaimed and reloaded frequently, causing visible stalls;
We currently use a hook [3] to skip reclaim for foreground apps.

Interesting. This somehow reflects that the LRU lacks the user’s context, especially on Android systems—for example, which apps are in the foreground, which are in the background, and how long an app has been in the background.

Quoted 5 lines
Quoted 3 lines

But this is not specific to MGLRU; it can also be an issue for the active/inactive LRU?

That's right, this affects both MGLRU and the traditional LRU. We believe this comes from a semantic gap between the kernel and

Android

Quoted 1 line

(e.g. fg/bg, per-app priorities), and this is one of the main topics we’d like

to discuss.

Quoted 1 line

Additionally, even with MGLRU’s memcg-LRU, this issue is still not fully

Quoted 9 lines

resolved

Quoted 1 line

in our workloads.

We might be able to leverage some existing infrastructure. MGLRU maintains an LRU of LRUs, and within this structure, it may be possible to adjust positions based on whether a cgroup is in the foreground or background. In other words, the LRU of LRUs could receive hints from userspace to influence a cgroup’s position.

Regarding the LRU‑of‑LRUs idea, that does sound like a promising direction (compare to vendor hook).
but it seems hardly support some more complex policies, e.g.,
- reclaim from bg apps first, but capping the reclaim amount and protect the bg 'super apps'
- preserving memcg MGLRU generation info after app frozen and not running. Looking forward to the discussion.

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines

Additionally, I’d like to add Q5 based on my observations:
Q5:
MGLRU places readahead folios in the newest generation. For example, if a page fault occurs at address 5, readahead fetches addresses 1–16, and all 16 folios are put in the youngest generation, even though many may not be needed. This can seriously impact reclamation performance, as these cold readahead folios occupy active slots.

See the code below and the checks performed by lru_gen_in_fault().

void folio_add_lru(struct folio *folio)

{
        ...
        /* see the comment in lru_gen_folio_seq() */
        if (lru_gen_enabled() && !folio_test_unevictable(folio) &&
            lru_gen_in_fault() && !(current->flags & PF_MEMALLOC))
                folio_set_active(folio);

        folio_batch_add_and_move(folio, lru_add);
}

EXPORT_SYMBOL(folio_add_lru);

I could have submitted a patchset to address this by initially marking only address 5 as active, and activating the other addresses later when they are actually mapped or accessed.

Quoted 6 lines
Quoted 2 lines

This sounds very reasonable to us, and we look forward to discussing and evaluating this direction together.

I sent an RFC today for Q5. I hope you can review, comment, and test it together:

For Q5, we are happy to see your RFC. We’ve already comment to the thread and will run it on our workloads to provide feedback.

Thanks,
Zicheng

Quoted 5 lines

https://lore.kernel.org/linux-mm/20260225223712.3685-1-
[email protected]/

Thanks
Barry

#9 lore raw
In reply to Barry Song

Hi Barry and Zicheng,

Quoted 23 lines · Barry Song
Quoted 14 lines · wangzicheng

*snip*
Below is a short summary of what we see.

Q1: anon/file imbalance and drop in available memory Android apps workload show a persistent anon/file generational imbalance under MGLRU:
anon pages tend to stay in the youngest 2 generations; file pages are spread across multiple generations and over-reclaimed. Tuning swappiness to 200 and ANON_ONLY does not fully fix this. On a 16G media workload we see:
MGLRU: MemAvailable ~ 6060 MB
legacy: MemAvailable ~ 6982 MB (differs by ~1G)
Today we mitigate this via explicit memcg aging in Android userspace [1], which is a vendor-only workaround.

One fundamental design of MGLRU is that file generations and anon generations catch up with each other when the generation gap reaches two or more. As a result, even if swappiness is set very high, its effect on aggressively reclaiming anonymous pages is much smaller than with the traditional LRU.

Quoted 7 lines

One workaround is to force old file folios to be promoted to relatively younger generations, but this could also cause problems by clustering file folios in the newer generations.

I wonder if anon and file generations can progress separately to some extent.

To provide some context, the file and anon generations are coupled primarily to simplify the MGLRU logic (Original Yu Zhao's intent). Separating the two should allow for more flexible policies for sure.

Quoted 9 lines
Quoted 8 lines

Q4: Lack of global hot/cold + priority view with per-app memcg Android uses a per-app memcg model and foreground/background levels for resource control. root reclaim lacks a cross-memcg hot/cold and priority view;
foreground app file pages may be reclaimed and reloaded frequently, causing visible stalls;
We currently use a hook [3] to skip reclaim for foreground apps.

Quoted 7 lines

Interesting. This somehow reflects that the LRU lacks the user’s context, especially on Android systems—for example, which apps are in the foreground, which are in the background, and how long an app has been in the background.

But this is not specific to MGLRU; it can also be an issue for the active/inactive LRU?

Pardon my lack of familiarity with the use case, in what ways are existing memcg protection features insufficient?

Quoted 22 lines

Additionally, I’d like to add Q5 based on my observations:
Q5:
MGLRU places readahead folios in the newest generation. For example, if a page fault occurs at address 5, readahead fetches addresses 1–16, and all 16 folios are put in the youngest generation, even though many may not be needed. This can seriously impact reclamation performance, as these cold readahead folios occupy active slots.

See the code below and the checks performed by lru_gen_in_fault().

void folio_add_lru(struct folio *folio)

{
        ...
        /* see the comment in lru_gen_folio_seq() */
        if (lru_gen_enabled() && !folio_test_unevictable(folio) &&
            lru_gen_in_fault() && !(current->flags & PF_MEMALLOC))
                folio_set_active(folio);

        folio_batch_add_and_move(folio, lru_add);
}
Quoted 5 lines

EXPORT_SYMBOL(folio_add_lru);

I could have submitted a patchset to address this by initially marking only address 5 as active, and activating the other addresses later when they are actually mapped or accessed.

I need to read it more closely but that's a good idea.

Thanks,
Yuanchu

#10 lore raw
Quoted 53 lines · Yuanchu Xie

Hi Barry and Zicheng,

Quoted 23 lines · Barry Song
Quoted 14 lines · wangzicheng

*snip*
Below is a short summary of what we see.

Q1: anon/file imbalance and drop in available memory Android apps workload show a persistent anon/file generational imbalance under MGLRU:
anon pages tend to stay in the youngest 2 generations; file pages are spread across multiple generations and over-reclaimed. Tuning swappiness to 200 and ANON_ONLY does not fully fix this. On a 16G media workload we see:
MGLRU: MemAvailable ~ 6060 MB
legacy: MemAvailable ~ 6982 MB (differs by ~1G)
Today we mitigate this via explicit memcg aging in Android userspace [1], which is a vendor-only workaround.

One fundamental design of MGLRU is that file generations and anon generations catch up with each other when the generation gap reaches two or more. As a result, even if swappiness is set very high, its effect on aggressively reclaiming anonymous pages is much smaller than with the traditional LRU.

Quoted 7 lines

One workaround is to force old file folios to be promoted to relatively younger generations, but this could also cause problems by clustering file folios in the newer generations.

I wonder if anon and file generations can progress separately to some extent.

To provide some context, the file and anon generations are coupled primarily to simplify the MGLRU logic (Original Yu Zhao's intent). Separating the two should allow for more flexible policies for sure.

Quoted 14 lines
Quoted 8 lines

Q4: Lack of global hot/cold + priority view with per-app memcg Android uses a per-app memcg model and foreground/background levels for resource control. root reclaim lacks a cross-memcg hot/cold and priority view;
foreground app file pages may be reclaimed and reloaded frequently, causing visible stalls;
We currently use a hook [3] to skip reclaim for foreground apps.

Interesting. This somehow reflects that the LRU lacks the user’s context, especially on Android systems—for example, which apps are in the foreground, which are in the background, and how long an app has been in the background.

Quoted 5 lines
Quoted 2 lines

But this is not specific to MGLRU; it can also be an issue for the active/inactive LRU?

Pardon my lack of familiarity with the use case, in what ways are existing memcg protection features insufficient?

I think he means that whichever memcg is next up on the memcg LRU is the one that gets reclaimed from first, regardless of whether that memcg is for a foreground app. Because there is currently no strong relationship between memcg LRU ordering and Android app state. Android updates oom_score_adj on app state transitions, so it'd be possible for the kernel to use that information from userspace to influence memcg LRU ordering, but I'm not really a great fan of that idea.

Right, reclaim from foreground apps is not exclusively a MGLRU problem. There is an effort in both MGLRU and active/inactive LRU to provide some sort of fairness (now both are eventually-fair) for global reclaim, so that no memcg is the subject of disproportionate reclaim.

I'm planning to investigate applying memory.low to foreground / top-app for reducing the amount of reclaim from foreground apps. However at the same time, we are also working on applying memory limits to all apps (memory.high) so that no single app (including a foreground app) can use all the system memory as a matter of Android policy.

Thanks,
T.J.

Quoted 31 lines
Quoted 20 lines

Additionally, I’d like to add Q5 based on my observations:
Q5:
MGLRU places readahead folios in the newest generation. For example, if a page fault occurs at address 5, readahead fetches addresses 1–16, and all 16 folios are put in the youngest generation, even though many may not be needed. This can seriously impact reclamation performance, as these cold readahead folios occupy active slots.

See the code below and the checks performed by lru_gen_in_fault().

void folio_add_lru(struct folio *folio)

{
        ...
        /* see the comment in lru_gen_folio_seq() */
        if (lru_gen_enabled() && !folio_test_unevictable(folio) &&
            lru_gen_in_fault() && !(current->flags & PF_MEMALLOC))
                folio_set_active(folio);

        folio_batch_add_and_move(folio, lru_add);
}
Quoted 5 lines

EXPORT_SYMBOL(folio_add_lru);

I could have submitted a patchset to address this by initially marking only address 5 as active, and activating the other addresses later when they are actually mapped or accessed.

I need to read it more closely but that's a good idea.

Thanks,
Yuanchu

#11 lore raw
Quoted 15 lines
Quoted 6 lines
Quoted 3 lines

But this is not specific to MGLRU; it can also be an issue for the active/inactive LRU?

Pardon my lack of familiarity with the use case, in what ways are existing memcg protection features insufficient?

I think he means that whichever memcg is next up on the memcg LRU is the one that gets reclaimed from first, regardless of whether that memcg is for a foreground app. Because there is currently no strong relationship between memcg LRU ordering and Android app state. Android updates oom_score_adj on app state transitions, so it'd be possible for the kernel to use that information from userspace to influence memcg LRU ordering, but I'm not really a great fan of that idea.

Quoted 5 lines

Right, reclaim from foreground apps is not exclusively a MGLRU problem. There is an effort in both MGLRU and active/inactive LRU to provide some sort of fairness (now both are eventually-fair) for global reclaim, so that no memcg is the subject of disproportionate reclaim.

Hi T.J.,

Yes, exactly.

Quoted 10 lines

I'm planning to investigate applying memory.low to foreground / top-app for reducing the amount of reclaim from foreground apps. However at the same time, we are also working on applying memory limits to all apps (memory.high) so that no single app (including a foreground app) can use all the system memory as a matter of Android policy.

Thanks,
T.J.

That sounds good to me. We're also experimenting with a similar approach.

In my opinion, these knobs are a good opportunity to combine some ML and autotuning (in userspace, not in kernel space as the kernel-ML patches did):
- explore a reasonable memory.low / memory.high range for a given app, to both save memory and still keep good UX (hot launch / animations);
- classify apps into a few categories, and then reuse the tuned limits for apps in the same class.

Best,
Zicheng

Quoted 32 lines
Quoted 32 lines
Quoted 21 lines

Additionally, I’d like to add Q5 based on my observations:
Q5:
MGLRU places readahead folios in the newest generation. For example, if a page fault occurs at address 5, readahead fetches addresses 1–16, and all 16 folios are put in the youngest generation, even though many may not be needed. This can seriously impact reclamation performance, as these cold readahead folios occupy active slots.

See the code below and the checks performed by lru_gen_in_fault().

void folio_add_lru(struct folio *folio)

{
        ...
        /* see the comment in lru_gen_folio_seq() */
        if (lru_gen_enabled() && !folio_test_unevictable(folio) &&
            lru_gen_in_fault() && !(current->flags & PF_MEMALLOC))
                folio_set_active(folio);

        folio_batch_add_and_move(folio, lru_add);
}
Quoted 5 lines

EXPORT_SYMBOL(folio_add_lru);

I could have submitted a patchset to address this by initially marking only address 5 as active, and activating the other addresses later when they are actually mapped or accessed.

I need to read it more closely but that's a good idea.

Thanks,
Yuanchu

#12 lore raw
In reply to wangzicheng

Hi everyone,

Following up on my previous email and our discussion regarding "MGLRU on Android: Real-World Problems and Challenges", I've uploaded the slides for the talk.

You can access them here:
https://docs.google.com/presentation/d/1hUogz6InyLn13c8CjHuvEIzE4rT7saVRUV6xpWZoNfQ/edit?usp=sharing

I look forward to further discussions and collaboration on these topics.

Btw, What I mean power consumption benefits, is MGLRU compared to LRU. There are 10% power consumption in MGLRU+ActiveAging compared to origin MGLRU(not LRU).

Best regards,

Zicheng Wang

#13 lore raw
Quoted 9 lines · wangzicheng

Hi everyone,

Following up on my previous email and our discussion regarding "MGLRU on Android: Real-World Problems and Challenges", I've uploaded the slides for the talk.

You can access them here:
https://docs.google.com/presentation/d/1hUogz6InyLn13c8CjHuvEIzE4rT7saVRUV6xpWZoNfQ/edit?usp=sharing

Quoted 8 lines

I look forward to further discussions and collaboration on these topics.

Btw, What I mean power consumption benefits, is MGLRU compared to LRU. There are 10% power consumption in MGLRU+ActiveAging compared to origin MGLRU(not LRU).

Best regards,

Zicheng Wang

For Q4: cross-memcg priority

I have sent an RFC series [1] that attempts to make memcg aware of hot/cold status when reclaiming pages. I think it would be a good idea to combine this with the question at hand. We can treat the foreground memcg as the hot memcg and the background memcg as the cold memcg. Only when the cold memcgs cannot provide enough pages will the hot memcgs be reclaimed.

[1]
https://lore.kernel.org/lkml/[email protected]/

Signature · 2 lines
Best regards,
Ridong