Skip to contentSkip to conversation

virtio_balloon: stay awake while adjusting balloon

PATCH Superseded David Stevens 2 participants 7 messages Dec 11, 2023 – Dec 18, 2023

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio_balloon: stay awake while adjusting balloon

From: David Stevens <[email protected]>

Add a wakeup event for when the balloon is inflating or deflating. Userspace can enable this wakeup event to prevent the system from suspending while the balloon is being adjusted. This allows /sys/power/wakeup_count to be used without breaking virtio_balloon's cooperative memory management.

---
 drivers/virtio/virtio_balloon.c | 59 +++++++++++++++++++++++++++------
 1 file changed, 49 insertions(+), 10 deletions(-)
drivers/virtio/virtio_balloon.c +49 -10
index 1fe93e93f5bc..811d8937246a 100644
@@ -119,6 +119,11 @@ struct virtio_balloon {
 	/* Free page reporting device */
 	struct virtqueue *reporting_vq;
 	struct page_reporting_dev_info pr_dev_info;
+
+	/* State for keeping the wakeup_source active while adjusting the balloon */
+	spinlock_t adjustment_lock;
+	u32 adjustment_seqno;
+	bool adjustment_in_progress;
 };
 
 static const struct virtio_device_id id_table[] = {
@@ -437,6 +442,31 @@ static void virtio_balloon_queue_free_page_work(struct virtio_balloon *vb)
 	queue_work(vb->balloon_wq, &vb->report_free_page_work);
 }
 
+static void start_update_balloon_size(struct virtio_balloon *vb)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&vb->adjustment_lock, flags);
+	vb->adjustment_seqno++;
+	if (!vb->adjustment_in_progress) {
+		vb->adjustment_in_progress = true;
+		pm_stay_awake(&vb->vdev->dev);
+	}
+	spin_unlock_irqrestore(&vb->adjustment_lock, flags);
+
+	queue_work(system_freezable_wq, &vb->update_balloon_size_work);
+}
+
+static void end_update_balloon_size(struct virtio_balloon *vb, u32 seqno)
+{
+	spin_lock(&vb->adjustment_lock);
+	if (vb->adjustment_seqno == seqno && vb->adjustment_in_progress) {
+		vb->adjustment_in_progress = false;
+		pm_relax(&vb->vdev->dev);
+	}
+	spin_unlock(&vb->adjustment_lock);
+}
+
 static void virtballoon_changed(struct virtio_device *vdev)
 {
 	struct virtio_balloon *vb = vdev->priv;
@@ -444,8 +474,7 @@ static void virtballoon_changed(struct virtio_device *vdev)
 
 	spin_lock_irqsave(&vb->stop_update_lock, flags);
 	if (!vb->stop_update) {
-		queue_work(system_freezable_wq,
-			   &vb->update_balloon_size_work);
+		start_update_balloon_size(vb);
 		virtio_balloon_queue_free_page_work(vb);
 	}
 	spin_unlock_irqrestore(&vb->stop_update_lock, flags);
@@ -473,22 +502,29 @@ static void update_balloon_size_func(struct work_struct *work)
 {
 	struct virtio_balloon *vb;
 	s64 diff;
+	u32 seqno;
 
 	vb = container_of(work, struct virtio_balloon,
 			  update_balloon_size_work);
-	diff = towards_target(vb);
 
-	if (!diff)
-		return;
+	spin_lock(&vb->adjustment_lock);
+	seqno = vb->adjustment_seqno;
+	spin_unlock(&vb->adjustment_lock);
 
-	if (diff > 0)
-		diff -= fill_balloon(vb, diff);
-	else
-		diff += leak_balloon(vb, -diff);
-	update_balloon_size(vb);
+	diff = towards_target(vb);
+
+	if (diff) {
+		if (diff > 0)
+			diff -= fill_balloon(vb, diff);
+		else
+			diff += leak_balloon(vb, -diff);
+		update_balloon_size(vb);
+	}
 
 	if (diff)
 		queue_work(system_freezable_wq, work);
+	else
+		end_update_balloon_size(vb, seqno);
 }
 
 static int init_vqs(struct virtio_balloon *vb)
@@ -992,6 +1028,9 @@ static int virtballoon_probe(struct virtio_device *vdev)
 			goto out_unregister_oom;
 	}
 
