systemd / zram-generator

Systemd unit generator for zram devices
MIT License
575 stars 48 forks source link

Swap device is created with unexpected size when VM balloon is used #177

Open keszybz opened 1 year ago

keszybz commented 1 year ago

I have a VM with a large Maximum allocation of 8 GB and Current allocation of 2 GB. It seems that during boot, the ballooning process is so slow, that systemd-zram-setup@zram0.service is executed early enough to see the "wrong" size. If I restart the unit later on, zram0 is recreated with the expected size (==RAM).

```console $ journalctl --grep 'Tworzenie obszaru wymiany'|cat gru 12 22:24:13 fedora zram-generator[597]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 1,9 GiB (bajtów: 2030039040) -- Boot ee9540d36e8743f093c312212b5d6dd8 -- gru 12 22:26:08 fedora zram-generator[604]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 1,9 GiB (bajtów: 2030039040) -- Boot 89bd50c9be804747bfd10033041d1e36 -- gru 12 22:36:36 fedora zram-generator[602]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 1,9 GiB (bajtów: 2030039040) -- Boot e397a9f6acb347f9aa0cac7a9b9c516d -- gru 12 22:49:16 fedora zram-generator[601]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 1,9 GiB (bajtów: 2030039040) -- Boot 1fd33a032a9843f2967855b324c9b174 -- gru 13 08:39:25 fedora-new zram-generator[612]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 1,9 GiB (bajtów: 2030039040) -- Boot beab8f7d88e54b718046350ba7621be5 -- gru 13 09:00:04 fedora-new zram-generator[613]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 1,9 GiB (bajtów: 2030039040) -- Boot 6b7dac05982d4b958e02bab9ee9972ad -- gru 13 09:34:17 fedora-new zram-generator[615]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 1,9 GiB (bajtów: 2030039040) -- Boot c9fee5906c6f43508f66fa14f85ffa16 -- gru 13 21:00:28 fedora-new zram-generator[611]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 1,9 GiB (bajtów: 2030039040) -- Boot a24ca96a76f543ed86d25b4f3a3fcd42 -- lis 22 10:42:03 fedora-new zram-generator[624]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,2 GiB (bajtów: 7781478400) -- Boot 18e311a22dfa43328208dd5b6b2f68d0 -- lis 29 09:33:33 fedora-new zram-generator[621]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,2 GiB (bajtów: 7718563840) -- Boot 4c915f583709425a848ca2e6c8070172 -- lis 29 16:05:13 fedora-new zram-generator[606]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,2 GiB (bajtów: 7725903872) -- Boot 74f1920523184c479f799ceaf72b892a -- gru 05 09:32:45 fedora-new zram-generator[613]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,4 GiB (bajtów: 7941910528) -- Boot 2e3b7c2044d1468b977288c4dcbd94ad -- gru 05 09:35:59 fedora-new zram-generator[619]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7 GiB (bajtów: 7465857024) -- Boot fd6edb3a03c94cadaca4b78dc0fc55a0 -- gru 05 09:36:39 fedora-new zram-generator[618]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,4 GiB (bajtów: 7911501824) -- Boot fc74750441064b5b933a33932a922eba -- gru 05 14:28:27 fedora-new zram-generator[620]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,1 GiB (bajtów: 7648309248) -- Boot ff78dad68fb846c1abc6f7a197fd597d -- gru 05 14:29:39 fedora-new zram-generator[614]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,1 GiB (bajtów: 7604269056) -- Boot 0b7427a4561a46c59bdeb9e005710257 -- gru 05 14:31:16 fedora-new zram-generator[615]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 6,9 GiB (bajtów: 7402942464) -- Boot f75b8fcff38045a0af56394f3a5975af -- gru 05 14:31:55 fedora-new zram-generator[614]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,1 GiB (bajtów: 7632580608) -- Boot aee3dc1344f44cecb15b041f2bd364f3 -- sty 25 15:40:28 fedora-new zram-generator[620]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,1 GiB (bajtów: 7625240576) -- Boot 9cba4bdb3d9f4e5ab83fff5bbc01beba -- lut 02 14:00:47 fedora-new zram-generator[577]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,7 GiB (bajtów: 8223977472) -- Boot 49dd16f999d94258b2e8b43e1d600aea -- lut 28 10:54:47 fedora-new zram-generator[572]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,7 GiB (bajtów: 8223977472) -- Boot 1927af5932164a60bd36b6fb586c73e0 -- lut 28 10:56:13 fedora-new zram-generator[567]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,7 GiB (bajtów: 8223977472) -- Boot c34a8724ae95447d9a70f0e164df1cd6 -- mar 01 09:35:50 fedora-new zram-generator[573]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,7 GiB (bajtów: 8223977472) -- Boot b964abc3822645a0836acecacbd4b7a3 -- mar 01 09:59:51 fedora-new zram-generator[573]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,7 GiB (bajtów: 8223977472) -- Boot e77db9afe058453fb8716fb6db71b17b -- mar 06 22:00:28 fedora-new zram-generator[578]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,7 GiB (bajtów: 8223977472) -- Boot ab1cf14e48c84b7686037560406c079b -- mar 06 22:01:54 fedora-new zram-generator[571]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,7 GiB (bajtów: 8223977472) -- Boot bf7cd15649284c849477d58f847df077 -- mar 06 22:03:40 fedora-new zram-generator[576]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,7 GiB (bajtów: 8223977472) -- Boot 379cd2df0c9c4f4db77bf984513e7cd3 -- mar 14 16:01:42 fedora-new zram-generator[563]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,6 GiB (bajtów: 8211394560) -- Boot f80b6815c0a240798321f275a7d1ef66 -- mar 14 18:18:16 fedora-new zram-generator[572]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,6 GiB (bajtów: 8211394560) mar 14 18:25:33 fedora-new zram-generator[2585]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 1,7 GiB (bajtów: 1869606912) -- Boot ec28ea452a124c69b7b7d318ee36625a -- mar 14 18:55:22 fedora-new zram-generator[573]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,6 GiB (bajtów: 8211394560) -- Boot 328d8c2b2b0d41bfb46f98990186ad1e -- mar 14 19:00:21 fedora-new zram-generator[573]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 7,6 GiB (bajtów: 8211394560) mar 14 19:02:10 fedora-new zram-generator[2127]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 1,7 GiB (bajtów: 1869606912) mar 14 19:23:59 fedora-new zram-generator[2252]: Tworzenie obszaru wymiany w wersji 1, rozmiar = 1,7 GiB (bajtów: 1869606912) ```

