Skip to content

RFC mm: Reduce direct reclaim stalls with RAM-backed swap

Matt Fleming linux-block linux-mm lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest1 patch 9 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/1mm: Reduce direct reclaim stalls with RAM-backed swapdrivers/block/brd.c drivers/block/zram/zram_drv.c include/linux/blkdev.h +3 more+89 -42 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [RFC PATCH 0/1] mm: Reduce direct reclaim stalls with RAM-backed swap

From: Matt Fleming <[email protected]>

Hi,

Systems with zram-only swap can spin in direct reclaim for 20-30 minutes without ever invoking the OOM killer. We've hit this repeatedly in production on machines with 377 GiB RAM and a 377 GiB zram device.

The problem
-----------

should_reclaim_retry() calls zone_reclaimable_pages() to estimate how much memory is still reclaimable. That estimate includes anonymous pages, on the assumption that swapping them out frees physical pages.

With disk-backed swap, that's true -- writing a page to disk frees a page of RAM, and SwapFree accurately reflects how many more pages can be written. With zram, the free slot count is inaccurate. A 377 GiB zram device with 10% used reports ~340 GiB of free swap slots, but filling those slots requires physical RAM that the system doesn't have -- that's why it's in direct reclaim in the first place.

The reclaimable estimate is off by orders of magnitude.

The fix
-------

This patch introduces two new flags: BLK_FEAT_RAM_BACKED at the block layer (set by zram and brd) and SWP_RAM_BACKED at the swap layer. When all active swap devices are RAM-backed, should_reclaim_retry() excludes anonymous pages from the reclaimable estimate and counts only file-backed pages. Once file pages are exhausted the watermark check fails and the kernel falls through to OOM.

Opting to OOM kill something over spinning in direct reclaim optimises for Mean Time To Recovery (MTTR) and prevents "brownout" situations where performance is degraded for prolonged periods (we've seen 20-30 minutes degraded system performance).

Design choices and known limitations
-------------------------------------

Why not fix zone_reclaimable_pages() globally?

Other callers (e.g. balance_pgdat() in kswapd) use the anon-inclusive count for different purposes. Changing it globally risks breaking kswapd's reclaim decisions in ways that are hard to test. Limiting the change to should_reclaim_retry() keeps the blast radius small and squarely in the direct reclaim path.

What about mixed swap configurations (zram + disk)?

When at least one disk-backed swap device is active, swap_all_ram_backed is false and the current behaviour is preserved. Per-device reclaimable accounting is possible but it's a much larger change, and mixed zram+disk configurations are uncommon in practice AFAIK.

Can we make zram free space accounting more accurate?

This is possible but probably the most complicated solution. Swap device drivers could provide a callback which RAM-backed drivers would use to estimate how much physical memory they could store given some average compression ratio (either historic or projected given a list of anon pages to swap) and the amount of free physical memory. Plus, this wouldn't be constant and would change on every invocation of the callback inline with the current compression ratio and the amount of free memory.

Build-testing
-------------

Built with defconfig, allnoconfig, allmodconfig, and multiple randconfig iterations on x86_64 / 7.0-rc2.

Matt Fleming (1):
mm: Reduce direct reclaim stalls with RAM-backed swap

 drivers/block/brd.c           |  3 ++-
 drivers/block/zram/zram_drv.c |  3 ++-
 include/linux/blkdev.h        |  8 ++++++
 include/linux/swap.h          |  9 +++++++
 mm/page_alloc.c               | 23 ++++++++++++++++-
 mm/swapfile.c                 | 47 ++++++++++++++++++++++++++++++++++-
 6 files changed, 89 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0