Skip to contentSkip to conversation

xen/events/fifo: Consume unprocessed events when a CPU dies

PATCH Applied Ross Lagerwall 3 participants 9 messages Jun 19, 2015 – Dec 2, 2015 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/events/fifo: Consume unprocessed events when a CPU dies

When a CPU is offlined, there may be unprocessed events on a port for that CPU. If the port is subsequently reused on a different CPU, it could be in an unexpected state with the link bit set, resulting in interrupts being missed. Fix this by consuming any unprocessed events for a particular CPU when that CPU dies.

---
 drivers/xen/events/events_fifo.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
drivers/xen/events/events_fifo.c +18 -5
index 417415d..1dd0ba12 100644
@@ -281,7 +281,8 @@ static void handle_irq_for_port(unsigned port)
 
 static void consume_one_event(unsigned cpu,
 			      struct evtchn_fifo_control_block *control_block,
-			      unsigned priority, unsigned long *ready)
+			      unsigned priority, unsigned long *ready,
+			      bool drop)
 {
 	struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
 	uint32_t head;
@@ -313,13 +314,17 @@ static void consume_one_event(unsigned cpu,
 	if (head == 0)
 		clear_bit(priority, ready);
 
-	if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port))
-		handle_irq_for_port(port);
+	if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port)) {
+		if (unlikely(drop))
+			pr_warn("Dropping pending event for port %u\n", port);
+		else
+			handle_irq_for_port(port);
+	}
 
 	q->head[priority] = head;
 }
 
-static void evtchn_fifo_handle_events(unsigned cpu)
+static void __evtchn_fifo_handle_events(unsigned cpu, bool drop)
 {
 	struct evtchn_fifo_control_block *control_block;
 	unsigned long ready;
@@ -331,11 +336,16 @@ static void evtchn_fifo_handle_events(unsigned cpu)
 
 	while (ready) {
 		q = find_first_bit(&ready, EVTCHN_FIFO_MAX_QUEUES);
-		consume_one_event(cpu, control_block, q, &ready);
+		consume_one_event(cpu, control_block, q, &ready, drop);
 		ready |= xchg(&control_block->ready, 0);
 	}
 }
 
+static void evtchn_fifo_handle_events(unsigned cpu)
+{
+	__evtchn_fifo_handle_events(cpu, false);
+}
+
 static void evtchn_fifo_resume(void)
 {
 	unsigned cpu;
@@ -420,6 +430,9 @@ static int evtchn_fifo_cpu_notification(struct notifier_block *self,
 		if (!per_cpu(cpu_control_block, cpu))
 			ret = evtchn_fifo_alloc_control_block(cpu);
 		break;
+	case CPU_DEAD:
+		__evtchn_fifo_handle_events(cpu, true);
+		break;
 	default:
 		break;
 	}
Signature · 1 line
2.1.0
#1 lore raw
Quoted 26 lines · Ross Lagerwall

When a CPU is offlined, there may be unprocessed events on a port for that CPU. If the port is subsequently reused on a different CPU, it could be in an unexpected state with the link bit set, resulting in interrupts being missed. Fix this by consuming any unprocessed events for a particular CPU when that CPU dies.

---
  drivers/xen/events/events_fifo.c | 23 ++++++++++++++++++-----
  1 file changed, 18 insertions(+), 5 deletions(-)
drivers/xen/events/events_fifo.c +2 -1
index 417415d..1dd0ba12 100644
@@ -281,7 +281,8 @@ static void handle_irq_for_port(unsigned port)
 
  static void consume_one_event(unsigned cpu,
  			      struct evtchn_fifo_control_block *control_block,
-			      unsigned priority, unsigned long *ready)
+			      unsigned priority, unsigned long *ready,
+			      bool drop)
  {
  	struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
  	uint32_t head;

drivers/xen/events/events_fifo.c static void consume_one_event(unsigned cpu, · line 319

Quoted 8 lines
drivers/xen/events/events_fifo.c +3 -2
@@ -313,13 +314,17 @@ static void consume_one_event(unsigned cpu,
  	if (head == 0)
  		clear_bit(priority, ready);
 
-	if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port))
-		handle_irq_for_port(port);
+	if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port)) {
+		if (unlikely(drop))
+			pr_warn("Dropping pending event for port %u\n", port);

Maybe pr_info (or pr_notice)?

Also, why not do this (testing for unprocessed events) in xen_evtchn_close()?

-boris

Quoted 40 lines
drivers/xen/events/events_fifo.c +13 -2
+		else
+			handle_irq_for_port(port);
+	}
 
  	q->head[priority] = head;
  }
 