+	spin_lock_init(&vb->adjustment_lock);
+	device_set_wakeup_capable(&vb->vdev->dev, true);
+
 	virtio_device_ready(vdev);
 
 	if (towards_target(vb))
Signature · 1 line
2.43.0.472.g3155946c3a-goog
#1 lore raw
Quoted 2 lines · David Stevens

From: David Stevens <[email protected]>

Hi David,

Quoted 5 lines

Add a wakeup event for when the balloon is inflating or deflating. Userspace can enable this wakeup event to prevent the system from suspending while the balloon is being adjusted. This allows /sys/power/wakeup_count to be used without breaking virtio_balloon's cooperative memory management.

Can you add/share some more details

Quoted 11 lines
---
  drivers/virtio/virtio_balloon.c | 59 +++++++++++++++++++++++++++------
  1 file changed, 49 insertions(+), 10 deletions(-)
drivers/virtio/virtio_balloon.c +0 -0
index 1fe93e93f5bc..811d8937246a 100644

drivers/virtio/virtio_balloon.c struct virtio_balloon { · line 125

Quoted 7 lines
drivers/virtio/virtio_balloon.c +4 -0
@@ -119,6 +119,11 @@ struct virtio_balloon {
  	/* Free page reporting device */
  	struct virtqueue *reporting_vq;
  	struct page_reporting_dev_info pr_dev_info;
+
+	/* State for keeping the wakeup_source active while adjusting the balloon */
+	spinlock_t adjustment_lock;
+	u32 adjustment_seqno;

Using a simple flag that gets set when updating the balloon size and test-and-clear when testing for changes should be easier to get.

bool adjustment_balloon_size_changed;

or sth like that.

Quoted 71 lines
drivers/virtio/virtio_balloon.c +35 -10
+	bool adjustment_in_progress;
  };
 
  static const struct virtio_device_id id_table[] = {
@@ -437,6 +442,31 @@ static void virtio_balloon_queue_free_page_work(struct virtio_balloon *vb)
  	queue_work(vb->balloon_wq, &vb->report_free_page_work);
  }
 
+static void start_update_balloon_size(struct virtio_balloon *vb)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&vb->adjustment_lock, flags);
+	vb->adjustment_seqno++;
+	if (!vb->adjustment_in_progress) {
+		vb->adjustment_in_progress = true;
+		pm_stay_awake(&vb->vdev->dev);
+	}
+	spin_unlock_irqrestore(&vb->adjustment_lock, flags);
+
+	queue_work(system_freezable_wq, &vb->update_balloon_size_work);
+}
+
+static void end_update_balloon_size(struct virtio_balloon *vb, u32 seqno)
+{
+	spin_lock(&vb->adjustment_lock);
+	if (vb->adjustment_seqno == seqno && vb->adjustment_in_progress) {
+		vb->adjustment_in_progress = false;
+		pm_relax(&vb->vdev->dev);
+	}
+	spin_unlock(&vb->adjustment_lock);
+}
+
  static void virtballoon_changed(struct virtio_device *vdev)
  {
  	struct virtio_balloon *vb = vdev->priv;
@@ -444,8 +474,7 @@ static void virtballoon_changed(struct virtio_device *vdev)
 
  	spin_lock_irqsave(&vb->stop_update_lock, flags);
  	if (!vb->stop_update) {
-		queue_work(system_freezable_wq,
-			   &vb->update_balloon_size_work);
+		start_update_balloon_size(vb);
  		virtio_balloon_queue_free_page_work(vb);
  	}
  	spin_unlock_irqrestore(&vb->stop_update_lock, flags);
@@ -473,22 +502,29 @@ static void update_balloon_size_func(struct work_struct *work)
  {
  	struct virtio_balloon *vb;
  	s64 diff;
+	u32 seqno;
 
  	vb = container_of(work, struct virtio_balloon,
  			  update_balloon_size_work);
-	diff = towards_target(vb);
 
-	if (!diff)
-		return;
+	spin_lock(&vb->adjustment_lock);
+	seqno = vb->adjustment_seqno;
+	spin_unlock(&vb->adjustment_lock);
 
-	if (diff > 0)
-		diff -= fill_balloon(vb, diff);
-	else
-		diff += leak_balloon(vb, -diff);
-	update_balloon_size(vb);
+	diff = towards_target(vb);
+
+	if (diff) {
+		if (diff > 0)

drivers/virtio/virtio_balloon.c static void update_balloon_size_func(struct work_struct *work) · line 527

Quoted 10 lines
drivers/virtio/virtio_balloon.c +7 -0
+			diff -= fill_balloon(vb, diff);
+		else
+			diff += leak_balloon(vb, -diff);
+		update_balloon_size(vb);
+	}
 
  	if (diff)
  		queue_work(system_freezable_wq, work);
+	else
+		end_update_balloon_size(vb, seqno);

What if we stop the workqueue and unload the driver -- see remove_common() -- won't you leave pm_stay_awake() wrongly set?

drivers/virtio/virtio_balloon.c static int virtballoon_probe(struct virtio_device *vdev) · line 1032

Quoted 9 lines
drivers/virtio/virtio_balloon.c +2 -0
  }
 
  static int init_vqs(struct virtio_balloon *vb)
@@ -992,6 +1028,9 @@ static int virtballoon_probe(struct virtio_device *vdev)
  			goto out_unregister_oom;
  	}
 
+	spin_lock_init(&vb->adjustment_lock);
+	device_set_wakeup_capable(&vb->vdev->dev, true);

I'm a bit confused: Documentation/driver-api/pm/devices.rst documents

"
The :c:member:power.can_wakeup flag just records whether the device (and its driver) can physically support wakeup events. The :c:func:device_set_wakeup_capable() routine affects this flag.
"

...

"
Whether or not a device is capable of issuing wakeup events is a hardware matter, and the kernel is responsible for keeping track of it. "

But how is the virtio-balloon device capable of waking up the machine? Your patch merely implies that the virtio-baloon device is capable to prohbit going to sleep.

What am I missing?

Signature · 3 lines
Cheers,

David / dhildenb
#2 lore raw
Quoted 7 lines · David Hildenbrand
Quoted 2 lines · David Stevens

From: David Stevens <[email protected]>

Hi David,

Quoted 7 lines
Quoted 5 lines

Add a wakeup event for when the balloon is inflating or deflating. Userspace can enable this wakeup event to prevent the system from suspending while the balloon is being adjusted. This allows /sys/power/wakeup_count to be used without breaking virtio_balloon's cooperative memory management.

Can you add/share some more details

I'm working on enabling support for Linux s2Idle in our Android virtual machine, to restrict apps from running in the background without holding an Android partial wakelock. With the patch I recently sent out [1], since crosvm advertises native PCI power management for virtio devices, the Android guest can properly enter s2idle, and it can be woken up by incoming IO. However, one of the remaining problems is that when the host needs to reclaim memory from the guest via the virtio-balloon, there is nothing preventing the guest from entering s2idle before the balloon driver finishes returning memory to the host.

One alternative to this approach would be to add a virtballoon_suspend callback to abort suspend if the balloon is inflating/adjusting. However, it seems cleaner to just prevent suspend in the first place.

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 22 lines
Quoted 11 lines
---
  drivers/virtio/virtio_balloon.c | 59 +++++++++++++++++++++++++++------
  1 file changed, 49 insertions(+), 10 deletions(-)
drivers/virtio/virtio_balloon.c +0 -0
index 1fe93e93f5bc..811d8937246a 100644

