Skip to contentSkip to conversation

Netback vif reference count mismatching in latest 3.11 kernels

PATCH Tomasz Wroblewski 4 participants 12 messages Nov 27, 2013 – Nov 28, 2013

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

Hi,

After update of our network backend vm kernel to 3.11.9 I'm seeing trouble with netback vif close which seem related to the recent changes which separated vif disconnect and free; It seems that now multiple disconnect/connect cycles can happen without freeing and reallocing the netdev in the processes, which confuses the vif refcount.

vif refcount is initialized to 1 in xenvif_alloc. Then first xenvif_disconnect brings it back to 0, instead of 1 which would seem more reasonable (since its initialized to 1 in xenvif_alloc i would expect it to not be dropped to 0 until xenvif_free). Second xenvif_disconnect brings it to -1 and hangs. For us (xenclient XT) this happens when we hibernate linux guest, since linux hibernate is a complex beast which transitions the drivers to between close/connected states multiple times (i.e. first it suspends/closes the drivers to take memory snapshot, then resumes/reconnects the drivers to the actual writing of hibernate image to disk, then finally it closes them again to shutdown the system)

I've hacked the attached patch which fixes it (for us), is the approach taken there correct/upstreamable/reasonable? It does the following

* reset tx_irq to 0 after unbinding the irqs on disconnect - because xenvif_connect tests for it being 0 and will not reconnect if it's
not reset
* reacquire one reference to vif in disconnect(). This is because the reference
vif should be 1, as initialized in xenvif_alloc(), until the vif is freed. Otherwise multiple disconne
and cause a hang. I imagine alternate way of fixing this could be to use "0" as the default
refcnt in xenvif_alloc()

I believe we didn't experience this issue on previous kernel because vif disconnect was also freeing the vif and netdev, hence it was not possible to get xenvif_connect/xenvif_disconnect called multiple times between vif alloc/free.

drivers/net/xen-netback/interface.c +3 -0
index 68d5102..ccb46c4 100644
@@ -420,6 +420,8 @@ void xenvif_disconnect(struct xenvif *vif)
 
 	atomic_dec(&vif->refcnt);
 	wait_event(vif->waiting_to_free, atomic_read(&vif->refcnt) == 0);
+	/* reacquire reference since it should be 1 until freed */
+	xenvif_get(vif);
 
 	if (vif->tx_irq) {
 		if (vif->tx_irq == vif->rx_irq)
@@ -428,6 +430,7 @@ void xenvif_disconnect(struct xenvif *vif)
 			unbind_from_irqhandler(vif->tx_irq, vif);
 			unbind_from_irqhandler(vif->rx_irq, vif);
 		}
+		vif->tx_irq = 0;
 	}
 
 	xen_netbk_unmap_frontend_rings(vif);

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#1 lore raw
Quoted 9 lines · Tomasz Wroblewski

Hi,

After update of our network backend vm kernel to 3.11.9 I'm seeing trouble with netback vif close which seem related to the recent changes which separated vif disconnect and free; It seems that now multiple disconnect/connect cycles can happen without freeing and reallocing the netdev in the processes, which confuses the vif refcount.

Quoted 12 lines

vif refcount is initialized to 1 in xenvif_alloc. Then first xenvif_disconnect brings it back to 0, instead of 1 which would seem more reasonable (since its initialized to 1 in xenvif_alloc i would expect it to not be dropped to 0 until xenvif_free). Second xenvif_disconnect brings it to -1 and hangs. For us (xenclient XT) this happens when we hibernate linux guest, since linux hibernate is a complex beast which transitions the drivers to between close/connected states multiple times (i.e. first it suspends/closes the drivers to take memory snapshot, then resumes/reconnects the drivers to the actual writing of hibernate image to disk, then finally it closes them again to shutdown the system)

Can you illustrate a graph of the whole process? I'm not very clear of the whole cycle.

There's a xenvif_get in xenvif_connect, which increases refcnt by 1, that should corresponds to the atomic_dec in xenvif_disconnect, right?

Quoted 10 lines

I've hacked the attached patch which fixes it (for us), is the approach taken there correct/upstreamable/reasonable? It does the following

