Skip to contentSkip to conversation

xen-netback: fix race between napi_complete() and interrupt handler

PATCH In review David Vrabel 5 participants 13 messages Mar 25, 2014 – May 15, 2014

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

PATCH 1/1 xen-netback: fix race between napi_complete() and interrupt handler

When the NAPI budget was not all used, xenvif_poll() would call napi_complete() /after/ enabling the interrupt. This resulted in a race between the napi_complete() and the napi_schedule() in the interrupt handler. The use of local_irq_save/restore() avoided by race iff the handler is running on the same CPU but not if it was running on a different CPU.

Fix this properly by calling napi_complete() before reenabling interrupts (in the xenvif_check_rx_xenvif() call).

---
 drivers/net/xen-netback/interface.c |   28 ++--------------------------
 1 files changed, 2 insertions(+), 26 deletions(-)
drivers/net/xen-netback/interface.c +2 -26
index 7669d49..ee322d9 100644
@@ -65,32 +65,8 @@ static int xenvif_poll(struct napi_struct *napi, int budget)
 	work_done = xenvif_tx_action(vif, budget);
 
 	if (work_done < budget) {
-		int more_to_do = 0;
-		unsigned long flags;
-
-		/* It is necessary to disable IRQ before calling
-		 * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
-		 * lose event from the frontend.
-		 *
-		 * Consider:
-		 *   RING_HAS_UNCONSUMED_REQUESTS
-		 *   <frontend generates event to trigger napi_schedule>
-		 *   __napi_complete
-		 *
-		 * This handler is still in scheduled state so the
-		 * event has no effect at all. After __napi_complete
-		 * this handler is descheduled and cannot get
-		 * scheduled again. We lose event in this case and the ring
-		 * will be completely stalled.
-		 */
-
-		local_irq_save(flags);
-
-		RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
-		if (!more_to_do)
-			__napi_complete(napi);
-
-		local_irq_restore(flags);
+		napi_complete(napi);
+		xenvif_check_rx_xenvif(vif);
 	}
 
 	return work_done;
Signature · 1 line
1.7.2.5
#1 lore raw

My idea was that the current code can't race with interrupt running on a different CPU, because if the interrupt was moved since the last napi_schedule (which scheduled NAPI on the same CPU as the interrupt), the kernel would make sure that the NAPI instance is moved along with it. However I couldn't find any trace of this in the kernel so far, but the current code actually works for me, even when I used a bash script to aggressively move the interrupts around while running. I've added David and Eric to the mailing, maybe they can quickly shed some light on this: how does the kernel make sure that if the interrupt is moved away from a CPU (e.g. by irqbalance), the NAPI instance already scheduled there won't race with it?

Zoli

Quoted 55 lines · David Vrabel

When the NAPI budget was not all used, xenvif_poll() would call napi_complete() /after/ enabling the interrupt. This resulted in a race between the napi_complete() and the napi_schedule() in the interrupt handler. The use of local_irq_save/restore() avoided by race iff the handler is running on the same CPU but not if it was running on a different CPU.

Fix this properly by calling napi_complete() before reenabling interrupts (in the xenvif_check_rx_xenvif() call).

---
  drivers/net/xen-netback/interface.c |   28 ++--------------------------
  1 files changed, 2 insertions(+), 26 deletions(-)
drivers/net/xen-netback/interface.c +2 -26
index 7669d49..ee322d9 100644
@@ -65,32 +65,8 @@ static int xenvif_poll(struct napi_struct *napi, int budget)
  	work_done = xenvif_tx_action(vif, budget);
 
  	if (work_done < budget) {
-		int more_to_do = 0;
-		unsigned long flags;
-
-		/* It is necessary to disable IRQ before calling
-		 * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
-		 * lose event from the frontend.
-		 *
-		 * Consider:
-		 *   RING_HAS_UNCONSUMED_REQUESTS
-		 *   <frontend generates event to trigger napi_schedule>
-		 *   __napi_complete
-		 *
-		 * This handler is still in scheduled state so the
-		 * event has no effect at all. After __napi_complete
-		 * this handler is descheduled and cannot get
-		 * scheduled again. We lose event in this case and the ring
-		 * will be completely stalled.
-		 */
-
-		local_irq_save(flags);
-
-		RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
-		if (!more_to_do)
-			__napi_complete(napi);
-
-		local_irq_restore(flags);
+		napi_complete(napi);
+		xenvif_check_rx_xenvif(vif);
  	}
 
  	return work_done;