I can't find a reference, but I'm pretty sure we had an issue about this previously when the zram0 device was created directly in the generator. Later on, when we switched to creating of the zram0 device in the unit, this problem "went away", because this step happens a bit later. But it seems that something changed, and the service now runs earlier.

This is ugly… I don't see any source of information about virtio_balloon state from within the machine.

nabijaczleweli commented 1 year ago

Tempted to say "works as expected", right? When the system has 2G of MemTotal attached ram is 2G in zram-size, and with 8G – 8G. Way I see it, balloon is just memory hot-plug?

keszybz commented 1 year ago

I don't think it can be considered to "work". First of all, the size is subject to a race condition on every boot. Second, I don't think it's useful to scale to Maximum, because for most VMs that is not relevant, and could possibly be many times higher than Current. You end up with a swap device that is too large for the amount of available RAM; there is no way that 8GB zram device can be filled with 2GB RAM available. (E.g. I can imagine a VM host with 256GB of RAM that is used for a hundred VMs, each configured by default with Current/Maximum of 2/32, just in case a VM needs to be dynamically scaled at some later point…)

FallingSnow commented 1 year ago

My situation is somewhat similar. I'm using virtio-mem. The system boots with 1G of ram and once the virtio-mem kernel module loads the system has access to an additional 15GB of ram. However the system's zram is set to 1G since that was the size of ram at boot.

I suppose this is works as intended but it would be nice-to-have if zram-generator detects memory hot-plugging automatically. Since this changes current behavior maybe it could be hidden behind a config option.

FallingSnow commented 1 year ago

I've come up with two possible solutions.

  1. Wait for kernel modules to load before starting zram device by adding After=systemd-modules-load.service to the zram systemd file. This is not dynamic and only solves the low zram size at boot.
  2. Detect kernel hotplugging and add/remove zram devices. This is more difficult to implement, would need to look into detecting hot plugging ram.