Skip to contentSkip to conversation

xen-netfront: remove warning when unloading module

PATCH Applied Eduardo Otubo 4 participants 11 messages Nov 23, 2017 – Feb 2, 2018

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

PATCH v2 1/1 xen-netfront: remove warning when unloading module

v2:
* Replace busy wait with wait_event()/wake_up_all()
* Cannot garantee that at the time xennet_remove is called, the xen_netback state will not be XenbusStateClosed, so added a condition for that
* There's a small chance for the xen_netback state is XenbusStateUnknown by the time the xen_netfront switches to Closed, so added a condition for that.

When unloading module xen_netfront from guest, dmesg would output warning messages like below:

  [  105.236836] xen:grant_table: WARNING: g.e. 0x903 still in use!
  [  105.236839] deferring g.e. 0x903 (pfn 0x35805)

This problem relies on netfront and netback being out of sync. By the time netfront revokes the g.e.'s netback didn't have enough time to free all of them, hence displaying the warnings on dmesg.

The trick here is to make netfront to wait until netback frees all the g.e.'s and only then continue to cleanup for the module removal, and this is done by manipulating both device states.

---
 drivers/net/xen-netfront.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/net/xen-netfront.c +18 -0
index 8b8689c6d887..391432e2725d 100644
@@ -87,6 +87,8 @@ struct netfront_cb {
 /* IRQ name is queue name with "-tx" or "-rx" appended */
 #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
 
+static DECLARE_WAIT_QUEUE_HEAD(module_unload_q);
+
 struct netfront_stats {
 	u64			packets;
 	u64			bytes;
@@ -2021,10 +2023,12 @@ static void netback_changed(struct xenbus_device *dev,
 		break;
 
 	case XenbusStateClosed:
+		wake_up_all(&module_unload_q);
 		if (dev->state == XenbusStateClosed)
 			break;
 		/* Missed the backend's CLOSING state -- fallthrough */
 	case XenbusStateClosing:
+		wake_up_all(&module_unload_q);
 		xenbus_frontend_closed(dev);
 		break;
 	}
@@ -2130,6 +2134,20 @@ static int xennet_remove(struct xenbus_device *dev)
 
 	dev_dbg(&dev->dev, "%s\n", dev->nodename);
 
+	if (xenbus_read_driver_state(dev->otherend) != XenbusStateClosed) {
+		xenbus_switch_state(dev, XenbusStateClosing);
+		wait_event(module_unload_q,
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateClosing);
+
+		xenbus_switch_state(dev, XenbusStateClosed);
+		wait_event(module_unload_q,
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateClosed ||
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateUnknown);
+	}
+
 	xennet_disconnect_backend(info);
 
 	unregister_netdev(info->netdev);
Signature · 1 line
2.13.6
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 19 lines · Eduardo Otubo

v2:
* Replace busy wait with wait_event()/wake_up_all()
* Cannot garantee that at the time xennet_remove is called, the xen_netback state will not be XenbusStateClosed, so added a condition for that
* There's a small chance for the xen_netback state is XenbusStateUnknown by the time the xen_netfront switches to Closed, so added a condition for that.

When unloading module xen_netfront from guest, dmesg would output warning messages like below:

  [  105.236836] xen:grant_table: WARNING: g.e. 0x903 still in use!
  [  105.236839] deferring g.e. 0x903 (pfn 0x35805)

This problem relies on netfront and netback being out of sync. By the time netfront revokes the g.e.'s netback didn't have enough time to free all of them, hence displaying the warnings on dmesg.

Quoted 5 lines

The trick here is to make netfront to wait until netback frees all the g.e.'s and only then continue to cleanup for the module removal, and this is done by manipulating both device states.

Juergen

maintainer Acked-by on v2 1/1
#2 lore raw
In reply to Eduardo Otubo
Quoted 19 lines · Eduardo Otubo

v2:
* Replace busy wait with wait_event()/wake_up_all()
* Cannot garantee that at the time xennet_remove is called, the xen_netback state will not be XenbusStateClosed, so added a condition for that
* There's a small chance for the xen_netback state is XenbusStateUnknown by the time the xen_netfront switches to Closed, so added a condition for that.

When unloading module xen_netfront from guest, dmesg would output warning messages like below:

  [  105.236836] xen:grant_table: WARNING: g.e. 0x903 still in use!
  [  105.236839] deferring g.e. 0x903 (pfn 0x35805)

This problem relies on netfront and netback being out of sync. By the time netfront revokes the g.e.'s netback didn't have enough time to free all of them, hence displaying the warnings on dmesg.

Quoted 5 lines

The trick here is to make netfront to wait until netback frees all the g.e.'s and only then continue to cleanup for the module removal, and this is done by manipulating both device states.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

maintainer Applied
#3 lore raw
In reply to Eduardo Otubo

From: Eduardo Otubo <[email protected]>
Date: Thu, 23 Nov 2017 15:18:35 +0100

Quoted 19 lines

v2:
* Replace busy wait with wait_event()/wake_up_all()
* Cannot garantee that at the time xennet_remove is called, the xen_netback state will not be XenbusStateClosed, so added a condition for that
* There's a small chance for the xen_netback state is XenbusStateUnknown by the time the xen_netfront switches to Closed, so added a condition for that.

When unloading module xen_netfront from guest, dmesg would output warning messages like below:

  [  105.236836] xen:grant_table: WARNING: g.e. 0x903 still in use!
  [  105.236839] deferring g.e. 0x903 (pfn 0x35805)

This problem relies on netfront and netback being out of sync. By the time netfront revokes the g.e.'s netback didn't have enough time to free all of them, hence displaying the warnings on dmesg.

Quoted 5 lines

The trick here is to make netfront to wait until netback frees all the g.e.'s and only then continue to cleanup for the module removal, and this is done by manipulating both device states.

Applied, thank you.

maintainer Applied
#4 lore raw
In reply to Eduardo Otubo

From: Eduardo Otubo <[email protected]>
Date: Thu, 23 Nov 2017 15:18:35 +0100

Quoted 19 lines

v2:
* Replace busy wait with wait_event()/wake_up_all()
* Cannot garantee that at the time xennet_remove is called, the xen_netback state will not be XenbusStateClosed, so added a condition for that
* There's a small chance for the xen_netback state is XenbusStateUnknown by the time the xen_netfront switches to Closed, so added a condition for that.

When unloading module xen_netfront from guest, dmesg would output warning messages like below:

  [  105.236836] xen:grant_table: WARNING: g.e. 0x903 still in use!
  [  105.236839] deferring g.e. 0x903 (pfn 0x35805)

This problem relies on netfront and netback being out of sync. By the time netfront revokes the g.e.'s netback didn't have enough time to free all of them, hence displaying the warnings on dmesg.

Quoted 5 lines

The trick here is to make netfront to wait until netback frees all the g.e.'s and only then continue to cleanup for the module removal, and this is done by manipulating both device states.

Applied, thank you.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
In reply to Eduardo Otubo

Hi, Eduardo!

I am working on a frontend driver (PV DRM) and also seeing some strange

things on driver unloading:

xt# rmmod -f drm_xen_front.ko

[ 3236.462497] [drm] Unregistering XEN PV vdispl
[ 3236.485745] [drm:xen_drv_remove [drm_xen_front]] *ERROR* Backend state is InitWait while removing driver
[ 3236.486950] vdispl vdispl-0: 22 freeing event channel 11
[ 3236.496123] vdispl vdispl-0: failed to write error node for device/vdispl/0 (22 freeing event channel 11)
[ 3236.496271] vdispl vdispl-0: 22 freeing event channel 12
[ 3236.501633] vdispl vdispl-0: failed to write error node for device/vdispl/0 (22 freeing event channel 12)

These are somewhat different from your use-case with grant references, but I have a question:

do you really see that XenbusStateClosed and XenbusStateClosing are

called? In my driver I can't see those and once I tried to dig deeper into the problem

I saw that on driver removal it is disconnected from XenBus, so no backend state

change events come in via .otherend_changed callback.

The only difference I see here is that the backend is a user-space application

Thank you,
Oleksandr

Quoted 75 lines · Eduardo Otubo

v2:
* Replace busy wait with wait_event()/wake_up_all()
* Cannot garantee that at the time xennet_remove is called, the xen_netback state will not be XenbusStateClosed, so added a condition for that
* There's a small chance for the xen_netback state is XenbusStateUnknown by the time the xen_netfront switches to Closed, so added a condition for that.

When unloading module xen_netfront from guest, dmesg would output warning messages like below:

   [  105.236836] xen:grant_table: WARNING: g.e. 0x903 still in use!
   [  105.236839] deferring g.e. 0x903 (pfn 0x35805)

This problem relies on netfront and netback being out of sync. By the time netfront revokes the g.e.'s netback didn't have enough time to free all of them, hence displaying the warnings on dmesg.

The trick here is to make netfront to wait until netback frees all the g.e.'s and only then continue to cleanup for the module removal, and this is done by manipulating both device states.

---
  drivers/net/xen-netfront.c | 18 ++++++++++++++++++
  1 file changed, 18 insertions(+)
drivers/net/xen-netfront.c +18 -0
index 8b8689c6d887..391432e2725d 100644
@@ -87,6 +87,8 @@ struct netfront_cb {
  /* IRQ name is queue name with "-tx" or "-rx" appended */
  #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
 
+static DECLARE_WAIT_QUEUE_HEAD(module_unload_q);
+
  struct netfront_stats {
  	u64			packets;
  	u64			bytes;
@@ -2021,10 +2023,12 @@ static void netback_changed(struct xenbus_device *dev,
  		break;
 
  	case XenbusStateClosed:
+		wake_up_all(&module_unload_q);
  		if (dev->state == XenbusStateClosed)
  			break;
  		/* Missed the backend's CLOSING state -- fallthrough */
  	case XenbusStateClosing:
+		wake_up_all(&module_unload_q);
  		xenbus_frontend_closed(dev);
  		break;
  	}
@@ -2130,6 +2134,20 @@ static int xennet_remove(struct xenbus_device *dev)
 
  	dev_dbg(&dev->dev, "%s\n", dev->nodename);
 
+	if (xenbus_read_driver_state(dev->otherend) != XenbusStateClosed) {
+		xenbus_switch_state(dev, XenbusStateClosing);
+		wait_event(module_unload_q,
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateClosing);
+
+		xenbus_switch_state(dev, XenbusStateClosed);
+		wait_event(module_unload_q,
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateClosed ||
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateUnknown);
+	}
+
  	xennet_disconnect_backend(info);
 
  	unregister_netdev(info->netdev);
#6 lore raw
Quoted 28 lines · Oleksandr Andrushchenko

Hi, Eduardo!

I am working on a frontend driver (PV DRM) and also seeing some strange

things on driver unloading:

xt# rmmod -f drm_xen_front.ko

[ 3236.462497] [drm] Unregistering XEN PV vdispl
[ 3236.485745] [drm:xen_drv_remove [drm_xen_front]] *ERROR* Backend state is
InitWait while removing driver
[ 3236.486950] vdispl vdispl-0: 22 freeing event channel 11
[ 3236.496123] vdispl vdispl-0: failed to write error node for
device/vdispl/0 (22 freeing event channel 11)
[ 3236.496271] vdispl vdispl-0: 22 freeing event channel 12
[ 3236.501633] vdispl vdispl-0: failed to write error node for

device/vdispl/0 (22 freeing event channel 12)

These are somewhat different from your use-case with grant references, but I have a question:

do you really see that XenbusStateClosed and XenbusStateClosing are

called? In my driver I can't see those and once I tried to dig deeper into the problem

I saw that on driver removal it is disconnected from XenBus, so no backend state

Quoted 7 lines

change events come in via .otherend_changed callback.

The only difference I see here is that the backend is a user-space application

Thank you,
Oleksandr

To be honest, most of the things I assumed were true, according to some talks on IRC with maintainers. Since I assumed it was true I started to write code based on that and all the behaviors that followed were correct according to my assumptions (and discussions).

But if you find something else weird, please let me know and we can fix it.

Quoted 73 lines · Eduardo Otubo
Quoted 70 lines

v2:
* Replace busy wait with wait_event()/wake_up_all()
* Cannot garantee that at the time xennet_remove is called, the xen_netback state will not be XenbusStateClosed, so added a condition for that
* There's a small chance for the xen_netback state is XenbusStateUnknown by the time the xen_netfront switches to Closed, so added a condition for that.

When unloading module xen_netfront from guest, dmesg would output warning messages like below:

   [  105.236836] xen:grant_table: WARNING: g.e. 0x903 still in use!
   [  105.236839] deferring g.e. 0x903 (pfn 0x35805)

This problem relies on netfront and netback being out of sync. By the time netfront revokes the g.e.'s netback didn't have enough time to free all of them, hence displaying the warnings on dmesg.

The trick here is to make netfront to wait until netback frees all the g.e.'s and only then continue to cleanup for the module removal, and this is done by manipulating both device states.

---
  drivers/net/xen-netfront.c | 18 ++++++++++++++++++
  1 file changed, 18 insertions(+)
drivers/net/xen-netfront.c +18 -0
index 8b8689c6d887..391432e2725d 100644
@@ -87,6 +87,8 @@ struct netfront_cb {
  /* IRQ name is queue name with "-tx" or "-rx" appended */
  #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
+static DECLARE_WAIT_QUEUE_HEAD(module_unload_q);
+
  struct netfront_stats {
  	u64			packets;
  	u64			bytes;
@@ -2021,10 +2023,12 @@ static void netback_changed(struct xenbus_device *dev,
  		break;
  	case XenbusStateClosed:
+		wake_up_all(&module_unload_q);
  		if (dev->state == XenbusStateClosed)
  			break;
  		/* Missed the backend's CLOSING state -- fallthrough */
  	case XenbusStateClosing:
+		wake_up_all(&module_unload_q);
  		xenbus_frontend_closed(dev);
  		break;
  	}
@@ -2130,6 +2134,20 @@ static int xennet_remove(struct xenbus_device *dev)
  	dev_dbg(&dev->dev, "%s\n", dev->nodename);
+	if (xenbus_read_driver_state(dev->otherend) != XenbusStateClosed) {
+		xenbus_switch_state(dev, XenbusStateClosing);
+		wait_event(module_unload_q,
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateClosing);
+
+		xenbus_switch_state(dev, XenbusStateClosed);
+		wait_event(module_unload_q,
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateClosed ||
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateUnknown);
+	}
+
  	xennet_disconnect_backend(info);
  	unregister_netdev(info->netdev);
Signature · 1 line
Eduardo Otubo
#7 lore raw
Quoted 34 lines · Eduardo Otubo
Quoted 33 lines · Oleksandr Andrushchenko

Hi, Eduardo!

I am working on a frontend driver (PV DRM) and also seeing some strange

things on driver unloading:

xt# rmmod -f drm_xen_front.ko

[ 3236.462497] [drm] Unregistering XEN PV vdispl
[ 3236.485745] [drm:xen_drv_remove [drm_xen_front]] *ERROR* Backend state is
InitWait while removing driver
[ 3236.486950] vdispl vdispl-0: 22 freeing event channel 11
[ 3236.496123] vdispl vdispl-0: failed to write error node for
device/vdispl/0 (22 freeing event channel 11)
[ 3236.496271] vdispl vdispl-0: 22 freeing event channel 12
[ 3236.501633] vdispl vdispl-0: failed to write error node for

device/vdispl/0 (22 freeing event channel 12)

These are somewhat different from your use-case with grant references, but I have a question:

do you really see that XenbusStateClosed and XenbusStateClosing are

called? In my driver I can't see those and once I tried to dig deeper into the problem

I saw that on driver removal it is disconnected from XenBus, so no backend state

change events come in via .otherend_changed callback.

The only difference I see here is that the backend is a user-space application

Quoted 8 lines
Quoted 2 lines

Thank you,
Oleksandr

To be honest, most of the things I assumed were true, according to some talks on IRC with maintainers. Since I assumed it was true I started to write code based on that and all the behaviors that followed were correct according to my assumptions (and discussions).

But if you find something else weird, please let me know and we can fix it.

There is nothing wrong with the patch. One thing that I cannot get in my driver is that .otherend_changed callback is not called on .remove. Please see [1]

Quoted 63 lines
Quoted 63 lines · Eduardo Otubo
Quoted 62 lines

v2:
* Replace busy wait with wait_event()/wake_up_all()
* Cannot garantee that at the time xennet_remove is called, the xen_netback state will not be XenbusStateClosed, so added a condition for that
* There's a small chance for the xen_netback state is XenbusStateUnknown by the time the xen_netfront switches to Closed, so added a condition for that.

When unloading module xen_netfront from guest, dmesg would output warning messages like below:

    [  105.236836] xen:grant_table: WARNING: g.e. 0x903 still in use!
    [  105.236839] deferring g.e. 0x903 (pfn 0x35805)

This problem relies on netfront and netback being out of sync. By the time netfront revokes the g.e.'s netback didn't have enough time to free all of them, hence displaying the warnings on dmesg.

The trick here is to make netfront to wait until netback frees all the g.e.'s and only then continue to cleanup for the module removal, and this is done by manipulating both device states.

---
   drivers/net/xen-netfront.c | 18 ++++++++++++++++++
   1 file changed, 18 insertions(+)
drivers/net/xen-netfront.c +12 -0
index 8b8689c6d887..391432e2725d 100644
@@ -87,6 +87,8 @@ struct netfront_cb {
   /* IRQ name is queue name with "-tx" or "-rx" appended */
   #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
+static DECLARE_WAIT_QUEUE_HEAD(module_unload_q);
+
   struct netfront_stats {
   	u64			packets;
   	u64			bytes;
@@ -2021,10 +2023,12 @@ static void netback_changed(struct xenbus_device *dev,
   		break;
   	case XenbusStateClosed:
+		wake_up_all(&module_unload_q);
   		if (dev->state == XenbusStateClosed)
   			break;
   		/* Missed the backend's CLOSING state -- fallthrough */
   	case XenbusStateClosing:
+		wake_up_all(&module_unload_q);
   		xenbus_frontend_closed(dev);
   		break;
   	}
@@ -2130,6 +2134,20 @@ static int xennet_remove(struct xenbus_device *dev)
   	dev_dbg(&dev->dev, "%s\n", dev->nodename);
+	if (xenbus_read_driver_state(dev->otherend) != XenbusStateClosed) {
+		xenbus_switch_state(dev, XenbusStateClosing);
+		wait_event(module_unload_q,
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateClosing);
+
+		xenbus_switch_state(dev, XenbusStateClosed);
+		wait_event(module_unload_q,
Quoted 8 lines
Quoted 8 lines

drivers/net/xen-netfront.c static int xennet_remove(struct xenbus_device *dev) · line 2153

Quoted 8 lines
drivers/net/xen-netfront.c +6 -0
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateClosed ||
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateUnknown);
+	}
+
   	xennet_disconnect_backend(info);
   	unregister_netdev(info->netdev);

[1] https://patchwork.kernel.org/patch/10195163/

#8 lore raw
In reply to Eduardo Otubo
Quoted 34 lines · Eduardo Otubo
Quoted 33 lines · Oleksandr Andrushchenko

Hi, Eduardo!

I am working on a frontend driver (PV DRM) and also seeing some strange

things on driver unloading:

xt# rmmod -f drm_xen_front.ko

[ 3236.462497] [drm] Unregistering XEN PV vdispl
[ 3236.485745] [drm:xen_drv_remove [drm_xen_front]] *ERROR* Backend state is
InitWait while removing driver
[ 3236.486950] vdispl vdispl-0: 22 freeing event channel 11
[ 3236.496123] vdispl vdispl-0: failed to write error node for
device/vdispl/0 (22 freeing event channel 11)
[ 3236.496271] vdispl vdispl-0: 22 freeing event channel 12
[ 3236.501633] vdispl vdispl-0: failed to write error node for

device/vdispl/0 (22 freeing event channel 12)

These are somewhat different from your use-case with grant references, but I have a question:

do you really see that XenbusStateClosed and XenbusStateClosing are

called? In my driver I can't see those and once I tried to dig deeper into the problem

I saw that on driver removal it is disconnected from XenBus, so no backend state

change events come in via .otherend_changed callback.

The only difference I see here is that the backend is a user-space application

Quoted 8 lines
Quoted 2 lines

Thank you,
Oleksandr

To be honest, most of the things I assumed were true, according to some talks on IRC with maintainers. Since I assumed it was true I started to write code based on that and all the behaviors that followed were correct according to my assumptions (and discussions).

But if you find something else weird, please let me know and we can fix it.

There is nothing wrong with the patch. One thing that I cannot get in my driver is that .otherend_changed callback is not called on .remove. Please see [1]

Quoted 63 lines
Quoted 63 lines · Eduardo Otubo
Quoted 62 lines

v2:
* Replace busy wait with wait_event()/wake_up_all()
* Cannot garantee that at the time xennet_remove is called, the xen_netback state will not be XenbusStateClosed, so added a condition for that
* There's a small chance for the xen_netback state is XenbusStateUnknown by the time the xen_netfront switches to Closed, so added a condition for that.

When unloading module xen_netfront from guest, dmesg would output warning messages like below:

    [  105.236836] xen:grant_table: WARNING: g.e. 0x903 still in use!
    [  105.236839] deferring g.e. 0x903 (pfn 0x35805)

This problem relies on netfront and netback being out of sync. By the time netfront revokes the g.e.'s netback didn't have enough time to free all of them, hence displaying the warnings on dmesg.

The trick here is to make netfront to wait until netback frees all the g.e.'s and only then continue to cleanup for the module removal, and this is done by manipulating both device states.

---
   drivers/net/xen-netfront.c | 18 ++++++++++++++++++
   1 file changed, 18 insertions(+)
drivers/net/xen-netfront.c +12 -0
index 8b8689c6d887..391432e2725d 100644
@@ -87,6 +87,8 @@ struct netfront_cb {
   /* IRQ name is queue name with "-tx" or "-rx" appended */
   #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
+static DECLARE_WAIT_QUEUE_HEAD(module_unload_q);
+
   struct netfront_stats {
   	u64			packets;
   	u64			bytes;
@@ -2021,10 +2023,12 @@ static void netback_changed(struct xenbus_device *dev,
   		break;
   	case XenbusStateClosed:
+		wake_up_all(&module_unload_q);
   		if (dev->state == XenbusStateClosed)
   			break;
   		/* Missed the backend's CLOSING state -- fallthrough */
   	case XenbusStateClosing:
+		wake_up_all(&module_unload_q);
   		xenbus_frontend_closed(dev);
   		break;
   	}
@@ -2130,6 +2134,20 @@ static int xennet_remove(struct xenbus_device *dev)
   	dev_dbg(&dev->dev, "%s\n", dev->nodename);
+	if (xenbus_read_driver_state(dev->otherend) != XenbusStateClosed) {
+		xenbus_switch_state(dev, XenbusStateClosing);
+		wait_event(module_unload_q,
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateClosing);
+
+		xenbus_switch_state(dev, XenbusStateClosed);
+		wait_event(module_unload_q,
Quoted 8 lines
Quoted 8 lines

drivers/net/xen-netfront.c static int xennet_remove(struct xenbus_device *dev) · line 2153

Quoted 8 lines
drivers/net/xen-netfront.c +6 -0
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateClosed ||
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateUnknown);
+	}
+
   	xennet_disconnect_backend(info);
   	unregister_netdev(info->netdev);

[1] https://patchwork.kernel.org/patch/10195163/

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw
In reply to Oleksandr Andrushchenko
Quoted 28 lines · Oleksandr Andrushchenko

Hi, Eduardo!

I am working on a frontend driver (PV DRM) and also seeing some strange

things on driver unloading:

xt# rmmod -f drm_xen_front.ko

[ 3236.462497] [drm] Unregistering XEN PV vdispl
[ 3236.485745] [drm:xen_drv_remove [drm_xen_front]] *ERROR* Backend state is
InitWait while removing driver
[ 3236.486950] vdispl vdispl-0: 22 freeing event channel 11
[ 3236.496123] vdispl vdispl-0: failed to write error node for
device/vdispl/0 (22 freeing event channel 11)
[ 3236.496271] vdispl vdispl-0: 22 freeing event channel 12
[ 3236.501633] vdispl vdispl-0: failed to write error node for

device/vdispl/0 (22 freeing event channel 12)

These are somewhat different from your use-case with grant references, but I have a question:

do you really see that XenbusStateClosed and XenbusStateClosing are

called? In my driver I can't see those and once I tried to dig deeper into the problem

I saw that on driver removal it is disconnected from XenBus, so no backend state

Quoted 7 lines

change events come in via .otherend_changed callback.

The only difference I see here is that the backend is a user-space application

Thank you,
Oleksandr

To be honest, most of the things I assumed were true, according to some talks on IRC with maintainers. Since I assumed it was true I started to write code based on that and all the behaviors that followed were correct according to my assumptions (and discussions).

But if you find something else weird, please let me know and we can fix it.

Quoted 73 lines · Eduardo Otubo
Quoted 70 lines

v2:
* Replace busy wait with wait_event()/wake_up_all()
* Cannot garantee that at the time xennet_remove is called, the xen_netback state will not be XenbusStateClosed, so added a condition for that
* There's a small chance for the xen_netback state is XenbusStateUnknown by the time the xen_netfront switches to Closed, so added a condition for that.

When unloading module xen_netfront from guest, dmesg would output warning messages like below:

   [  105.236836] xen:grant_table: WARNING: g.e. 0x903 still in use!
   [  105.236839] deferring g.e. 0x903 (pfn 0x35805)

This problem relies on netfront and netback being out of sync. By the time netfront revokes the g.e.'s netback didn't have enough time to free all of them, hence displaying the warnings on dmesg.

The trick here is to make netfront to wait until netback frees all the g.e.'s and only then continue to cleanup for the module removal, and this is done by manipulating both device states.

---
  drivers/net/xen-netfront.c | 18 ++++++++++++++++++
  1 file changed, 18 insertions(+)
drivers/net/xen-netfront.c +18 -0
index 8b8689c6d887..391432e2725d 100644
@@ -87,6 +87,8 @@ struct netfront_cb {
  /* IRQ name is queue name with "-tx" or "-rx" appended */
  #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
+static DECLARE_WAIT_QUEUE_HEAD(module_unload_q);
+
  struct netfront_stats {
  	u64			packets;
  	u64			bytes;
@@ -2021,10 +2023,12 @@ static void netback_changed(struct xenbus_device *dev,
  		break;
  	case XenbusStateClosed:
+		wake_up_all(&module_unload_q);
  		if (dev->state == XenbusStateClosed)
  			break;
  		/* Missed the backend's CLOSING state -- fallthrough */
  	case XenbusStateClosing:
+		wake_up_all(&module_unload_q);
  		xenbus_frontend_closed(dev);
  		break;
  	}
@@ -2130,6 +2134,20 @@ static int xennet_remove(struct xenbus_device *dev)
  	dev_dbg(&dev->dev, "%s\n", dev->nodename);
+	if (xenbus_read_driver_state(dev->otherend) != XenbusStateClosed) {
+		xenbus_switch_state(dev, XenbusStateClosing);
+		wait_event(module_unload_q,
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateClosing);
+
+		xenbus_switch_state(dev, XenbusStateClosed);
+		wait_event(module_unload_q,
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateClosed ||
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateUnknown);
+	}
+
  	xennet_disconnect_backend(info);
  	unregister_netdev(info->netdev);
Signature · 6 lines
Eduardo Otubo

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#10 lore raw
In reply to Eduardo Otubo

Hi, Eduardo!

I am working on a frontend driver (PV DRM) and also seeing some strange

things on driver unloading:

xt# rmmod -f drm_xen_front.ko

[ 3236.462497] [drm] Unregistering XEN PV vdispl
[ 3236.485745] [drm:xen_drv_remove [drm_xen_front]] *ERROR* Backend state is InitWait while removing driver
[ 3236.486950] vdispl vdispl-0: 22 freeing event channel 11
[ 3236.496123] vdispl vdispl-0: failed to write error node for device/vdispl/0 (22 freeing event channel 11)
[ 3236.496271] vdispl vdispl-0: 22 freeing event channel 12
[ 3236.501633] vdispl vdispl-0: failed to write error node for device/vdispl/0 (22 freeing event channel 12)

These are somewhat different from your use-case with grant references, but I have a question:

do you really see that XenbusStateClosed and XenbusStateClosing are

called? In my driver I can't see those and once I tried to dig deeper into the problem

I saw that on driver removal it is disconnected from XenBus, so no backend state

change events come in via .otherend_changed callback.

The only difference I see here is that the backend is a user-space application

Thank you,
Oleksandr

Quoted 65 lines · Eduardo Otubo

v2:
* Replace busy wait with wait_event()/wake_up_all()
* Cannot garantee that at the time xennet_remove is called, the xen_netback state will not be XenbusStateClosed, so added a condition for that
* There's a small chance for the xen_netback state is XenbusStateUnknown by the time the xen_netfront switches to Closed, so added a condition for that.

When unloading module xen_netfront from guest, dmesg would output warning messages like below:

   [  105.236836] xen:grant_table: WARNING: g.e. 0x903 still in use!
   [  105.236839] deferring g.e. 0x903 (pfn 0x35805)

This problem relies on netfront and netback being out of sync. By the time netfront revokes the g.e.'s netback didn't have enough time to free all of them, hence displaying the warnings on dmesg.

The trick here is to make netfront to wait until netback frees all the g.e.'s and only then continue to cleanup for the module removal, and this is done by manipulating both device states.

---
  drivers/net/xen-netfront.c | 18 ++++++++++++++++++
  1 file changed, 18 insertions(+)
drivers/net/xen-netfront.c +11 -0
index 8b8689c6d887..391432e2725d 100644
@@ -87,6 +87,8 @@ struct netfront_cb {
  /* IRQ name is queue name with "-tx" or "-rx" appended */
  #define IRQ_NAME_SIZE (QUEUE_NAME_SIZE + 3)
 
+static DECLARE_WAIT_QUEUE_HEAD(module_unload_q);
+
  struct netfront_stats {
  	u64			packets;
  	u64			bytes;
@@ -2021,10 +2023,12 @@ static void netback_changed(struct xenbus_device *dev,
  		break;
 
  	case XenbusStateClosed:
+		wake_up_all(&module_unload_q);
  		if (dev->state == XenbusStateClosed)
  			break;
  		/* Missed the backend's CLOSING state -- fallthrough */
  	case XenbusStateClosing:
+		wake_up_all(&module_unload_q);
  		xenbus_frontend_closed(dev);
  		break;
  	}
@@ -2130,6 +2134,20 @@ static int xennet_remove(struct xenbus_device *dev)
 
  	dev_dbg(&dev->dev, "%s\n", dev->nodename);
 
+	if (xenbus_read_driver_state(dev->otherend) != XenbusStateClosed) {
+		xenbus_switch_state(dev, XenbusStateClosing);
+		wait_event(module_unload_q,
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateClosing);
+
+		xenbus_switch_state(dev, XenbusStateClosed);

drivers/net/xen-netfront.c static int xennet_remove(struct xenbus_device *dev) · line 2153

Quoted 10 lines
drivers/net/xen-netfront.c +7 -0
+		wait_event(module_unload_q,
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateClosed ||
+			   xenbus_read_driver_state(dev->otherend) ==
+			   XenbusStateUnknown);
+	}
+
  	xennet_disconnect_backend(info);
 
  	unregister_netdev(info->netdev);

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel