Skip to contentSkip to conversation

xen/balloon: add late_initcall_sync() for initial ballooning done

PATCH Superseded Juergen Gross 3 participants 7 messages Oct 28, 2021 – Oct 29, 2021

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Juergen Gross

author maintainer
#0 lore raw

PATCH 1/1 xen/balloon: add late_initcall_sync() for initial ballooning done

When running as PVH or HVM guest with actual memory < max memory the hypervisor is using "populate on demand" in order to allow the guest to balloon down from its maximum memory size. For this to work correctly the guest must not touch more memory pages than its target memory size as otherwise the PoD cache will be exhausted and the guest is crashed as a result of that.

In extreme cases ballooning down might not be finished today before the init process is started, which can consume lots of memory.

In order to avoid random boot crashes in such cases, add a late init call to wait for ballooning down having finished for PVH/HVM guests.

---
 drivers/xen/balloon.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/xen/balloon.c +20 -0
index 3a50f097ed3e..d19b851c3d3b 100644
@@ -765,3 +765,23 @@ static int __init balloon_init(void)
 	return 0;
 }
 subsys_initcall(balloon_init);
+
+static int __init balloon_wait_finish(void)
+{
+	if (!xen_domain())
+		return -ENODEV;
+
+	/* PV guests don't need to wait. */
+	if (xen_pv_domain() || !current_credit())
+		return 0;
+
+	pr_info("Waiting for initial ballooning down having finished.\n");
+
+	while (current_credit())
+		schedule_timeout_interruptible(HZ / 10);
+
+	pr_info("Initial ballooning down finished.\n");
+
+	return 0;
+}
+late_initcall_sync(balloon_wait_finish);
Signature · 1 line
2.26.2
#1 lore raw

drivers/xen/balloon.c static int __init balloon_init(void) · line 781

Quoted 3 lines · Juergen Gross
drivers/xen/balloon.c +3 -0
+
+	while (current_credit())
+		schedule_timeout_interruptible(HZ / 10);

Should we be concerned that we may get stuck here forever if for some reason we can't balloon down everything?

-boris

#2 lore raw
In reply to Juergen Gross
Quoted 10 lines · Juergen Gross

When running as PVH or HVM guest with actual memory < max memory the hypervisor is using "populate on demand" in order to allow the guest to balloon down from its maximum memory size. For this to work correctly the guest must not touch more memory pages than its target memory size as otherwise the PoD cache will be exhausted and the guest is crashed as a result of that.

In extreme cases ballooning down might not be finished today before the init process is started, which can consume lots of memory.

Quoted 6 lines

In order to avoid random boot crashes in such cases, add a late init call to wait for ballooning down having finished for PVH/HVM guests.

It may happen that initial balloon down fails (state==BP_ECANCELED). In that case, it waits indefinitely. I think it should rather report a failure (and panic? it's similar to OOM before PID 1 starts, so rather hard to recover), instead of hanging.

Anyway, it does fix the boot crashes.

Quoted 35 lines
---
 drivers/xen/balloon.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/xen/balloon.c +20 -0
index 3a50f097ed3e..d19b851c3d3b 100644
@@ -765,3 +765,23 @@ static int __init balloon_init(void)
 	return 0;
 }
 subsys_initcall(balloon_init);
+
+static int __init balloon_wait_finish(void)
+{
+	if (!xen_domain())
+		return -ENODEV;
+
+	/* PV guests don't need to wait. */
+	if (xen_pv_domain() || !current_credit())
+		return 0;
+
+	pr_info("Waiting for initial ballooning down having finished.\n");
+
+	while (current_credit())
+		schedule_timeout_interruptible(HZ / 10);
+
+	pr_info("Initial ballooning down finished.\n");
+
+	return 0;
+}
+late_initcall_sync(balloon_wait_finish);
Signature · 1 line
2.26.2
Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
author maintainer
#3 lore raw
Quoted 14 lines · Marek Marczykowski-Górecki
Quoted 13 lines · Juergen Gross

When running as PVH or HVM guest with actual memory < max memory the hypervisor is using "populate on demand" in order to allow the guest to balloon down from its maximum memory size. For this to work correctly the guest must not touch more memory pages than its target memory size as otherwise the PoD cache will be exhausted and the guest is crashed as a result of that.

In extreme cases ballooning down might not be finished today before the init process is started, which can consume lots of memory.

In order to avoid random boot crashes in such cases, add a late init call to wait for ballooning down having finished for PVH/HVM guests.

Quoted 8 lines
Quoted 3 lines