* reset tx_irq to 0 after unbinding the irqs on disconnect -
because xenvif_connect tests for it being 0 and will not reconnect
if it's not reset
* reacquire one reference to vif in disconnect(). This is because the reference
vif should be 1, as initialized in xenvif_alloc(), until the vif is freed. Otherwise multiple disconne and cause a hang. I imagine alternate way of fixing this could be to use "0" as the default refcnt in xenvif_alloc()

You mean the numbers of connect's and disconnect's don't match? Even after you reset tx_irq to 0?

Wei.

Quoted 32 lines

I believe we didn't experience this issue on previous kernel because vif disconnect was also freeing the vif and netdev, hence it was not possible to get xenvif_connect/xenvif_disconnect called multiple times between vif alloc/free.

drivers/net/xen-netback/interface.c +3 -0
index 68d5102..ccb46c4 100644
@@ -420,6 +420,8 @@ void xenvif_disconnect(struct xenvif *vif)
 
 	atomic_dec(&vif->refcnt);
 	wait_event(vif->waiting_to_free, atomic_read(&vif->refcnt) == 0);
+	/* reacquire reference since it should be 1 until freed */
+	xenvif_get(vif);
 
 	if (vif->tx_irq) {
 		if (vif->tx_irq == vif->rx_irq)
@@ -428,6 +430,7 @@ void xenvif_disconnect(struct xenvif *vif)
 			unbind_from_irqhandler(vif->tx_irq, vif);
 			unbind_from_irqhandler(vif->rx_irq, vif);
 		}
+		vif->tx_irq = 0;
 	}
 
 	xen_netbk_unmap_frontend_rings(vif);
#2 lore raw
Quoted 32 lines

-----Original Message-----
From: [email protected] [mailto:xen-devel- [email protected]] On Behalf Of Wei Liu
Sent: 27 November 2013 14:41
To: Tomasz Wroblewski
Cc: xen-devel; Paul Durrant; Wei Liu; David Vrabel
Subject: Re: [Xen-devel] Netback vif reference count mismatching in latest 3.11 kernels

Quoted 9 lines · Tomasz Wroblewski

Hi,

After update of our network backend vm kernel to 3.11.9 I'm seeing trouble with netback vif close which seem related to the recent changes which separated vif disconnect and free; It seems that now multiple disconnect/connect cycles can happen without freeing and reallocing the netdev in the processes, which confuses the vif refcount.

Quoted 12 lines

vif refcount is initialized to 1 in xenvif_alloc. Then first xenvif_disconnect brings it back to 0, instead of 1 which would seem more reasonable (since its initialized to 1 in xenvif_alloc i would expect it to not be dropped to 0 until xenvif_free). Second xenvif_disconnect brings it to -1 and hangs. For us (xenclient XT) this happens when we hibernate linux guest, since linux hibernate is a complex beast which transitions the drivers to between close/connected states multiple times (i.e. first it suspends/closes the drivers to take memory snapshot, then resumes/reconnects the drivers to the actual writing of hibernate image to disk, then finally it closes them again to shutdown the system)

Quoted 6 lines

Can you illustrate a graph of the whole process? I'm not very clear of the whole cycle.

There's a xenvif_get in xenvif_connect, which increases refcnt by 1, that should corresponds to the atomic_dec in xenvif_disconnect, right?

ISTR this happening with some frontends that did not drive the state model entirely correctly. Commit ea732dff5cfa10789007bf4a5b935388a0bb2a8f (Handle backend state transitions in a more robust way ) should fix this.

Paul

Quoted 59 lines
Quoted 1 line

I've hacked the attached patch which fixes it (for us), is the approach taken

there correct/upstreamable/reasonable? It does the following

Quoted 5 lines

* reset tx_irq to 0 after unbinding the irqs on disconnect -
because xenvif_connect tests for it being 0 and will not reconnect if it's not reset
* reacquire one reference to vif in disconnect(). This is because the

reference

Quoted 1 line

vif should be 1, as initialized in xenvif_alloc(), until the vif is freed.

Otherwise multiple disconne

Quoted 1 line

and cause a hang. I imagine alternate way of fixing this could be to use

"0" as the default

Quoted 2 lines

refcnt in xenvif_alloc()

