mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Stefan Wold <ratler@lunar-linux.org>
To: Phillip Lougher <phillip@squashfs.org.uk>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: Hsin-Yi Wang <hsinyi@chromium.org>,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH] squashfs: fix readahead start on 32-bit for files larger than 4 GiB
Date: Fri,  4 Dec 2026 23:26:06 +0000	[thread overview]
Message-ID: <20261204232606.1078776-1-ratler@lunar-linux.org> (raw)

In squashfs_readahead() the block mask is a size_t:

	size_t mask = (1UL << msblk->block_log) - 1;
	loff_t start = readahead_pos(ractl) & ~mask;

On 32-bit architectures size_t is 32 bits wide, so ~mask is a 32-bit
value (e.g. 0xfffe0000 for 128 KiB blocks). It is zero-extended when
promoted to loff_t, which clears the upper 32 bits of the readahead
position. For a read at or beyond 4 GiB, start then points into the
first 4 GiB of the file, a multiple of 4 GiB below the block being
read. readahead_expand() extends the request downwards towards start,
adding locked pages to the page cache for everything in between. It
stops early only if it finds a page that is already cached or an
allocation fails.

Reproducer, on a 32-bit kernel:

	truncate -s 16G big
	mksquashfs big test.sqsh
	mount -t squashfs -o loop test.sqsh /mnt
	dd if=/mnt/big of=/dev/null bs=64k count=1 skip=81920

On i686 with 2 GiB of RAM this 64 KiB read at offset 5 GiB takes 3 to
4 seconds and uses more than 1.6 GiB of memory while it runs (most of
what was free). The same read below 4 GiB (skip=49152) takes less than
a millisecond and uses well under 1 MiB.

A live ISO that stores its root filesystem as a 16 GiB sparse ext4
image inside squashfs (dracut dmsquash-live layout) runs out of memory
during boot as soon as udev probes the end of the loop device, and
panics with "System is deadlocked on memory".

Widen the mask to loff_t before inverting it.

With this change the read at 5 GiB behaves like the one below 4 GiB,
and that live ISO boots to its installer. Tested on i686 (PAE) under
QEMU with 7.4-rc5 and 7.3.6.

The bug was found with the help of an AI coding assistant (Claude
Code), which also drafted the patch and this changelog.

Fixes: 8fc78b6fe24c ("squashfs: implement readahead")
Cc: stable@vger.kernel.org
Signed-off-by: Stefan Wold <ratler@lunar-linux.org>
---

Notes:
    "Squashfs: very large files can exceed an int or size_t" [1], currently
    in mm-nonmm-unstable, changes mask to s64 and with that also fixes this
    bug. That patch is a larger change and carries no Fixes: or stable tag,
    so this one-liner is intended as the fix that can be backported. It
    applies to mainline, mm-hotfixes-unstable and linux-6.1.y through
    linux-7.3.y.
    
    The two patches conflict in one place: the line changed here is a
    context line in the squashfs_readahead() hunk of [1]. With both applied
    the cast is redundant and can be dropped.
    
    Tool use: I noticed that the i686 live ISO I build panicked in the
    initrd during boot, and directed the debugging in a Claude Code
    session. The session narrowed the failure down by experiment (RAM size,
    THP, SMP, an x86_64 comparison, reads at offsets around 4 GiB) and then
    found the cause by reading squashfs_readahead(). It wrote the one-line
    change and drafted the changelog. A second session reviewed both, and I
    reviewed the result. The kernel builds and test runs below were also
    carried out in those sessions.
    
    Testing: i686 PAE kernels built from 7.4-rc5 and 7.3.6, with and
    without the change, run in QEMU (qemu32). Both patched kernels boot the
    live ISO. The reproducer above was run on 7.4-rc5 with 2 GiB of RAM;
    the image was made with squashfs-tools 4.7.2 on an x86_64 host. 64 KiB
    reads, with MemFree sampled while dd runs:
    
      offset            unpatched            patched
      3 GiB             0 kB,     0.0004 s   456 kB, 0.0005 s
      4 GiB - 64 KiB    0 kB,     0.0002 s   224 kB, 0.0004 s
      5 GiB             1.62 GiB, 3.88 s     252 kB, 0.0002 s
      16 GiB - 64 KiB   1.73 GiB, 1.02 s     456 kB, 0.0009 s
    
    With this fully sparse file the memory is released when the read
    returns and nothing panics. Equivalent reads from the live ISO's image,
    which contains data, ended in the OOM panic at 16 GiB - 64 KiB on the
    unpatched kernel. Not tested: other 32-bit architectures.
    
    [1] https://patch.msgid.link/20261125014737.347231-15-phillip@squashfs.org.uk

 fs/squashfs/file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/squashfs/file.c b/fs/squashfs/file.c
index d83594ce9..6fa04f988 100644
--- a/fs/squashfs/file.c
+++ b/fs/squashfs/file.c
@@ -577,7 +577,7 @@ static void squashfs_readahead(struct readahead_control *ractl)
 	struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
 	size_t mask = (1UL << msblk->block_log) - 1;
 	unsigned short shift = msblk->block_log - PAGE_SHIFT;
-	loff_t start = readahead_pos(ractl) & ~mask;
+	loff_t start = readahead_pos(ractl) & ~(loff_t)mask;
 	size_t len = readahead_length(ractl) + readahead_pos(ractl) - start;
 	struct squashfs_page_actor *actor;
 	unsigned int nr_pages = 0;

base-commit: 5dff0c81e94cd72d52bdfbb6044462c1be10f8d0
-- 
2.51.1


                 reply	other threads:[~2026-12-04 23:26 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204232606.1078776-1-ratler@lunar-linux.org \
    --to=ratler@lunar-linux.org \
    --cc=akpm@linux-foundation.org \
    --cc=hsinyi@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=phillip@squashfs.org.uk \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®