It may happen that initial balloon down fails (state==BP_ECANCELED). In that case, it waits indefinitely. I think it should rather report a failure (and panic? it's similar to OOM before PID 1 starts, so rather hard to recover), instead of hanging.

Okay, I can add something like that. I'm thinking of issuing a failure message in case of credit not having changed for 1 minute and panic() after two more minutes. Is this fine?

Juergen

#4 lore raw
Quoted 19 lines · Juergen Gross
Quoted 18 lines · Marek Marczykowski-Górecki
Quoted 16 lines · Juergen Gross

When running as PVH or HVM guest with actual memory < max memory the hypervisor is using "populate on demand" in order to allow the guest to balloon down from its maximum memory size. For this to work correctly the guest must not touch more memory pages than its target memory size as otherwise the PoD cache will be exhausted and the guest is crashed as a result of that.

In extreme cases ballooning down might not be finished today before the init process is started, which can consume lots of memory.

In order to avoid random boot crashes in such cases, add a late init call to wait for ballooning down having finished for PVH/HVM guests.

Quoted 8 lines
Quoted 4 lines

It may happen that initial balloon down fails (state==BP_ECANCELED). In that case, it waits indefinitely. I think it should rather report a failure (and panic? it's similar to OOM before PID 1 starts, so rather hard to recover), instead of hanging.

Okay, I can add something like that. I'm thinking of issuing a failure message in case of credit not having changed for 1 minute and panic() after two more minutes. Is this fine?

Isn't it better to get a state from balloon_thread()? If the balloon fails it won't really try anymore (until 3600s timeout), so waiting in that state doesn't help. And reporting the failure earlier may be more user friendly. Or maybe there is something that could wakeup the thread earlier, that I don't see? Hot plugging more RAM is rather unlikely at this stage...
See my patch at [1], although rather hacky (and likely - racy).

[1] https://lore.kernel.org/xen-devel/YXFxKC4shCATB913@mail-itl/

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
author maintainer
#5 lore raw
Quoted 29 lines · Marek Marczykowski-Górecki
Quoted 19 lines · Juergen Gross
Quoted 18 lines · Marek Marczykowski-Górecki
Quoted 16 lines · Juergen Gross

When running as PVH or HVM guest with actual memory < max memory the hypervisor is using "populate on demand" in order to allow the guest to balloon down from its maximum memory size. For this to work correctly the guest must not touch more memory pages than its target memory size as otherwise the PoD cache will be exhausted and the guest is crashed as a result of that.

In extreme cases ballooning down might not be finished today before the init process is started, which can consume lots of memory.

In order to avoid random boot crashes in such cases, add a late init call to wait for ballooning down having finished for PVH/HVM guests.

Quoted 8 lines
Quoted 4 lines

It may happen that initial balloon down fails (state==BP_ECANCELED). In that case, it waits indefinitely. I think it should rather report a failure (and panic? it's similar to OOM before PID 1 starts, so rather hard to recover), instead of hanging.

Okay, I can add something like that. I'm thinking of issuing a failure message in case of credit not having changed for 1 minute and panic() after two more minutes. Is this fine?

Quoted 6 lines

Isn't it better to get a state from balloon_thread()? If the balloon fails it won't really try anymore (until 3600s timeout), so waiting in that state doesn't help. And reporting the failure earlier may be more user friendly. Or maybe there is something that could wakeup the thread earlier, that I don't see? Hot plugging more RAM is rather unlikely at this stage...

Waking up the thread would be easy, but probably that wouldn't really help.

The idea was that maybe a Xen admin would see the guest not booting up further and then adding some more memory to the guest (this should wake up the balloon thread again).

I agree that stopping to wait for ballooning to finish in case of it having failed is probably a sensible thing to do. Additionally I could add a boot parameter to control the timeout after the fail message and the panic().

What do you think?

Juergen

Juergen

#6 lore raw
Quoted 37 lines · Juergen Gross
Quoted 29 lines · Marek Marczykowski-Górecki
Quoted 19 lines · Juergen Gross
Quoted 18 lines · Marek Marczykowski-Górecki
Quoted 16 lines · Juergen Gross

When running as PVH or HVM guest with actual memory < max memory the hypervisor is using "populate on demand" in order to allow the guest to balloon down from its maximum memory size. For this to work correctly the guest must not touch more memory pages than its target memory size as otherwise the PoD cache will be exhausted and the guest is crashed as a result of that.

In extreme cases ballooning down might not be finished today before the init process is started, which can consume lots of memory.

In order to avoid random boot crashes in such cases, add a late init call to wait for ballooning down having finished for PVH/HVM guests.

Quoted 8 lines
Quoted 4 lines

It may happen that initial balloon down fails (state==BP_ECANCELED). In that case, it waits indefinitely. I think it should rather report a failure (and panic? it's similar to OOM before PID 1 starts, so rather hard to recover), instead of hanging.

Okay, I can add something like that. I'm thinking of issuing a failure message in case of credit not having changed for 1 minute and panic() after two more minutes. Is this fine?

Quoted 6 lines

Isn't it better to get a state from balloon_thread()? If the balloon fails it won't really try anymore (until 3600s timeout), so waiting in that state doesn't help. And reporting the failure earlier may be more user friendly. Or maybe there is something that could wakeup the thread earlier, that I don't see? Hot plugging more RAM is rather unlikely at this stage...

Quoted 2 lines

Waking up the thread would be easy, but probably that wouldn't really help.

Waking it up alone no. I was thinking what could wake it up - if nothing, then definitely waiting wouldn't help. You explained that just below:

Quoted 8 lines

The idea was that maybe a Xen admin would see the guest not booting up further and then adding some more memory to the guest (this should wake up the balloon thread again).

I agree that stopping to wait for ballooning to finish in case of it having failed is probably a sensible thing to do. Additionally I could add a boot parameter to control the timeout after the fail message and the panic().

Right, that would make sense: it's basically a time admin has to plug in more memory to the VM.

Quoted 1 line

What do you think?

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab