Skip to contentSkip to conversation

cpuidle: add poll_source API and virtio vq polling

RFC In review Stefan Hajnoczi 4 participants 14 messages Jul 13, 2021 – Jul 27, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover cpuidle: add poll_source API and virtio vq polling

These patches are not polished yet but I would like request feedback on this approach and share performance results with you.

Idle CPUs tentatively enter a busy wait loop before halting when the cpuidle haltpoll driver is enabled inside a virtual machine. This reduces wakeup latency for events that occur soon after the vCPU becomes idle.

This patch series extends the cpuidle busy wait loop with the new poll_source API so drivers can participate in polling. Such polling-aware drivers disable their device's irq during the busy wait loop to avoid the cost of interrupts. This reduces latency further than regular cpuidle haltpoll, which still relies on irqs.

Virtio drivers are modified to use the poll_source API so all virtio device types get this feature. The following virtio-blk fio benchmark results show the improvement:

IOPS (numjobs=4, iodepth=1, 4 virtqueues)
before poll_source io_poll

4k randread    167102  186049 (+11%)  186654 (+11%)
4k randwrite   162204  181214 (+11%)  181850 (+12%)
4k randrw      159520  177071 (+11%)  177928 (+11%)

The comparison against io_poll shows that cpuidle poll_source achieves equivalent performance to the block layer's io_poll feature (which I implemented in a separate patch series [1]).

The advantage of poll_source is that applications do not need to explicitly set the RWF_HIPRI I/O request flag. The poll_source approach is attractive because few applications actually use RWF_HIPRI and it takes advantage of CPU cycles we would have spent in cpuidle haltpoll anyway.

The current series does not improve virtio-net. I haven't investigated deeply, but it is possible that NAPI and poll_source do not combine. See the final patch for a starting point on making the two work together.

I have not tried this on bare metal but it might help there too. The cost of disabling a device's irq must be less than the savings from avoiding irq handling for this optimization to make sense.

[1] https://lore.kernel.org/linux-block/[email protected]/

Stefan Hajnoczi (3):
cpuidle: add poll_source API
virtio: add poll_source virtqueue polling
softirq: participate in cpuidle polling

 drivers/cpuidle/Makefile           |   1 +
 drivers/virtio/virtio_pci_common.h |   7 ++
 include/linux/interrupt.h          |   2 +
 include/linux/poll_source.h        |  53 +++++++++++++++
 include/linux/virtio.h             |   2 +
 include/linux/virtio_config.h      |   2 +
 drivers/cpuidle/poll_source.c      | 102 +++++++++++++++++++++++++++++
 drivers/cpuidle/poll_state.c       |   6 ++
 drivers/virtio/virtio.c            |  34 ++++++++++
 drivers/virtio/virtio_pci_common.c |  86 ++++++++++++++++++++++++
 drivers/virtio/virtio_pci_modern.c |   2 +
 kernel/softirq.c                   |  14 ++++
 12 files changed, 311 insertions(+)
 create mode 100644 include/linux/poll_source.h
 create mode 100644 drivers/cpuidle/poll_source.c
Signature · 1 line
2.31.1
#1 lore raw

RFC 1/3 cpuidle: add poll_source API

Introduce an API for adding cpuidle poll callbacks:

  struct poll_source_ops {
      void (*start)(struct poll_source *src);
      void (*stop)(struct poll_source *src);
      void (*poll)(struct poll_source *src);
  };

  int poll_source_register(struct poll_source *src);
  int poll_source_unregister(struct poll_source *src);

When cpuidle enters the poll state it invokes ->start() and then invokes ->poll() repeatedly from the busy wait loop. Finally ->stop() is invoked when the busy wait loop finishes.

The ->poll() function should check for activity and cause TIF_NEED_RESCHED to be set in order to stop the busy wait loop.

This API is intended to be used by drivers that can cheaply poll for events. Participating in cpuidle polling allows them to avoid interrupt latencies during periods where the CPU is going to poll anyway.

Note that each poll_source is bound to a particular CPU. The API is mainly intended to by used by drivers that have multiple queues with irq affinity.

---
 drivers/cpuidle/Makefile      |  1 +
 include/linux/poll_source.h   | 53 +++++++++++++++++++
 drivers/cpuidle/poll_source.c | 99 +++++++++++++++++++++++++++++++++++
 drivers/cpuidle/poll_state.c  |  6 +++
 4 files changed, 159 insertions(+)
 create mode 100644 include/linux/poll_source.h
 create mode 100644 drivers/cpuidle/poll_source.c
drivers/cpuidle/Makefile +1 -0
index 26bbc5e74123..994f72d6fe95 100644
@@ -7,6 +7,7 @@ obj-y += cpuidle.o driver.o governor.o sysfs.o governors/
 obj-$(CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED) += coupled.o
 obj-$(CONFIG_DT_IDLE_STATES)		  += dt_idle_states.o
 obj-$(CONFIG_ARCH_HAS_CPU_RELAX)	  += poll_state.o
+obj-$(CONFIG_ARCH_HAS_CPU_RELAX)	  += poll_source.o
 obj-$(CONFIG_HALTPOLL_CPUIDLE)		  += cpuidle-haltpoll.o
 
 ##################################################################################
include/linux/poll_source.h new file +53 -0

53 lines not on this page · raw message

drivers/cpuidle/poll_source.c new file +99 -0

99 lines not on this page · raw message

drivers/cpuidle/poll_state.c +6 -0
index f7e83613ae94..aa26870034ac 100644
@@ -7,6 +7,7 @@
 #include <linux/sched.h>
 #include <linux/sched/clock.h>
 #include <linux/sched/idle.h>
+#include <linux/poll_source.h>
 
 #define POLL_IDLE_RELAX_COUNT	200
 
@@ -22,9 +23,12 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev,
 		unsigned int loop_count = 0;
 		u64 limit;
 
+		poll_source_start();
+
 		limit = cpuidle_poll_time(drv, dev);
 
 		while (!need_resched()) {
+			poll_source_run_once();
 			cpu_relax();
 			if (loop_count++ < POLL_IDLE_RELAX_COUNT)
 				continue;
@@ -35,6 +39,8 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev,
 				break;
 			}
 		}
+
+		poll_source_stop();
 	}
 	current_clr_polling();
 
Signature · 1 line
2.31.1
#2 lore raw

Re: [RFC 1/3] cpuidle: add poll_source API

Hi Stefan,

Quoted 159 lines · Stefan Hajnoczi

Introduce an API for adding cpuidle poll callbacks:

  struct poll_source_ops {
      void (*start)(struct poll_source *src);
      void (*stop)(struct poll_source *src);
      void (*poll)(struct poll_source *src);
  };

  int poll_source_register(struct poll_source *src);
  int poll_source_unregister(struct poll_source *src);

When cpuidle enters the poll state it invokes ->start() and then invokes ->poll() repeatedly from the busy wait loop. Finally ->stop() is invoked when the busy wait loop finishes.

The ->poll() function should check for activity and cause TIF_NEED_RESCHED to be set in order to stop the busy wait loop.

This API is intended to be used by drivers that can cheaply poll for events. Participating in cpuidle polling allows them to avoid interrupt latencies during periods where the CPU is going to poll anyway.

Note that each poll_source is bound to a particular CPU. The API is mainly intended to by used by drivers that have multiple queues with irq affinity.

---
 drivers/cpuidle/Makefile      |  1 +
 include/linux/poll_source.h   | 53 +++++++++++++++++++
 drivers/cpuidle/poll_source.c | 99 +++++++++++++++++++++++++++++++++++
 drivers/cpuidle/poll_state.c  |  6 +++
 4 files changed, 159 insertions(+)
 create mode 100644 include/linux/poll_source.h
 create mode 100644 drivers/cpuidle/poll_source.c