#6 lore raw
In reply to David Vrabel

My idea was that the current code can't race with interrupt running on a different CPU, because if the interrupt was moved since the last napi_schedule (which scheduled NAPI on the same CPU as the interrupt), the kernel would make sure that the NAPI instance is moved along with it. However I couldn't find any trace of this in the kernel so far, but the current code actually works for me, even when I used a bash script to aggressively move the interrupts around while running. I've added David and Eric to the mailing, maybe they can quickly shed some light on this: how does the kernel make sure that if the interrupt is moved away from a CPU (e.g. by irqbalance), the NAPI instance already scheduled there won't race with it?

Zoli

Quoted 55 lines · David Vrabel

When the NAPI budget was not all used, xenvif_poll() would call napi_complete() /after/ enabling the interrupt. This resulted in a race between the napi_complete() and the napi_schedule() in the interrupt handler. The use of local_irq_save/restore() avoided by race iff the handler is running on the same CPU but not if it was running on a different CPU.

Fix this properly by calling napi_complete() before reenabling interrupts (in the xenvif_check_rx_xenvif() call).

---
  drivers/net/xen-netback/interface.c |   28 ++--------------------------
  1 files changed, 2 insertions(+), 26 deletions(-)
drivers/net/xen-netback/interface.c +2 -26
index 7669d49..ee322d9 100644
@@ -65,32 +65,8 @@ static int xenvif_poll(struct napi_struct *napi, int budget)
  	work_done = xenvif_tx_action(vif, budget);
 
  	if (work_done < budget) {
-		int more_to_do = 0;
-		unsigned long flags;
-
-		/* It is necessary to disable IRQ before calling
-		 * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
-		 * lose event from the frontend.
-		 *
-		 * Consider:
-		 *   RING_HAS_UNCONSUMED_REQUESTS
-		 *   <frontend generates event to trigger napi_schedule>
-		 *   __napi_complete
-		 *
-		 * This handler is still in scheduled state so the
-		 * event has no effect at all. After __napi_complete
-		 * this handler is descheduled and cannot get
-		 * scheduled again. We lose event in this case and the ring
-		 * will be completely stalled.
-		 */
-
-		local_irq_save(flags);
-
-		RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
-		if (!more_to_do)
-			__napi_complete(napi);
-
-		local_irq_restore(flags);
+		napi_complete(napi);
+		xenvif_check_rx_xenvif(vif);
  	}
 
  	return work_done;
#7 lore raw
In reply to David Vrabel

You forgot to target this patch to "net" tree in subject line.

Quoted 7 lines · David Vrabel

When the NAPI budget was not all used, xenvif_poll() would call napi_complete() /after/ enabling the interrupt. This resulted in a race between the napi_complete() and the napi_schedule() in the interrupt handler. The use of local_irq_save/restore() avoided by race iff the handler is running on the same CPU but not if it was running on a different CPU.

OK, I understand this issue now. You mentioned it in the other email which made me a bit confused.

Just curious, how do you trigger this? By re-binding the interrupt to another CPU when xenvif_poll is running? I used to run irqbalance (the one that works with xen virtual interrupt) but could not trigger a race. Probably the race window is too small to trigger?

Quoted 33 lines

Fix this properly by calling napi_complete() before reenabling interrupts (in the xenvif_check_rx_xenvif() call).

---
 drivers/net/xen-netback/interface.c |   28 ++--------------------------
 1 files changed, 2 insertions(+), 26 deletions(-)