You mean the numbers of connect's and disconnect's don't match? Even after you reset tx_irq to 0?

Wei.

drivers/net/xen-

Quoted 12 lines

I believe we didn't experience this issue on previous kernel because vif disconnect was also freeing the vif and netdev, hence it was not possible to get xenvif_connect/xenvif_disconnect called multiple times between vif alloc/free.

drivers/net/xen- renamed from drivers/net/xen-netback/interface.c +0 -0

netback/interface.c

Quoted 13 lines

index 68d5102..ccb46c4 100644

drivers/net/xen-netback/interface.c +2 -0
@@ -420,6 +420,8 @@ void xenvif_disconnect(struct xenvif *vif)
 
 	atomic_dec(&vif->refcnt);
 	wait_event(vif->waiting_to_free, atomic_read(&vif->refcnt) == 0);
+	/* reacquire reference since it should be 1 until freed */
+	xenvif_get(vif);
 
 	if (vif->tx_irq) {
 		if (vif->tx_irq == vif->rx_irq)

drivers/net/xen-netback/interface.c void xenvif_disconnect(struct xenvif *vif) · line 436

Quoted 7 lines
drivers/net/xen-netback/interface.c +1 -0
@@ -428,6 +430,7 @@ void xenvif_disconnect(struct xenvif *vif)
 			unbind_from_irqhandler(vif->tx_irq, vif);
 			unbind_from_irqhandler(vif->rx_irq, vif);
 		}
+		vif->tx_irq = 0;
 	}
 
 	xen_netbk_unmap_frontend_rings(vif);

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw
Quoted 32 lines · Paul Durrant
Quoted 32 lines

-----Original Message-----
From: [email protected] [mailto:xen-devel- [email protected]] On Behalf Of Wei Liu
Sent: 27 November 2013 14:41
To: Tomasz Wroblewski
Cc: xen-devel; Paul Durrant; Wei Liu; David Vrabel
Subject: Re: [Xen-devel] Netback vif reference count mismatching in latest 3.11 kernels

Quoted 21 lines · Tomasz Wroblewski

Hi,

After update of our network backend vm kernel to 3.11.9 I'm seeing trouble with netback vif close which seem related to the recent changes which separated vif disconnect and free; It seems that now multiple disconnect/connect cycles can happen without freeing and reallocing the netdev in the processes, which confuses the vif refcount.

vif refcount is initialized to 1 in xenvif_alloc. Then first xenvif_disconnect brings it back to 0, instead of 1 which would seem more reasonable (since its initialized to 1 in xenvif_alloc i would expect it to not be dropped to 0 until xenvif_free). Second xenvif_disconnect brings it to -1 and hangs. For us (xenclient XT) this happens when we hibernate linux guest, since linux hibernate is a complex beast which transitions the drivers to between close/connected states multiple times (i.e. first it suspends/closes the drivers to take memory snapshot, then resumes/reconnects the drivers to the actual writing of hibernate image to disk, then finally it closes them again to shutdown the system)

Quoted 9 lines
Quoted 6 lines

Can you illustrate a graph of the whole process? I'm not very clear of the whole cycle.

There's a xenvif_get in xenvif_connect, which increases refcnt by 1, that should corresponds to the atomic_dec in xenvif_disconnect, right?

ISTR this happening with some frontends that did not drive the state model entirely correctly. Commit ea732dff5cfa10789007bf4a5b935388a0bb2a8f (Handle backend state transitions in a more robust way ) should fix this.

FWIW 3.11.9 already has that patch.

Wei.

#4 lore raw
In reply to Wei Liu
Quoted 23 lines · Wei Liu
Quoted 9 lines · Tomasz Wroblewski

Hi,

After update of our network backend vm kernel to 3.11.9 I'm seeing trouble with netback vif close which seem related to the recent changes which separated vif disconnect and free; It seems that now multiple disconnect/connect cycles can happen without freeing and reallocing the netdev in the processes, which confuses the vif refcount.

Quoted 12 lines