drivers/virtio/virtio_balloon.c struct virtio_balloon { · line 125

Quoted 7 lines
drivers/virtio/virtio_balloon.c +4 -0
@@ -119,6 +119,11 @@ struct virtio_balloon {
      /* Free page reporting device */
      struct virtqueue *reporting_vq;
      struct page_reporting_dev_info pr_dev_info;
+
+     /* State for keeping the wakeup_source active while adjusting the balloon */
+     spinlock_t adjustment_lock;
+     u32 adjustment_seqno;

Using a simple flag that gets set when updating the balloon size and test-and-clear when testing for changes should be easier to get.

Quoted 3 lines

bool adjustment_balloon_size_changed;

or sth like that.

That's a good simplification, thanks.

Quoted 77 lines
Quoted 77 lines
drivers/virtio/virtio_balloon.c +40 -10
+     bool adjustment_in_progress;
  };
 
  static const struct virtio_device_id id_table[] = {
@@ -437,6 +442,31 @@ static void virtio_balloon_queue_free_page_work(struct virtio_balloon *vb)
      queue_work(vb->balloon_wq, &vb->report_free_page_work);
  }
 
+static void start_update_balloon_size(struct virtio_balloon *vb)
+{
+     unsigned long flags;
+
+     spin_lock_irqsave(&vb->adjustment_lock, flags);
+     vb->adjustment_seqno++;
+     if (!vb->adjustment_in_progress) {
+             vb->adjustment_in_progress = true;
+             pm_stay_awake(&vb->vdev->dev);
+     }
+     spin_unlock_irqrestore(&vb->adjustment_lock, flags);
+
+     queue_work(system_freezable_wq, &vb->update_balloon_size_work);
+}
+
+static void end_update_balloon_size(struct virtio_balloon *vb, u32 seqno)
+{
+     spin_lock(&vb->adjustment_lock);
+     if (vb->adjustment_seqno == seqno && vb->adjustment_in_progress) {
+             vb->adjustment_in_progress = false;
+             pm_relax(&vb->vdev->dev);
+     }
+     spin_unlock(&vb->adjustment_lock);
+}
+
  static void virtballoon_changed(struct virtio_device *vdev)
  {
      struct virtio_balloon *vb = vdev->priv;
@@ -444,8 +474,7 @@ static void virtballoon_changed(struct virtio_device *vdev)
 
      spin_lock_irqsave(&vb->stop_update_lock, flags);
      if (!vb->stop_update) {
-             queue_work(system_freezable_wq,
-                        &vb->update_balloon_size_work);
+             start_update_balloon_size(vb);
              virtio_balloon_queue_free_page_work(vb);
      }
      spin_unlock_irqrestore(&vb->stop_update_lock, flags);
@@ -473,22 +502,29 @@ static void update_balloon_size_func(struct work_struct *work)
  {
      struct virtio_balloon *vb;
      s64 diff;
+     u32 seqno;
 
      vb = container_of(work, struct virtio_balloon,
                        update_balloon_size_work);
-     diff = towards_target(vb);
 
-     if (!diff)
-             return;
+     spin_lock(&vb->adjustment_lock);
+     seqno = vb->adjustment_seqno;
+     spin_unlock(&vb->adjustment_lock);
 
-     if (diff > 0)
-             diff -= fill_balloon(vb, diff);
-     else
-             diff += leak_balloon(vb, -diff);
-     update_balloon_size(vb);
+     diff = towards_target(vb);
+
+     if (diff) {
+             if (diff > 0)
+                     diff -= fill_balloon(vb, diff);
+             else
+                     diff += leak_balloon(vb, -diff);
+             update_balloon_size(vb);
+     }
 
Quoted 7 lines

drivers/virtio/virtio_balloon.c static void update_balloon_size_func(struct work_struct *work) · line 527

Quoted 4 lines
drivers/virtio/virtio_balloon.c +2 -0
      if (diff)
              queue_work(system_freezable_wq, work);
+     else
+             end_update_balloon_size(vb, seqno);

What if we stop the workqueue and unload the driver -- see remove_common() -- won't you leave pm_stay_awake() wrongly set?

When a device gets removed, its wakeup source is destroyed, which automatically calls __pm_relax.

Quoted 26 lines

drivers/virtio/virtio_balloon.c static int virtballoon_probe(struct virtio_device *vdev) · line 1032

Quoted 9 lines
drivers/virtio/virtio_balloon.c +2 -0
  }
 
  static int init_vqs(struct virtio_balloon *vb)
@@ -992,6 +1028,9 @@ static int virtballoon_probe(struct virtio_device *vdev)
                      goto out_unregister_oom;
      }
 
+     spin_lock_init(&vb->adjustment_lock);
+     device_set_wakeup_capable(&vb->vdev->dev, true);

I'm a bit confused: Documentation/driver-api/pm/devices.rst documents

"
The :c:member:power.can_wakeup flag just records whether the device (and its driver) can physically support wakeup events. The :c:func:device_set_wakeup_capable() routine affects this flag. "

...

"
Whether or not a device is capable of issuing wakeup events is a hardware matter, and the kernel is responsible for keeping track of it. "

Quoted 5 lines

But how is the virtio-balloon device capable of waking up the machine? Your patch merely implies that the virtio-baloon device is capable to prohbit going to sleep.

What am I missing?

The underlying virtio_pci_device is capable of waking up the machine, if it supports PCI power management. The core PCI code will keep the machine awake while processing the interrupt (i.e. during virtballoon_changed), but after processing is handed off to the virtio-balloon driver, the virtio-balloon driver needs to keep the machine awake until the processing is actually completed.

An alternative to making vb->vdev->dev wakeup capable is to plumb the pm_stay_awake/pm_relax calls to the underlying virtio_pci_device. Would that be a preferable approach?

-David

#3 lore raw
Quoted 16 lines · David Stevens
Quoted 7 lines · David Hildenbrand
Quoted 2 lines · David Stevens

From: David Stevens <[email protected]>

Hi David,

Quoted 7 lines
Quoted 5 lines

Add a wakeup event for when the balloon is inflating or deflating. Userspace can enable this wakeup event to prevent the system from suspending while the balloon is being adjusted. This allows /sys/power/wakeup_count to be used without breaking virtio_balloon's cooperative memory management.

Can you add/share some more details

Quoted 10 lines

I'm working on enabling support for Linux s2Idle in our Android virtual machine, to restrict apps from running in the background without holding an Android partial wakelock. With the patch I recently sent out [1], since crosvm advertises native PCI power management for virtio devices, the Android guest can properly enter s2idle, and it can be woken up by incoming IO. However, one of the remaining problems is that when the host needs to reclaim memory from the guest via the virtio-balloon, there is nothing preventing the guest from entering s2idle before the balloon driver finishes returning memory to the host.

Thanks for the information. So you also want to wakeup the VM when wanting to get more memory from the VM?

Using which mechanism would that wakeup happen? Likely not the device itself?

Quoted 4 lines

One alternative to this approach would be to add a virtballoon_suspend callback to abort suspend if the balloon is inflating/adjusting. However, it seems cleaner to just prevent suspend in the first place.

Also, the PM notifier could also be used with very high priority, so the device would respond early to PM_SUSPEND_PREPARE.

[...]

Quoted 9 lines
Quoted 6 lines

Review

Quoted 3 lines
               queue_work(system_freezable_wq, work);
(quoted diff) +2 -0
+     else
+             end_update_balloon_size(vb, seqno);

What if we stop the workqueue and unload the driver -- see remove_common() -- won't you leave pm_stay_awake() wrongly set?

When a device gets removed, its wakeup source is destroyed, which automatically calls __pm_relax.

Ah, thanks.

Quoted 40 lines
Quoted 27 lines

drivers/virtio/virtio_balloon.c static int virtballoon_probe(struct virtio_device *vdev) · line 1032

Quoted 10 lines
   }