drivers/net/xen-netback/interface.c +0 -17
index 7669d49..ee322d9 100644
@@ -65,32 +65,8 @@ static int xenvif_poll(struct napi_struct *napi, int budget)
 	work_done = xenvif_tx_action(vif, budget);
 
 	if (work_done < budget) {
-		int more_to_do = 0;
-		unsigned long flags;
-
-		/* It is necessary to disable IRQ before calling
-		 * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
-		 * lose event from the frontend.
-		 *
-		 * Consider:
-		 *   RING_HAS_UNCONSUMED_REQUESTS
-		 *   <frontend generates event to trigger napi_schedule>
-		 *   __napi_complete
-		 *
-		 * This handler is still in scheduled state so the
-		 * event has no effect at all. After __napi_complete
-		 * this handler is descheduled and cannot get
-		 * scheduled again. We lose event in this case and the ring
-		 * will be completely stalled.

drivers/net/xen-netback/interface.c static int xenvif_poll(struct napi_struct *napi, int budget) · line 68

Quoted 10 lines
drivers/net/xen-netback/interface.c +1 -9
-		 */
-
-		local_irq_save(flags);
-
-		RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
-		if (!more_to_do)
-			__napi_complete(napi);
-
-		local_irq_restore(flags);
+		napi_complete(napi);

You need to add comment here to say interrupt is in fact "disabled" before this point, and "enabled" by xenvif_check_rx_xenvif().

drivers/net/xen-netback/interface.c static int xenvif_poll(struct napi_struct *napi, int budget) · line 69

Quoted 1 line
drivers/net/xen-netback/interface.c +1 -0
+		xenvif_check_rx_xenvif(vif);

To be honest this function call is not immediately obvious about it's side effect. I don't mind you copy the code in that function here.

Wei.

Quoted 5 lines
drivers/net/xen-netback/interface.c +0 -0
 	}
 
 	return work_done;
Signature · 1 line
1.7.2.5
#12 lore raw
In reply to David Vrabel

You forgot to target this patch to "net" tree in subject line.

Quoted 7 lines · David Vrabel

When the NAPI budget was not all used, xenvif_poll() would call napi_complete() /after/ enabling the interrupt. This resulted in a race between the napi_complete() and the napi_schedule() in the interrupt handler. The use of local_irq_save/restore() avoided by race iff the handler is running on the same CPU but not if it was running on a different CPU.

OK, I understand this issue now. You mentioned it in the other email which made me a bit confused.

Just curious, how do you trigger this? By re-binding the interrupt to another CPU when xenvif_poll is running? I used to run irqbalance (the one that works with xen virtual interrupt) but could not trigger a race. Probably the race window is too small to trigger?

Quoted 33 lines

Fix this properly by calling napi_complete() before reenabling interrupts (in the xenvif_check_rx_xenvif() call).

---
 drivers/net/xen-netback/interface.c |   28 ++--------------------------
 1 files changed, 2 insertions(+), 26 deletions(-)
drivers/net/xen-netback/interface.c +0 -17
index 7669d49..ee322d9 100644
@@ -65,32 +65,8 @@ static int xenvif_poll(struct napi_struct *napi, int budget)
 	work_done = xenvif_tx_action(vif, budget);
 
 	if (work_done < budget) {
-		int more_to_do = 0;
-		unsigned long flags;
-
-		/* It is necessary to disable IRQ before calling
-		 * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
-		 * lose event from the frontend.
-		 *
-		 * Consider:
-		 *   RING_HAS_UNCONSUMED_REQUESTS
-		 *   <frontend generates event to trigger napi_schedule>
-		 *   __napi_complete
-		 *
-		 * This handler is still in scheduled state so the
-		 * event has no effect at all. After __napi_complete
-		 * this handler is descheduled and cannot get
-		 * scheduled again. We lose event in this case and the ring
-		 * will be completely stalled.

drivers/net/xen-netback/interface.c static int xenvif_poll(struct napi_struct *napi, int budget) · line 68

Quoted 10 lines
drivers/net/xen-netback/interface.c +1 -9
-		 */
-
-		local_irq_save(flags);
-
-		RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
-		if (!more_to_do)
-			__napi_complete(napi);
-
-		local_irq_restore(flags);
+		napi_complete(napi);

You need to add comment here to say interrupt is in fact "disabled" before this point, and "enabled" by xenvif_check_rx_xenvif().

drivers/net/xen-netback/interface.c static int xenvif_poll(struct napi_struct *napi, int budget) · line 69

Quoted 1 line
drivers/net/xen-netback/interface.c +1 -0
+		xenvif_check_rx_xenvif(vif);

To be honest this function call is not immediately obvious about it's side effect. I don't mind you copy the code in that function here.

Wei.

Quoted 5 lines
drivers/net/xen-netback/interface.c +0 -0
 	}
 
 	return work_done;
