Skip to contentSkip to conversation

virtio: add driver_override support

PATCH In review Max Gurtovoy 3 participants 7 messages Dec 8, 2025 – Dec 9, 2025

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

PATCH v2 1/1 virtio: add driver_override support

Add support for the 'driver_override' attribute to Virtio devices. This allows users to control which Virtio bus driver binds to a given Virtio device.

If 'driver_override' is not set, the existing behavior is preserved and devices will continue to auto-bind to the first matching Virtio bus driver.

Tested with virtio blk device (virtio core and pci drivers are loaded):

  $ modprobe my_virtio_blk
  # automatically unbind from virtio_blk driver and override + bind to
  # my_virtio_blk driver.
  $ driverctl -v -b virtio set-override virtio0 my_virtio_blk

In addition, driverctl saves the configuration persistently under /etc/driverctl.d/.

---

changes from v1:
- use !strcmp() to compare strings (MST)
- extend commit msg with example (MST)

---
 drivers/virtio/virtio.c | 34 ++++++++++++++++++++++++++++++++++
 include/linux/virtio.h  |  4 ++++
 2 files changed, 38 insertions(+)
drivers/virtio/virtio.c +34 -0
index a09eb4d62f82..993dc928be49 100644
@@ -61,12 +61,41 @@ static ssize_t features_show(struct device *_d,
 }
 static DEVICE_ATTR_RO(features);
 
+static ssize_t driver_override_store(struct device *_d,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	int ret;
+
+	ret = driver_set_override(_d, &dev->driver_override, buf, count);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t driver_override_show(struct device *_d,
+				    struct device_attribute *attr, char *buf)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	ssize_t len;
+
+	device_lock(_d);
+	len = sysfs_emit(buf, "%s\n", dev->driver_override);
+	device_unlock(_d);
+
+	return len;
+}
+static DEVICE_ATTR_RW(driver_override);
+
 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_driver_override.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(virtio_dev);
@@ -88,6 +117,10 @@ static int virtio_dev_match(struct device *_dv, const struct device_driver *_dr)
 	struct virtio_device *dev = dev_to_virtio(_dv);
 	const struct virtio_device_id *ids;
 
+	/* Check override first, and if set, only use the named driver */
+	if (dev->driver_override)
+		return !strcmp(dev->driver_override, _dr->name);
+
 	ids = drv_to_virtio(_dr)->id_table;
 	for (i = 0; ids[i].device; i++)
 		if (virtio_id_match(dev, &ids[i]))
@@ -582,6 +615,7 @@ void unregister_virtio_device(struct virtio_device *dev)
 {
 	int index = dev->index; /* save for after device release */
 
+	kfree(dev->driver_override);
 	device_unregister(&dev->dev);
 	virtio_debug_device_exit(dev);
 	ida_free(&virtio_index_ida, index);
include/linux/virtio.h +4 -0
index db31fc6f4f1f..418bb490bdc6 100644
@@ -138,6 +138,9 @@ struct virtio_admin_cmd {
  * @config_lock: protects configuration change reporting
  * @vqs_list_lock: protects @vqs.
  * @dev: underlying device.
+ * @driver_override: driver name to force a match; do not set directly,
+ *                   because core frees it; use driver_set_override() to
+ *                   set or clear it.
  * @id: the device type identification (used to match it with a driver).
  * @config: the configuration ops for this device.
  * @vringh_config: configuration ops for host vrings.
@@ -158,6 +161,7 @@ struct virtio_device {
 	spinlock_t config_lock;
 	spinlock_t vqs_list_lock;
 	struct device dev;
+	const char *driver_override;
 	struct virtio_device_id id;
 	const struct virtio_config_ops *config;
 	const struct vringh_config_ops *vringh_config;
Signature · 1 line
2.18.1
#1 lore raw
Quoted 8 lines · Max Gurtovoy

Add support for the 'driver_override' attribute to Virtio devices. This allows users to control which Virtio bus driver binds to a given Virtio device.

If 'driver_override' is not set, the existing behavior is preserved and devices will continue to auto-bind to the first matching Virtio bus driver.

Quoted 7 lines

Tested with virtio blk device (virtio core and pci drivers are loaded):

  $ modprobe my_virtio_blk
  # automatically unbind from virtio_blk driver and override + bind to
  # my_virtio_blk driver.
  $ driverctl -v -b virtio set-override virtio0 my_virtio_blk

Why a second virtio-blk driver implementation? Please explain the use case.

Thanks,
Stefan

Quoted 106 lines

In addition, driverctl saves the configuration persistently under /etc/driverctl.d/.

---

changes from v1:
- use !strcmp() to compare strings (MST)
- extend commit msg with example (MST)

---
 drivers/virtio/virtio.c | 34 ++++++++++++++++++++++++++++++++++
 include/linux/virtio.h  |  4 ++++
 2 files changed, 38 insertions(+)
drivers/virtio/virtio.c +34 -0
index a09eb4d62f82..993dc928be49 100644
@@ -61,12 +61,41 @@ static ssize_t features_show(struct device *_d,
 }
 static DEVICE_ATTR_RO(features);
 
+static ssize_t driver_override_store(struct device *_d,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	int ret;
+
+	ret = driver_set_override(_d, &dev->driver_override, buf, count);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t driver_override_show(struct device *_d,
+				    struct device_attribute *attr, char *buf)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	ssize_t len;
+
+	device_lock(_d);
+	len = sysfs_emit(buf, "%s\n", dev->driver_override);
+	device_unlock(_d);
+
+	return len;
+}
+static DEVICE_ATTR_RW(driver_override);
+
 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_driver_override.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(virtio_dev);
@@ -88,6 +117,10 @@ static int virtio_dev_match(struct device *_dv, const struct device_driver *_dr)
 	struct virtio_device *dev = dev_to_virtio(_dv);
 	const struct virtio_device_id *ids;
 
+	/* Check override first, and if set, only use the named driver */
+	if (dev->driver_override)
+		return !strcmp(dev->driver_override, _dr->name);
+
 	ids = drv_to_virtio(_dr)->id_table;
 	for (i = 0; ids[i].device; i++)
 		if (virtio_id_match(dev, &ids[i]))
@@ -582,6 +615,7 @@ void unregister_virtio_device(struct virtio_device *dev)
 {
 	int index = dev->index; /* save for after device release */
 
+	kfree(dev->driver_override);
 	device_unregister(&dev->dev);
 	virtio_debug_device_exit(dev);
 	ida_free(&virtio_index_ida, index);
include/linux/virtio.h +4 -0
index db31fc6f4f1f..418bb490bdc6 100644
@@ -138,6 +138,9 @@ struct virtio_admin_cmd {
  * @config_lock: protects configuration change reporting
  * @vqs_list_lock: protects @vqs.
  * @dev: underlying device.
+ * @driver_override: driver name to force a match; do not set directly,
+ *                   because core frees it; use driver_set_override() to
+ *                   set or clear it.
  * @id: the device type identification (used to match it with a driver).
  * @config: the configuration ops for this device.
  * @vringh_config: configuration ops for host vrings.
@@ -158,6 +161,7 @@ struct virtio_device {
 	spinlock_t config_lock;
 	spinlock_t vqs_list_lock;
 	struct device dev;
+	const char *driver_override;
 	struct virtio_device_id id;
 	const struct virtio_config_ops *config;
 	const struct vringh_config_ops *vringh_config;
Signature · 1 line
2.18.1
#2 lore raw
In reply to Max Gurtovoy
Quoted 7 lines · Max Gurtovoy

Add support for the 'driver_override' attribute to Virtio devices. This allows users to control which Virtio bus driver binds to a given Virtio device.

If 'driver_override' is not set, the existing behavior is preserved and devices will continue to auto-bind to the first matching Virtio bus driver.

oh, it's a device driver not the bus driver, actually.

Quoted 10 lines

Tested with virtio blk device (virtio core and pci drivers are loaded):

  $ modprobe my_virtio_blk
  # automatically unbind from virtio_blk driver and override + bind to
  # my_virtio_blk driver.
  $ driverctl -v -b virtio set-override virtio0 my_virtio_blk

In addition, driverctl saves the configuration persistently under /etc/driverctl.d/.

what is this "mydriver" though? what are valid examples that we want to support?

Quoted 103 lines

---

changes from v1:
- use !strcmp() to compare strings (MST)
- extend commit msg with example (MST)

---
 drivers/virtio/virtio.c | 34 ++++++++++++++++++++++++++++++++++
 include/linux/virtio.h  |  4 ++++
 2 files changed, 38 insertions(+)
drivers/virtio/virtio.c +34 -0
index a09eb4d62f82..993dc928be49 100644
@@ -61,12 +61,41 @@ static ssize_t features_show(struct device *_d,
 }
 static DEVICE_ATTR_RO(features);
 
+static ssize_t driver_override_store(struct device *_d,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	int ret;
+
+	ret = driver_set_override(_d, &dev->driver_override, buf, count);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t driver_override_show(struct device *_d,
+				    struct device_attribute *attr, char *buf)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	ssize_t len;
+
+	device_lock(_d);
+	len = sysfs_emit(buf, "%s\n", dev->driver_override);
+	device_unlock(_d);
+
+	return len;
+}
+static DEVICE_ATTR_RW(driver_override);
+
 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_driver_override.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(virtio_dev);
@@ -88,6 +117,10 @@ static int virtio_dev_match(struct device *_dv, const struct device_driver *_dr)
 	struct virtio_device *dev = dev_to_virtio(_dv);
 	const struct virtio_device_id *ids;
 
+	/* Check override first, and if set, only use the named driver */
+	if (dev->driver_override)
+		return !strcmp(dev->driver_override, _dr->name);
+
 	ids = drv_to_virtio(_dr)->id_table;
 	for (i = 0; ids[i].device; i++)
 		if (virtio_id_match(dev, &ids[i]))
@@ -582,6 +615,7 @@ void unregister_virtio_device(struct virtio_device *dev)
 {
 	int index = dev->index; /* save for after device release */
 
+	kfree(dev->driver_override);
 	device_unregister(&dev->dev);
 	virtio_debug_device_exit(dev);
 	ida_free(&virtio_index_ida, index);
include/linux/virtio.h +4 -0
index db31fc6f4f1f..418bb490bdc6 100644
@@ -138,6 +138,9 @@ struct virtio_admin_cmd {
  * @config_lock: protects configuration change reporting
  * @vqs_list_lock: protects @vqs.
  * @dev: underlying device.
+ * @driver_override: driver name to force a match; do not set directly,
+ *                   because core frees it; use driver_set_override() to
+ *                   set or clear it.
  * @id: the device type identification (used to match it with a driver).
  * @config: the configuration ops for this device.
  * @vringh_config: configuration ops for host vrings.
@@ -158,6 +161,7 @@ struct virtio_device {
 	spinlock_t config_lock;
 	spinlock_t vqs_list_lock;
 	struct device dev;
+	const char *driver_override;
 	struct virtio_device_id id;
 	const struct virtio_config_ops *config;
 	const struct vringh_config_ops *vringh_config;
Signature · 1 line
2.18.1
#3 lore raw
Quoted 9 lines · Michael S. Tsirkin
Quoted 7 lines · Max Gurtovoy

Add support for the 'driver_override' attribute to Virtio devices. This allows users to control which Virtio bus driver binds to a given Virtio device.

If 'driver_override' is not set, the existing behavior is preserved and devices will continue to auto-bind to the first matching Virtio bus driver.

oh, it's a device driver not the bus driver, actually.

Yes. I'll fix the commit message.

Quoted 5 lines
Quoted 5 lines

Tested with virtio blk device (virtio core and pci drivers are loaded):

   $ modprobe my_virtio_blk
Quoted 8 lines
Quoted 6 lines
   # automatically unbind from virtio_blk driver and override + bind to
   # my_virtio_blk driver.
   $ driverctl -v -b virtio set-override virtio0 my_virtio_blk

In addition, driverctl saves the configuration persistently under /etc/driverctl.d/.

what is this "mydriver" though? what are valid examples that we want to support?

This is an example for a custom virtio block driver.

It can be any custom virtio-XX driver (XX=FS/NET/..).

Quoted 102 lines
Quoted 102 lines

---

changes from v1:
- use !strcmp() to compare strings (MST)
- extend commit msg with example (MST)

---
  drivers/virtio/virtio.c | 34 ++++++++++++++++++++++++++++++++++
  include/linux/virtio.h  |  4 ++++
  2 files changed, 38 insertions(+)
drivers/virtio/virtio.c +34 -0
index a09eb4d62f82..993dc928be49 100644
@@ -61,12 +61,41 @@ static ssize_t features_show(struct device *_d,
  }
  static DEVICE_ATTR_RO(features);
 
+static ssize_t driver_override_store(struct device *_d,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	int ret;
+
+	ret = driver_set_override(_d, &dev->driver_override, buf, count);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t driver_override_show(struct device *_d,
+				    struct device_attribute *attr, char *buf)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	ssize_t len;
+
+	device_lock(_d);
+	len = sysfs_emit(buf, "%s\n", dev->driver_override);
+	device_unlock(_d);
+
+	return len;
+}
+static DEVICE_ATTR_RW(driver_override);
+
  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_driver_override.attr,
  	NULL,
  };
  ATTRIBUTE_GROUPS(virtio_dev);
@@ -88,6 +117,10 @@ static int virtio_dev_match(struct device *_dv, const struct device_driver *_dr)
  	struct virtio_device *dev = dev_to_virtio(_dv);
  	const struct virtio_device_id *ids;
 
+	/* Check override first, and if set, only use the named driver */
+	if (dev->driver_override)
+		return !strcmp(dev->driver_override, _dr->name);
+
  	ids = drv_to_virtio(_dr)->id_table;
  	for (i = 0; ids[i].device; i++)
  		if (virtio_id_match(dev, &ids[i]))
@@ -582,6 +615,7 @@ void unregister_virtio_device(struct virtio_device *dev)
  {
  	int index = dev->index; /* save for after device release */
 
+	kfree(dev->driver_override);
  	device_unregister(&dev->dev);
  	virtio_debug_device_exit(dev);
  	ida_free(&virtio_index_ida, index);
include/linux/virtio.h +4 -0
index db31fc6f4f1f..418bb490bdc6 100644
@@ -138,6 +138,9 @@ struct virtio_admin_cmd {
   * @config_lock: protects configuration change reporting
   * @vqs_list_lock: protects @vqs.
   * @dev: underlying device.
+ * @driver_override: driver name to force a match; do not set directly,
+ *                   because core frees it; use driver_set_override() to
+ *                   set or clear it.
   * @id: the device type identification (used to match it with a driver).
   * @config: the configuration ops for this device.
   * @vringh_config: configuration ops for host vrings.
@@ -158,6 +161,7 @@ struct virtio_device {
  	spinlock_t config_lock;
  	spinlock_t vqs_list_lock;
  	struct device dev;
+	const char *driver_override;
  	struct virtio_device_id id;
  	const struct virtio_config_ops *config;
  	const struct vringh_config_ops *vringh_config;
Signature · 1 line
2.18.1
#4 lore raw
Quoted 24 lines · Max Gurtovoy
Quoted 9 lines · Michael S. Tsirkin
Quoted 7 lines · Max Gurtovoy

Add support for the 'driver_override' attribute to Virtio devices. This allows users to control which Virtio bus driver binds to a given Virtio device.

If 'driver_override' is not set, the existing behavior is preserved and devices will continue to auto-bind to the first matching Virtio bus driver.

oh, it's a device driver not the bus driver, actually.

Yes. I'll fix the commit message.

Quoted 9 lines
Quoted 9 lines

Tested with virtio blk device (virtio core and pci drivers are loaded):

   $ modprobe my_virtio_blk
   # automatically unbind from virtio_blk driver and override + bind to
   # my_virtio_blk driver.
   $ driverctl -v -b virtio set-override virtio0 my_virtio_blk
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

In addition, driverctl saves the configuration persistently under /etc/driverctl.d/.

what is this "mydriver" though? what are valid examples that we want to support?

This is an example for a custom virtio block driver.

It can be any custom virtio-XX driver (XX=FS/NET/..).

Is "custom" a way to say "out of tree"?

Quoted 99 lines
Quoted 99 lines
Quoted 99 lines

---

changes from v1:
- use !strcmp() to compare strings (MST)
- extend commit msg with example (MST)

---
  drivers/virtio/virtio.c | 34 ++++++++++++++++++++++++++++++++++
  include/linux/virtio.h  |  4 ++++
  2 files changed, 38 insertions(+)
drivers/virtio/virtio.c +34 -0
index a09eb4d62f82..993dc928be49 100644
@@ -61,12 +61,41 @@ static ssize_t features_show(struct device *_d,
  }
  static DEVICE_ATTR_RO(features);
+static ssize_t driver_override_store(struct device *_d,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	int ret;
+
+	ret = driver_set_override(_d, &dev->driver_override, buf, count);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t driver_override_show(struct device *_d,
+				    struct device_attribute *attr, char *buf)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	ssize_t len;
+
+	device_lock(_d);
+	len = sysfs_emit(buf, "%s\n", dev->driver_override);
+	device_unlock(_d);
+
+	return len;
+}
+static DEVICE_ATTR_RW(driver_override);
+
  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_driver_override.attr,
  	NULL,
  };
  ATTRIBUTE_GROUPS(virtio_dev);
@@ -88,6 +117,10 @@ static int virtio_dev_match(struct device *_dv, const struct device_driver *_dr)
  	struct virtio_device *dev = dev_to_virtio(_dv);
  	const struct virtio_device_id *ids;
+	/* Check override first, and if set, only use the named driver */
+	if (dev->driver_override)
+		return !strcmp(dev->driver_override, _dr->name);
+
  	ids = drv_to_virtio(_dr)->id_table;
  	for (i = 0; ids[i].device; i++)
  		if (virtio_id_match(dev, &ids[i]))
@@ -582,6 +615,7 @@ void unregister_virtio_device(struct virtio_device *dev)
  {
  	int index = dev->index; /* save for after device release */
+	kfree(dev->driver_override);
  	device_unregister(&dev->dev);
  	virtio_debug_device_exit(dev);
  	ida_free(&virtio_index_ida, index);
include/linux/virtio.h +4 -0
index db31fc6f4f1f..418bb490bdc6 100644
@@ -138,6 +138,9 @@ struct virtio_admin_cmd {
   * @config_lock: protects configuration change reporting
   * @vqs_list_lock: protects @vqs.
   * @dev: underlying device.
+ * @driver_override: driver name to force a match; do not set directly,
+ *                   because core frees it; use driver_set_override() to
+ *                   set or clear it.
   * @id: the device type identification (used to match it with a driver).
   * @config: the configuration ops for this device.
   * @vringh_config: configuration ops for host vrings.
@@ -158,6 +161,7 @@ struct virtio_device {
  	spinlock_t config_lock;
  	spinlock_t vqs_list_lock;
  	struct device dev;
+	const char *driver_override;
  	struct virtio_device_id id;
  	const struct virtio_config_ops *config;
  	const struct vringh_config_ops *vringh_config;
Signature · 1 line
2.18.1
#5 lore raw
Quoted 22 lines · Michael S. Tsirkin
Quoted 21 lines · Max Gurtovoy
Quoted 9 lines · Michael S. Tsirkin
Quoted 7 lines · Max Gurtovoy

Add support for the 'driver_override' attribute to Virtio devices. This allows users to control which Virtio bus driver binds to a given Virtio device.

If 'driver_override' is not set, the existing behavior is preserved and devices will continue to auto-bind to the first matching Virtio bus driver.

oh, it's a device driver not the bus driver, actually.

Yes. I'll fix the commit message.

Quoted 8 lines
Quoted 8 lines

Tested with virtio blk device (virtio core and pci drivers are loaded):

    $ modprobe my_virtio_blk
    # automatically unbind from virtio_blk driver and override + bind to
    # my_virtio_blk driver.
    $ driverctl -v -b virtio set-override virtio0 my_virtio_blk
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

In addition, driverctl saves the configuration persistently under /etc/driverctl.d/.

what is this "mydriver" though? what are valid examples that we want to support?

This is an example for a custom virtio block driver.

It can be any custom virtio-XX driver (XX=FS/NET/..).

Is "custom" a way to say "out of tree"?

The driver_override mechanism fits both in-tree/out-of-tree drivers.

Quoted 100 lines
Quoted 100 lines
Quoted 100 lines
Quoted 100 lines

---

changes from v1:
- use !strcmp() to compare strings (MST)
- extend commit msg with example (MST)

---
   drivers/virtio/virtio.c | 34 ++++++++++++++++++++++++++++++++++
   include/linux/virtio.h  |  4 ++++
   2 files changed, 38 insertions(+)
drivers/virtio/virtio.c +34 -0
index a09eb4d62f82..993dc928be49 100644
@@ -61,12 +61,41 @@ static ssize_t features_show(struct device *_d,
   }
   static DEVICE_ATTR_RO(features);
+static ssize_t driver_override_store(struct device *_d,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	int ret;
+
+	ret = driver_set_override(_d, &dev->driver_override, buf, count);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t driver_override_show(struct device *_d,
+				    struct device_attribute *attr, char *buf)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	ssize_t len;
+
+	device_lock(_d);
+	len = sysfs_emit(buf, "%s\n", dev->driver_override);
+	device_unlock(_d);
+
+	return len;
+}
+static DEVICE_ATTR_RW(driver_override);
+
   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_driver_override.attr,
   	NULL,
   };
   ATTRIBUTE_GROUPS(virtio_dev);
@@ -88,6 +117,10 @@ static int virtio_dev_match(struct device *_dv, const struct device_driver *_dr)
   	struct virtio_device *dev = dev_to_virtio(_dv);
   	const struct virtio_device_id *ids;
+	/* Check override first, and if set, only use the named driver */
+	if (dev->driver_override)
+		return !strcmp(dev->driver_override, _dr->name);
+
   	ids = drv_to_virtio(_dr)->id_table;
   	for (i = 0; ids[i].device; i++)
   		if (virtio_id_match(dev, &ids[i]))
@@ -582,6 +615,7 @@ void unregister_virtio_device(struct virtio_device *dev)
   {
   	int index = dev->index; /* save for after device release */
+	kfree(dev->driver_override);
   	device_unregister(&dev->dev);
   	virtio_debug_device_exit(dev);
   	ida_free(&virtio_index_ida, index);
include/linux/virtio.h +4 -0
index db31fc6f4f1f..418bb490bdc6 100644
@@ -138,6 +138,9 @@ struct virtio_admin_cmd {
    * @config_lock: protects configuration change reporting
    * @vqs_list_lock: protects @vqs.
    * @dev: underlying device.
+ * @driver_override: driver name to force a match; do not set directly,
+ *                   because core frees it; use driver_set_override() to
+ *                   set or clear it.
    * @id: the device type identification (used to match it with a driver).
    * @config: the configuration ops for this device.
    * @vringh_config: configuration ops for host vrings.
@@ -158,6 +161,7 @@ struct virtio_device {
   	spinlock_t config_lock;
   	spinlock_t vqs_list_lock;
   	struct device dev;
+	const char *driver_override;
   	struct virtio_device_id id;
   	const struct virtio_config_ops *config;
   	const struct vringh_config_ops *vringh_config;
Signature · 1 line
2.18.1
#6 lore raw
Quoted 26 lines · Max Gurtovoy
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Max Gurtovoy
Quoted 9 lines · Michael S. Tsirkin
Quoted 7 lines · Max Gurtovoy

Add support for the 'driver_override' attribute to Virtio devices. This allows users to control which Virtio bus driver binds to a given Virtio device.

If 'driver_override' is not set, the existing behavior is preserved and devices will continue to auto-bind to the first matching Virtio bus driver.

oh, it's a device driver not the bus driver, actually.

Yes. I'll fix the commit message.

Quoted 10 lines
Quoted 10 lines

Tested with virtio blk device (virtio core and pci drivers are loaded):

    $ modprobe my_virtio_blk
    # automatically unbind from virtio_blk driver and override + bind to
    # my_virtio_blk driver.
    $ driverctl -v -b virtio set-override virtio0 my_virtio_blk

In addition, driverctl saves the configuration persistently under /etc/driverctl.d/.

Quoted 8 lines
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

what is this "mydriver" though? what are valid examples that we want to support?

This is an example for a custom virtio block driver.

It can be any custom virtio-XX driver (XX=FS/NET/..).

Is "custom" a way to say "out of tree"?

The driver_override mechanism fits both in-tree/out-of-tree drivers.

good to know. which in tree drivers can use it? this is the part that's missing in the commit log.

thanks

Quoted 100 lines
Quoted 100 lines
Quoted 100 lines
Quoted 100 lines
Quoted 100 lines

---

changes from v1:
- use !strcmp() to compare strings (MST)
- extend commit msg with example (MST)

---
   drivers/virtio/virtio.c | 34 ++++++++++++++++++++++++++++++++++
   include/linux/virtio.h  |  4 ++++
   2 files changed, 38 insertions(+)
drivers/virtio/virtio.c +34 -0
index a09eb4d62f82..993dc928be49 100644
@@ -61,12 +61,41 @@ static ssize_t features_show(struct device *_d,
   }
   static DEVICE_ATTR_RO(features);
+static ssize_t driver_override_store(struct device *_d,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	int ret;
+
+	ret = driver_set_override(_d, &dev->driver_override, buf, count);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t driver_override_show(struct device *_d,
+				    struct device_attribute *attr, char *buf)
+{
+	struct virtio_device *dev = dev_to_virtio(_d);
+	ssize_t len;
+
+	device_lock(_d);
+	len = sysfs_emit(buf, "%s\n", dev->driver_override);
+	device_unlock(_d);
+
+	return len;
+}
+static DEVICE_ATTR_RW(driver_override);
+
   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_driver_override.attr,
   	NULL,
   };
   ATTRIBUTE_GROUPS(virtio_dev);
@@ -88,6 +117,10 @@ static int virtio_dev_match(struct device *_dv, const struct device_driver *_dr)
   	struct virtio_device *dev = dev_to_virtio(_dv);
   	const struct virtio_device_id *ids;
+	/* Check override first, and if set, only use the named driver */
+	if (dev->driver_override)
+		return !strcmp(dev->driver_override, _dr->name);
+
   	ids = drv_to_virtio(_dr)->id_table;
   	for (i = 0; ids[i].device; i++)
   		if (virtio_id_match(dev, &ids[i]))
@@ -582,6 +615,7 @@ void unregister_virtio_device(struct virtio_device *dev)
   {
   	int index = dev->index; /* save for after device release */
+	kfree(dev->driver_override);
   	device_unregister(&dev->dev);
   	virtio_debug_device_exit(dev);
   	ida_free(&virtio_index_ida, index);
include/linux/virtio.h +4 -0
index db31fc6f4f1f..418bb490bdc6 100644
@@ -138,6 +138,9 @@ struct virtio_admin_cmd {
    * @config_lock: protects configuration change reporting
    * @vqs_list_lock: protects @vqs.
    * @dev: underlying device.
+ * @driver_override: driver name to force a match; do not set directly,
+ *                   because core frees it; use driver_set_override() to
+ *                   set or clear it.
    * @id: the device type identification (used to match it with a driver).
    * @config: the configuration ops for this device.
    * @vringh_config: configuration ops for host vrings.
@@ -158,6 +161,7 @@ struct virtio_device {
   	spinlock_t config_lock;
   	spinlock_t vqs_list_lock;
   	struct device dev;
+	const char *driver_override;
   	struct virtio_device_id id;
   	const struct virtio_config_ops *config;
   	const struct vringh_config_ops *vringh_config;
Signature · 1 line
2.18.1