static int init_vqs(struct virtio_balloon *vb)

drivers/virtio/virtio_balloon.c +2 -0
@@ -992,6 +1028,9 @@ static int virtballoon_probe(struct virtio_device *vdev)
                       goto out_unregister_oom;
       }
 
+     spin_lock_init(&vb->adjustment_lock);
+     device_set_wakeup_capable(&vb->vdev->dev, true);

I'm a bit confused: Documentation/driver-api/pm/devices.rst documents

"
The :c:member:power.can_wakeup flag just records whether the device (and its driver) can physically support wakeup events. The :c:func:device_set_wakeup_capable() routine affects this flag. "

...

"
Whether or not a device is capable of issuing wakeup events is a hardware matter, and the kernel is responsible for keeping track of it. "

Quoted 5 lines

But how is the virtio-balloon device capable of waking up the machine? Your patch merely implies that the virtio-baloon device is capable to prohbit going to sleep.

What am I missing?

The underlying virtio_pci_device is capable of waking up the machine, if it supports PCI power management. The core PCI code will keep the machine awake while processing the interrupt (i.e. during virtballoon_changed), but after processing is handed off to the virtio-balloon driver, the virtio-balloon driver needs to keep the machine awake until the processing is actually completed.

Quoted 3 lines

An alternative to making vb->vdev->dev wakeup capable is to plumb the pm_stay_awake/pm_relax calls to the underlying virtio_pci_device. Would that be a preferable approach?

