Skip to contentSkip to conversation

virtio: only reset device and restore status if needed in device resume

PATCH Superseded [email protected] 4 participants 9 messages Oct 31, 2024 – Nov 8, 2024

Subsystems (20)
Networking Network drivers SCSI Sound +16 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio: only reset device and restore status if needed in device resume

From: Qiang Zhang <[email protected]>

Virtio core unconditionally reset and restore status for all virtio devices before calling restore method. This breaks some virtio drivers which don't need to do anything in suspend and resume because they just want to keep device state retained.

Virtio GPIO is a typical example. GPIO states should be kept unchanged after suspend and resume (e.g. output pins keep driving the output) and Virtio GPIO driver does nothing in freeze and restore methods. But the reset operation in virtio_device_restore breaks this.

Since some devices need reset in suspend and resume while some needn't, create a new helper function for the original reset and status restore logic so that virtio drivers can invoke it in their restore method if necessary.

---
 drivers/block/virtio_blk.c                 |  4 +++
 drivers/char/hw_random/virtio-rng.c        |  4 +++
 drivers/char/virtio_console.c              |  4 +++
 drivers/crypto/virtio/virtio_crypto_core.c |  4 +++
 drivers/i2c/busses/i2c-virtio.c            |  6 ++++
 drivers/net/virtio_net.c                   |  4 +++
 drivers/scsi/virtio_scsi.c                 |  4 +++
 drivers/virtio/virtio.c                    | 35 ++++++++++++++--------
 drivers/virtio/virtio_balloon.c            |  4 +++
 drivers/virtio/virtio_input.c              |  4 +++
 drivers/virtio/virtio_mem.c                |  4 +++
 include/linux/virtio.h                     |  1 +
 sound/virtio/virtio_card.c                 |  4 +++
 13 files changed, 70 insertions(+), 12 deletions(-)
drivers/block/virtio_blk.c +4 -0
index 194417abc105..bba7148bd219 100644
@@ -1612,6 +1612,10 @@ static int virtblk_restore(struct virtio_device *vdev)
 	struct virtio_blk *vblk = vdev->priv;
 	int ret;
 
+	ret = virtio_device_reset_and_restore_status(vdev);
+	if (ret)
+		return ret;
+
 	ret = init_vq(vdev->priv);
 	if (ret)
 		return ret;