drivers/cpuidle/Makefile +1 -0
index 26bbc5e74123..994f72d6fe95 100644
@@ -7,6 +7,7 @@ obj-y += cpuidle.o driver.o governor.o sysfs.o governors/
 obj-$(CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED) += coupled.o
 obj-$(CONFIG_DT_IDLE_STATES)		  += dt_idle_states.o
 obj-$(CONFIG_ARCH_HAS_CPU_RELAX)	  += poll_state.o
+obj-$(CONFIG_ARCH_HAS_CPU_RELAX)	  += poll_source.o
 obj-$(CONFIG_HALTPOLL_CPUIDLE)		  += cpuidle-haltpoll.o
 
 ##################################################################################
include/linux/poll_source.h new file +53 -0
new file mode 100644
index 000000000000..ccfb424e170b
@@ -0,0 +1,53 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * poll_source.h - cpuidle busy waiting API
+ */
+#ifndef __LINUX_POLLSOURCE_H__
+#define __LINUX_POLLSOURCE_H__
+
+#include <linux/list.h>
+
+struct poll_source;
+
+struct poll_source_ops {
+	void (*start)(struct poll_source *src);
+	void (*stop)(struct poll_source *src);
+	void (*poll)(struct poll_source *src);
+};
+
+struct poll_source {
+	const struct poll_source_ops *ops;
+	struct list_head node;
+	int cpu;
+};
+
+/**
+ * poll_source_register - Add a poll_source for a CPU
+ */
+#if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX)
+int poll_source_register(struct poll_source *src);
+#else
+static inline int poll_source_register(struct poll_source *src)
+{
+	return 0;
+}
+#endif
+
+/**
+ * poll_source_unregister - Remove a previously registered poll_source
+ */
+#if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX)
+int poll_source_unregister(struct poll_source *src);
+#else
+static inline int poll_source_unregister(struct poll_source *src)
+{
+	return 0;
+}
+#endif
+
+/* Used by the cpuidle driver */
+void poll_source_start(void);
+void poll_source_run_once(void);
+void poll_source_stop(void);
+
+#endif /* __LINUX_POLLSOURCE_H__ */
drivers/cpuidle/poll_source.c new file +46 -0
new file mode 100644
index 000000000000..46100e5a71e4
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * poll_source.c - cpuidle busy waiting API
+ */
+
+#include <linux/lockdep.h>
+#include <linux/percpu.h>
+#include <linux/poll_source.h>
+
+/* The per-cpu list of registered poll sources */
+DEFINE_PER_CPU(struct list_head, poll_source_list);
+
+/* Called from idle task with TIF_POLLING_NRFLAG set and irqs enabled */
+void poll_source_start(void)
+{
+	struct poll_source *src;
+
+	list_for_each_entry(src, this_cpu_ptr(&poll_source_list), node)
+		src->ops->start(src);
+}
+
+/* Called from idle task with TIF_POLLING_NRFLAG set and irqs enabled */
+void poll_source_run_once(void)
+{
+	struct poll_source *src;
+
+	list_for_each_entry(src, this_cpu_ptr(&poll_source_list), node)
+		src->ops->poll(src);
+}
+
+/* Called from idle task with TIF_POLLING_NRFLAG set and irqs enabled */
+void poll_source_stop(void)
+{
+	struct poll_source *src;
+
+	list_for_each_entry(src, this_cpu_ptr(&poll_source_list), node)
+		src->ops->stop(src);
+}
+
+static void poll_source_register_this_cpu(void *opaque)
+{
+	struct poll_source *src = opaque;
+
+	lockdep_assert_irqs_disabled();
+
+	list_add_tail(&src->node, this_cpu_ptr(&poll_source_list));

drivers/cpuidle/poll_source.c line 56

Quoted 10 lines
drivers/cpuidle/poll_source.c new file +10 -0
+}
+
+int poll_source_register(struct poll_source *src)
+{
+	if (!list_empty(&src->node))
+		return -EBUSY;
+
+	/*
+	 * There is no race with src->cpu iterating over poll_source_list
+	 * because smp_call_function_single() just sets TIF_NEED_RESCHED

Hum... what about

CPU-0 CPU-1

poll_source_start poll_source_register IPI'ing CPU-0

Perhaps a llist can be used?

Quoted 2 lines
              while (!need_resched()) {

+ poll_source_run_once();

Probably want to use static_key's for ->start, ->stop and ->poll?

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#3 lore raw

Re: [RFC 1/3] cpuidle: add poll_source API

Quoted 175 lines · Marcelo Tosatti

Hi Stefan,

Quoted 159 lines · Stefan Hajnoczi

Introduce an API for adding cpuidle poll callbacks:

  struct poll_source_ops {
      void (*start)(struct poll_source *src);
      void (*stop)(struct poll_source *src);
      void (*poll)(struct poll_source *src);
  };

  int poll_source_register(struct poll_source *src);
  int poll_source_unregister(struct poll_source *src);

When cpuidle enters the poll state it invokes ->start() and then invokes ->poll() repeatedly from the busy wait loop. Finally ->stop() is invoked when the busy wait loop finishes.

The ->poll() function should check for activity and cause TIF_NEED_RESCHED to be set in order to stop the busy wait loop.

This API is intended to be used by drivers that can cheaply poll for events. Participating in cpuidle polling allows them to avoid interrupt latencies during periods where the CPU is going to poll anyway.

Note that each poll_source is bound to a particular CPU. The API is mainly intended to by used by drivers that have multiple queues with irq affinity.

---
 drivers/cpuidle/Makefile      |  1 +
 include/linux/poll_source.h   | 53 +++++++++++++++++++
 drivers/cpuidle/poll_source.c | 99 +++++++++++++++++++++++++++++++++++
 drivers/cpuidle/poll_state.c  |  6 +++
 4 files changed, 159 insertions(+)
 create mode 100644 include/linux/poll_source.h
 create mode 100644 drivers/cpuidle/poll_source.c
drivers/cpuidle/Makefile +1 -0
index 26bbc5e74123..994f72d6fe95 100644
@@ -7,6 +7,7 @@ obj-y += cpuidle.o driver.o governor.o sysfs.o governors/
 obj-$(CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED) += coupled.o
 obj-$(CONFIG_DT_IDLE_STATES)		  += dt_idle_states.o
 obj-$(CONFIG_ARCH_HAS_CPU_RELAX)	  += poll_state.o
+obj-$(CONFIG_ARCH_HAS_CPU_RELAX)	  += poll_source.o
 obj-$(CONFIG_HALTPOLL_CPUIDLE)		  += cpuidle-haltpoll.o
 
 ##################################################################################
include/linux/poll_source.h new file +53 -0
new file mode 100644
index 000000000000..ccfb424e170b
@@ -0,0 +1,53 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * poll_source.h - cpuidle busy waiting API
+ */
+#ifndef __LINUX_POLLSOURCE_H__
+#define __LINUX_POLLSOURCE_H__
+
+#include <linux/list.h>
+
+struct poll_source;
+
+struct poll_source_ops {
+	void (*start)(struct poll_source *src);
+	void (*stop)(struct poll_source *src);
+	void (*poll)(struct poll_source *src);
+};
+
+struct poll_source {
+	const struct poll_source_ops *ops;
+	struct list_head node;
+	int cpu;
+};
+
+/**
+ * poll_source_register - Add a poll_source for a CPU
+ */
+#if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX)
+int poll_source_register(struct poll_source *src);
+#else
+static inline int poll_source_register(struct poll_source *src)
+{
+	return 0;
+}
+#endif
+
+/**
+ * poll_source_unregister - Remove a previously registered poll_source
+ */
+#if defined(CONFIG_CPU_IDLE) && defined(CONFIG_ARCH_HAS_CPU_RELAX)
+int poll_source_unregister(struct poll_source *src);
+#else
+static inline int poll_source_unregister(struct poll_source *src)
+{
+	return 0;
+}
+#endif
+
+/* Used by the cpuidle driver */
+void poll_source_start(void);
+void poll_source_run_once(void);
+void poll_source_stop(void);
+
+#endif /* __LINUX_POLLSOURCE_H__ */
drivers/cpuidle/poll_source.c new file +46 -0
new file mode 100644
index 000000000000..46100e5a71e4
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * poll_source.c - cpuidle busy waiting API
+ */
+
+#include <linux/lockdep.h>
+#include <linux/percpu.h>
+#include <linux/poll_source.h>
+
+/* The per-cpu list of registered poll sources */
+DEFINE_PER_CPU(struct list_head, poll_source_list);
+
+/* Called from idle task with TIF_POLLING_NRFLAG set and irqs enabled */
+void poll_source_start(void)
+{
+	struct poll_source *src;
+
+	list_for_each_entry(src, this_cpu_ptr(&poll_source_list), node)
+		src->ops->start(src);
+}
+
+/* Called from idle task with TIF_POLLING_NRFLAG set and irqs enabled */
+void poll_source_run_once(void)
+{
+	struct poll_source *src;
+
+	list_for_each_entry(src, this_cpu_ptr(&poll_source_list), node)
+		src->ops->poll(src);
+}
+
+/* Called from idle task with TIF_POLLING_NRFLAG set and irqs enabled */
+void poll_source_stop(void)
+{
+	struct poll_source *src;
+
+	list_for_each_entry(src, this_cpu_ptr(&poll_source_list), node)
+		src->ops->stop(src);
+}
+
+static void poll_source_register_this_cpu(void *opaque)
+{
+	struct poll_source *src = opaque;
+
+	lockdep_assert_irqs_disabled();
+
+	list_add_tail(&src->node, this_cpu_ptr(&poll_source_list));

drivers/cpuidle/poll_source.c line 56

Quoted 10 lines
drivers/cpuidle/poll_source.c new file +10 -0
+}
+
+int poll_source_register(struct poll_source *src)
+{
+	if (!list_empty(&src->node))
+		return -EBUSY;
+
+	/*
+	 * There is no race with src->cpu iterating over poll_source_list
+	 * because smp_call_function_single() just sets TIF_NEED_RESCHED

Hum... what about

Quoted 5 lines

CPU-0 CPU-1

poll_source_start poll_source_register IPI'ing CPU-0

Perhaps a llist can be used?

The simplest solution might be queue_work_on(src->cpu, system_hipri_wq, &work) plus a completion. That way we know poll_source_start/stop/run() are not executing and there can be no re-entrancy issues. The nice thing is it's simple - no low-level tricks.

Those are the semantics I wanted with smp_call_function_single() but I think I chose the wrong API :).

Quoted 4 lines
Quoted 2 lines
              while (!need_resched()) {

+ poll_source_run_once();

Probably want to use static_key's for ->start, ->stop and ->poll?

Good idea, thanks.

Stefan

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#4 lore raw

RFC 2/3 virtio: add poll_source virtqueue polling

In reply to Stefan Hajnoczi

VIRTIO drivers can cheaply disable interrupts by setting RING_EVENT_FLAGS_DISABLE in the packed virtqueues's Driver Event Suppression structure. See "2.7.10 Driver and Device Event Suppression" in the VIRTIO 1.1 specification for details
(https://docs.oasis-open.org/virtio/virtio/v1.1/virtio-v1.1.html).

Add a per-virtqueue poll_source that disables events in ->start(), processes completed virtqueue buffers in ->poll(), and re-enables events in ->stop().

This optimization avoids interrupt injection during cpuidle polling. Workloads that submit requests and then halt the vCPU until completion benefit from this.

To enable this optimization:

1. Enable the cpuidle haltpoll driver:
https://www.kernel.org/doc/html/latest/virt/guest-halt-polling.html

2. Enable poll_source on the virtio device:

   # echo -n 1 > /sys/block/vda/device/poll_source

Note that this feature currently as no effect on split virtqueues when VIRTIO_F_EVENT_IDX is negotiated. It may be possible to tweak virtqueue_disable_cb_split() but I haven't attempted it here.

This optimization has been benchmarked successfully with virtio-blk devices. Currently it does not improve virtio-net performance, probably because it doesn't combine with NAPI polling.

---
 drivers/virtio/virtio_pci_common.h |  7 +++
 include/linux/virtio.h             |  2 +
 include/linux/virtio_config.h      |  2 +
 drivers/virtio/virtio.c            | 34 ++++++++++++
 drivers/virtio/virtio_pci_common.c | 86 ++++++++++++++++++++++++++++++
 drivers/virtio/virtio_pci_modern.c |  2 +
 6 files changed, 133 insertions(+)
drivers/virtio/virtio_pci_common.h +7 -0
index beec047a8f8d..630746043183 100644
@@ -21,6 +21,7 @@
 #include <linux/pci.h>
 #include <linux/slab.h>
 #include <linux/interrupt.h>
+#include <linux/poll_source.h>
 #include <linux/virtio.h>
 #include <linux/virtio_config.h>
 #include <linux/virtio_ring.h>
@@ -38,6 +39,9 @@ struct virtio_pci_vq_info {
 
 	/* MSI-X vector (or none) */
 	unsigned msix_vector;
+
+	/* the cpuidle poll_source */
+	struct poll_source poll_source;
 };
 
 /* Our device structure */
@@ -102,6 +106,9 @@ static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
 	return container_of(vdev, struct virtio_pci_device, vdev);
 }
 
+/* enable poll_source API for vq polling */
+int vp_enable_poll_source(struct virtio_device *vdev, bool enable);
+
 /* wait for pending irq handlers */
 void vp_synchronize_vectors(struct virtio_device *vdev);
 /* the notify function used when creating a virt queue */
include/linux/virtio.h +2 -0
index b1894e0323fa..baaa3c8fadb1 100644
@@ -93,6 +93,7 @@ dma_addr_t virtqueue_get_used_addr(struct virtqueue *vq);
  * virtio_device - representation of a device using virtio
  * @index: unique position on the virtio bus
  * @failed: saved value for VIRTIO_CONFIG_S_FAILED bit (for restore)
+ * @poll_source_enabled: poll_source API enabled for vq polling
  * @config_enabled: configuration change reporting enabled
  * @config_change_pending: configuration change reported while disabled
  * @config_lock: protects configuration change reporting
@@ -107,6 +108,7 @@ dma_addr_t virtqueue_get_used_addr(struct virtqueue *vq);
 struct virtio_device {
 	int index;
 	bool failed;
+	bool poll_source_enabled;
 	bool config_enabled;
 	bool config_change_pending;
 	spinlock_t config_lock;
include/linux/virtio_config.h +2 -0
index 8519b3ae5d52..5fb78d56fd44 100644
@@ -72,6 +72,7 @@ struct virtio_shm_region {
  * @set_vq_affinity: set the affinity for a virtqueue (optional).
  * @get_vq_affinity: get the affinity for a virtqueue (optional).
  * @get_shm_region: get a shared memory region based on the index.
+ * @enable_poll_source: enable/disable poll_source API vq polling (optional).
  */
 typedef void vq_callback_t(struct virtqueue *);
 struct virtio_config_ops {
@@ -97,6 +98,7 @@ struct virtio_config_ops {
 			int index);
 	bool (*get_shm_region)(struct virtio_device *vdev,
 			       struct virtio_shm_region *region, u8 id);
+	int (*enable_poll_source)(struct virtio_device *dev, bool enable);
 };
 
 /* If driver didn't advertise the feature, it will never appear. */
drivers/virtio/virtio.c +34 -0
index 4b15c00c0a0a..22fee71bbe34 100644
@@ -59,12 +59,44 @@ static ssize_t features_show(struct device *_d,
 }
 static DEVICE_ATTR_RO(features);
 
+static ssize_t poll_source_show(struct device *_d,
+				struct device_attribute *attr, char *buf)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	return sprintf(buf, "%d\n", dev->poll_source_enabled);
+}
+
+static ssize_t poll_source_store(struct device *_d,
+		                 struct device_attribute *attr,
+				 const char *buf, size_t count)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	bool val;
+	int rc;
+
+	rc = kstrtobool(buf, &val);
+	if (rc)
+		return rc;
+
+	if (val == dev->poll_source_enabled)
+		return count;
+	if (!dev->config->enable_poll_source)
+		return -ENOTSUPP;
+
+	rc = dev->config->enable_poll_source(dev, val);
+	if (rc)
+		return rc;
+	return count;
+}
+static DEVICE_ATTR_RW(poll_source);
+
 static struct attribute *virtio_dev_attrs[] = {
 	&dev_attr_device.attr,
 	&dev_attr_vendor.attr,
 	&dev_attr_status.attr,
 	&dev_attr_modalias.attr,
 	&dev_attr_features.attr,
+	&dev_attr_poll_source.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(virtio_dev);
@@ -343,6 +375,8 @@ int register_virtio_device(struct virtio_device *dev)
 	dev->index = err;
 	dev_set_name(&dev->dev, "virtio%u", dev->index);
 
+	dev->poll_source_enabled = false;
+
 	spin_lock_init(&dev->config_lock);
 	dev->config_enabled = false;
 	dev->config_change_pending = false;
drivers/virtio/virtio_pci_common.c +86 -0

116 lines not on this page · raw message

drivers/virtio/virtio_pci_modern.c +2 -0
index 30654d3a0b41..417d568590f2 100644
@@ -394,6 +394,7 @@ static const struct virtio_config_ops virtio_pci_config_nodev_ops = {
 	.set_vq_affinity = vp_set_vq_affinity,
 	.get_vq_affinity = vp_get_vq_affinity,
 	.get_shm_region  = vp_get_shm_region,
+	.enable_poll_source = vp_enable_poll_source,
 };
 
 static const struct virtio_config_ops virtio_pci_config_ops = {
@@ -411,6 +412,7 @@ static const struct virtio_config_ops virtio_pci_config_ops = {
 	.set_vq_affinity = vp_set_vq_affinity,
 	.get_vq_affinity = vp_get_vq_affinity,
 	.get_shm_region  = vp_get_shm_region,
+	.enable_poll_source = vp_enable_poll_source,
 };
 
 /* the PCI probing function */
Signature · 1 line
2.31.1
#5 lore raw

RFC 3/3 softirq: participate in cpuidle polling

In reply to Stefan Hajnoczi

Normally softirqs are invoked when exiting irqs. When polling in the cpuidle driver there may be no irq. Therefore pending softirqs go unnoticed and polling continues without invoking them.

Add a softirq_poll() function to explicitly check for and invoke softirqs.

---
This commit is not needed for virtio-blk. I added it when I realized virtio-net's NAPI scheduling might not be detected by the cpuidle busy wait loop because it is unaware of softirqs. However, even after doing this virtio-net's NAPI polling doesn't combine with cpuidle haltpoll.

Perhaps this patch is still desirable for cpuidle poll_state in case a softirq is raised?

---
 include/linux/interrupt.h     |  2 ++
 drivers/cpuidle/poll_source.c |  3 +++
 kernel/softirq.c              | 14 ++++++++++++++
 3 files changed, 19 insertions(+)
include/linux/interrupt.h +2 -0
index 4777850a6dc7..9bfdcc466ba8 100644
@@ -573,6 +573,8 @@ struct softirq_action
 asmlinkage void do_softirq(void);
 asmlinkage void __do_softirq(void);
 
+extern void softirq_poll(void);
+
 extern void open_softirq(int nr, void (*action)(struct softirq_action *));
 extern void softirq_init(void);
 extern void __raise_softirq_irqoff(unsigned int nr);
drivers/cpuidle/poll_source.c +3 -0
index 46100e5a71e4..ed200feb0daa 100644
@@ -6,6 +6,7 @@
 #include <linux/lockdep.h>
 #include <linux/percpu.h>
 #include <linux/poll_source.h>
+#include <linux/interrupt.h>
 
 /* The per-cpu list of registered poll sources */
 DEFINE_PER_CPU(struct list_head, poll_source_list);
@@ -26,6 +27,8 @@ void poll_source_run_once(void)
 
 	list_for_each_entry(src, this_cpu_ptr(&poll_source_list), node)
 		src->ops->poll(src);
+
+	softirq_poll();
 }
 
 /* Called from idle task with TIF_POLLING_NRFLAG set and irqs enabled */
kernel/softirq.c +14 -0
index 4992853ef53d..f45bf0204218 100644
@@ -611,6 +611,20 @@ void irq_enter(void)
 	irq_enter_rcu();
 }
 
+/**
+ * softirq_poll() - invoke pending softirqs
+ *
+ * Normally it is not necessary to explicitly poll for softirqs, but in the
+ * cpuidle driver a polling function may have raised a softirq with no irq exit
+ * to invoke it. Therefore it is necessary to poll for pending softirqs and
+ * invoke them explicitly.
+ */
+void softirq_poll(void)
+{
+	if (!in_interrupt() && local_softirq_pending())
+		invoke_softirq();
+}
+
 static inline void tick_irq_exit(void)
 {
 #ifdef CONFIG_NO_HZ_COMMON
Signature · 1 line
2.31.1
#6 lore raw
In reply to Stefan Hajnoczi
Quoted 36 lines · Stefan Hajnoczi

These patches are not polished yet but I would like request feedback on this approach and share performance results with you.

Idle CPUs tentatively enter a busy wait loop before halting when the cpuidle haltpoll driver is enabled inside a virtual machine. This reduces wakeup latency for events that occur soon after the vCPU becomes idle.

This patch series extends the cpuidle busy wait loop with the new poll_source API so drivers can participate in polling. Such polling-aware drivers disable their device's irq during the busy wait loop to avoid the cost of interrupts. This reduces latency further than regular cpuidle haltpoll, which still relies on irqs.

Virtio drivers are modified to use the poll_source API so all virtio device types get this feature. The following virtio-blk fio benchmark results show the improvement:

IOPS (numjobs=4, iodepth=1, 4 virtqueues)
before poll_source io_poll

4k randread    167102  186049 (+11%)  186654 (+11%)
4k randwrite   162204  181214 (+11%)  181850 (+12%)
4k randrw      159520  177071 (+11%)  177928 (+11%)

The comparison against io_poll shows that cpuidle poll_source achieves equivalent performance to the block layer's io_poll feature (which I implemented in a separate patch series [1]).

The advantage of poll_source is that applications do not need to explicitly set the RWF_HIPRI I/O request flag. The poll_source approach is attractive because few applications actually use RWF_HIPRI and it takes advantage of CPU cycles we would have spent in cpuidle haltpoll anyway.

The current series does not improve virtio-net. I haven't investigated deeply, but it is possible that NAPI and poll_source do not combine. See the final patch for a starting point on making the two work together.

Quoted 5 lines

I have not tried this on bare metal but it might help there too. The cost of disabling a device's irq must be less than the savings from avoiding irq handling for this optimization to make sense.

[1] https://lore.kernel.org/linux-block/[email protected]/

Hi Stefan:

Some questions:

1) What's the advantages of introducing polling at virtio level instead of doing it at each subsystems? Polling in virtio level may only work well if all (or most) of the devices are virtio
2) What's the advantages of using cpuidle instead of using a thread (and leverage the scheduler)?
3) Any reason it's virtio_pci specific not a general virtio one?

Thanks

(Btw, do we need to cc scheduler guys?)

Quoted 19 lines

Stefan Hajnoczi (3):
cpuidle: add poll_source API
virtio: add poll_source virtqueue polling
softirq: participate in cpuidle polling

  drivers/cpuidle/Makefile           |   1 +
  drivers/virtio/virtio_pci_common.h |   7 ++
  include/linux/interrupt.h          |   2 +
  include/linux/poll_source.h        |  53 +++++++++++++++
  include/linux/virtio.h             |   2 +
  include/linux/virtio_config.h      |   2 +
  drivers/cpuidle/poll_source.c      | 102 +++++++++++++++++++++++++++++
  drivers/cpuidle/poll_state.c       |   6 ++
  drivers/virtio/virtio.c            |  34 ++++++++++
  drivers/virtio/virtio_pci_common.c |  86 ++++++++++++++++++++++++
  drivers/virtio/virtio_pci_modern.c |   2 +
  kernel/softirq.c                   |  14 ++++
  12 files changed, 311 insertions(+)
Quoted 3 lines

create mode 100644 include/linux/poll_source.h create mode 100644 drivers/cpuidle/poll_source.c

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#7 lore raw
Quoted 45 lines · Jason Wang
Quoted 36 lines · Stefan Hajnoczi

These patches are not polished yet but I would like request feedback on this approach and share performance results with you.

Idle CPUs tentatively enter a busy wait loop before halting when the cpuidle haltpoll driver is enabled inside a virtual machine. This reduces wakeup latency for events that occur soon after the vCPU becomes idle.

This patch series extends the cpuidle busy wait loop with the new poll_source API so drivers can participate in polling. Such polling-aware drivers disable their device's irq during the busy wait loop to avoid the cost of interrupts. This reduces latency further than regular cpuidle haltpoll, which still relies on irqs.

Virtio drivers are modified to use the poll_source API so all virtio device types get this feature. The following virtio-blk fio benchmark results show the improvement:

IOPS (numjobs=4, iodepth=1, 4 virtqueues)
before poll_source io_poll

4k randread    167102  186049 (+11%)  186654 (+11%)
4k randwrite   162204  181214 (+11%)  181850 (+12%)
4k randrw      159520  177071 (+11%)  177928 (+11%)

The comparison against io_poll shows that cpuidle poll_source achieves equivalent performance to the block layer's io_poll feature (which I implemented in a separate patch series [1]).

The advantage of poll_source is that applications do not need to explicitly set the RWF_HIPRI I/O request flag. The poll_source approach is attractive because few applications actually use RWF_HIPRI and it takes advantage of CPU cycles we would have spent in cpuidle haltpoll anyway.

The current series does not improve virtio-net. I haven't investigated deeply, but it is possible that NAPI and poll_source do not combine. See the final patch for a starting point on making the two work together.

Quoted 5 lines

I have not tried this on bare metal but it might help there too. The cost of disabling a device's irq must be less than the savings from avoiding irq handling for this optimization to make sense.

[1] https://lore.kernel.org/linux-block/[email protected]/

Quoted 7 lines

Hi Stefan:

Some questions:

1) What's the advantages of introducing polling at virtio level instead of doing it at each subsystems? Polling in virtio level may only work well if all (or most) of the devices are virtio

I'm not sure I understand the question. cpuidle haltpoll benefits all devices today, except it incurs interrupt latency. The poll_source API eliminates the interrupt latency for drivers that can disable device interrupts cheaply.

This patch adds poll_source to core virtio code so that all virtio drivers get this feature for free. No driver-specific changes are needed.

If you mean networking, block layer, etc by "subsystems" then there's nothing those subsystems can do to help. Whether poll_source can be used depends on the specific driver, not the subsystem. If you consider drivers/virtio/ a subsystem, then that's exactly what the patch series is doing.

Quoted 2 lines

2) What's the advantages of using cpuidle instead of using a thread (and leverage the scheduler)?

In order to combine with the existing cpuidle infrastructure. No new polling loop is introduced and no additional CPU cycles are spent on polling.

If cpuidle itself is converted to threads then poll_source would automatically operate in a thread too, but this patch series doesn't change how the core cpuidle code works.

Quoted 1 line

3) Any reason it's virtio_pci specific not a general virtio one?

Good idea, it is possible to move the virtio_pci changes into virtio.c.

Other transports can't use this feature yet though. Only virtio_pci supports vq irq affinity. But the code can be generic and if other transports ever support vq irq affinity they'll get it for free.

Quoted 1 line

(Btw, do we need to cc scheduler guys?)

I'm not sure. This patch series doesn't change how cpuidle interacts with the scheduler. The cpuidle maintainers can pull in more people, if necessary.

Stefan

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#8 lore raw
Quoted 60 lines · Stefan Hajnoczi
Quoted 43 lines · Jason Wang
Quoted 36 lines · Stefan Hajnoczi

These patches are not polished yet but I would like request feedback on this approach and share performance results with you.

Idle CPUs tentatively enter a busy wait loop before halting when the cpuidle haltpoll driver is enabled inside a virtual machine. This reduces wakeup latency for events that occur soon after the vCPU becomes idle.

This patch series extends the cpuidle busy wait loop with the new poll_source API so drivers can participate in polling. Such polling-aware drivers disable their device's irq during the busy wait loop to avoid the cost of interrupts. This reduces latency further than regular cpuidle haltpoll, which still relies on irqs.

Virtio drivers are modified to use the poll_source API so all virtio device types get this feature. The following virtio-blk fio benchmark results show the improvement:

IOPS (numjobs=4, iodepth=1, 4 virtqueues)
before poll_source io_poll

4k randread    167102  186049 (+11%)  186654 (+11%)
4k randwrite   162204  181214 (+11%)  181850 (+12%)
4k randrw      159520  177071 (+11%)  177928 (+11%)

The comparison against io_poll shows that cpuidle poll_source achieves equivalent performance to the block layer's io_poll feature (which I implemented in a separate patch series [1]).

The advantage of poll_source is that applications do not need to explicitly set the RWF_HIPRI I/O request flag. The poll_source approach is attractive because few applications actually use RWF_HIPRI and it takes advantage of CPU cycles we would have spent in cpuidle haltpoll anyway.

The current series does not improve virtio-net. I haven't investigated deeply, but it is possible that NAPI and poll_source do not combine. See the final patch for a starting point on making the two work together.

Quoted 5 lines

I have not tried this on bare metal but it might help there too. The cost of disabling a device's irq must be less than the savings from avoiding irq handling for this optimization to make sense.

[1] https://lore.kernel.org/linux-block/[email protected]/

Quoted 7 lines

Hi Stefan:

Some questions:

1) What's the advantages of introducing polling at virtio level instead of doing it at each subsystems? Polling in virtio level may only work well if all (or most) of the devices are virtio

I'm not sure I understand the question. cpuidle haltpoll benefits all devices today, except it incurs interrupt latency. The poll_source API eliminates the interrupt latency for drivers that can disable device interrupts cheaply.

This patch adds poll_source to core virtio code so that all virtio drivers get this feature for free. No driver-specific changes are needed.

Quoted 5 lines

If you mean networking, block layer, etc by "subsystems" then there's nothing those subsystems can do to help. Whether poll_source can be used depends on the specific driver, not the subsystem. If you consider drivers/virtio/ a subsystem, then that's exactly what the patch series is doing.

I meant, if we choose to use idle poll, we have some several choices:

1) bus level (e.g the virtio)
2) subsystem level (e.g the networking and block)

I'm not sure which one is better.

Quoted 10 lines
Quoted 3 lines

2) What's the advantages of using cpuidle instead of using a thread (and leverage the scheduler)?

In order to combine with the existing cpuidle infrastructure. No new polling loop is introduced and no additional CPU cycles are spent on polling.

If cpuidle itself is converted to threads then poll_source would automatically operate in a thread too, but this patch series doesn't change how the core cpuidle code works.

So networking subsystem can use NAPI busy polling in the process context which means it can be leveraged by the scheduler.

I'm not sure it's a good idea to poll drivers for a specific bus in the general cpu idle layer.

Another questions, are those numbers measured by APICV capable machine?

Virtio-net turns on the tx interrupts since 2 years ago. And we don't see too much difference when measured with a APICV host.

Quoted 7 lines
Quoted 2 lines

3) Any reason it's virtio_pci specific not a general virtio one?

Good idea, it is possible to move the virtio_pci changes into virtio.c.

Other transports can't use this feature yet though. Only virtio_pci supports vq irq affinity. But the code can be generic and if other transports ever support vq irq affinity they'll get it for free.

Yes.

Thanks

Quoted 7 lines
Quoted 2 lines

(Btw, do we need to cc scheduler guys?)

I'm not sure. This patch series doesn't change how cpuidle interacts with the scheduler. The cpuidle maintainers can pull in more people, if necessary.

Stefan

#9 lore raw
Quoted 69 lines · Jason Wang
Quoted 60 lines · Stefan Hajnoczi
Quoted 43 lines · Jason Wang
Quoted 36 lines · Stefan Hajnoczi

These patches are not polished yet but I would like request feedback on this approach and share performance results with you.

Idle CPUs tentatively enter a busy wait loop before halting when the cpuidle haltpoll driver is enabled inside a virtual machine. This reduces wakeup latency for events that occur soon after the vCPU becomes idle.

This patch series extends the cpuidle busy wait loop with the new poll_source API so drivers can participate in polling. Such polling-aware drivers disable their device's irq during the busy wait loop to avoid the cost of interrupts. This reduces latency further than regular cpuidle haltpoll, which still relies on irqs.

Virtio drivers are modified to use the poll_source API so all virtio device types get this feature. The following virtio-blk fio benchmark results show the improvement:

IOPS (numjobs=4, iodepth=1, 4 virtqueues)
before poll_source io_poll

4k randread    167102  186049 (+11%)  186654 (+11%)
4k randwrite   162204  181214 (+11%)  181850 (+12%)
4k randrw      159520  177071 (+11%)  177928 (+11%)

The comparison against io_poll shows that cpuidle poll_source achieves equivalent performance to the block layer's io_poll feature (which I implemented in a separate patch series [1]).

The advantage of poll_source is that applications do not need to explicitly set the RWF_HIPRI I/O request flag. The poll_source approach is attractive because few applications actually use RWF_HIPRI and it takes advantage of CPU cycles we would have spent in cpuidle haltpoll anyway.

The current series does not improve virtio-net. I haven't investigated deeply, but it is possible that NAPI and poll_source do not combine. See the final patch for a starting point on making the two work together.

Quoted 5 lines

I have not tried this on bare metal but it might help there too. The cost of disabling a device's irq must be less than the savings from avoiding irq handling for this optimization to make sense.

[1] https://lore.kernel.org/linux-block/[email protected]/

Quoted 7 lines

Hi Stefan:

Some questions:

1) What's the advantages of introducing polling at virtio level instead of doing it at each subsystems? Polling in virtio level may only work well if all (or most) of the devices are virtio

I'm not sure I understand the question. cpuidle haltpoll benefits all devices today, except it incurs interrupt latency. The poll_source API eliminates the interrupt latency for drivers that can disable device interrupts cheaply.

This patch adds poll_source to core virtio code so that all virtio drivers get this feature for free. No driver-specific changes are needed.

Quoted 5 lines

If you mean networking, block layer, etc by "subsystems" then there's nothing those subsystems can do to help. Whether poll_source can be used depends on the specific driver, not the subsystem. If you consider drivers/virtio/ a subsystem, then that's exactly what the patch series is doing.

Quoted 6 lines

I meant, if we choose to use idle poll, we have some several choices:

1) bus level (e.g the virtio)
2) subsystem level (e.g the networking and block)

I'm not sure which one is better.

This API is intended to be driver- or bus-level. I don't think subsystems can do very much since they don't know the hardware capabilities (cheap interrupt disabling) and in most cases there's no advantage of plumbing it through subsystems when drivers can call the API directly.

Quoted 11 lines
Quoted 9 lines
Quoted 2 lines

2) What's the advantages of using cpuidle instead of using a thread (and leverage the scheduler)?

In order to combine with the existing cpuidle infrastructure. No new polling loop is introduced and no additional CPU cycles are spent on polling.

If cpuidle itself is converted to threads then poll_source would automatically operate in a thread too, but this patch series doesn't change how the core cpuidle code works.

Quoted 5 lines

So networking subsystem can use NAPI busy polling in the process context which means it can be leveraged by the scheduler.

I'm not sure it's a good idea to poll drivers for a specific bus in the general cpu idle layer.

Why? Maybe because the cpuidle execution environment is a little special?

Quoted 1 line

Another questions, are those numbers measured by APICV capable machine?

Yes.

Quoted 2 lines

Virtio-net turns on the tx interrupts since 2 years ago. And we don't see too much difference when measured with a APICV host.

My understand is NAPI always takes the first interrupt. Polling only happens on subsequent rounds until there's no more work to do.

There seem to be multiple factors that would influence tx performance like how full the tx queues are, whether more packets are sent during NAPI polling, whether you're benchmarking a physical PCIe NIC or a vhost_net software device, etc.

Regarding APICV and software devices, the benchmark results I posted show that avoiding the interrupt injection helps even with APICV.

Stefan

#10 lore raw
Quoted 95 lines · Stefan Hajnoczi
Quoted 69 lines · Jason Wang
Quoted 60 lines · Stefan Hajnoczi
Quoted 43 lines · Jason Wang
Quoted 36 lines · Stefan Hajnoczi

These patches are not polished yet but I would like request feedback on this approach and share performance results with you.

Idle CPUs tentatively enter a busy wait loop before halting when the cpuidle haltpoll driver is enabled inside a virtual machine. This reduces wakeup latency for events that occur soon after the vCPU becomes idle.

This patch series extends the cpuidle busy wait loop with the new poll_source API so drivers can participate in polling. Such polling-aware drivers disable their device's irq during the busy wait loop to avoid the cost of interrupts. This reduces latency further than regular cpuidle haltpoll, which still relies on irqs.

Virtio drivers are modified to use the poll_source API so all virtio device types get this feature. The following virtio-blk fio benchmark results show the improvement:

IOPS (numjobs=4, iodepth=1, 4 virtqueues)
before poll_source io_poll

4k randread    167102  186049 (+11%)  186654 (+11%)
4k randwrite   162204  181214 (+11%)  181850 (+12%)
4k randrw      159520  177071 (+11%)  177928 (+11%)

The comparison against io_poll shows that cpuidle poll_source achieves equivalent performance to the block layer's io_poll feature (which I implemented in a separate patch series [1]).

The advantage of poll_source is that applications do not need to explicitly set the RWF_HIPRI I/O request flag. The poll_source approach is attractive because few applications actually use RWF_HIPRI and it takes advantage of CPU cycles we would have spent in cpuidle haltpoll anyway.

The current series does not improve virtio-net. I haven't investigated deeply, but it is possible that NAPI and poll_source do not combine. See the final patch for a starting point on making the two work together.

Quoted 5 lines

I have not tried this on bare metal but it might help there too. The cost of disabling a device's irq must be less than the savings from avoiding irq handling for this optimization to make sense.

[1] https://lore.kernel.org/linux-block/[email protected]/

Quoted 7 lines

Hi Stefan:

Some questions:

1) What's the advantages of introducing polling at virtio level instead of doing it at each subsystems? Polling in virtio level may only work well if all (or most) of the devices are virtio

I'm not sure I understand the question. cpuidle haltpoll benefits all devices today, except it incurs interrupt latency. The poll_source API eliminates the interrupt latency for drivers that can disable device interrupts cheaply.

This patch adds poll_source to core virtio code so that all virtio drivers get this feature for free. No driver-specific changes are needed.

Quoted 5 lines

If you mean networking, block layer, etc by "subsystems" then there's nothing those subsystems can do to help. Whether poll_source can be used depends on the specific driver, not the subsystem. If you consider drivers/virtio/ a subsystem, then that's exactly what the patch series is doing.

Quoted 6 lines

I meant, if we choose to use idle poll, we have some several choices:

1) bus level (e.g the virtio)
2) subsystem level (e.g the networking and block)

I'm not sure which one is better.

This API is intended to be driver- or bus-level. I don't think subsystems can do very much since they don't know the hardware capabilities (cheap interrupt disabling) and in most cases there's no advantage of plumbing it through subsystems when drivers can call the API directly.

Quoted 11 lines
Quoted 9 lines
Quoted 2 lines

2) What's the advantages of using cpuidle instead of using a thread (and leverage the scheduler)?

In order to combine with the existing cpuidle infrastructure. No new polling loop is introduced and no additional CPU cycles are spent on polling.

If cpuidle itself is converted to threads then poll_source would automatically operate in a thread too, but this patch series doesn't change how the core cpuidle code works.

Quoted 7 lines
Quoted 5 lines

So networking subsystem can use NAPI busy polling in the process context which means it can be leveraged by the scheduler.

I'm not sure it's a good idea to poll drivers for a specific bus in the general cpu idle layer.

Why? Maybe because the cpuidle execution environment is a little special?

Well, this would be prone to abuse.

The time spent in that driver callback counts as CPU idle time while it really is the driver running and there is not limit on how much time the callback can take, while doing costly things in the idle loop is generally avoided, because on wakeup the CPU needs to be available to the task needing it as soon as possible. IOW, the callback potentially add unbounded latency to the CPU wakeup path.
_______________________________________________
Virtualization mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#11 lore raw
Quoted 104 lines · Rafael J. Wysocki
Quoted 95 lines · Stefan Hajnoczi
Quoted 69 lines · Jason Wang
Quoted 60 lines · Stefan Hajnoczi
Quoted 43 lines · Jason Wang
Quoted 36 lines · Stefan Hajnoczi

These patches are not polished yet but I would like request feedback on this approach and share performance results with you.

Idle CPUs tentatively enter a busy wait loop before halting when the cpuidle haltpoll driver is enabled inside a virtual machine. This reduces wakeup latency for events that occur soon after the vCPU becomes idle.

This patch series extends the cpuidle busy wait loop with the new poll_source API so drivers can participate in polling. Such polling-aware drivers disable their device's irq during the busy wait loop to avoid the cost of interrupts. This reduces latency further than regular cpuidle haltpoll, which still relies on irqs.

Virtio drivers are modified to use the poll_source API so all virtio device types get this feature. The following virtio-blk fio benchmark results show the improvement:

IOPS (numjobs=4, iodepth=1, 4 virtqueues)
before poll_source io_poll

4k randread    167102  186049 (+11%)  186654 (+11%)
4k randwrite   162204  181214 (+11%)  181850 (+12%)
4k randrw      159520  177071 (+11%)  177928 (+11%)

The comparison against io_poll shows that cpuidle poll_source achieves equivalent performance to the block layer's io_poll feature (which I implemented in a separate patch series [1]).

The advantage of poll_source is that applications do not need to explicitly set the RWF_HIPRI I/O request flag. The poll_source approach is attractive because few applications actually use RWF_HIPRI and it takes advantage of CPU cycles we would have spent in cpuidle haltpoll anyway.

The current series does not improve virtio-net. I haven't investigated deeply, but it is possible that NAPI and poll_source do not combine. See the final patch for a starting point on making the two work together.

Quoted 5 lines

I have not tried this on bare metal but it might help there too. The cost of disabling a device's irq must be less than the savings from avoiding irq handling for this optimization to make sense.

[1] https://lore.kernel.org/linux-block/[email protected]/

Quoted 7 lines

Hi Stefan:

Some questions:

1) What's the advantages of introducing polling at virtio level instead of doing it at each subsystems? Polling in virtio level may only work well if all (or most) of the devices are virtio

I'm not sure I understand the question. cpuidle haltpoll benefits all devices today, except it incurs interrupt latency. The poll_source API eliminates the interrupt latency for drivers that can disable device interrupts cheaply.

This patch adds poll_source to core virtio code so that all virtio drivers get this feature for free. No driver-specific changes are needed.

Quoted 5 lines

If you mean networking, block layer, etc by "subsystems" then there's nothing those subsystems can do to help. Whether poll_source can be used depends on the specific driver, not the subsystem. If you consider drivers/virtio/ a subsystem, then that's exactly what the patch series is doing.

Quoted 6 lines

I meant, if we choose to use idle poll, we have some several choices:

1) bus level (e.g the virtio)
2) subsystem level (e.g the networking and block)

I'm not sure which one is better.

This API is intended to be driver- or bus-level. I don't think subsystems can do very much since they don't know the hardware capabilities (cheap interrupt disabling) and in most cases there's no advantage of plumbing it through subsystems when drivers can call the API directly.

Quoted 11 lines
Quoted 9 lines
Quoted 2 lines

2) What's the advantages of using cpuidle instead of using a thread (and leverage the scheduler)?

In order to combine with the existing cpuidle infrastructure. No new polling loop is introduced and no additional CPU cycles are spent on polling.

If cpuidle itself is converted to threads then poll_source would automatically operate in a thread too, but this patch series doesn't change how the core cpuidle code works.

Quoted 7 lines
Quoted 5 lines

So networking subsystem can use NAPI busy polling in the process context which means it can be leveraged by the scheduler.

I'm not sure it's a good idea to poll drivers for a specific bus in the general cpu idle layer.

Why? Maybe because the cpuidle execution environment is a little special?

Quoted 8 lines

Well, this would be prone to abuse.

The time spent in that driver callback counts as CPU idle time while it really is the driver running and there is not limit on how much time the callback can take, while doing costly things in the idle loop is generally avoided, because on wakeup the CPU needs to be available to the task needing it as soon as possible. IOW, the callback potentially add unbounded latency to the CPU wakeup path.

How is this different from driver interrupt handlers running during cpuidle?

Stefan

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#12 lore raw
Quoted 115 lines · Stefan Hajnoczi
Quoted 104 lines · Rafael J. Wysocki
Quoted 95 lines · Stefan Hajnoczi
Quoted 69 lines · Jason Wang
Quoted 60 lines · Stefan Hajnoczi
Quoted 43 lines · Jason Wang
Quoted 36 lines · Stefan Hajnoczi

These patches are not polished yet but I would like request feedback on this approach and share performance results with you.

Idle CPUs tentatively enter a busy wait loop before halting when the cpuidle haltpoll driver is enabled inside a virtual machine. This reduces wakeup latency for events that occur soon after the vCPU becomes idle.

This patch series extends the cpuidle busy wait loop with the new poll_source API so drivers can participate in polling. Such polling-aware drivers disable their device's irq during the busy wait loop to avoid the cost of interrupts. This reduces latency further than regular cpuidle haltpoll, which still relies on irqs.

Virtio drivers are modified to use the poll_source API so all virtio device types get this feature. The following virtio-blk fio benchmark results show the improvement:

IOPS (numjobs=4, iodepth=1, 4 virtqueues)
before poll_source io_poll

4k randread    167102  186049 (+11%)  186654 (+11%)
4k randwrite   162204  181214 (+11%)  181850 (+12%)
4k randrw      159520  177071 (+11%)  177928 (+11%)

The comparison against io_poll shows that cpuidle poll_source achieves equivalent performance to the block layer's io_poll feature (which I implemented in a separate patch series [1]).

The advantage of poll_source is that applications do not need to explicitly set the RWF_HIPRI I/O request flag. The poll_source approach is attractive because few applications actually use RWF_HIPRI and it takes advantage of CPU cycles we would have spent in cpuidle haltpoll anyway.