Signature · 1 line
1.7.2.5
#8 lore raw
In reply to Wei Liu
Quoted 11 lines · Wei Liu

You forgot to target this patch to "net" tree in subject line.

Quoted 7 lines · David Vrabel

When the NAPI budget was not all used, xenvif_poll() would call napi_complete() /after/ enabling the interrupt. This resulted in a race between the napi_complete() and the napi_schedule() in the interrupt handler. The use of local_irq_save/restore() avoided by race iff the handler is running on the same CPU but not if it was running on a different CPU.

Quoted 7 lines

OK, I understand this issue now. You mentioned it in the other email which made me a bit confused.

Just curious, how do you trigger this? By re-binding the interrupt to another CPU when xenvif_poll is running? I used to run irqbalance (the one that works with xen virtual interrupt) but could not trigger a race. Probably the race window is too small to trigger?

I haven't seen the race occur, but I've not tried to trigger it.

It may be that as Zoltan says, that the race is harmless but I still think avoiding the race entirely is preferable.

David

#9 lore raw
In reply to Wei Liu
Quoted 11 lines · Wei Liu

You forgot to target this patch to "net" tree in subject line.

Quoted 7 lines · David Vrabel

When the NAPI budget was not all used, xenvif_poll() would call napi_complete() /after/ enabling the interrupt. This resulted in a race between the napi_complete() and the napi_schedule() in the interrupt handler. The use of local_irq_save/restore() avoided by race iff the handler is running on the same CPU but not if it was running on a different CPU.

Quoted 7 lines

OK, I understand this issue now. You mentioned it in the other email which made me a bit confused.

Just curious, how do you trigger this? By re-binding the interrupt to another CPU when xenvif_poll is running? I used to run irqbalance (the one that works with xen virtual interrupt) but could not trigger a race. Probably the race window is too small to trigger?

I haven't seen the race occur, but I've not tried to trigger it.

It may be that as Zoltan says, that the race is harmless but I still think avoiding the race entirely is preferable.

David

maintainer
#10 lore raw
In reply to Wei Liu

From: Wei Liu <[email protected]>
Date: Tue, 25 Mar 2014 14:50:21 +0000

Quoted 57 lines

You forgot to target this patch to "net" tree in subject line.

Quoted 7 lines · David Vrabel

When the NAPI budget was not all used, xenvif_poll() would call napi_complete() /after/ enabling the interrupt. This resulted in a race between the napi_complete() and the napi_schedule() in the interrupt handler. The use of local_irq_save/restore() avoided by race iff the handler is running on the same CPU but not if it was running on a different CPU.

OK, I understand this issue now. You mentioned it in the other email which made me a bit confused.

Just curious, how do you trigger this? By re-binding the interrupt to another CPU when xenvif_poll is running? I used to run irqbalance (the one that works with xen virtual interrupt) but could not trigger a race. Probably the race window is too small to trigger?

Quoted 38 lines

Fix this properly by calling napi_complete() before reenabling interrupts (in the xenvif_check_rx_xenvif() call).

---
 drivers/net/xen-netback/interface.c |   28 ++--------------------------
 1 files changed, 2 insertions(+), 26 deletions(-)
