From: Robert Beckett <bob.beckett@collabora.com>
To: "Mikulas Patocka" <mpatocka@redhat.com>
Cc: "dm-devel" <dm-devel@lists.linux.dev>,
"linux-block" <linux-block@vger.kernel.org>,
"kernel" <kernel@collabora.com>
Subject: Re: deadlock when swapping to encrypted swapfile
Date: Tue, 09 Sep 2025 17:50:08 +0100 [thread overview]
Message-ID: <1992f628105.2bf0303b1373545.4844645742991812595@collabora.com> (raw)
In-Reply-To: <2d517844-b7bf-3930-e811-e073ec347d4a@redhat.com>
---- On Tue, 09 Sep 2025 15:37:09 +0100 Mikulas Patocka <mpatocka@redhat.com> wrote ---
>
>
> On Mon, 8 Sep 2025, Robert Beckett wrote:
>
> > Hi,
> >
> > While testing resiliency of encrypted swap using dmcrypt we encounter easily reproducible deadlocks.
> > The setup is a simple 1GB encrypted swap file [1] with a little mem chewer program [2] to consume all ram.
> >
> > [1] Swap file setup
> > ```
> > $ swapoff /home/swapfile
> > $ echo 'swap /home/swapfile /dev/urandom swap,cipher=aes-cbc-essiv:sha256,size=256' >> /etc/crypttab
> > $ systemctl daemon-reload
> > $ systemctl start systemd-cryptsetup@swap.service
> > $ swapon /dev/mapper/swap
> > ```
>
> I have tried to swap on encrypted block device and it worked for me.
>
> I've just realized that you are swapping to a file with the loopback
> driver on the top of it and with the dm-crypt device on the top of the
> loopback device.
>
> This can't work in principle - the problem is that the filesystem needs to
> allocate memory when you write to it, so it deadlocks when the machine
> runs out of memory and needs to write back some pages. There is no easy
> fix - fixing this would require major rewrite of the VFS layer.
>
> When you swap to a file directly, the kernel bypasses the filesystem, so
> it should work - but when you put encryption on the top of a file, there
> is no way how to bypass the filesystem.
>
> So, I suggest to create a partition or a logical volume for swap and put
> dm-crypt on the top of it.
>
> Mikulas
>
>
Yeah, unfortunately we are currently restricted to using a swapfile due to many units already shipped with that.
We have longer term plans to dynamically allocate the swapfiles as neded based on a new query for estimated size
required for hibernation etc. Moving to swap partition is just not viable currently.
I tried halving /sys/module/dm_mod/parameters/swap_bios but it didn't help, which based on your more recent
reply is not unexpected.
I have a work around for now, which is to run a userland earlyoom daemon. That seems to get in and oomkill in time.
I guess another option would be to have the swapfile in a luks encrypted partition, but that equally is not viable for
steamdeck currently.
However, I'm still interested in the longer term solution of fixing the kernel so that it can handle scenarios
like this no matter how ill advised they may be. Telling users not to do something seems like a bad solution :)
Do you have any ideas about the unreliable kernel oomkiller stepping in? I definitely fill ram and swap, seems like
it should be firing.
Thanks
Bob
next prev parent reply other threads:[~2025-09-09 16:50 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-08 18:27 deadlock when swapping to encrypted swapfile Robert Beckett
2025-09-08 19:56 ` Mikulas Patocka
2025-09-09 14:37 ` Mikulas Patocka
2025-09-09 16:50 ` Robert Beckett [this message]
2025-09-10 11:26 ` Mikulas Patocka
2025-09-10 15:24 ` Robert Beckett
2025-09-10 17:45 ` Bryn M. Reeves
2025-09-11 16:56 ` Mikulas Patocka
2025-09-11 17:12 ` Robert Beckett
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=1992f628105.2bf0303b1373545.4844645742991812595@collabora.com \
--to=bob.beckett@collabora.com \
--cc=dm-devel@lists.linux.dev \
--cc=kernel@collabora.com \
--cc=linux-block@vger.kernel.org \
--cc=mpatocka@redhat.com \
/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