The current series does not improve virtio-net. I haven't investigated deeply, but it is possible that NAPI and poll_source do not combine. See the final patch for a starting point on making the two work together.

Quoted 5 lines

I have not tried this on bare metal but it might help there too. The cost of disabling a device's irq must be less than the savings from avoiding irq handling for this optimization to make sense.

[1] https://lore.kernel.org/linux-block/[email protected]/

Quoted 7 lines

Hi Stefan:

Some questions:

1) What's the advantages of introducing polling at virtio level instead of doing it at each subsystems? Polling in virtio level may only work well if all (or most) of the devices are virtio

I'm not sure I understand the question. cpuidle haltpoll benefits all devices today, except it incurs interrupt latency. The poll_source API eliminates the interrupt latency for drivers that can disable device interrupts cheaply.

This patch adds poll_source to core virtio code so that all virtio drivers get this feature for free. No driver-specific changes are needed.

Quoted 5 lines

If you mean networking, block layer, etc by "subsystems" then there's nothing those subsystems can do to help. Whether poll_source can be used depends on the specific driver, not the subsystem. If you consider drivers/virtio/ a subsystem, then that's exactly what the patch series is doing.

Quoted 6 lines

I meant, if we choose to use idle poll, we have some several choices:

1) bus level (e.g the virtio)
2) subsystem level (e.g the networking and block)

I'm not sure which one is better.

This API is intended to be driver- or bus-level. I don't think subsystems can do very much since they don't know the hardware capabilities (cheap interrupt disabling) and in most cases there's no advantage of plumbing it through subsystems when drivers can call the API directly.

Quoted 11 lines
Quoted 9 lines
Quoted 2 lines

2) What's the advantages of using cpuidle instead of using a thread (and leverage the scheduler)?

In order to combine with the existing cpuidle infrastructure. No new polling loop is introduced and no additional CPU cycles are spent on polling.

If cpuidle itself is converted to threads then poll_source would automatically operate in a thread too, but this patch series doesn't change how the core cpuidle code works.

Quoted 7 lines
Quoted 5 lines

So networking subsystem can use NAPI busy polling in the process context which means it can be leveraged by the scheduler.

I'm not sure it's a good idea to poll drivers for a specific bus in the general cpu idle layer.

Why? Maybe because the cpuidle execution environment is a little special?

Quoted 8 lines

Well, this would be prone to abuse.

The time spent in that driver callback counts as CPU idle time while it really is the driver running and there is not limit on how much time the callback can take, while doing costly things in the idle loop is generally avoided, because on wakeup the CPU needs to be available to the task needing it as soon as possible. IOW, the callback potentially add unbounded latency to the CPU wakeup path.

Quoted 2 lines

How is this different from driver interrupt handlers running during cpuidle?

The time spent on handling interrupts does not count as CPU idle time.
_______________________________________________
Virtualization mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#13 lore raw
Quoted 116 lines · Rafael J. Wysocki
Quoted 115 lines · Stefan Hajnoczi
Quoted 112 lines · Rafael J. Wysocki
Quoted 95 lines · Stefan Hajnoczi
Quoted 69 lines · Jason Wang
Quoted 60 lines · Stefan Hajnoczi
Quoted 43 lines · Jason Wang
Quoted 36 lines · Stefan Hajnoczi

These patches are not polished yet but I would like request feedback on this approach and share performance results with you.

Idle CPUs tentatively enter a busy wait loop before halting when the cpuidle haltpoll driver is enabled inside a virtual machine. This reduces wakeup latency for events that occur soon after the vCPU becomes idle.

This patch series extends the cpuidle busy wait loop with the new poll_source API so drivers can participate in polling. Such polling-aware drivers disable their device's irq during the busy wait loop to avoid the cost of interrupts. This reduces latency further than regular cpuidle haltpoll, which still relies on irqs.

Virtio drivers are modified to use the poll_source API so all virtio device types get this feature. The following virtio-blk fio benchmark results show the improvement:

IOPS (numjobs=4, iodepth=1, 4 virtqueues)
before poll_source io_poll

4k randread    167102  186049 (+11%)  186654 (+11%)
4k randwrite   162204  181214 (+11%)  181850 (+12%)
4k randrw      159520  177071 (+11%)  177928 (+11%)

The comparison against io_poll shows that cpuidle poll_source achieves equivalent performance to the block layer's io_poll feature (which I implemented in a separate patch series [1]).

The advantage of poll_source is that applications do not need to explicitly set the RWF_HIPRI I/O request flag. The poll_source approach is attractive because few applications actually use RWF_HIPRI and it takes advantage of CPU cycles we would have spent in cpuidle haltpoll anyway.

The current series does not improve virtio-net. I haven't investigated deeply, but it is possible that NAPI and poll_source do not combine. See the final patch for a starting point on making the two work together.

Quoted 5 lines

I have not tried this on bare metal but it might help there too. The cost of disabling a device's irq must be less than the savings from avoiding irq handling for this optimization to make sense.

[1] https://lore.kernel.org/linux-block/[email protected]/

Quoted 7 lines

Hi Stefan:

Some questions:

1) What's the advantages of introducing polling at virtio level instead of doing it at each subsystems? Polling in virtio level may only work well if all (or most) of the devices are virtio

I'm not sure I understand the question. cpuidle haltpoll benefits all devices today, except it incurs interrupt latency. The poll_source API eliminates the interrupt latency for drivers that can disable device interrupts cheaply.

This patch adds poll_source to core virtio code so that all virtio drivers get this feature for free. No driver-specific changes are needed.

Quoted 5 lines

If you mean networking, block layer, etc by "subsystems" then there's nothing those subsystems can do to help. Whether poll_source can be used depends on the specific driver, not the subsystem. If you consider drivers/virtio/ a subsystem, then that's exactly what the patch series is doing.

Quoted 6 lines

I meant, if we choose to use idle poll, we have some several choices:

1) bus level (e.g the virtio)
2) subsystem level (e.g the networking and block)

I'm not sure which one is better.

This API is intended to be driver- or bus-level. I don't think subsystems can do very much since they don't know the hardware capabilities (cheap interrupt disabling) and in most cases there's no advantage of plumbing it through subsystems when drivers can call the API directly.

Quoted 11 lines
Quoted 9 lines
Quoted 2 lines

2) What's the advantages of using cpuidle instead of using a thread (and leverage the scheduler)?

In order to combine with the existing cpuidle infrastructure. No new polling loop is introduced and no additional CPU cycles are spent on polling.

If cpuidle itself is converted to threads then poll_source would automatically operate in a thread too, but this patch series doesn't change how the core cpuidle code works.

Quoted 7 lines
Quoted 5 lines

So networking subsystem can use NAPI busy polling in the process context which means it can be leveraged by the scheduler.

I'm not sure it's a good idea to poll drivers for a specific bus in the general cpu idle layer.

Why? Maybe because the cpuidle execution environment is a little special?

Well, this would be prone to abuse.

The time spent in that driver callback counts as CPU idle time while it really is the driver running and there is not limit on how much time the callback can take, while doing costly things in the idle loop is generally avoided, because on wakeup the CPU needs to be available to the task needing it as soon as possible. IOW, the callback potentially add unbounded latency to the CPU wakeup path.

Quoted 4 lines
Quoted 2 lines

How is this different from driver interrupt handlers running during cpuidle?

The time spent on handling interrupts does not count as CPU idle time.

Is that taken care of by account_hardirq_enter()?

Drivers using poll_source have two pieces:
1. A small piece of code that polls the device.
2. A larger piece of code that runs when polling succeeds. This is basically the irq handler.

Would it be acceptable to run #1 from cpuidle but defer #2 so it's not accounted as idle time?

Thanks,
Stefan