-static void evtchn_fifo_handle_events(unsigned cpu)
+static void __evtchn_fifo_handle_events(unsigned cpu, bool drop)
  {
  	struct evtchn_fifo_control_block *control_block;
  	unsigned long ready;
@@ -331,11 +336,16 @@ static void evtchn_fifo_handle_events(unsigned cpu)
 
  	while (ready) {
  		q = find_first_bit(&ready, EVTCHN_FIFO_MAX_QUEUES);
-		consume_one_event(cpu, control_block, q, &ready);
+		consume_one_event(cpu, control_block, q, &ready, drop);
  		ready |= xchg(&control_block->ready, 0);
  	}
  }
 
+static void evtchn_fifo_handle_events(unsigned cpu)
+{
+	__evtchn_fifo_handle_events(cpu, false);
+}
+
  static void evtchn_fifo_resume(void)
  {
  	unsigned cpu;
@@ -420,6 +430,9 @@ static int evtchn_fifo_cpu_notification(struct notifier_block *self,
  		if (!per_cpu(cpu_control_block, cpu))
  			ret = evtchn_fifo_alloc_control_block(cpu);
  		break;
+	case CPU_DEAD:
+		__evtchn_fifo_handle_events(cpu, true);
+		break;
  	default:
  		break;
  	}
#2 lore raw
Quoted 28 lines · Boris Ostrovsky
Quoted 27 lines · Ross Lagerwall

When a CPU is offlined, there may be unprocessed events on a port for that CPU. If the port is subsequently reused on a different CPU, it could be in an unexpected state with the link bit set, resulting in interrupts being missed. Fix this by consuming any unprocessed events for a particular CPU when that CPU dies.

---
  drivers/xen/events/events_fifo.c | 23 ++++++++++++++++++-----
  1 file changed, 18 insertions(+), 5 deletions(-)
drivers/xen/events/events_fifo.c +2 -1
index 417415d..1dd0ba12 100644
@@ -281,7 +281,8 @@ static void handle_irq_for_port(unsigned port)
    static void consume_one_event(unsigned cpu,
                    struct evtchn_fifo_control_block *control_block,
-                  unsigned priority, unsigned long *ready)
+                  unsigned priority, unsigned long *ready,
+                  bool drop)
  {
      struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
      uint32_t head;
@@ -313,13 +314,17 @@ static void consume_one_event(unsigned cpu,
      if (head == 0)
Quoted 8 lines

drivers/xen/events/events_fifo.c static void consume_one_event(unsigned cpu, · line 319

Quoted 6 lines
drivers/xen/events/events_fifo.c +3 -1
          clear_bit(priority, ready);
  -    if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port))
-        handle_irq_for_port(port);
+    if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port)) {
+        if (unlikely(drop))
+            pr_warn("Dropping pending event for port %u\n", port);

Maybe pr_info (or pr_notice)?

We want a warning here because we think this shouldn't happen -- if it does we actually need to retrigger the event on its new CPU.

Quoted 2 lines

Also, why not do this (testing for unprocessed events) in xen_evtchn_close()?

We can't do anything about them when closing because they may be in the middle of a queue.

David

#3 lore raw
Quoted 41 lines · David Vrabel
Quoted 28 lines · Boris Ostrovsky
Quoted 27 lines · Ross Lagerwall

When a CPU is offlined, there may be unprocessed events on a port for that CPU. If the port is subsequently reused on a different CPU, it could be in an unexpected state with the link bit set, resulting in interrupts being missed. Fix this by consuming any unprocessed events for a particular CPU when that CPU dies.

---
   drivers/xen/events/events_fifo.c | 23 ++++++++++++++++++-----
   1 file changed, 18 insertions(+), 5 deletions(-)
drivers/xen/events/events_fifo.c +2 -1
index 417415d..1dd0ba12 100644
@@ -281,7 +281,8 @@ static void handle_irq_for_port(unsigned port)
     static void consume_one_event(unsigned cpu,
                     struct evtchn_fifo_control_block *control_block,
-                  unsigned priority, unsigned long *ready)
+                  unsigned priority, unsigned long *ready,
+                  bool drop)
   {
       struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
       uint32_t head;
@@ -313,13 +314,17 @@ static void consume_one_event(unsigned cpu,
       if (head == 0)
Quoted 8 lines

drivers/xen/events/events_fifo.c static void consume_one_event(unsigned cpu, · line 319

Quoted 6 lines
drivers/xen/events/events_fifo.c +3 -1
           clear_bit(priority, ready);
   -    if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port))
-        handle_irq_for_port(port);
+    if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port)) {
+        if (unlikely(drop))
+            pr_warn("Dropping pending event for port %u\n", port);

Maybe pr_info (or pr_notice)?

We want a warning here because we think this shouldn't happen -- if it does we actually need to retrigger the event on its new CPU.

Quoted 8 lines
Quoted 2 lines

Also, why not do this (testing for unprocessed events) in xen_evtchn_close()?

We can't do anything about them when closing because they may be in the middle of a queue.

David

Ping. Is this change OK?

Signature · 1 line
Ross Lagerwall
#4 lore raw
Quoted 41 lines · Ross Lagerwall
Quoted 40 lines · David Vrabel
Quoted 38 lines · Boris Ostrovsky
Quoted 26 lines · Ross Lagerwall

When a CPU is offlined, there may be unprocessed events on a port for that CPU. If the port is subsequently reused on a different CPU, it could be in an unexpected state with the link bit set, resulting in interrupts being missed. Fix this by consuming any unprocessed events for a particular CPU when that CPU dies.

---
   drivers/xen/events/events_fifo.c | 23 ++++++++++++++++++-----
   1 file changed, 18 insertions(+), 5 deletions(-)
drivers/xen/events/events_fifo.c +2 -1
index 417415d..1dd0ba12 100644
@@ -281,7 +281,8 @@ static void handle_irq_for_port(unsigned port)
     static void consume_one_event(unsigned cpu,
                     struct evtchn_fifo_control_block *control_block,
-                  unsigned priority, unsigned long *ready)
+                  unsigned priority, unsigned long *ready,
+                  bool drop)
   {
       struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
       uint32_t head;

drivers/xen/events/events_fifo.c static void consume_one_event(unsigned cpu, · line 321

Quoted 9 lines
drivers/xen/events/events_fifo.c +3 -1
@@ -313,13 +314,17 @@ static void consume_one_event(unsigned cpu,
       if (head == 0)
           clear_bit(priority, ready);
   -    if (evtchn_fifo_is_pending(port) && 
 !evtchn_fifo_is_masked(port))
-        handle_irq_for_port(port);
+    if (evtchn_fifo_is_pending(port) && 
 !evtchn_fifo_is_masked(port)) {
+        if (unlikely(drop))
+            pr_warn("Dropping pending event for port %u\n", port);

Maybe pr_info (or pr_notice)?

Quoted 8 lines
Quoted 8 lines

We want a warning here because we think this shouldn't happen -- if it does we actually need to retrigger the event on its new CPU.

Quoted 2 lines

Also, why not do this (testing for unprocessed events) in xen_evtchn_close()?

We can't do anything about them when closing because they may be in the middle of a queue.

(Sorry, I missed this)

Why can't (actually, why doesn't) the cpu that is being offlined drain its queue?

-boris

Quoted 6 lines
Quoted 3 lines

David

Ping. Is this change OK?

#5 lore raw
Quoted 48 lines · Boris Ostrovsky
Quoted 47 lines · Ross Lagerwall
Quoted 46 lines · David Vrabel
Quoted 30 lines · Boris Ostrovsky
Quoted 29 lines · Ross Lagerwall

When a CPU is offlined, there may be unprocessed events on a port for that CPU. If the port is subsequently reused on a different CPU, it could be in an unexpected state with the link bit set, resulting in interrupts being missed. Fix this by consuming any unprocessed events for a particular CPU when that CPU dies.

---
   drivers/xen/events/events_fifo.c | 23 ++++++++++++++++++-----
   1 file changed, 18 insertions(+), 5 deletions(-)
drivers/xen/events/events_fifo.c +2 -1
index 417415d..1dd0ba12 100644
@@ -281,7 +281,8 @@ static void handle_irq_for_port(unsigned port)
     static void consume_one_event(unsigned cpu,
                     struct evtchn_fifo_control_block *control_block,
-                  unsigned priority, unsigned long *ready)
+                  unsigned priority, unsigned long *ready,
+                  bool drop)
   {
       struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
       uint32_t head;
@@ -313,13 +314,17 @@ static void consume_one_event(unsigned cpu,
       if (head == 0)
           clear_bit(priority, ready);
   -    if (evtchn_fifo_is_pending(port) &&
Quoted 8 lines

drivers/xen/events/events_fifo.c static void consume_one_event(unsigned cpu, · line 319

Quoted 6 lines
drivers/xen/events/events_fifo.c +3 -1
 !evtchn_fifo_is_masked(port))
-        handle_irq_for_port(port);
+    if (evtchn_fifo_is_pending(port) &&
 !evtchn_fifo_is_masked(port)) {
+        if (unlikely(drop))
+            pr_warn("Dropping pending event for port %u\n", port);

Maybe pr_info (or pr_notice)?

We want a warning here because we think this shouldn't happen -- if it does we actually need to retrigger the event on its new CPU.

Quoted 2 lines

Also, why not do this (testing for unprocessed events) in xen_evtchn_close()?

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

We can't do anything about them when closing because they may be in the middle of a queue.

(Sorry, I missed this)

Why can't (actually, why doesn't) the cpu that is being offlined drain its queue?

Where would this be done? I thought using CPU notifiers was the correct way to hook when a CPU goes down without having to stick fifo event channel code in the core Xen code.

Signature · 1 line
Ross Lagerwall
#6 lore raw
Quoted 53 lines · Ross Lagerwall
Quoted 52 lines · Boris Ostrovsky
Quoted 50 lines · Ross Lagerwall
Quoted 49 lines · David Vrabel
Quoted 31 lines · Boris Ostrovsky
Quoted 30 lines · Ross Lagerwall

When a CPU is offlined, there may be unprocessed events on a port for that CPU. If the port is subsequently reused on a different CPU, it could be in an unexpected state with the link bit set, resulting in interrupts being missed. Fix this by consuming any unprocessed events for a particular CPU when that CPU dies.

---
   drivers/xen/events/events_fifo.c | 23 ++++++++++++++++++-----
   1 file changed, 18 insertions(+), 5 deletions(-)
drivers/xen/events/events_fifo.c +2 -1
index 417415d..1dd0ba12 100644
@@ -281,7 +281,8 @@ static void handle_irq_for_port(unsigned port)
     static void consume_one_event(unsigned cpu,
                     struct evtchn_fifo_control_block 
 *control_block,
-                  unsigned priority, unsigned long *ready)
+                  unsigned priority, unsigned long *ready,
+                  bool drop)
   {
       struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
       uint32_t head;
@@ -313,13 +314,17 @@ static void consume_one_event(unsigned cpu,
       if (head == 0)
           clear_bit(priority, ready);
   -    if (evtchn_fifo_is_pending(port) &&
Quoted 8 lines

drivers/xen/events/events_fifo.c static void consume_one_event(unsigned cpu, · line 319

Quoted 6 lines
drivers/xen/events/events_fifo.c +3 -1
 !evtchn_fifo_is_masked(port))
-        handle_irq_for_port(port);
+    if (evtchn_fifo_is_pending(port) &&
 !evtchn_fifo_is_masked(port)) {
+        if (unlikely(drop))
+            pr_warn("Dropping pending event for port %u\n", port);

Maybe pr_info (or pr_notice)?

We want a warning here because we think this shouldn't happen -- if it does we actually need to retrigger the event on its new CPU.

Quoted 2 lines

Also, why not do this (testing for unprocessed events) in xen_evtchn_close()?

We can't do anything about them when closing because they may be in the middle of a queue.

Quoted 7 lines
Quoted 5 lines

(Sorry, I missed this)

Why can't (actually, why doesn't) the cpu that is being offlined drain its queue?

Where would this be done? I thought using CPU notifiers was the correct way to hook when a CPU goes down without having to stick fifo event channel code in the core Xen code.

In xen_evtchn_close(). We should be getting there (roughly) as cpu_die() -> xen_cpu_die() -> xen_smp_intr_free() -> unbind_from_irqhandler(). In fact, this path is taken right before cpu_down() sends CPU_DEAD notifications.

I think cleaning up in xen_evtchn_close() is better because it is possible to close event channel for reasons other than CPU going away, in which case we also may need to deal with unprocessed events.

(BTW, I noticed that you are cleaning up fifo events only. Do we need to do the same for 2-level?)

-boris

#7 lore raw
Quoted 67 lines · Boris Ostrovsky
Quoted 58 lines · Ross Lagerwall
Quoted 57 lines · Boris Ostrovsky
Quoted 47 lines · Ross Lagerwall
Quoted 46 lines · David Vrabel
Quoted 33 lines · Boris Ostrovsky
Quoted 32 lines · Ross Lagerwall

When a CPU is offlined, there may be unprocessed events on a port for
that CPU. If the port is subsequently reused on a different CPU, it could be in an unexpected state with the link bit set, resulting in interrupts being missed. Fix this by consuming any unprocessed events
for a particular CPU when that CPU dies.

---
   drivers/xen/events/events_fifo.c | 23 ++++++++++++++++++-----
   1 file changed, 18 insertions(+), 5 deletions(-)
drivers/xen/events/events_fifo.c +2 -1
index 417415d..1dd0ba12 100644
@@ -281,7 +281,8 @@ static void handle_irq_for_port(unsigned port)
     static void consume_one_event(unsigned cpu,
                     struct evtchn_fifo_control_block
 *control_block,
-                  unsigned priority, unsigned long *ready)
+                  unsigned priority, unsigned long *ready,
+                  bool drop)
   {
       struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
       uint32_t head;
@@ -313,13 +314,17 @@ static void consume_one_event(unsigned cpu,
       if (head == 0)
           clear_bit(priority, ready);
   -    if (evtchn_fifo_is_pending(port) &&
Quoted 8 lines

drivers/xen/events/events_fifo.c static void consume_one_event(unsigned cpu, · line 321

Quoted 6 lines
drivers/xen/events/events_fifo.c +3 -1
 !evtchn_fifo_is_masked(port))
-        handle_irq_for_port(port);
+    if (evtchn_fifo_is_pending(port) &&
 !evtchn_fifo_is_masked(port)) {
+        if (unlikely(drop))
+            pr_warn("Dropping pending event for port %u\n", port);

Maybe pr_info (or pr_notice)?

We want a warning here because we think this shouldn't happen -- if it does we actually need to retrigger the event on its new CPU.

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

Also, why not do this (testing for unprocessed events) in xen_evtchn_close()?

We can't do anything about them when closing because they may be in the
middle of a queue.

(Sorry, I missed this)

Quoted 7 lines
Quoted 3 lines

Why can't (actually, why doesn't) the cpu that is being offlined drain its queue?

Where would this be done? I thought using CPU notifiers was the correct way to hook when a CPU goes down without having to stick fifo event channel code in the core Xen code.

Quoted 8 lines

In xen_evtchn_close(). We should be getting there (roughly) as cpu_die() -> xen_cpu_die() -> xen_smp_intr_free() -> unbind_from_irqhandler(). In fact, this path is taken right before cpu_down() sends CPU_DEAD notifications.

I think cleaning up in xen_evtchn_close() is better because it is possible to close event channel for reasons other than CPU going away, in which case we also may need to deal with unprocessed events.

Having looked at this further and attempted to do this, draining events in close is difficult because

a) we can't wait for LINKED to clear when closing since we're holding the desc spin lock; and deferring the close to a tasklet or work doesn't work because:

b) rebinding a PIRQ will fail if the close is deferred and not yet completed and there is no way to ensure the close happens promptly without changes to core irq code.

c) Xen will be fixed to not reuse ports that are still LINKED.

I'm going to apply Ross's original patch and Cc stable.

David

Applied
#8 lore raw
In reply to Ross Lagerwall
Quoted 5 lines · Ross Lagerwall

When a CPU is offlined, there may be unprocessed events on a port for that CPU. If the port is subsequently reused on a different CPU, it could be in an unexpected state with the link bit set, resulting in interrupts being missed. Fix this by consuming any unprocessed events for a particular CPU when that CPU dies.

Applied to for-linus-4.4, thanks.

David