The way you describe it, it rather belongs into the PCI code, because that's what actually makes the device PM-capable (i.e., would not apply to virtio-ccw or virtio-mmio). The virtio-balloon itself is not PM-capable. But how hard is it to move that handling into PCI specific code?

Signature · 3 lines
Cheers,

David / dhildenb
#4 lore raw
Quoted 29 lines · David Hildenbrand
Quoted 16 lines · David Stevens
Quoted 7 lines · David Hildenbrand
Quoted 2 lines · David Stevens

From: David Stevens <[email protected]>

Hi David,

Quoted 7 lines
Quoted 5 lines

Add a wakeup event for when the balloon is inflating or deflating. Userspace can enable this wakeup event to prevent the system from suspending while the balloon is being adjusted. This allows /sys/power/wakeup_count to be used without breaking virtio_balloon's cooperative memory management.

Can you add/share some more details

Quoted 10 lines

I'm working on enabling support for Linux s2Idle in our Android virtual machine, to restrict apps from running in the background without holding an Android partial wakelock. With the patch I recently sent out [1], since crosvm advertises native PCI power management for virtio devices, the Android guest can properly enter s2idle, and it can be woken up by incoming IO. However, one of the remaining problems is that when the host needs to reclaim memory from the guest via the virtio-balloon, there is nothing preventing the guest from entering s2idle before the balloon driver finishes returning memory to the host.

Quoted 5 lines

Thanks for the information. So you also want to wakeup the VM when wanting to get more memory from the VM?

Using which mechanism would that wakeup happen? Likely not the device itself?

The wakeup would happen via the parent device's interrupt. I've sent a new version of this patch that uses the parent device's wakeup event instead of adding a new one.

Quoted 7 lines
Quoted 4 lines

One alternative to this approach would be to add a virtballoon_suspend callback to abort suspend if the balloon is inflating/adjusting. However, it seems cleaner to just prevent suspend in the first place.

Also, the PM notifier could also be used with very high priority, so the device would respond early to PM_SUSPEND_PREPARE.

One drawback of blocking suspend via a PM notifier is that the behavior isn't configurable by userspace, whereas wakeup events can be enabled/disabled by userspace.

-David

#5 lore raw
Quoted 45 lines · David Stevens
Quoted 29 lines · David Hildenbrand
Quoted 16 lines · David Stevens
Quoted 7 lines · David Hildenbrand
Quoted 2 lines · David Stevens

From: David Stevens <[email protected]>

Hi David,

Quoted 7 lines
Quoted 5 lines

Add a wakeup event for when the balloon is inflating or deflating. Userspace can enable this wakeup event to prevent the system from suspending while the balloon is being adjusted. This allows /sys/power/wakeup_count to be used without breaking virtio_balloon's cooperative memory management.