drivers/char/hw_random/virtio-rng.c +4 -0
index dd998f4fe4f2..954280514f5a 100644
@@ -218,6 +218,10 @@ static int virtrng_restore(struct virtio_device *vdev)
 {
 	int err;
 
+	err = virtio_device_reset_and_restore_status(vdev);
+	if (err)
+		return err;
+
 	err = probe_common(vdev);
 	if (!err) {
 		struct virtrng_info *vi = vdev->priv;
drivers/char/virtio_console.c +4 -0
index c62b208b42f1..c431e4d5cd29 100644
@@ -2132,6 +2132,10 @@ static int virtcons_restore(struct virtio_device *vdev)
 
 	portdev = vdev->priv;
 
+	ret = virtio_device_reset_and_restore_status(vdev);
+	if (ret)
+		return ret;
+
 	ret = init_vqs(portdev);
 	if (ret)
 		return ret;
drivers/crypto/virtio/virtio_crypto_core.c +4 -0
index d0278eb568b9..b3363dd80448 100644
@@ -536,6 +536,10 @@ static int virtcrypto_restore(struct virtio_device *vdev)
 	struct virtio_crypto *vcrypto = vdev->priv;
 	int err;
 
+	err = virtio_device_reset_and_restore_status(vdev);
+	if (err)
+		return err;
+
 	err = virtcrypto_init_vqs(vcrypto);
 	if (err)
 		return err;
drivers/i2c/busses/i2c-virtio.c +6 -0
index 2a351f961b89..ce6493d6fe8e 100644
@@ -251,6 +251,12 @@ static int virtio_i2c_freeze(struct virtio_device *vdev)
 
 static int virtio_i2c_restore(struct virtio_device *vdev)
 {
+	int ret;
+
+	ret = virtio_device_reset_and_restore_status(vdev);
+	if (ret)
+		return ret;
+
 	return virtio_i2c_setup_vqs(vdev->priv);
 }
 
drivers/net/virtio_net.c +4 -0
index 792e9eadbfc3..5be2a5f68f68 100644
@@ -6693,6 +6693,10 @@ static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
 	struct virtnet_info *vi = vdev->priv;
 	int err;
 
+	err = virtio_device_reset_and_restore_status(vdev);
+	if (err)
+		return err;
+
 	err = virtnet_restore_up(vdev);
 	if (err)
 		return err;
drivers/scsi/virtio_scsi.c +4 -0
index 8471f38b730e..3aeddf6331d3 100644
@@ -1016,6 +1016,10 @@ static int virtscsi_restore(struct virtio_device *vdev)
 	struct virtio_scsi *vscsi = shost_priv(sh);
 	int err;
 
+	err = virtio_device_reset_and_restore_status(vdev);
+	if (err)
+		return err;
+
 	err = virtscsi_init(vdev, vscsi);
 	if (err)
 		return err;
drivers/virtio/virtio.c +23 -12
index b9095751e43b..0446b30c83d6 100644
@@ -549,7 +549,7 @@ int virtio_device_freeze(struct virtio_device *dev)
 }
 EXPORT_SYMBOL_GPL(virtio_device_freeze);
 
-int virtio_device_restore(struct virtio_device *dev)
+int virtio_device_reset_and_restore_status(struct virtio_device *dev)
 {
 	struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
 	int ret;
@@ -574,23 +574,34 @@ int virtio_device_restore(struct virtio_device *dev)
 
 	ret = dev->config->finalize_features(dev);
 	if (ret)
-		goto err;
+		return ret;
 
 	ret = virtio_features_ok(dev);
 	if (ret)
-		goto err;
+		return ret;
 
-	if (drv->restore) {
-		ret = drv->restore(dev);
-		if (ret)
-			goto err;
-	}
+	return 0;
+}
+EXPORT_SYMBOL_GPL(virtio_device_reset_and_restore_status);
 
-	/* If restore didn't do it, mark device DRIVER_OK ourselves. */
-	if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
-		virtio_device_ready(dev);
+int virtio_device_restore(struct virtio_device *dev)
+{
+	struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
+	int ret;
 
-	virtio_config_core_enable(dev);
+	if (drv) {
+		if (drv->restore) {
+			ret = drv->restore(dev);
+			if (ret)
+				goto err;
+		}
+
+		/* If restore didn't do it, mark device DRIVER_OK ourselves. */
+		if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
+			virtio_device_ready(dev);
+
+		virtio_config_core_enable(dev);
+	}
 
 	return 0;
 
drivers/virtio/virtio_balloon.c +4 -0
index b36d2803674e..ba92b3e56391 100644
@@ -1163,6 +1163,10 @@ static int virtballoon_restore(struct virtio_device *vdev)
 	struct virtio_balloon *vb = vdev->priv;
 	int ret;
 
+	ret = virtio_device_reset_and_restore_status(vdev);
+	if (ret)
+		return ret;
+
 	ret = init_vqs(vdev->priv);
 	if (ret)
 		return ret;
drivers/virtio/virtio_input.c +4 -0
index a5d63269f20b..81c2ffd0e76e 100644
@@ -374,6 +374,10 @@ static int virtinput_restore(struct virtio_device *vdev)
 	struct virtio_input *vi = vdev->priv;
 	int err;
 
+	err = virtio_device_reset_and_restore_status(vdev);
+	if (err)
+		return err;
+
 	err = virtinput_init_vqs(vi);
 	if (err)
 		return err;
drivers/virtio/virtio_mem.c +4 -0
index b0b871441578..47c23aa43c20 100644
@@ -3018,6 +3018,10 @@ static int virtio_mem_restore(struct virtio_device *vdev)
 	struct virtio_mem *vm = vdev->priv;
 	int ret;
 
+	ret = virtio_device_reset_and_restore_status(vdev);
+	if (ret)
+		return ret;
+
 	ret = virtio_mem_init_vq(vm);
 	if (ret)
 		return ret;
include/linux/virtio.h +1 -0
index 306137a15d07..ab850146f0a8 100644
@@ -175,6 +175,7 @@ void virtio_config_driver_enable(struct virtio_device *dev);
 
 #ifdef CONFIG_PM_SLEEP
 int virtio_device_freeze(struct virtio_device *dev);
+int virtio_device_reset_and_restore_status(struct virtio_device *dev);
 int virtio_device_restore(struct virtio_device *dev);
 #endif
 void virtio_reset_device(struct virtio_device *dev);
sound/virtio/virtio_card.c +4 -0
index 965209e1d872..3c7a6d76c46c 100644
@@ -412,6 +412,10 @@ static int virtsnd_restore(struct virtio_device *vdev)
 	struct virtio_snd *snd = vdev->priv;
 	int rc;
 
+	rc = virtio_device_reset_and_restore_status(vdev);
+	if (rc)
+		return rc;
+
 	rc = virtsnd_find_vqs(snd);
 	if (rc)
 		return rc;
Signature · 1 line
2.45.2
#1 lore raw
kernel test robot noticed the following build errors:

Hi,

kernel test robot noticed the following build errors:

[auto build test ERROR on char-misc/char-misc-testing]
[also build test ERROR on char-misc/char-misc-next char-misc/char-misc-linus mst-vhost/linux-next axboe-block/for-next herbert-cryptodev-2.6/master andi-shyti/i2c/i2c-host mkp-scsi/for-next jejb-scsi/for-next tiwai-sound/for-next tiwai-sound/for-linus linus/master v6.12-rc5 next-20241031] [cannot apply to herbert-crypto-2.6/master]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/qiang4-zhang-linux-intel-com/virtio-only-reset-device-and-restore-status-if-needed-in-device-resume/20241031-111315
base:   char-misc/char-misc-testing
patch link:    https://lore.kernel.org/r/20241031030847.3253873-1-qiang4.zhang%40linux.intel.com
patch subject: [PATCH] virtio: only reset device and restore status if needed in device resume
config: arc-randconfig-002-20241031 (https://download.01.org/0day-ci/archive/20241031/[email protected]/config)
compiler: arc-elf-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241031/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/char/hw_random/virtio-rng.c: In function 'virtrng_restore':

>> drivers/char/hw_random/virtio-rng.c:221:15: error: implicit declaration of function 'virtio_device_reset_and_restore_status' [-Werror=implicit-function-declaration]
     221 |         err = virtio_device_reset_and_restore_status(vdev);
         |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   cc1: some warnings being treated as errors
--
   drivers/i2c/busses/i2c-virtio.c: In function 'virtio_i2c_restore':
>> drivers/i2c/busses/i2c-virtio.c:256:15: error: implicit declaration of function 'virtio_device_reset_and_restore_status' [-Werror=implicit-function-declaration]
     256 |         ret = virtio_device_reset_and_restore_status(vdev);
         |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors

Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for GET_FREE_REGION Depends on [n]: SPARSEMEM [=n]
Selected by [y]:
- RESOURCE_KUNIT_TEST [=y] && RUNTIME_TESTING_MENU [=y] && KUNIT [=y]

vim +/virtio_device_reset_and_restore_status +221 drivers/char/hw_random/virtio-rng.c

216
217 static int virtrng_restore(struct virtio_device *vdev)
218 {
219 int err;
220

 > 221		err = virtio_device_reset_and_restore_status(vdev);

222 if (err)
223 return err;
224
225 err = probe_common(vdev);
226 if (!err) {
227 struct virtrng_info *vi = vdev->priv;
228
229 /*
230 * Set hwrng_removed to ensure that virtio_read()
231 * does not block waiting for data before the
232 * registration is complete.
233 */
234 vi->hwrng_removed = true;
235 err = hwrng_register(&vi->hwrng);
236 if (!err) {
237 vi->hwrng_register_done = true;
238 vi->hwrng_removed = false;
239 }
240 }
241
242 return err;
243 }
244

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to [email protected]
kernel test robot noticed the following build errors:

Hi,

kernel test robot noticed the following build errors:

[auto build test ERROR on char-misc/char-misc-testing]
[also build test ERROR on char-misc/char-misc-next char-misc/char-misc-linus mst-vhost/linux-next axboe-block/for-next herbert-cryptodev-2.6/master andi-shyti/i2c/i2c-host mkp-scsi/for-next jejb-scsi/for-next tiwai-sound/for-next tiwai-sound/for-linus linus/master v6.12-rc5 next-20241031] [cannot apply to herbert-crypto-2.6/master]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/qiang4-zhang-linux-intel-com/virtio-only-reset-device-and-restore-status-if-needed-in-device-resume/20241031-111315
base:   char-misc/char-misc-testing
patch link:    https://lore.kernel.org/r/20241031030847.3253873-1-qiang4.zhang%40linux.intel.com
patch subject: [PATCH] virtio: only reset device and restore status if needed in device resume
config: x86_64-buildonly-randconfig-003-20241031 (https://download.01.org/0day-ci/archive/20241031/[email protected]/config)
compiler: clang version 19.1.3 (https://github.com/llvm/llvm-project ab51eccf88f5321e7c60591c5546b254b6afab99)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241031/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from drivers/i2c/busses/i2c-virtio.c:14:
In file included from include/linux/i2c.h:19:
In file included from include/linux/regulator/consumer.h:35:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:21:
In file included from include/linux/mm.h:2213:
include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]

     518 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
>> drivers/i2c/busses/i2c-virtio.c:256:8: error: call to undeclared function 'virtio_device_reset_and_restore_status'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     256 |         ret = virtio_device_reset_and_restore_status(vdev);
         |               ^

1 warning and 1 error generated.

vim +/virtio_device_reset_and_restore_status +256 drivers/i2c/busses/i2c-virtio.c

251
252 static int virtio_i2c_restore(struct virtio_device *vdev)
253 {
254 int ret;
255

 > 256		ret = virtio_device_reset_and_restore_status(vdev);

257 if (ret)
258 return ret;
259
260 return virtio_i2c_setup_vqs(vdev->priv);
261 }
262

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw

PATCH v2 1/1 virtio: only reset device and restore status if needed in device resume

In reply to [email protected]

From: Qiang Zhang <[email protected]>

Virtio core unconditionally reset and restore status for all virtio devices before calling restore method. This breaks some virtio drivers which don't need to do anything in suspend and resume because they just want to keep device state retained.

Virtio GPIO is a typical example. GPIO states should be kept unchanged after suspend and resume (e.g. output pins keep driving the output) and Virtio GPIO driver does nothing in freeze and restore methods. But the reset operation in virtio_device_restore breaks this.

Since some devices need reset in suspend and resume while some needn't, create a new helper function for the original reset and status restore logic so that virtio drivers can invoke it in their restore method if necessary.

---
v1 -> v2:
- Fix compilation error when CONFIG_PM_SLEEP is disabled. Move virtio_device_reset_and_restore_status out of CONFIG_PM_SLEEP scope.

 drivers/block/virtio_blk.c                 |  4 ++
 drivers/char/hw_random/virtio-rng.c        |  4 ++
 drivers/char/virtio_console.c              |  4 ++
 drivers/crypto/virtio/virtio_crypto_core.c |  4 ++
 drivers/i2c/busses/i2c-virtio.c            |  6 ++
 drivers/net/virtio_net.c                   |  4 ++
 drivers/scsi/virtio_scsi.c                 |  4 ++
 drivers/virtio/virtio.c                    | 83 ++++++++++++----------
 drivers/virtio/virtio_balloon.c            |  4 ++
 drivers/virtio/virtio_input.c              |  4 ++
 drivers/virtio/virtio_mem.c                |  4 ++
 include/linux/virtio.h                     |  1 +
 sound/virtio/virtio_card.c                 |  4 ++
 13 files changed, 94 insertions(+), 36 deletions(-)
drivers/block/virtio_blk.c +4 -0
index 194417abc105..bba7148bd219 100644
@@ -1612,6 +1612,10 @@ static int virtblk_restore(struct virtio_device *vdev)
 	struct virtio_blk *vblk = vdev->priv;
 	int ret;
 
+	ret = virtio_device_reset_and_restore_status(vdev);
+	if (ret)
+		return ret;
+
 	ret = init_vq(vdev->priv);
 	if (ret)
 		return ret;
drivers/char/hw_random/virtio-rng.c +4 -0
index dd998f4fe4f2..954280514f5a 100644
@@ -218,6 +218,10 @@ static int virtrng_restore(struct virtio_device *vdev)
 {
 	int err;
 
+	err = virtio_device_reset_and_restore_status(vdev);
+	if (err)
+		return err;
+
 	err = probe_common(vdev);
 	if (!err) {
 		struct virtrng_info *vi = vdev->priv;
drivers/char/virtio_console.c +4 -0
index c62b208b42f1..c431e4d5cd29 100644
@@ -2132,6 +2132,10 @@ static int virtcons_restore(struct virtio_device *vdev)
 
 	portdev = vdev->priv;
 
+	ret = virtio_device_reset_and_restore_status(vdev);
+	if (ret)
+		return ret;
+
 	ret = init_vqs(portdev);
 	if (ret)
 		return ret;
drivers/crypto/virtio/virtio_crypto_core.c +4 -0
index d0278eb568b9..b3363dd80448 100644
@@ -536,6 +536,10 @@ static int virtcrypto_restore(struct virtio_device *vdev)
 	struct virtio_crypto *vcrypto = vdev->priv;
 	int err;
 
+	err = virtio_device_reset_and_restore_status(vdev);
+	if (err)
+		return err;
+
 	err = virtcrypto_init_vqs(vcrypto);
 	if (err)
 		return err;
drivers/i2c/busses/i2c-virtio.c +6 -0
index 2a351f961b89..ce6493d6fe8e 100644
@@ -251,6 +251,12 @@ static int virtio_i2c_freeze(struct virtio_device *vdev)
 
 static int virtio_i2c_restore(struct virtio_device *vdev)
 {
+	int ret;
+
+	ret = virtio_device_reset_and_restore_status(vdev);
+	if (ret)
+		return ret;
+
 	return virtio_i2c_setup_vqs(vdev->priv);
 }
 
drivers/net/virtio_net.c +4 -0
index 792e9eadbfc3..5be2a5f68f68 100644
@@ -6693,6 +6693,10 @@ static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
 	struct virtnet_info *vi = vdev->priv;
 	int err;
 
+	err = virtio_device_reset_and_restore_status(vdev);
+	if (err)
+		return err;
+
 	err = virtnet_restore_up(vdev);
 	if (err)
 		return err;
drivers/scsi/virtio_scsi.c +4 -0
index 8471f38b730e..3aeddf6331d3 100644
@@ -1016,6 +1016,10 @@ static int virtscsi_restore(struct virtio_device *vdev)
 	struct virtio_scsi *vscsi = shost_priv(sh);
 	int err;
 
+	err = virtio_device_reset_and_restore_status(vdev);
+	if (err)
+		return err;
+
 	err = virtscsi_init(vdev, vscsi);
 	if (err)
 		return err;
drivers/virtio/virtio.c +47 -36

97 lines not on this page · raw message

drivers/virtio/virtio_balloon.c +4 -0
index b36d2803674e..ba92b3e56391 100644
@@ -1163,6 +1163,10 @@ static int virtballoon_restore(struct virtio_device *vdev)
 	struct virtio_balloon *vb = vdev->priv;
 	int ret;
 
+	ret = virtio_device_reset_and_restore_status(vdev);
+	if (ret)
+		return ret;
+
 	ret = init_vqs(vdev->priv);
 	if (ret)
 		return ret;
drivers/virtio/virtio_input.c +4 -0
index a5d63269f20b..81c2ffd0e76e 100644
@@ -374,6 +374,10 @@ static int virtinput_restore(struct virtio_device *vdev)
 	struct virtio_input *vi = vdev->priv;
 	int err;
 
+	err = virtio_device_reset_and_restore_status(vdev);
+	if (err)
+		return err;
+
 	err = virtinput_init_vqs(vi);
 	if (err)
 		return err;
drivers/virtio/virtio_mem.c +4 -0
index b0b871441578..47c23aa43c20 100644
@@ -3018,6 +3018,10 @@ static int virtio_mem_restore(struct virtio_device *vdev)
 	struct virtio_mem *vm = vdev->priv;
 	int ret;
 
+	ret = virtio_device_reset_and_restore_status(vdev);
+	if (ret)
+		return ret;
+
 	ret = virtio_mem_init_vq(vm);
 	if (ret)
 		return ret;
include/linux/virtio.h +1 -0
index 306137a15d07..bce26597b8fc 100644
@@ -178,6 +178,7 @@ int virtio_device_freeze(struct virtio_device *dev);
 int virtio_device_restore(struct virtio_device *dev);
 #endif
 void virtio_reset_device(struct virtio_device *dev);
+int virtio_device_reset_and_restore_status(struct virtio_device *dev);
 
 size_t virtio_max_dma_size(const struct virtio_device *vdev);
 
sound/virtio/virtio_card.c +4 -0
index 965209e1d872..3c7a6d76c46c 100644
@@ -412,6 +412,10 @@ static int virtsnd_restore(struct virtio_device *vdev)
 	struct virtio_snd *snd = vdev->priv;
 	int rc;
 
+	rc = virtio_device_reset_and_restore_status(vdev);
+	if (rc)
+		return rc;
+
 	rc = virtsnd_find_vqs(snd);
 	if (rc)
 		return rc;
Signature · 1 line
2.45.2
#4 lore raw
Quoted 7 lines · On Fri, Nov 1, 2024 at 9:54 AM <[email protected]> wrote:

From: Qiang Zhang <[email protected]>

Virtio core unconditionally reset and restore status for all virtio devices before calling restore method. This breaks some virtio drivers which don't need to do anything in suspend and resume because they just want to keep device state retained.

The challenge is how can driver know device doesn't need rest.

For example, PCI has no_soft_reset which has been done in the commit "virtio: Add support for no-reset virtio PCI PM".

And there's a ongoing long discussion of adding suspend support in the virtio spec, then driver know it's safe to suspend/resume without reset.

Quoted 5 lines

Virtio GPIO is a typical example. GPIO states should be kept unchanged after suspend and resume (e.g. output pins keep driving the output) and Virtio GPIO driver does nothing in freeze and restore methods. But the reset operation in virtio_device_restore breaks this.

Is this mandated by GPIO or virtio spec? If yes, let's quote the revelant part.

Quoted 5 lines

Since some devices need reset in suspend and resume while some needn't, create a new helper function for the original reset and status restore logic so that virtio drivers can invoke it in their restore method if necessary.

How are those drivers classified?

Quoted 2 lines

Thanks

#5 lore raw
Quoted 10 lines · Jason Wang
Quoted 7 lines · On Fri, Nov 1, 2024 at 9:54 AM <[email protected]> wrote:

From: Qiang Zhang <[email protected]>

Virtio core unconditionally reset and restore status for all virtio devices before calling restore method. This breaks some virtio drivers which don't need to do anything in suspend and resume because they just want to keep device state retained.

The challenge is how can driver know device doesn't need rest.

Hi,

Per my understanding to PM, in the suspend flow, device drivers need to
1. First manage/stop accesses from upper level software and
2. Store the volatile context into in-memory data structures.
3. Put devices into some low power (suspended) state.
The resume process does the reverse.
If a device context won't loose after entering some low power state (optional), it's OK to skip step 2.

For virtio devices, spec doesn't define whether their states will lost after platform entering suspended state. So to work with different hypervisors, virtio drivers typically trigger a reset in suspend/resume flow. This works fine for virtio devices if following conditions are met:
- Device state can be totally recoverable.
- There isn't any working behaviour expected in suspended state, i.e. the suspended state should be sub-state of reset.
However, the first point may be hard to implement from driver side for some devices. The second point may be unacceptable for some kind of devices.

For your question, for devices whose suspended state is alike reset state, the hypervisor have the flexibility to retain its state or not, kernel driver can unconditionally reset it with proper re-initialization to accomplish better compatibility. For others, hypervisor *must* retain device state and driver just keeps using it.

Quoted 7 lines

For example, PCI has no_soft_reset which has been done in the commit "virtio: Add support for no-reset virtio PCI PM".

And there's a ongoing long discussion of adding suspend support in the virtio spec, then driver know it's safe to suspend/resume without reset.

That's great! Hopefully it can fill the gap.
Currently, I think we can safely move the reset to drivers' freeze methods, virtio core has no reason to take it as a common action required by all devices. And the reset operation can be optional skipped if driver have hints from device that it can retain state.

Quoted 8 lines
Quoted 6 lines

Virtio GPIO is a typical example. GPIO states should be kept unchanged after suspend and resume (e.g. output pins keep driving the output) and Virtio GPIO driver does nothing in freeze and restore methods. But the reset operation in virtio_device_restore breaks this.

Is this mandated by GPIO or virtio spec? If yes, let's quote the revelant part.

No. But in actual hardware design (e.g. Intel PCH GPIO), or from the requirement perspective, GPIO pin state can be (should support) retained in suspended state.
If Virtio GPIO is used to let VM operate such physical GPIO chip indirectly, it can't be reset in suspend and resume. Meanwhile the hypervisor will retain pin states after suspension.

Quoted 8 lines
Quoted 6 lines

Since some devices need reset in suspend and resume while some needn't, create a new helper function for the original reset and status restore logic so that virtio drivers can invoke it in their restore method if necessary.

How are those drivers classified?

I think this depends whether hypervisor will keep devices state in platform suspend process. I think hypervisor should because suspend and reset are conceptually two different things.

Thanks
Qiang

#6 lore raw
Quoted 23 lines · Qiang Zhang
Quoted 10 lines · Jason Wang
Quoted 7 lines · On Fri, Nov 1, 2024 at 9:54 AM <[email protected]> wrote:

From: Qiang Zhang <[email protected]>

Virtio core unconditionally reset and restore status for all virtio devices before calling restore method. This breaks some virtio drivers which don't need to do anything in suspend and resume because they just want to keep device state retained.

The challenge is how can driver know device doesn't need rest.

Hi,

Per my understanding to PM, in the suspend flow, device drivers need to
1. First manage/stop accesses from upper level software and
2. Store the volatile context into in-memory data structures.
3. Put devices into some low power (suspended) state.
The resume process does the reverse.
If a device context won't loose after entering some low power state (optional), it's OK to skip step 2.

Quoted 2 lines

For virtio devices, spec doesn't define whether their states will lost after platform entering suspended state.

This is exactly what suspend patch tries to define.

Quoted 9 lines

So to work with different
hypervisors, virtio drivers typically trigger a reset in suspend/resume flow. This works fine for virtio devices if following conditions are met:
- Device state can be totally recoverable.
- There isn't any working behaviour expected in suspended state, i.e. the suspended state should be sub-state of reset.
However, the first point may be hard to implement from driver side for some devices. The second point may be unacceptable for some kind of devices.

Quoted 5 lines

For your question, for devices whose suspended state is alike reset state, the hypervisor have the flexibility to retain its state or not, kernel driver can unconditionally reset it with proper re-initialization to accomplish better compatibility. For others, hypervisor *must* retain device state and driver just keeps using it.

Right, so my question is how did the driver know the behaviour of a device? We usually do that via a feature bit.

Note that the thing that matters here is the migration compatibility.

Quoted 9 lines
Quoted 8 lines

For example, PCI has no_soft_reset which has been done in the commit "virtio: Add support for no-reset virtio PCI PM".

And there's a ongoing long discussion of adding suspend support in the virtio spec, then driver know it's safe to suspend/resume without reset.

Quoted 5 lines

That's great! Hopefully it can fill the gap.
Currently, I think we can safely move the reset to drivers' freeze methods, virtio core has no reason to take it as a common action required by all devices. And the reset operation can be optional skipped if driver have hints from device that it can retain state.

The problem here is whether the device can be resumed without "soft reset" seems a general feature which could be either the knowledge of

1) virtio core (a feature bit or not)

or

2) transport layer (like PCI)

Quoted 24 lines
Quoted 9 lines
Quoted 7 lines

Virtio GPIO is a typical example. GPIO states should be kept unchanged after suspend and resume (e.g. output pins keep driving the output) and Virtio GPIO driver does nothing in freeze and restore methods. But the reset operation in virtio_device_restore breaks this.

Is this mandated by GPIO or virtio spec? If yes, let's quote the revelant part.

No. But in actual hardware design (e.g. Intel PCH GPIO), or from the requirement perspective, GPIO pin state can be (should support) retained in suspended state.
If Virtio GPIO is used to let VM operate such physical GPIO chip indirectly, it can't be reset in suspend and resume. Meanwhile the hypervisor will retain pin states after suspension.

Quoted 7 lines
Quoted 6 lines

Since some devices need reset in suspend and resume while some needn't, create a new helper function for the original reset and status restore logic so that virtio drivers can invoke it in their restore method if necessary.

Quoted 4 lines
Quoted 1 line

How are those drivers classified?

I think this depends whether hypervisor will keep devices state in platform suspend process.

So the problem is that the actual implementation (hypervisor, physical device or mediation) is transparent to the driver. Driver needs a general way to know whether it's safe (or not) to reset during the suspend/resume.

Quoted 2 lines

I think hypervisor should because suspend and reset are conceptually two different things.

Probably, but rest is and doing software state load/save is common practice for devices that will lose their state during PM.

Thanks

Quoted 5 lines

Thanks
Qiang

#7 lore raw
In reply to Jason Wang
Quoted 10 lines · Jason Wang
Quoted 7 lines · On Fri, Nov 1, 2024 at 9:54 AM <[email protected]> wrote:

From: Qiang Zhang <[email protected]>

Virtio core unconditionally reset and restore status for all virtio devices before calling restore method. This breaks some virtio drivers which don't need to do anything in suspend and resume because they just want to keep device state retained.

The challenge is how can driver know device doesn't need rest.

I actually don't remember why do we do reset on restore. Do you?

Quoted 27 lines

For example, PCI has no_soft_reset which has been done in the commit "virtio: Add support for no-reset virtio PCI PM".

And there's a ongoing long discussion of adding suspend support in the virtio spec, then driver know it's safe to suspend/resume without reset.

Quoted 5 lines

Virtio GPIO is a typical example. GPIO states should be kept unchanged after suspend and resume (e.g. output pins keep driving the output) and Virtio GPIO driver does nothing in freeze and restore methods. But the reset operation in virtio_device_restore breaks this.

Is this mandated by GPIO or virtio spec? If yes, let's quote the revelant part.

Quoted 5 lines

Since some devices need reset in suspend and resume while some needn't, create a new helper function for the original reset and status restore logic so that virtio drivers can invoke it in their restore method if necessary.

How are those drivers classified?

Quoted 2 lines

Thanks

#8 lore raw
Quoted 6 lines · Michael S. Tsirkin
Quoted 4 lines · Jason Wang
Quoted 3 lines · On Fri, Nov 1, 2024 at 9:54 AM <[email protected]> wrote:

From: Qiang Zhang <[email protected]>

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

Virtio core unconditionally reset and restore status for all virtio devices before calling restore method. This breaks some virtio drivers which don't need to do anything in suspend and resume because they just want to keep device state retained.

The challenge is how can driver know device doesn't need rest.

I actually don't remember why do we do reset on restore. Do you?

Because the driver doesn't know if the device can keep its state, so it chooses to start from that.

Thanks