drivers/net/xen-netback/interface.c +0 -22
index 7669d49..ee322d9 100644
@@ -65,32 +65,8 @@ static int xenvif_poll(struct napi_struct *napi, int budget)
 	work_done = xenvif_tx_action(vif, budget);
 
 	if (work_done < budget) {
-		int more_to_do = 0;
-		unsigned long flags;
-
-		/* It is necessary to disable IRQ before calling
-		 * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
-		 * lose event from the frontend.
-		 *
-		 * Consider:
-		 *   RING_HAS_UNCONSUMED_REQUESTS
-		 *   <frontend generates event to trigger napi_schedule>
-		 *   __napi_complete
-		 *
-		 * This handler is still in scheduled state so the
-		 * event has no effect at all. After __napi_complete
-		 * this handler is descheduled and cannot get
-		 * scheduled again. We lose event in this case and the ring
-		 * will be completely stalled.
-		 */
-
-		local_irq_save(flags);
-
-		RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
Quoted 8 lines

drivers/net/xen-netback/interface.c static int xenvif_poll(struct napi_struct *napi, int budget) · line 68

Quoted 5 lines
drivers/net/xen-netback/interface.c +1 -4
-		if (!more_to_do)
-			__napi_complete(napi);
-
-		local_irq_restore(flags);
+		napi_complete(napi);

You need to add comment here to say interrupt is in fact "disabled" before this point, and "enabled" by xenvif_check_rx_xenvif().

Agreed.

maintainer
#11 lore raw
In reply to Wei Liu

From: Wei Liu <[email protected]>
Date: Tue, 25 Mar 2014 14:50:21 +0000

Quoted 57 lines

You forgot to target this patch to "net" tree in subject line.

Quoted 7 lines · David Vrabel

When the NAPI budget was not all used, xenvif_poll() would call napi_complete() /after/ enabling the interrupt. This resulted in a race between the napi_complete() and the napi_schedule() in the interrupt handler. The use of local_irq_save/restore() avoided by race iff the handler is running on the same CPU but not if it was running on a different CPU.

OK, I understand this issue now. You mentioned it in the other email which made me a bit confused.

Just curious, how do you trigger this? By re-binding the interrupt to another CPU when xenvif_poll is running? I used to run irqbalance (the one that works with xen virtual interrupt) but could not trigger a race. Probably the race window is too small to trigger?

Quoted 38 lines

Fix this properly by calling napi_complete() before reenabling interrupts (in the xenvif_check_rx_xenvif() call).

---
 drivers/net/xen-netback/interface.c |   28 ++--------------------------
 1 files changed, 2 insertions(+), 26 deletions(-)
drivers/net/xen-netback/interface.c +0 -22
index 7669d49..ee322d9 100644
@@ -65,32 +65,8 @@ static int xenvif_poll(struct napi_struct *napi, int budget)
 	work_done = xenvif_tx_action(vif, budget);
 
 	if (work_done < budget) {
-		int more_to_do = 0;
-		unsigned long flags;
-
-		/* It is necessary to disable IRQ before calling
-		 * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
-		 * lose event from the frontend.
-		 *
-		 * Consider:
-		 *   RING_HAS_UNCONSUMED_REQUESTS
-		 *   <frontend generates event to trigger napi_schedule>
-		 *   __napi_complete
-		 *
-		 * This handler is still in scheduled state so the
-		 * event has no effect at all. After __napi_complete
-		 * this handler is descheduled and cannot get
-		 * scheduled again. We lose event in this case and the ring
-		 * will be completely stalled.
-		 */
-
-		local_irq_save(flags);
-
-		RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
Quoted 8 lines

drivers/net/xen-netback/interface.c static int xenvif_poll(struct napi_struct *napi, int budget) · line 68

Quoted 5 lines
drivers/net/xen-netback/interface.c +1 -4
-		if (!more_to_do)
-			__napi_complete(napi);
-
-		local_irq_restore(flags);
+		napi_complete(napi);

You need to add comment here to say interrupt is in fact "disabled" before this point, and "enabled" by xenvif_check_rx_xenvif().

Agreed.

#2 lore raw

interrupt moving and NAPI scheduling

In reply to Zoltan Kiss

Hi,

Let me bump this question: how does the kernel make sure that if an interrupt is moved away from CPU X to Y (e.g. by irqbalance), the NAPI instance already scheduled on CPU Y won't race with the interrupt? I mean the following scenario:

1. instance calls napi_complete on CPU X
2. that removes the instance from the list
3. interrupt happens on CPU Y
4. "!test_and_set_bit(NAPI_STATE_SCHED, &n->state)" fails, as the bit is still set
5. instance not added to the list therefore
6. napi_complete on CPU X clear the bit

My assumption is that when the interrupt moved, the kernel makes sure the NAPI instance won't get scheduled on the old CPU, but I'm not sure about it

Quoted 77 lines · Zoltan Kiss

My idea was that the current code can't race with interrupt running on a different CPU, because if the interrupt was moved since the last napi_schedule (which scheduled NAPI on the same CPU as the interrupt), the kernel would make sure that the NAPI instance is moved along with it. However I couldn't find any trace of this in the kernel so far, but the current code actually works for me, even when I used a bash script to aggressively move the interrupts around while running. I've added David and Eric to the mailing, maybe they can quickly shed some light on this: how does the kernel make sure that if the interrupt is moved away from a CPU (e.g. by irqbalance), the NAPI instance already scheduled there won't race with it?

Zoli

Quoted 55 lines · David Vrabel

When the NAPI budget was not all used, xenvif_poll() would call napi_complete() /after/ enabling the interrupt. This resulted in a race between the napi_complete() and the napi_schedule() in the interrupt handler. The use of local_irq_save/restore() avoided by race iff the handler is running on the same CPU but not if it was running on a different CPU.

Fix this properly by calling napi_complete() before reenabling interrupts (in the xenvif_check_rx_xenvif() call).

---
  drivers/net/xen-netback/interface.c |   28 ++--------------------------
  1 files changed, 2 insertions(+), 26 deletions(-)
drivers/net/xen-netback/interface.c +2 -26
index 7669d49..ee322d9 100644
@@ -65,32 +65,8 @@ static int xenvif_poll(struct napi_struct *napi,
 int budget)
      work_done = xenvif_tx_action(vif, budget);
 
      if (work_done < budget) {
-        int more_to_do = 0;
-        unsigned long flags;
-
-        /* It is necessary to disable IRQ before calling
-         * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
-         * lose event from the frontend.
-         *
-         * Consider:
-         *   RING_HAS_UNCONSUMED_REQUESTS
-         *   <frontend generates event to trigger napi_schedule>
-         *   __napi_complete
-         *
-         * This handler is still in scheduled state so the
-         * event has no effect at all. After __napi_complete
-         * this handler is descheduled and cannot get
-         * scheduled again. We lose event in this case and the ring
-         * will be completely stalled.
-         */
-
-        local_irq_save(flags);
-
-        RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
-        if (!more_to_do)
-            __napi_complete(napi);
-
-        local_irq_restore(flags);
+        napi_complete(napi);
+        xenvif_check_rx_xenvif(vif);
      }
 
Quoted 2 lines
      return work_done;

--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

interrupt moving and NAPI scheduling

In reply to Zoltan Kiss

Hi,

Let me bump this question: how does the kernel make sure that if an interrupt is moved away from CPU X to Y (e.g. by irqbalance), the NAPI instance already scheduled on CPU Y won't race with the interrupt? I mean the following scenario:

1. instance calls napi_complete on CPU X
2. that removes the instance from the list
3. interrupt happens on CPU Y
4. "!test_and_set_bit(NAPI_STATE_SCHED, &n->state)" fails, as the bit is still set
5. instance not added to the list therefore
6. napi_complete on CPU X clear the bit

My assumption is that when the interrupt moved, the kernel makes sure the NAPI instance won't get scheduled on the old CPU, but I'm not sure about it

Quoted 77 lines · Zoltan Kiss

My idea was that the current code can't race with interrupt running on a different CPU, because if the interrupt was moved since the last napi_schedule (which scheduled NAPI on the same CPU as the interrupt), the kernel would make sure that the NAPI instance is moved along with it. However I couldn't find any trace of this in the kernel so far, but the current code actually works for me, even when I used a bash script to aggressively move the interrupts around while running. I've added David and Eric to the mailing, maybe they can quickly shed some light on this: how does the kernel make sure that if the interrupt is moved away from a CPU (e.g. by irqbalance), the NAPI instance already scheduled there won't race with it?

Zoli

Quoted 55 lines · David Vrabel

When the NAPI budget was not all used, xenvif_poll() would call napi_complete() /after/ enabling the interrupt. This resulted in a race between the napi_complete() and the napi_schedule() in the interrupt handler. The use of local_irq_save/restore() avoided by race iff the handler is running on the same CPU but not if it was running on a different CPU.

Fix this properly by calling napi_complete() before reenabling interrupts (in the xenvif_check_rx_xenvif() call).

---
  drivers/net/xen-netback/interface.c |   28 ++--------------------------
  1 files changed, 2 insertions(+), 26 deletions(-)
drivers/net/xen-netback/interface.c +2 -26
index 7669d49..ee322d9 100644
@@ -65,32 +65,8 @@ static int xenvif_poll(struct napi_struct *napi,
 int budget)
      work_done = xenvif_tx_action(vif, budget);
 
      if (work_done < budget) {
-        int more_to_do = 0;
-        unsigned long flags;
-
-        /* It is necessary to disable IRQ before calling
-         * RING_HAS_UNCONSUMED_REQUESTS. Otherwise we might
-         * lose event from the frontend.
-         *
-         * Consider:
-         *   RING_HAS_UNCONSUMED_REQUESTS
-         *   <frontend generates event to trigger napi_schedule>
-         *   __napi_complete
-         *
-         * This handler is still in scheduled state so the
-         * event has no effect at all. After __napi_complete
-         * this handler is descheduled and cannot get
-         * scheduled again. We lose event in this case and the ring
-         * will be completely stalled.
-         */
-
-        local_irq_save(flags);
-
-        RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
-        if (!more_to_do)
-            __napi_complete(napi);
-
-        local_irq_restore(flags);
+        napi_complete(napi);
+        xenvif_check_rx_xenvif(vif);
      }
 
Quoted 2 lines
      return work_done;

--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw

Re: interrupt moving and NAPI scheduling

In reply to Zoltan Kiss
Quoted 15 lines · Zoltan Kiss

Hi,

Let me bump this question: how does the kernel make sure that if an interrupt is moved away from CPU X to Y (e.g. by irqbalance), the NAPI instance already scheduled on CPU Y won't race with the interrupt? I mean the following scenario:

1. instance calls napi_complete on CPU X
2. that removes the instance from the list
3. interrupt happens on CPU Y
4. "!test_and_set_bit(NAPI_STATE_SCHED, &n->state)" fails, as the bit is still set
5. instance not added to the list therefore
6. napi_complete on CPU X clear the bit

Quoted 3 lines

My assumption is that when the interrupt moved, the kernel makes sure the NAPI instance won't get scheduled on the old CPU, but I'm not sure about it

Interrupts are supposed to be disabled at the time we call napi_complete().

Some drivers are buggy.

#4 lore raw

Re: interrupt moving and NAPI scheduling

In reply to Zoltan Kiss
Quoted 15 lines · Zoltan Kiss

Hi,

Let me bump this question: how does the kernel make sure that if an interrupt is moved away from CPU X to Y (e.g. by irqbalance), the NAPI instance already scheduled on CPU Y won't race with the interrupt? I mean the following scenario:

1. instance calls napi_complete on CPU X
2. that removes the instance from the list
3. interrupt happens on CPU Y
4. "!test_and_set_bit(NAPI_STATE_SCHED, &n->state)" fails, as the bit is still set
5. instance not added to the list therefore
6. napi_complete on CPU X clear the bit

Quoted 3 lines

My assumption is that when the interrupt moved, the kernel makes sure the NAPI instance won't get scheduled on the old CPU, but I'm not sure about it

Interrupts are supposed to be disabled at the time we call napi_complete().

Some drivers are buggy.