Can you add/share some more details

Quoted 10 lines

I'm working on enabling support for Linux s2Idle in our Android virtual machine, to restrict apps from running in the background without holding an Android partial wakelock. With the patch I recently sent out [1], since crosvm advertises native PCI power management for virtio devices, the Android guest can properly enter s2idle, and it can be woken up by incoming IO. However, one of the remaining problems is that when the host needs to reclaim memory from the guest via the virtio-balloon, there is nothing preventing the guest from entering s2idle before the balloon driver finishes returning memory to the host.

Quoted 5 lines

Thanks for the information. So you also want to wakeup the VM when wanting to get more memory from the VM?

Using which mechanism would that wakeup happen? Likely not the device itself?

The wakeup would happen via the parent device's interrupt. I've sent a new version of this patch that uses the parent device's wakeup event instead of adding a new one.

Quoted 5 lines
Quoted 4 lines

One alternative to this approach would be to add a virtballoon_suspend callback to abort suspend if the balloon is inflating/adjusting. However, it seems cleaner to just prevent suspend in the first place.

Quoted 6 lines
Quoted 2 lines

Also, the PM notifier could also be used with very high priority, so the device would respond early to PM_SUSPEND_PREPARE.

One drawback of blocking suspend via a PM notifier is that the behavior isn't configurable by userspace, whereas wakeup events can be enabled/disabled by userspace.

The question is if that behavior for the balloon is really worth it being configured by user space?

Signature · 3 lines
Cheers,

David / dhildenb
#6 lore raw
Quoted 50 lines · David Hildenbrand
Quoted 48 lines · David Stevens
Quoted 29 lines · David Hildenbrand
Quoted 16 lines · David Stevens
Quoted 7 lines · David Hildenbrand
Quoted 2 lines · David Stevens

From: David Stevens <[email protected]>

Hi David,

Quoted 7 lines
Quoted 5 lines

Add a wakeup event for when the balloon is inflating or deflating. Userspace can enable this wakeup event to prevent the system from suspending while the balloon is being adjusted. This allows /sys/power/wakeup_count to be used without breaking virtio_balloon's cooperative memory management.

Can you add/share some more details

Quoted 10 lines

I'm working on enabling support for Linux s2Idle in our Android virtual machine, to restrict apps from running in the background without holding an Android partial wakelock. With the patch I recently sent out [1], since crosvm advertises native PCI power management for virtio devices, the Android guest can properly enter s2idle, and it can be woken up by incoming IO. However, one of the remaining problems is that when the host needs to reclaim memory from the guest via the virtio-balloon, there is nothing preventing the guest from entering s2idle before the balloon driver finishes returning memory to the host.

Quoted 5 lines

Thanks for the information. So you also want to wakeup the VM when wanting to get more memory from the VM?

Using which mechanism would that wakeup happen? Likely not the device itself?

The wakeup would happen via the parent device's interrupt. I've sent a new version of this patch that uses the parent device's wakeup event instead of adding a new one.

Quoted 7 lines
Quoted 4 lines

One alternative to this approach would be to add a virtballoon_suspend callback to abort suspend if the balloon is inflating/adjusting. However, it seems cleaner to just prevent suspend in the first place.

Also, the PM notifier could also be used with very high priority, so the device would respond early to PM_SUSPEND_PREPARE.

Quoted 6 lines
Quoted 3 lines

One drawback of blocking suspend via a PM notifier is that the behavior isn't configurable by userspace, whereas wakeup events can be enabled/disabled by userspace.

The question is if that behavior for the balloon is really worth it being configured by user space?

It seems to me that the current behavior of completely resetting the virtio balloon in virtballoon_freeze is basically antithetical to the power management integration I'm adding, where power management doesn't interrupt the virtio balloon's operation at all. So if there are any systems that are actually happy with the current power management behavior, they probably don't want suspend to be blocked by inflation/deflation.

-David