vif refcount is initialized to 1 in xenvif_alloc. Then first xenvif_disconnect brings it back to 0, instead of 1 which would seem more reasonable (since its initialized to 1 in xenvif_alloc i would expect it to not be dropped to 0 until xenvif_free). Second xenvif_disconnect brings it to -1 and hangs. For us (xenclient XT) this happens when we hibernate linux guest, since linux hibernate is a complex beast which transitions the drivers to between close/connected states multiple times (i.e. first it suspends/closes the drivers to take memory snapshot, then resumes/reconnects the drivers to the actual writing of hibernate image to disk, then finally it closes them again to shutdown the system)

Quoted 6 lines

Can you illustrate a graph of the whole process? I'm not very clear of the whole cycle.

There's a xenvif_get in xenvif_connect, which increases refcnt by 1, that should corresponds to the atomic_dec in xenvif_disconnect, right?

Not if I'm reading the code right. I think the atomic_dec in xenvif_disconnect corresponds to atomic_set(..., 1) in xenvif_alloc, and xenvif_get() in xenvif_connect corresponds to xenvif_put in xenvif_carrier_off() (which is called at top of xenvif_disconnect).

Therefore xenvif_disconnect() decrements the refcnt twice whilst xenvif_connect() increments it once, which results in negative refcnt after one cycle. The graph I'm seeing looks like this:

| guest vm boots
|
v
netback:xenvif_alloc() refcnt after=1

     |
     v
netback:xenvif_connect() refcnt after=2
     |
     v
   ..... stuff
     |
     | guest hibernate starts and suspends netfront (netfront goes to Closed xenbus state)
     |
     v
netback:xenvif_disconnect() refcnt after=0
     |
     | hibernate takes memory snapshot and resumes netfront afterwards (which will go thru initialising, connected etc states)
     |
     v
netback:xenvif_connect() refcnt after=1
     |
     | hibernate suspends netfront again (netfront goes to Closed xenbus state)
     |
     v
netback:xenvif_disconnect() refcnt=-1
     |
     v
  netback stuck
     |
     v

netback:xenvif_free() never happens

Quoted 11 lines
Quoted 10 lines

I've hacked the attached patch which fixes it (for us), is the approach taken there correct/upstreamable/reasonable? It does the following

* reset tx_irq to 0 after unbinding the irqs on disconnect -
because xenvif_connect tests for it being 0 and will not reconnect
if it's not reset
* reacquire one reference to vif in disconnect(). This is because the reference
vif should be 1, as initialized in xenvif_alloc(), until the vif is freed. Otherwise multiple disconne and cause a hang. I imagine alternate way of fixing this could be to use "0" as the default refcnt in xenvif_alloc()

Quoted 3 lines

You mean the numbers of connect's and disconnect's don't match? Even after you reset tx_irq to 0?

The number of connects and disconnect matches, but the amounts of refcnt changes do not seem to be symmetric in these functions. And yeah, the kernel I'm using already has Paul's "robustness" fix and subsequent zombie fix by David Vrabel.

#5 lore raw
Quoted 31 lines · Tomasz Wroblewski
Quoted 23 lines · Wei Liu
Quoted 9 lines · Tomasz Wroblewski

Hi,

After update of our network backend vm kernel to 3.11.9 I'm seeing trouble with netback vif close which seem related to the recent changes which separated vif disconnect and free; It seems that now multiple disconnect/connect cycles can happen without freeing and reallocing the netdev in the processes, which confuses the vif refcount.

Quoted 12 lines

vif refcount is initialized to 1 in xenvif_alloc. Then first xenvif_disconnect brings it back to 0, instead of 1 which would seem more reasonable (since its initialized to 1 in xenvif_alloc i would expect it to not be dropped to 0 until xenvif_free). Second xenvif_disconnect brings it to -1 and hangs. For us (xenclient XT) this happens when we hibernate linux guest, since linux hibernate is a complex beast which transitions the drivers to between close/connected states multiple times (i.e. first it suspends/closes the drivers to take memory snapshot, then resumes/reconnects the drivers to the actual writing of hibernate image to disk, then finally it closes them again to shutdown the system)

Quoted 6 lines

Can you illustrate a graph of the whole process? I'm not very clear of the whole cycle.

There's a xenvif_get in xenvif_connect, which increases refcnt by 1, that should corresponds to the atomic_dec in xenvif_disconnect, right?

Quoted 9 lines

Not if I'm reading the code right. I think the atomic_dec in xenvif_disconnect corresponds to atomic_set(..., 1) in xenvif_alloc, and xenvif_get() in xenvif_connect corresponds to xenvif_put in xenvif_carrier_off() (which is called at top of xenvif_disconnect).

Therefore xenvif_disconnect() decrements the refcnt twice whilst xenvif_connect() increments it once, which results in negative refcnt after one cycle. The graph I'm seeing looks like this:

OK, I get what you mean. The separation of vif disconnect and free is indeed causing problem.

I think the right fix would be to make things symmetric again.

Does the following *untested* patch work for you? If so I will submit a proper patch to netdev.

Wei.

---8<---

drivers/net/xen-netback/interface.c +4 -3
index d28324a..342d4e5 100644
@@ -418,9 +418,6 @@ void xenvif_disconnect(struct xenvif *vif)
 	if (netif_carrier_ok(vif->dev))
 		xenvif_carrier_off(vif);
 
-	atomic_dec(&vif->refcnt);
-	wait_event(vif->waiting_to_free, atomic_read(&vif->refcnt) == 0);
-
 	if (vif->tx_irq) {
 		if (vif->tx_irq == vif->rx_irq)
 			unbind_from_irqhandler(vif->tx_irq, vif);
@@ -428,6 +425,7 @@ void xenvif_disconnect(struct xenvif *vif)
 			unbind_from_irqhandler(vif->tx_irq, vif);
 			unbind_from_irqhandler(vif->rx_irq, vif);
 		}
+		vif->tx_irq = 0;
 	}
 
 	xen_netbk_unmap_frontend_rings(vif);
@@ -435,6 +433,9 @@ void xenvif_disconnect(struct xenvif *vif)
 
 void xenvif_free(struct xenvif *vif)
 {
+	atomic_dec(&vif->refcnt);
+	wait_event(vif->waiting_to_free, atomic_read(&vif->refcnt) == 0);
+
 	unregister_netdev(vif->dev);
 
 	free_netdev(vif->dev);
#6 lore raw
Quoted 9 lines

OK, I get what you mean. The separation of vif disconnect and free is indeed causing problem.

I think the right fix would be to make things symmetric again.

Does the following *untested* patch work for you? If so I will submit a proper patch to netdev.

Thanks, indeed your patch works fine after a quick linux hibernate tests. But I will do more comprehensive test tomorrow morning (such as with windows guests as well, and things like rebooting backend network vm while front stays up) and get back to you with the result, so probably best to hold off a little :) thanks!

#7 lore raw
Quoted 4 lines · Tomasz Wroblewski
Quoted 4 lines

OK, I get what you mean. The separation of vif disconnect and free is indeed causing problem.

Quoted 9 lines
Quoted 5 lines

I think the right fix would be to make things symmetric again.

Does the following *untested* patch work for you? If so I will submit a proper patch to netdev.

Thanks, indeed your patch works fine after a quick linux hibernate tests. But I will do more comprehensive test tomorrow morning (such as with windows guests as well, and things like rebooting backend network vm while front stays up) and get back to you with the result, so probably best to hold off a little :) thanks!

Wei, gave your patch more testing now, as stated, and haven't noticed any trouble.

#8 lore raw
Quoted 11 lines · Tomasz Wroblewski
Quoted 10 lines · Tomasz Wroblewski
Quoted 10 lines

OK, I get what you mean. The separation of vif disconnect and free is indeed causing problem.

I think the right fix would be to make things symmetric again.

Does the following *untested* patch work for you? If so I will submit a proper patch to netdev.

Quoted 9 lines
Quoted 6 lines

Thanks, indeed your patch works fine after a quick linux hibernate tests. But I will do more comprehensive test tomorrow morning (such as with windows guests as well, and things like rebooting backend network vm while front stays up) and get back to you with the result, so probably best to hold off a little :) thanks!

Wei, gave your patch more testing now, as stated, and haven't noticed any trouble.

Thanks, please give me final confirmation if everything passes.

Wei.

Quoted 5 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#9 lore raw
Quoted 19 lines · Wei Liu
Quoted 17 lines · Tomasz Wroblewski
Quoted 16 lines · Tomasz Wroblewski
Quoted 10 lines

OK, I get what you mean. The separation of vif disconnect and free is indeed causing problem.

I think the right fix would be to make things symmetric again.

Does the following *untested* patch work for you? If so I will submit a proper patch to netdev.

Thanks, indeed your patch works fine after a quick linux hibernate tests. But I will do more comprehensive test tomorrow morning (such as with windows guests as well, and things like rebooting backend network vm while front stays up) and get back to you with the result, so probably best to hold off a little :) thanks!

Quoted 6 lines
Quoted 3 lines

Wei, gave your patch more testing now, as stated, and haven't noticed any trouble.

Thanks, please give me final confirmation if everything passes.

it did, not planning to do more testing as of now (I tested S3/S4 in linuxes/windowses, reboots of network vm whilst frontend stays up and general lifecycle ops)

Quoted 7 lines

Wei.

Quoted 5 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#10 lore raw
Quoted 24 lines · Tomasz Wroblewski
Quoted 23 lines · Wei Liu
Quoted 20 lines · Tomasz Wroblewski
Quoted 7 lines · Tomasz Wroblewski
Quoted 7 lines

OK, I get what you mean. The separation of vif disconnect and free is indeed causing problem.

I think the right fix would be to make things symmetric again.

Quoted 9 lines
Quoted 3 lines

Does the following *untested* patch work for you? If so I will submit a proper patch to netdev.

Thanks, indeed your patch works fine after a quick linux hibernate tests. But I will do more comprehensive test tomorrow morning (such as with windows guests as well, and things like rebooting backend network vm while front stays up) and get back to you with the result, so probably best to hold off a little :) thanks!

Wei, gave your patch more testing now, as stated, and haven't noticed any trouble.

Quoted 6 lines
Quoted 2 lines

Thanks, please give me final confirmation if everything passes.

it did, not planning to do more testing as of now (I tested S3/S4 in linuxes/windowses, reboots of network vm whilst frontend stays up and general lifecycle ops)

Thanks for the confirmation, I will add a tag

to the final patch.

Wei.

Quoted 7 lines
Quoted 7 lines

Wei.

Quoted 5 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#11 lore raw
In reply to Tomasz Wroblewski
Quoted 31 lines · Tomasz Wroblewski

Hi,

After update of our network backend vm kernel to 3.11.9 I'm seeing trouble with netback vif close which seem related to the recent changes which separated vif disconnect and free; It seems that now multiple disconnect/connect cycles can happen without freeing and reallocing the netdev in the processes, which confuses the vif refcount.

vif refcount is initialized to 1 in xenvif_alloc. Then first xenvif_disconnect brings it back to 0, instead of 1 which would seem more reasonable (since its initialized to 1 in xenvif_alloc i would expect it to not be dropped to 0 until xenvif_free). Second xenvif_disconnect brings it to -1 and hangs. For us (xenclient XT) this happens when we hibernate linux guest, since linux hibernate is a complex beast which transitions the drivers to between close/connected states multiple times (i.e. first it suspends/closes the drivers to take memory snapshot, then resumes/reconnects the drivers to the actual writing of hibernate image to disk, then finally it closes them again to shutdown the system)

I've hacked the attached patch which fixes it (for us), is the approach taken there correct/upstreamable/reasonable? It does the following

* reset tx_irq to 0 after unbinding the irqs on disconnect -
because xenvif_connect tests for it being 0 and will not reconnect
if it's not reset
* reacquire one reference to vif in disconnect(). This is because the reference
vif should be 1, as initialized in xenvif_alloc(), until the vif is freed. Otherwise multiple disconne and cause a hang. I imagine alternate way of fixing this could be to use "0" as the default refcnt in xenvif_alloc()

Quoted 6 lines

I believe we didn't experience this issue on previous kernel because vif disconnect was also freeing the vif and netdev, hence it was not possible to get xenvif_connect/xenvif_disconnect called multiple times between vif alloc/free.

For the record this bug only manifests in stable trees prior 3.12. A patch for 1:1 model went in 3.12 and removed a bunch of ref counting stuffs. On the other hand stable trees picked up Paul's state machine patch which separated disconnect and free functions, not handling those ref counting bits correctly.

Wei.