Skip to contentSkip to conversation

evtchn: recent XSAs follow-on

PATCH Superseded Jan Beulich 6 participants 44 messages Oct 20, 2020 – Nov 3, 2020

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover evtchn: recent XSAs follow-on

These are grouped into a series largely because of their origin, not so much because there are heavy dependencies among them. Compared to v1, besides there being 3 new patches, some re-ordering has been done; in particular the last patch isn't ready yet, but I still wanted to include it to have a chance to discuss what changes to make. See also the individual patches.

1: avoid race in get_xen_consumer()
2: replace FIFO-specific header by generic private one
3: rename and adjust guest_enabled_event()
4: let evtchn_set_priority() acquire the per-channel lock
5: drop acquiring of per-channel lock from send_guest_{global,vcpu}_virq() 6: convert vIRQ lock to an r/w one
7: convert domain event lock to an r/w one
8: don't call Xen consumer callback with per-channel lock held

Jan

#1 lore raw

PATCH v2 1/8 evtchn: avoid race in get_xen_consumer()

There's no global lock around the updating of this global piece of data. Make use of cmpxchgptr() to avoid two entities racing with their updates.

While touching the functionality, mark xen_consumers[] read-mostly (or else the if() condition could use the result of cmpxchgptr(), writing to the slot unconditionally).

The use of cmpxchgptr() here points out (by way of clang warning about it) that its original use of const was slightly wrong. Adjust the placement, or else undefined behavior of const qualifying a function type will result.

---
v2: Use (and hence generalize) cmpxchgptr(). Add comment. Expand /
adjust description.

xen/common/event_channel.c +4 -2
@@ -57,7 +57,8 @@
  * with a pointer, we stash them dynamically in a small lookup array which
  * can be indexed by a small integer.
  */
-static xen_event_channel_notification_t xen_consumers[NR_XEN_CONSUMERS];
+static xen_event_channel_notification_t __read_mostly
+    xen_consumers[NR_XEN_CONSUMERS];
 
 /* Default notification action: wake up from wait_on_xen_event_channel(). */
 static void default_xen_notification_fn(struct vcpu *v, unsigned int port)
@@ -80,8 +81,9 @@ static uint8_t get_xen_consumer(xen_even
 
     for ( i = 0; i < ARRAY_SIZE(xen_consumers); i++ )
     {
+        /* Use cmpxchgptr() in lieu of a global lock. */
         if ( xen_consumers[i] == NULL )
-            xen_consumers[i] = fn;
+            cmpxchgptr(&xen_consumers[i], NULL, fn);
         if ( xen_consumers[i] == fn )
             break;
     }
xen/include/asm-x86/system.h +0 -7
@@ -148,13 +148,6 @@ static always_inline unsigned long cmpxc
     return prev;
 }
 
-#define cmpxchgptr(ptr,o,n) ({                                          \
-    const __typeof__(**(ptr)) *__o = (o);                               \
-    __typeof__(**(ptr)) *__n = (n);                                     \
-    ((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)__o,            \
-                                   (unsigned long)__n,sizeof(*(ptr)))); \
-})
-
 /*
  * Undefined symbol to cause link failure if a wrong size is used with
  * arch_fetch_and_add().
xen/include/xen/lib.h +11 -0
@@ -178,6 +178,17 @@ unsigned long long parse_size_and_unit(c
 
 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c);
 
+/*
+ * A slightly more typesafe variant of cmpxchg() when the entities dealt with
+ * are pointers.
+ */
+#define cmpxchgptr(ptr, o, n) ({                                        \
+    __typeof__(**(ptr)) *const o_ = (o);                                \
+    __typeof__(**(ptr)) *n_ = (n);                                      \
+    ((__typeof__(*(ptr)))__cmpxchg(ptr, (unsigned long)o_,              \
+                                   (unsigned long)n_, sizeof(*(ptr)))); \
+})
+
 #define TAINT_SYNC_CONSOLE              (1u << 0)
 #define TAINT_MACHINE_CHECK             (1u << 1)
 #define TAINT_ERROR_INJECT              (1u << 2)
#2 lore raw

Re: [PATCH v2 1/8] evtchn: avoid race in get_xen_consumer()

Quoted 7 lines · Jan Beulich

There's no global lock around the updating of this global piece of data. Make use of cmpxchgptr() to avoid two entities racing with their updates.

While touching the functionality, mark xen_consumers[] read-mostly (or else the if() condition could use the result of cmpxchgptr(), writing to the slot unconditionally).

I'm not sure I get this, likely related to the comment I have below.

Quoted 24 lines

The use of cmpxchgptr() here points out (by way of clang warning about it) that its original use of const was slightly wrong. Adjust the placement, or else undefined behavior of const qualifying a function type will result.

---
v2: Use (and hence generalize) cmpxchgptr(). Add comment. Expand /
adjust description.

xen/common/event_channel.c +2 -1
@@ -57,7 +57,8 @@
  * with a pointer, we stash them dynamically in a small lookup array which
  * can be indexed by a small integer.
  */
-static xen_event_channel_notification_t xen_consumers[NR_XEN_CONSUMERS];
+static xen_event_channel_notification_t __read_mostly
+    xen_consumers[NR_XEN_CONSUMERS];
 
 /* Default notification action: wake up from wait_on_xen_event_channel(). */
 static void default_xen_notification_fn(struct vcpu *v, unsigned int port)

xen/common/event_channel.c static uint8_t get_xen_consumer(xen_even · line 88

Quoted 9 lines
xen/common/event_channel.c +2 -1
@@ -80,8 +81,9 @@ static uint8_t get_xen_consumer(xen_even
 
     for ( i = 0; i < ARRAY_SIZE(xen_consumers); i++ )
     {
+        /* Use cmpxchgptr() in lieu of a global lock. */
         if ( xen_consumers[i] == NULL )
-            xen_consumers[i] = fn;
+            cmpxchgptr(&xen_consumers[i], NULL, fn);
         if ( xen_consumers[i] == fn )
             break;

I think you could join it as:

if ( !xen_consumers[i] &&
!cmpxchgptr(&xen_consumers[i], NULL, fn) )
break;

As cmpxchgptr will return the previous value of &xen_consumers[i]?

Thanks, Roger.

#3 lore raw

Re: [PATCH v2 1/8] evtchn: avoid race in get_xen_consumer()

Quoted 10 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich

There's no global lock around the updating of this global piece of data. Make use of cmpxchgptr() to avoid two entities racing with their updates.

While touching the functionality, mark xen_consumers[] read-mostly (or else the if() condition could use the result of cmpxchgptr(), writing to the slot unconditionally).

I'm not sure I get this, likely related to the comment I have below.

This is about the alternative case of invoking cmpxchgptr() without the if() around it. On x86 this would mean always writing the field, even if the designated value is already in place.

Quoted 25 lines
Quoted 13 lines
xen/common/event_channel.c +2 -1
@@ -57,7 +57,8 @@
  * with a pointer, we stash them dynamically in a small lookup array which
  * can be indexed by a small integer.
  */
-static xen_event_channel_notification_t xen_consumers[NR_XEN_CONSUMERS];
+static xen_event_channel_notification_t __read_mostly
+    xen_consumers[NR_XEN_CONSUMERS];
 
 /* Default notification action: wake up from wait_on_xen_event_channel(). */
 static void default_xen_notification_fn(struct vcpu *v, unsigned int port)

xen/common/event_channel.c static uint8_t get_xen_consumer(xen_even · line 88

Quoted 9 lines
xen/common/event_channel.c +2 -1
@@ -80,8 +81,9 @@ static uint8_t get_xen_consumer(xen_even
 
     for ( i = 0; i < ARRAY_SIZE(xen_consumers); i++ )
     {
+        /* Use cmpxchgptr() in lieu of a global lock. */
         if ( xen_consumers[i] == NULL )
-            xen_consumers[i] = fn;
+            cmpxchgptr(&xen_consumers[i], NULL, fn);
         if ( xen_consumers[i] == fn )
             break;

I think you could join it as:

Quoted 5 lines

if ( !xen_consumers[i] &&
!cmpxchgptr(&xen_consumers[i], NULL, fn) )
break;

As cmpxchgptr will return the previous value of &xen_consumers[i]?

But then you also have to check whether the returned value is fn (or retain the 2nd if()).

Jan

#4 lore raw

Re: [PATCH v2 1/8] evtchn: avoid race in get_xen_consumer()

Quoted 42 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monné
Quoted 7 lines · Jan Beulich

There's no global lock around the updating of this global piece of data. Make use of cmpxchgptr() to avoid two entities racing with their updates.

While touching the functionality, mark xen_consumers[] read-mostly (or else the if() condition could use the result of cmpxchgptr(), writing to the slot unconditionally).

I'm not sure I get this, likely related to the comment I have below.

This is about the alternative case of invoking cmpxchgptr() without the if() around it. On x86 this would mean always writing the field, even if the designated value is already in place.

Quoted 25 lines
Quoted 13 lines
xen/common/event_channel.c +2 -1
@@ -57,7 +57,8 @@
  * with a pointer, we stash them dynamically in a small lookup array which
  * can be indexed by a small integer.
  */
-static xen_event_channel_notification_t xen_consumers[NR_XEN_CONSUMERS];
+static xen_event_channel_notification_t __read_mostly
+    xen_consumers[NR_XEN_CONSUMERS];
 
 /* Default notification action: wake up from wait_on_xen_event_channel(). */
 static void default_xen_notification_fn(struct vcpu *v, unsigned int port)

xen/common/event_channel.c static uint8_t get_xen_consumer(xen_even · line 88

Quoted 9 lines
xen/common/event_channel.c +2 -1
@@ -80,8 +81,9 @@ static uint8_t get_xen_consumer(xen_even
 
     for ( i = 0; i < ARRAY_SIZE(xen_consumers); i++ )
     {
+        /* Use cmpxchgptr() in lieu of a global lock. */
         if ( xen_consumers[i] == NULL )
-            xen_consumers[i] = fn;
+            cmpxchgptr(&xen_consumers[i], NULL, fn);
         if ( xen_consumers[i] == fn )
             break;

I think you could join it as:

Quoted 8 lines
Quoted 5 lines

if ( !xen_consumers[i] &&
!cmpxchgptr(&xen_consumers[i], NULL, fn) )
break;

As cmpxchgptr will return the previous value of &xen_consumers[i]?

But then you also have to check whether the returned value is fn (or retain the 2nd if()).

__cmpxchg comment says that success of the operation is indicated when the returned value equals the old value, so it's my understanding that cmpxchgptr returning NULL would mean the exchange has succeed and that xen_consumers[i] == fn?

Roger.

#5 lore raw

Re: [PATCH v2 1/8] evtchn: avoid race in get_xen_consumer()

Quoted 22 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 19 lines · Roger Pau Monné

xen/common/event_channel.c static uint8_t get_xen_consumer(xen_even · line 88

Quoted 10 lines · Jan Beulich
xen/common/event_channel.c +2 -1
@@ -80,8 +81,9 @@ static uint8_t get_xen_consumer(xen_even
 
     for ( i = 0; i < ARRAY_SIZE(xen_consumers); i++ )
     {
+        /* Use cmpxchgptr() in lieu of a global lock. */
         if ( xen_consumers[i] == NULL )
-            xen_consumers[i] = fn;
+            cmpxchgptr(&xen_consumers[i], NULL, fn);
         if ( xen_consumers[i] == fn )
             break;

I think you could join it as:

if ( !xen_consumers[i] &&
!cmpxchgptr(&xen_consumers[i], NULL, fn) )
break;

As cmpxchgptr will return the previous value of &xen_consumers[i]?

Quoted 7 lines
Quoted 2 lines

But then you also have to check whether the returned value is fn (or retain the 2nd if()).

__cmpxchg comment says that success of the operation is indicated when the returned value equals the old value, so it's my understanding that cmpxchgptr returning NULL would mean the exchange has succeed and that xen_consumers[i] == fn?

Correct. But if xen_consumers[i] == fn before the call, the return value will be fn. The cmpxchg() wasn't "successful" in this case (it didn't update anything), but the state of the array slot is what we want.

Jan

#6 lore raw

Re: [PATCH v2 1/8] evtchn: avoid race in get_xen_consumer()

Quoted 31 lines · Jan Beulich
Quoted 22 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 19 lines · Roger Pau Monné

xen/common/event_channel.c static uint8_t get_xen_consumer(xen_even · line 88

Quoted 10 lines · Jan Beulich
xen/common/event_channel.c +2 -1
@@ -80,8 +81,9 @@ static uint8_t get_xen_consumer(xen_even
 
     for ( i = 0; i < ARRAY_SIZE(xen_consumers); i++ )
     {
+        /* Use cmpxchgptr() in lieu of a global lock. */
         if ( xen_consumers[i] == NULL )
-            xen_consumers[i] = fn;
+            cmpxchgptr(&xen_consumers[i], NULL, fn);
         if ( xen_consumers[i] == fn )
             break;

I think you could join it as:

if ( !xen_consumers[i] &&
!cmpxchgptr(&xen_consumers[i], NULL, fn) )
break;

As cmpxchgptr will return the previous value of &xen_consumers[i]?

Quoted 7 lines
Quoted 2 lines

But then you also have to check whether the returned value is fn (or retain the 2nd if()).

__cmpxchg comment says that success of the operation is indicated when the returned value equals the old value, so it's my understanding that cmpxchgptr returning NULL would mean the exchange has succeed and that xen_consumers[i] == fn?

Quoted 4 lines

Correct. But if xen_consumers[i] == fn before the call, the return value will be fn. The cmpxchg() wasn't "successful" in this case (it didn't update anything), but the state of the array slot is what we want.

Oh, I get it now. You don't want the same fn populating more than one slot.

I assume the reads of xen_consumers are not using ACCESS_ONCE or read_atomic because we rely on the compiler performing such reads as single instructions?

Roger.

#7 lore raw

Re: [PATCH v2 1/8] evtchn: avoid race in get_xen_consumer()

Quoted 32 lines · Roger Pau Monné
Quoted 31 lines · Jan Beulich
Quoted 29 lines · Roger Pau Monné
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Roger Pau Monné

xen/common/event_channel.c static uint8_t get_xen_consumer(xen_even · line 88

Quoted 10 lines · Jan Beulich
xen/common/event_channel.c +2 -1
@@ -80,8 +81,9 @@ static uint8_t get_xen_consumer(xen_even
 
     for ( i = 0; i < ARRAY_SIZE(xen_consumers); i++ )
     {
+        /* Use cmpxchgptr() in lieu of a global lock. */
         if ( xen_consumers[i] == NULL )
-            xen_consumers[i] = fn;
+            cmpxchgptr(&xen_consumers[i], NULL, fn);
         if ( xen_consumers[i] == fn )
             break;

I think you could join it as:

Quoted 8 lines
Quoted 5 lines

if ( !xen_consumers[i] &&
!cmpxchgptr(&xen_consumers[i], NULL, fn) )
break;

As cmpxchgptr will return the previous value of &xen_consumers[i]?

But then you also have to check whether the returned value is fn (or retain the 2nd if()).

__cmpxchg comment says that success of the operation is indicated when the returned value equals the old value, so it's my understanding that cmpxchgptr returning NULL would mean the exchange has succeed and that xen_consumers[i] == fn?

Quoted 7 lines
Quoted 4 lines

Correct. But if xen_consumers[i] == fn before the call, the return value will be fn. The cmpxchg() wasn't "successful" in this case (it didn't update anything), but the state of the array slot is what we want.

Oh, I get it now. You don't want the same fn populating more than one slot.

FAOD it's not just "want", it's a strict requirement.

Quoted 3 lines

I assume the reads of xen_consumers are not using ACCESS_ONCE or read_atomic because we rely on the compiler performing such reads as single instructions?

Yes, as in so many other places in the code base.

Jan

#8 lore raw

Re: [PATCH v2 1/8] evtchn: avoid race in get_xen_consumer()

Quoted 38 lines · Jan Beulich
Quoted 37 lines · Roger Pau Monné
Quoted 35 lines · Jan Beulich
Quoted 22 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 19 lines · Roger Pau Monné

Review static uint8_t get_xen_consumer(xen_even · line 88

Quoted 10 lines · Jan Beulich
(quoted diff) +2 -1
@@ -80,8 +81,9 @@ static uint8_t get_xen_consumer(xen_even
 
     for ( i = 0; i < ARRAY_SIZE(xen_consumers); i++ )
     {
+        /* Use cmpxchgptr() in lieu of a global lock. */
         if ( xen_consumers[i] == NULL )
-            xen_consumers[i] = fn;
+            cmpxchgptr(&xen_consumers[i], NULL, fn);
         if ( xen_consumers[i] == fn )
             break;

I think you could join it as:

if ( !xen_consumers[i] &&
!cmpxchgptr(&xen_consumers[i], NULL, fn) )
break;

As cmpxchgptr will return the previous value of &xen_consumers[i]?

Quoted 7 lines
Quoted 2 lines

But then you also have to check whether the returned value is fn (or retain the 2nd if()).

__cmpxchg comment says that success of the operation is indicated when the returned value equals the old value, so it's my understanding that cmpxchgptr returning NULL would mean the exchange has succeed and that xen_consumers[i] == fn?

Correct. But if xen_consumers[i] == fn before the call, the return value will be fn. The cmpxchg() wasn't "successful" in this case (it didn't update anything), but the state of the array slot is what we want.

Quoted 4 lines
Quoted 2 lines

Oh, I get it now. You don't want the same fn populating more than one slot.

FAOD it's not just "want", it's a strict requirement.

I wouldn't mind having a comment to that effect in the function, but I won't insist.

Thanks, Roger.

Reviewed-by on v2 1/8
#9 lore raw

Re: [PATCH v2 1/8] evtchn: avoid race in get_xen_consumer()

In reply to Jan Beulich
Quoted 8 lines · Jan Beulich

There's no global lock around the updating of this global piece of data. Make use of cmpxchgptr() to avoid two entities racing with their updates.

While touching the functionality, mark xen_consumers[] read-mostly (or else the if() condition could use the result of cmpxchgptr(), writing to the slot unconditionally).

Quoted 6 lines

The use of cmpxchgptr() here points out (by way of clang warning about it) that its original use of const was slightly wrong. Adjust the placement, or else undefined behavior of const qualifying a function type will result.

Thanks, Roger.

Acked-by on v2 1/8
#10 lore raw

Re: [PATCH v2 1/8] evtchn: avoid race in get_xen_consumer()

In reply to Jan Beulich

Hi Jan,

Quoted 8 lines · Jan Beulich

There's no global lock around the updating of this global piece of data. Make use of cmpxchgptr() to avoid two entities racing with their updates.

While touching the functionality, mark xen_consumers[] read-mostly (or else the if() condition could use the result of cmpxchgptr(), writing to the slot unconditionally).

Quoted 6 lines

The use of cmpxchgptr() here points out (by way of clang warning about it) that its original use of const was slightly wrong. Adjust the placement, or else undefined behavior of const qualifying a function type will result.

Cheers,

Signature · 1 line
Julien Grall
#11 lore raw

PATCH v2 2/8 evtchn: replace FIFO-specific header by generic private one

In reply to Jan Beulich

Having a FIFO specific header is not (or at least no longer) warranted with just three function declarations left there. Introduce a private header instead, moving there some further items from xen/event.h.

---
v2: New.
---
TBD: If - considering the layering violation that's there anyway - we
allowed PV shim code to make use of this header, a few more items could be moved out of "public sight".

xen/common/event_2l.c +2 -1
@@ -7,11 +7,12 @@
  * Version 2 or later.  See the file COPYING for more details.
  */
 
+#include "event_channel.h"
+
 #include <xen/init.h>
 #include <xen/lib.h>
 #include <xen/errno.h>
 #include <xen/sched.h>
-#include <xen/event.h>
 
 #include <asm/guest_atomics.h>
 
xen/common/event_channel.c +2 -2
@@ -14,17 +14,17 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "event_channel.h"
+
 #include <xen/init.h>
 #include <xen/lib.h>
 #include <xen/errno.h>
 #include <xen/sched.h>
-#include <xen/event.h>
 #include <xen/irq.h>
 #include <xen/iocap.h>
 #include <xen/compat.h>
 #include <xen/guest_access.h>
 #include <xen/keyhandler.h>
-#include <xen/event_fifo.h>
 #include <asm/current.h>
 
 #include <public/xen.h>
xen/common/event_channel.h new file +63 -0

63 lines not on this page · raw message

xen/common/event_fifo.c +2 -2
@@ -7,12 +7,12 @@
  * Version 2 or later.  See the file COPYING for more details.
  */
 
+#include "event_channel.h"
+
 #include <xen/init.h>
 #include <xen/lib.h>
 #include <xen/errno.h>
 #include <xen/sched.h>
-#include <xen/event.h>
-#include <xen/event_fifo.h>
 #include <xen/paging.h>
 #include <xen/mm.h>
 #include <xen/domain_page.h>
xen/include/xen/event.h +0 -39
@@ -105,12 +105,6 @@ void notify_via_xen_event_channel(struct
 #define bucket_from_port(d, p) \
     ((group_from_port(d, p))[((p) % EVTCHNS_PER_GROUP) / EVTCHNS_PER_BUCKET])
 
-static inline unsigned int max_evtchns(const struct domain *d)
-{
-    return d->evtchn_fifo ? EVTCHN_FIFO_NR_CHANNELS
-                          : BITS_PER_EVTCHN_WORD(d) * BITS_PER_EVTCHN_WORD(d);
-}
-
 static inline bool_t port_is_valid(struct domain *d, unsigned int p)
 {
     if ( p >= read_atomic(&d->valid_evtchns) )
@@ -176,8 +170,6 @@ static bool evtchn_usable(const struct e
 
 void evtchn_check_pollers(struct domain *d, unsigned int port);
 
-void evtchn_2l_init(struct domain *d);
-
 /* Close all event channels and reset to 2-level ABI. */
 int evtchn_reset(struct domain *d, bool resuming);
 
@@ -227,13 +219,6 @@ static inline void evtchn_port_clear_pen
         d->evtchn_port_ops->clear_pending(d, evtchn);
 }
 
-static inline void evtchn_port_unmask(struct domain *d,
-                                      struct evtchn *evtchn)
-{
-    if ( evtchn_usable(evtchn) )
-        d->evtchn_port_ops->unmask(d, evtchn);
-}
-
 static inline bool evtchn_is_pending(const struct domain *d,
                                      const struct evtchn *evtchn)
 {
@@ -259,13 +244,6 @@ static inline bool evtchn_port_is_masked
     return rc;
 }
 
-static inline bool evtchn_is_busy(const struct domain *d,
-                                  const struct evtchn *evtchn)
-{
-    return d->evtchn_port_ops->is_busy &&
-           d->evtchn_port_ops->is_busy(d, evtchn);
-}
-
 /* Returns negative errno, zero for not pending, or positive for pending. */
 static inline int evtchn_port_poll(struct domain *d, evtchn_port_t port)
 {
@@ -285,21 +263,4 @@ static inline int evtchn_port_poll(struc
     return rc;
 }
 
-static inline int evtchn_port_set_priority(struct domain *d,
-                                           struct evtchn *evtchn,
-                                           unsigned int priority)
-{
-    if ( !d->evtchn_port_ops->set_priority )
-        return -ENOSYS;
-    if ( !evtchn_usable(evtchn) )
-        return -EACCES;
-    return d->evtchn_port_ops->set_priority(d, evtchn, priority);
-}
-
-static inline void evtchn_port_print_state(struct domain *d,
-                                           const struct evtchn *evtchn)
-{
-    d->evtchn_port_ops->print_state(d, evtchn);
-}
-
 #endif /* __XEN_EVENT_H__ */
xen/include/xen/event_fifo.h deleted +0 -26
@@ -1,26 +0,0 @@
-/*
- * FIFO-based event channel ABI.
- *
- * Copyright (C) 2013 Citrix Systems R&D Ltd.
- *
- * This source code is licensed under the GNU General Public License,
- * Version 2 or later.  See the file COPYING for more details.
- */
-#ifndef __XEN_EVENT_FIFO_H__
-#define __XEN_EVENT_FIFO_H__
-
-int evtchn_fifo_init_control(struct evtchn_init_control *init_control);
-int evtchn_fifo_expand_array(const struct evtchn_expand_array *expand_array);
-void evtchn_fifo_destroy(struct domain *domain);
-
-#endif /* __XEN_EVENT_FIFO_H__ */
-
-/*
- * Local variables:
- * mode: C
- * c-file-style: "BSD"
- * c-basic-offset: 4
- * tab-width: 4
- * indent-tabs-mode: nil
- * End:
- */
Reviewed-by on v2 2/8
#12 lore raw

Re: [PATCH v2 2/8] evtchn: replace FIFO-specific header by generic private one

Quoted 5 lines · Jan Beulich

Having a FIFO specific header is not (or at least no longer) warranted with just three function declarations left there. Introduce a private header instead, moving there some further items from xen/event.h.

Quoted 48 lines

---
v2: New.
---
TBD: If - considering the layering violation that's there anyway - we
allowed PV shim code to make use of this header, a few more items could be moved out of "public sight".

xen/common/event_2l.c +2 -1
@@ -7,11 +7,12 @@
  * Version 2 or later.  See the file COPYING for more details.
  */
 
+#include "event_channel.h"
+
 #include <xen/init.h>
 #include <xen/lib.h>
 #include <xen/errno.h>
 #include <xen/sched.h>
-#include <xen/event.h>
 
 #include <asm/guest_atomics.h>
 
xen/common/event_channel.c +2 -2
@@ -14,17 +14,17 @@
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "event_channel.h"
+
 #include <xen/init.h>
 #include <xen/lib.h>
 #include <xen/errno.h>
 #include <xen/sched.h>
-#include <xen/event.h>
 #include <xen/irq.h>
 #include <xen/iocap.h>
 #include <xen/compat.h>
 #include <xen/guest_access.h>
 #include <xen/keyhandler.h>
-#include <xen/event_fifo.h>
 #include <asm/current.h>
 
 #include <public/xen.h>

xen/common/event_channel.h line 1

Quoted 1 line
xen/common/event_channel.h new file +1 -0
@@ -0,0 +1,63 @@
+/* Event channel handling private header. */

I've got no idea whether it matters or not, but the code moved here from xen/events.h had an explicit copyright banner with Keirs name, should this be kept?

Thanks, Roger.

Acked-by on v2 2/8
#13 lore raw

Re: [PATCH v2 2/8] evtchn: replace FIFO-specific header by generic private one

In reply to Jan Beulich

Hi Jan,

Quoted 5 lines · Jan Beulich

Having a FIFO specific header is not (or at least no longer) warranted with just three function declarations left there. Introduce a private header instead, moving there some further items from xen/event.h.

Quoted 6 lines

---
v2: New.
---
TBD: If - considering the layering violation that's there anyway - we
allowed PV shim code to make use of this header, a few more items could be moved out of "public sight".

Are you referring to PV shim calling function such as evtchn_bind_vcpu()?

Cheers,

Signature · 1 line
Julien Grall
#14 lore raw

Re: [PATCH v2 2/8] evtchn: replace FIFO-specific header by generic private one

Quoted 8 lines · Julien Grall
Quoted 5 lines · Jan Beulich

Having a FIFO specific header is not (or at least no longer) warranted with just three function declarations left there. Introduce a private header instead, moving there some further items from xen/event.h.

Thanks; perhaps you didn't notice this went in already?

Quoted 8 lines
Quoted 6 lines

---
v2: New.
---
TBD: If - considering the layering violation that's there anyway - we
allowed PV shim code to make use of this header, a few more items could be moved out of "public sight".

Are you referring to PV shim calling function such as evtchn_bind_vcpu()?

Yes.

Jan

#15 lore raw

Re: [PATCH v2 2/8] evtchn: replace FIFO-specific header by generic private one

Quoted 11 lines · Jan Beulich
Quoted 8 lines · Julien Grall
Quoted 5 lines · Jan Beulich

Having a FIFO specific header is not (or at least no longer) warranted with just three function declarations left there. Introduce a private header instead, moving there some further items from xen/event.h.

Thanks; perhaps you didn't notice this went in already?

I only noticed it afterwards. I find useful when committers send a quick message mentioning that part of the series has been committed.

Cheers,

Signature · 1 line
Julien Grall
#16 lore raw

PATCH v2 3/8 evtchn: rename and adjust guest_enabled_event()

In reply to Jan Beulich

The function isn't about an "event" in general, but about a vIRQ. The function also failed to honor global vIRQ-s, instead assuming the caller would pass vCPU 0 in such a case.

While at it also adjust the
- types the function uses,
- single user to make use of domain_vcpu().

---
v2: New.

xen/arch/x86/cpu/mcheck/vmce.h +3 -3
@@ -5,9 +5,9 @@
 
 int vmce_init(struct cpuinfo_x86 *c);
 
-#define dom0_vmce_enabled() (hardware_domain && hardware_domain->max_vcpus \
-        && hardware_domain->vcpu[0] \
-        && guest_enabled_event(hardware_domain->vcpu[0], VIRQ_MCA))
+#define dom0_vmce_enabled() \
+    (hardware_domain && \
+     evtchn_virq_enabled(domain_vcpu(hardware_domain, 0), VIRQ_MCA))
 
 int unmmap_broken_page(struct domain *d, mfn_t mfn, unsigned long gfn);
 
xen/common/event_channel.c +8 -2
@@ -778,9 +778,15 @@ out:
     return ret;
 }
 
-int guest_enabled_event(struct vcpu *v, uint32_t virq)
+bool evtchn_virq_enabled(const struct vcpu *v, unsigned int virq)
 {
-    return ((v != NULL) && (v->virq_to_evtchn[virq] != 0));
+    if ( !v )
+        return false;
+
+    if ( virq_is_global(virq) && v->vcpu_id )
+        v = domain_vcpu(v->domain, 0);
+
+    return v->virq_to_evtchn[virq];
 }
 
 void send_guest_vcpu_virq(struct vcpu *v, uint32_t virq)
xen/include/xen/event.h +2 -2
@@ -85,8 +85,8 @@ int alloc_unbound_xen_event_channel(
     xen_event_channel_notification_t notification_fn);
 void free_xen_event_channel(struct domain *d, int port);
 
-/* Query if event channel is in use by the guest */
-int guest_enabled_event(struct vcpu *v, uint32_t virq);
+/* Query whether a vIRQ is in use by the guest. */
+bool evtchn_virq_enabled(const struct vcpu *v, unsigned int virq);
 
 /* Notify remote end of a Xen-attached event channel.*/
 void notify_via_xen_event_channel(struct domain *ld, int lport);
Reviewed-by on v2 3/8
#17 lore raw

Re: [PATCH v2 3/8] evtchn: rename and adjust guest_enabled_event()

Quoted 9 lines · Jan Beulich

The function isn't about an "event" in general, but about a vIRQ. The function also failed to honor global vIRQ-s, instead assuming the caller would pass vCPU 0 in such a case.

While at it also adjust the
- types the function uses,
- single user to make use of domain_vcpu().

Quoted 21 lines

---
v2: New.

xen/arch/x86/cpu/mcheck/vmce.h +3 -3
@@ -5,9 +5,9 @@
 
 int vmce_init(struct cpuinfo_x86 *c);
 
-#define dom0_vmce_enabled() (hardware_domain && hardware_domain->max_vcpus \
-        && hardware_domain->vcpu[0] \
-        && guest_enabled_event(hardware_domain->vcpu[0], VIRQ_MCA))
+#define dom0_vmce_enabled() \
+    (hardware_domain && \
+     evtchn_virq_enabled(domain_vcpu(hardware_domain, 0), VIRQ_MCA))
 
 int unmmap_broken_page(struct domain *d, mfn_t mfn, unsigned long gfn);
 

xen/common/event_channel.c out: · line 783

Quoted 8 lines
xen/common/event_channel.c +2 -2
@@ -778,9 +778,15 @@ out:
     return ret;
 }
 
-int guest_enabled_event(struct vcpu *v, uint32_t virq)
+bool evtchn_virq_enabled(const struct vcpu *v, unsigned int virq)
 {
-    return ((v != NULL) && (v->virq_to_evtchn[virq] != 0));
+    if ( !v )

Not sure it's worth adding a check that virq < NR_VIRQS here just to be on the safe side...

xen/common/event_channel.c out: · line 786

Quoted 3 lines
xen/common/event_channel.c +3 -0
+        return false;
+
+    if ( virq_is_global(virq) && v->vcpu_id )

...as virq_is_global has an ASSERT to that extend (but that would be a nop on release builds).

Thanks, Roger.

#18 lore raw

PATCH v2 4/8 evtchn: let evtchn_set_priority() acquire the per-channel lock

In reply to Jan Beulich

Some lock wants to be held to make sure the port doesn't change state, but there's no point holding the per-domain event lock here. Switch to using the finer grained per-channel lock instead.

FAOD this doesn't guarantee anything towards in particular evtchn_fifo_set_pending(), as for interdomain channels that function would be called with the remote side's per-channel lock held.

---
v2: Drop acquiring of event lock. Re-write title and description.

xen/common/event_channel.c +6 -9
@@ -1154,20 +1154,17 @@ static long evtchn_set_priority(const st
 {
     struct domain *d = current->domain;
     unsigned int port = set_priority->port;
+    struct evtchn *chn;
     long ret;
-
-    spin_lock(&d->event_lock);
+    unsigned long flags;
 
     if ( !port_is_valid(d, port) )
-    {
-        spin_unlock(&d->event_lock);
         return -EINVAL;
-    }
-
-    ret = evtchn_port_set_priority(d, evtchn_from_port(d, port),
-                                   set_priority->priority);
 
-    spin_unlock(&d->event_lock);
+    chn = evtchn_from_port(d, port);
+    spin_lock_irqsave(&chn->lock, flags);
+    ret = evtchn_port_set_priority(d, chn, set_priority->priority);
+    spin_unlock_irqrestore(&chn->lock, flags);
 
     return ret;
 }
Reviewed-by on v2 4/8
#19 lore raw

Re: [PATCH v2 4/8] evtchn: let evtchn_set_priority() acquire the per-channel lock

Quoted 3 lines · Jan Beulich

Some lock wants to be held to make sure the port doesn't change state, but there's no point holding the per-domain event lock here. Switch to using the finer grained per-channel lock instead.

While true that's a fine grained lock, it also disables interrupts, which the global event_lock didn't.

Quoted 5 lines

FAOD this doesn't guarantee anything towards in particular evtchn_fifo_set_pending(), as for interdomain channels that function would be called with the remote side's per-channel lock held.

Thanks, Roger.

#20 lore raw

Re: [PATCH v2 4/8] evtchn: let evtchn_set_priority() acquire the per-channel lock

Quoted 7 lines · Roger Pau Monné
Quoted 3 lines · Jan Beulich

Some lock wants to be held to make sure the port doesn't change state, but there's no point holding the per-domain event lock here. Switch to using the finer grained per-channel lock instead.

While true that's a fine grained lock, it also disables interrupts, which the global event_lock didn't.

True, yet we're aiming at dropping this aspect again. Hence I've added "(albeit as a downside for the time being this requires disabling interrupts for a short period of time)".

Quoted 7 lines
Quoted 5 lines

FAOD this doesn't guarantee anything towards in particular evtchn_fifo_set_pending(), as for interdomain channels that function would be called with the remote side's per-channel lock held.

Thanks.

Jan

#21 lore raw

PATCH v2 5/8 evtchn: drop acquiring of per-channel lock from send_guest_{global,vcpu}_virq()

In reply to Jan Beulich

The per-vCPU virq_lock, which is being held anyway, together with there not being any call to evtchn_port_set_pending() when v->virq_to_evtchn[] is zero, provide sufficient guarantees. Undo the lock addition done for XSA-343 (commit e045199c7c9c "evtchn: address races with evtchn_reset()"). Update the description next to struct evtchn_port_ops accordingly.

---
v2: New.

xen/common/event_channel.c +1 -7
@@ -794,7 +794,6 @@ void send_guest_vcpu_virq(struct vcpu *v
     unsigned long flags;
     int port;
     struct domain *d;
-    struct evtchn *chn;
 
     ASSERT(!virq_is_global(virq));
 
@@ -805,10 +804,7 @@ void send_guest_vcpu_virq(struct vcpu *v
         goto out;
 
     d = v->domain;
-    chn = evtchn_from_port(d, port);
-    spin_lock(&chn->lock);
-    evtchn_port_set_pending(d, v->vcpu_id, chn);
-    spin_unlock(&chn->lock);
+    evtchn_port_set_pending(d, v->vcpu_id, evtchn_from_port(d, port));
 
  out:
     spin_unlock_irqrestore(&v->virq_lock, flags);
@@ -837,9 +833,7 @@ void send_guest_global_virq(struct domai
         goto out;
 
     chn = evtchn_from_port(d, port);
-    spin_lock(&chn->lock);
     evtchn_port_set_pending(d, chn->notify_vcpu_id, chn);
-    spin_unlock(&chn->lock);
 
  out:
     spin_unlock_irqrestore(&v->virq_lock, flags);
xen/include/xen/event.h +10 -3
@@ -177,9 +177,16 @@ int evtchn_reset(struct domain *d, bool
  * Low-level event channel port ops.
  *
  * All hooks have to be called with a lock held which prevents the channel
- * from changing state. This may be the domain event lock, the per-channel
- * lock, or in the case of sending interdomain events also the other side's
- * per-channel lock. Exceptions apply in certain cases for the PV shim.
+ * from changing state. This may be
+ * - the domain event lock,
+ * - the per-channel lock,
+ * - in the case of sending interdomain events the other side's per-channel
+ *   lock,
+ * - in the case of sending non-global vIRQ-s the per-vCPU virq_lock (in
+ *   combination with the ordering enforced through how the vCPU's
+ *   virq_to_evtchn[] gets updated),
+ * - in the case of sending global vIRQ-s vCPU 0's virq_lock.
+ * Exceptions apply in certain cases for the PV shim.
  */
 struct evtchn_port_ops {
     void (*init)(struct domain *d, struct evtchn *evtchn);
#22 lore raw

Re: [PATCH v2 5/8] evtchn: drop acquiring of per-channel lock from send_guest_{global,vcpu}_virq()

Quoted 3 lines · Jan Beulich

The per-vCPU virq_lock, which is being held anyway, together with there not being any call to evtchn_port_set_pending() when v->virq_to_evtchn[] is zero, provide sufficient guarantees.

This is also fine because closing the event channel will be done with the VIRQ hold held also AFAICT.

Quoted 50 lines

Undo the lock addition done for
XSA-343 (commit e045199c7c9c "evtchn: address races with evtchn_reset()"). Update the description next to struct evtchn_port_ops accordingly.

---
v2: New.

xen/common/event_channel.c +1 -7
@@ -794,7 +794,6 @@ void send_guest_vcpu_virq(struct vcpu *v
     unsigned long flags;
     int port;
     struct domain *d;
-    struct evtchn *chn;
 
     ASSERT(!virq_is_global(virq));
 
@@ -805,10 +804,7 @@ void send_guest_vcpu_virq(struct vcpu *v
         goto out;
 
     d = v->domain;
-    chn = evtchn_from_port(d, port);
-    spin_lock(&chn->lock);
-    evtchn_port_set_pending(d, v->vcpu_id, chn);
-    spin_unlock(&chn->lock);
+    evtchn_port_set_pending(d, v->vcpu_id, evtchn_from_port(d, port));
 
  out:
     spin_unlock_irqrestore(&v->virq_lock, flags);
@@ -837,9 +833,7 @@ void send_guest_global_virq(struct domai
         goto out;
 
     chn = evtchn_from_port(d, port);
-    spin_lock(&chn->lock);
     evtchn_port_set_pending(d, chn->notify_vcpu_id, chn);
-    spin_unlock(&chn->lock);
 
  out:
     spin_unlock_irqrestore(&v->virq_lock, flags);
xen/include/xen/event.h +0 -3
@@ -177,9 +177,16 @@ int evtchn_reset(struct domain *d, bool
  * Low-level event channel port ops.
  *
  * All hooks have to be called with a lock held which prevents the channel
- * from changing state. This may be the domain event lock, the per-channel
- * lock, or in the case of sending interdomain events also the other side's
- * per-channel lock. Exceptions apply in certain cases for the PV shim.

xen/include/xen/event.h int evtchn_reset(struct domain *d, bool · line 189

Quoted 10 lines
xen/include/xen/event.h +10 -0
+ * from changing state. This may be
+ * - the domain event lock,
+ * - the per-channel lock,
+ * - in the case of sending interdomain events the other side's per-channel
+ *   lock,
+ * - in the case of sending non-global vIRQ-s the per-vCPU virq_lock (in
+ *   combination with the ordering enforced through how the vCPU's
+ *   virq_to_evtchn[] gets updated),
+ * - in the case of sending global vIRQ-s vCPU 0's virq_lock.
+ * Exceptions apply in certain cases for the PV shim.

Having such a wide locking discipline looks dangerous to me, it's easy to get things wrong without notice IMO.

Maybe we could add an assert to that effect in evtchn_port_set_pending in order to make sure callers follow the discipline?

Roger.

#23 lore raw

Re: [PATCH v2 5/8] evtchn: drop acquiring of per-channel lock from send_guest_{global,vcpu}_virq()

Quoted 7 lines · Roger Pau Monné
Quoted 3 lines · Jan Beulich

The per-vCPU virq_lock, which is being held anyway, together with there not being any call to evtchn_port_set_pending() when v->virq_to_evtchn[] is zero, provide sufficient guarantees.

This is also fine because closing the event channel will be done with the VIRQ hold held also AFAICT.

Right, I'm not going into these details (or else binding would also need mentioning) here, as the code comment update should sufficiently cover it. Hence just saying "sufficient guarantees".

Quoted 14 lines
Quoted 14 lines
xen/include/xen/event.h +5 -3
@@ -177,9 +177,16 @@ int evtchn_reset(struct domain *d, bool
  * Low-level event channel port ops.
  *
  * All hooks have to be called with a lock held which prevents the channel
- * from changing state. This may be the domain event lock, the per-channel
- * lock, or in the case of sending interdomain events also the other side's
- * per-channel lock. Exceptions apply in certain cases for the PV shim.
+ * from changing state. This may be
+ * - the domain event lock,
+ * - the per-channel lock,
+ * - in the case of sending interdomain events the other side's per-channel
+ *   lock,
Quoted 8 lines

xen/include/xen/event.h int evtchn_reset(struct domain *d, bool · line 189

Quoted 5 lines
xen/include/xen/event.h +5 -0
+ * - in the case of sending non-global vIRQ-s the per-vCPU virq_lock (in
+ *   combination with the ordering enforced through how the vCPU's
+ *   virq_to_evtchn[] gets updated),
+ * - in the case of sending global vIRQ-s vCPU 0's virq_lock.
+ * Exceptions apply in certain cases for the PV shim.

Having such a wide locking discipline looks dangerous to me, it's easy to get things wrong without notice IMO.

It is effectively only describing how things are (or were before XSA-343, getting restored here).

Quoted 3 lines

Maybe we could add an assert to that effect in evtchn_port_set_pending in order to make sure callers follow the discipline?

Would be nice, but (a) see the last sentence of the comment still in context above and (b) it shouldn't be just set_pending(). The comment starts with "All hooks ..." after all.

Jan

#24 lore raw

Re: [PATCH v2 5/8] evtchn: drop acquiring of per-channel lock from send_guest_{global,vcpu}_virq()

Hi Jan,

Quoted 36 lines · Jan Beulich
Quoted 7 lines · Roger Pau Monné
Quoted 3 lines · Jan Beulich

The per-vCPU virq_lock, which is being held anyway, together with there not being any call to evtchn_port_set_pending() when v->virq_to_evtchn[] is zero, provide sufficient guarantees.

This is also fine because closing the event channel will be done with the VIRQ hold held also AFAICT.

Right, I'm not going into these details (or else binding would also need mentioning) here, as the code comment update should sufficiently cover it. Hence just saying "sufficient guarantees".

Quoted 14 lines
Quoted 14 lines
xen/include/xen/event.h +5 -3
@@ -177,9 +177,16 @@ int evtchn_reset(struct domain *d, bool
   * Low-level event channel port ops.
   *
   * All hooks have to be called with a lock held which prevents the channel
- * from changing state. This may be the domain event lock, the per-channel
- * lock, or in the case of sending interdomain events also the other side's
- * per-channel lock. Exceptions apply in certain cases for the PV shim.
+ * from changing state. This may be
+ * - the domain event lock,
+ * - the per-channel lock,
+ * - in the case of sending interdomain events the other side's per-channel
+ *   lock,
Quoted 8 lines

xen/include/xen/event.h int evtchn_reset(struct domain *d, bool · line 189

Quoted 5 lines
xen/include/xen/event.h +5 -0
+ * - in the case of sending non-global vIRQ-s the per-vCPU virq_lock (in
+ *   combination with the ordering enforced through how the vCPU's
+ *   virq_to_evtchn[] gets updated),
+ * - in the case of sending global vIRQ-s vCPU 0's virq_lock.
+ * Exceptions apply in certain cases for the PV shim.

Having such a wide locking discipline looks dangerous to me, it's easy to get things wrong without notice IMO.

Quoted 2 lines

It is effectively only describing how things are (or were before XSA-343, getting restored here).

I agree with Roger here, the new/old locking discipline is dangerous and it is only a matter of time before it will bite us again.

I think we should consider Juergen's series because the locking for the event channel is easier to understand.

With his series in place, this patch will become unecessary.

Cheers,

Signature · 1 line
Julien Grall
#25 lore raw

Re: [PATCH v2 5/8] evtchn: drop acquiring of per-channel lock from send_guest_{global,vcpu}_virq()

Quoted 26 lines · Julien Grall
Quoted 25 lines · Jan Beulich
Quoted 23 lines · Roger Pau Monné
Quoted 9 lines · Jan Beulich
xen/include/xen/event.h +0 -3
@@ -177,9 +177,16 @@ int evtchn_reset(struct domain *d, bool
   * Low-level event channel port ops.
   *
   * All hooks have to be called with a lock held which prevents the channel
- * from changing state. This may be the domain event lock, the per-channel
- * lock, or in the case of sending interdomain events also the other side's
- * per-channel lock. Exceptions apply in certain cases for the PV shim.

xen/include/xen/event.h int evtchn_reset(struct domain *d, bool · line 189

Quoted 10 lines
xen/include/xen/event.h +10 -0
+ * from changing state. This may be
+ * - the domain event lock,
+ * - the per-channel lock,
+ * - in the case of sending interdomain events the other side's per-channel
+ *   lock,
+ * - in the case of sending non-global vIRQ-s the per-vCPU virq_lock (in
+ *   combination with the ordering enforced through how the vCPU's
+ *   virq_to_evtchn[] gets updated),
+ * - in the case of sending global vIRQ-s vCPU 0's virq_lock.
+ * Exceptions apply in certain cases for the PV shim.

Having such a wide locking discipline looks dangerous to me, it's easy to get things wrong without notice IMO.

Quoted 8 lines
Quoted 2 lines

It is effectively only describing how things are (or were before XSA-343, getting restored here).

I agree with Roger here, the new/old locking discipline is dangerous and it is only a matter of time before it will bite us again.

I think we should consider Juergen's series because the locking for the event channel is easier to understand.

We should, yes. The one thing I'm a little uneasy with is the new lock "variant" that gets introduced. Custom locking methods also are a common source of problems (which isn't to say I see any here).

Quoted 1 line

With his series in place, this patch will become unecessary.

It'll become less important, but not pointless - any unnecessary locking would better be removed imo.

I'd also like to note that the non-straightforward locking rules wouldn't really change with his series; the benefit there really is the dropping of the need for IRQ-safe locking.

Jan

#26 lore raw

Re: [PATCH v2 5/8] evtchn: drop acquiring of per-channel lock from send_guest_{global,vcpu}_virq()

Quoted 33 lines · Jan Beulich
Quoted 32 lines · Julien Grall
Quoted 25 lines · Jan Beulich
Quoted 15 lines · Roger Pau Monné
Quoted 14 lines · Jan Beulich
xen/include/xen/event.h +5 -3
@@ -177,9 +177,16 @@ int evtchn_reset(struct domain *d, bool
    * Low-level event channel port ops.
    *
    * All hooks have to be called with a lock held which prevents the channel
- * from changing state. This may be the domain event lock, the per-channel
- * lock, or in the case of sending interdomain events also the other side's
- * per-channel lock. Exceptions apply in certain cases for the PV shim.
+ * from changing state. This may be
+ * - the domain event lock,
+ * - the per-channel lock,
+ * - in the case of sending interdomain events the other side's per-channel
+ *   lock,
Quoted 8 lines

xen/include/xen/event.h int evtchn_reset(struct domain *d, bool · line 189

Quoted 5 lines
xen/include/xen/event.h +5 -0
+ * - in the case of sending non-global vIRQ-s the per-vCPU virq_lock (in
+ *   combination with the ordering enforced through how the vCPU's
+ *   virq_to_evtchn[] gets updated),
+ * - in the case of sending global vIRQ-s vCPU 0's virq_lock.
+ * Exceptions apply in certain cases for the PV shim.

Having such a wide locking discipline looks dangerous to me, it's easy to get things wrong without notice IMO.

Quoted 2 lines

It is effectively only describing how things are (or were before XSA-343, getting restored here).

I agree with Roger here, the new/old locking discipline is dangerous and it is only a matter of time before it will bite us again.

Quoted 7 lines
Quoted 2 lines

I think we should consider Juergen's series because the locking for the event channel is easier to understand.

We should, yes. The one thing I'm a little uneasy with is the new lock "variant" that gets introduced. Custom locking methods also are a common source of problems (which isn't to say I see any here).

I am also unease with a new lock "variant". However, this is the best proposal I have seen so far to unblock the issue.

I am open to other suggestion with simple locking discipline.

Quoted 5 lines
Quoted 2 lines

With his series in place, this patch will become unecessary.

It'll become less important, but not pointless - any unnecessary locking would better be removed imo.

They may be unnecessary today but if tomorrow someone decide to rework the other lock, then you are just re-opening a security hole.

IHMO, having a sane locking system is far more important than removing locking that look "unnecessary".

Quoted 4 lines

I'd also like to note that the non-straightforward locking rules wouldn't really change with his series; the benefit there really is the dropping of the need for IRQ-safe locking.

Well, it is at least going towards that...

Cheers,

Quoted 3 lines

Jan

Signature · 1 line
Julien Grall
#27 lore raw

Re: [PATCH v2 5/8] evtchn: drop acquiring of per-channel lock from send_guest_{global,vcpu}_virq()

Quoted 44 lines · Julien Grall
Quoted 41 lines · Jan Beulich
Quoted 39 lines · Julien Grall
Quoted 30 lines · Jan Beulich
Quoted 20 lines · Roger Pau Monné
Quoted 19 lines · Jan Beulich
xen/include/xen/event.h +0 -1
@@ -177,9 +177,16 @@ int evtchn_reset(struct domain *d, bool
     * Low-level event channel port ops.
     *
     * All hooks have to be called with a lock held which prevents the channel
- * from changing state. This may be the domain event lock, the 

per-channel
- * lock, or in the case of sending interdomain events also the other side's
- * per-channel lock. Exceptions apply in certain cases for the PV shim.
+ * from changing state. This may be
+ * - the domain event lock,
+ * - the per-channel lock,
+ * - in the case of sending interdomain events the other side's per-channel
+ *   lock,
+ * - in the case of sending non-global vIRQ-s the per-vCPU

Quoted 8 lines
Quoted 5 lines

virq_lock (in
+ *   combination with the ordering enforced through how the vCPU's
+ *   virq_to_evtchn[] gets updated),
+ * - in the case of sending global vIRQ-s vCPU 0's virq_lock.
+ * Exceptions apply in certain cases for the PV shim.

Having such a wide locking discipline looks dangerous to me, it's easy to get things wrong without notice IMO.

Quoted 2 lines

It is effectively only describing how things are (or were before XSA-343, getting restored here).

I agree with Roger here, the new/old locking discipline is dangerous and it is only a matter of time before it will bite us again.

I think we should consider Juergen's series because the locking for the event channel is easier to understand.

Quoted 8 lines
Quoted 4 lines

We should, yes. The one thing I'm a little uneasy with is the new lock "variant" that gets introduced. Custom locking methods also are a common source of problems (which isn't to say I see any here).

I am also unease with a new lock "variant". However, this is the best proposal I have seen so far to unblock the issue.

I am open to other suggestion with simple locking discipline.

In theory my new lock variant could easily be replaced by a rwlock and using the try-variant for the readers only. The disadvantage of that approach would be a growth of struct evtchn.

Juergen

#28 lore raw

Re: [PATCH v2 5/8] evtchn: drop acquiring of per-channel lock from send_guest_{global,vcpu}_virq()

Quoted 51 lines · Jürgen Groß
Quoted 50 lines · Julien Grall
Quoted 46 lines · Jan Beulich
Quoted 32 lines · Julien Grall
Quoted 31 lines · Jan Beulich
Quoted 29 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
xen/include/xen/event.h +0 -0
@@ -177,9 +177,16 @@ int evtchn_reset(struct domain *d, bool
     * Low-level event channel port ops.
     *
     * All hooks have to be called with a lock held which prevents 

the channel
- * from changing state. This may be the domain event lock, the per-channel
- * lock, or in the case of sending interdomain events also the other side's
- * per-channel lock. Exceptions apply in certain cases for the PV shim.
+ * from changing state. This may be
+ * - the domain event lock,
+ * - the per-channel lock,
+ * - in the case of sending interdomain events the other side's

Quoted 8 lines

per-channel
+ *   lock,
+ * - in the case of sending non-global vIRQ-s the per-vCPU virq_lock (in
+ *   combination with the ordering enforced through how the vCPU's
+ *   virq_to_evtchn[] gets updated),
+ * - in the case of sending global vIRQ-s vCPU 0's virq_lock.
+ * Exceptions apply in certain cases for the PV shim.

Having such a wide locking discipline looks dangerous to me, it's easy to get things wrong without notice IMO.

Quoted 8 lines
Quoted 2 lines

It is effectively only describing how things are (or were before XSA-343, getting restored here).

I agree with Roger here, the new/old locking discipline is dangerous and it is only a matter of time before it will bite us again.

I think we should consider Juergen's series because the locking for the event channel is easier to understand.

We should, yes. The one thing I'm a little uneasy with is the new lock "variant" that gets introduced. Custom locking methods also are a common source of problems (which isn't to say I see any here).

Quoted 7 lines
Quoted 4 lines

I am also unease with a new lock "variant". However, this is the best proposal I have seen so far to unblock the issue.

I am open to other suggestion with simple locking discipline.

In theory my new lock variant could easily be replaced by a rwlock and using the try-variant for the readers only.

Well, only until we would add check_lock() there, which I think we should really have (not just on the slow paths, thanks to the use of spin_lock() there). The read-vs-write properties you're utilizing aren't applicable in the general case afaict, and hence such checking would get in the way.

Quoted 1 line

The disadvantage of that approach would be a growth of struct evtchn.

Wasn't it you who had pointed out to me the aligned(64) attribute on the struct (in a different context), which afaict would subsume any possible growth?

Jan

#29 lore raw

Re: [PATCH v2 5/8] evtchn: drop acquiring of per-channel lock from send_guest_{global,vcpu}_virq()

Quoted 57 lines · Jan Beulich
Quoted 56 lines · Jürgen Groß
Quoted 54 lines · Julien Grall
Quoted 39 lines · Jan Beulich
Quoted 38 lines · Julien Grall
Quoted 31 lines · Jan Beulich
Quoted 21 lines · Roger Pau Monné
Quoted 20 lines · Jan Beulich
xen/include/xen/event.h +0 -0
@@ -177,9 +177,16 @@ int evtchn_reset(struct domain *d, bool
     * Low-level event channel port ops.
     *
     * All hooks have to be called with a lock held which prevents

the channel
- * from changing state. This may be the domain event lock, the per-channel
- * lock, or in the case of sending interdomain events also the other side's
- * per-channel lock. Exceptions apply in certain cases for the PV shim.
+ * from changing state. This may be
+ * - the domain event lock,
+ * - the per-channel lock,
+ * - in the case of sending interdomain events the other side's per-channel
+ *   lock,
+ * - in the case of sending non-global vIRQ-s the per-vCPU

Quoted 8 lines
Quoted 5 lines

virq_lock (in
+ *   combination with the ordering enforced through how the vCPU's
+ *   virq_to_evtchn[] gets updated),
+ * - in the case of sending global vIRQ-s vCPU 0's virq_lock.
+ * Exceptions apply in certain cases for the PV shim.

Having such a wide locking discipline looks dangerous to me, it's easy to get things wrong without notice IMO.

Quoted 2 lines

It is effectively only describing how things are (or were before XSA-343, getting restored here).

I agree with Roger here, the new/old locking discipline is dangerous and it is only a matter of time before it will bite us again.

Quoted 7 lines
Quoted 2 lines

I think we should consider Juergen's series because the locking for the event channel is easier to understand.

We should, yes. The one thing I'm a little uneasy with is the new lock "variant" that gets introduced. Custom locking methods also are a common source of problems (which isn't to say I see any here).

I am also unease with a new lock "variant". However, this is the best proposal I have seen so far to unblock the issue.

I am open to other suggestion with simple locking discipline.

Quoted 8 lines
Quoted 2 lines

In theory my new lock variant could easily be replaced by a rwlock and using the try-variant for the readers only.

Well, only until we would add check_lock() there, which I think we should really have (not just on the slow paths, thanks to the use of spin_lock() there). The read-vs-write properties you're utilizing aren't applicable in the general case afaict, and hence such checking would get in the way.

No, I don't think so.

As long as there is no read_lock() user with interrupts off we should be fine. read_trylock() is no problem as it can't block.

Quoted 6 lines
Quoted 2 lines

The disadvantage of that approach would be a growth of struct evtchn.

Wasn't it you who had pointed out to me the aligned(64) attribute on the struct (in a different context), which afaict would subsume any possible growth?

Oh, indeed.

The growth would be 8 bytes, leading to a max of 56 bytes then.

Juergen

#30 lore raw

Re: [PATCH v2 5/8] evtchn: drop acquiring of per-channel lock from send_guest_{global,vcpu}_virq()

Quoted 27 lines · Jürgen Groß
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Jürgen Groß
Quoted 14 lines · Julien Grall
Quoted 8 lines · Jan Beulich
Quoted 2 lines · Julien Grall

I think we should consider Juergen's series because the locking for the event channel is easier to understand.

We should, yes. The one thing I'm a little uneasy with is the new lock "variant" that gets introduced. Custom locking methods also are a common source of problems (which isn't to say I see any here).

I am also unease with a new lock "variant". However, this is the best proposal I have seen so far to unblock the issue.

I am open to other suggestion with simple locking discipline.

Quoted 8 lines
Quoted 2 lines

In theory my new lock variant could easily be replaced by a rwlock and using the try-variant for the readers only.

Well, only until we would add check_lock() there, which I think we should really have (not just on the slow paths, thanks to the use of spin_lock() there). The read-vs-write properties you're utilizing aren't applicable in the general case afaict, and hence such checking would get in the way.

Quoted 4 lines

No, I don't think so.

As long as there is no read_lock() user with interrupts off we should be fine. read_trylock() is no problem as it can't block.

How would check_lock() notice the difference? It would be all the same for read and write acquires of the lock, I think, and hence it would still get unhappy about uses from IRQ paths afaict.

Jan

#31 lore raw

Re: [PATCH v2 5/8] evtchn: drop acquiring of per-channel lock from send_guest_{global,vcpu}_virq()

Quoted 28 lines · Jan Beulich
Quoted 27 lines · Jürgen Groß
Quoted 25 lines · Jan Beulich
Quoted 11 lines · Jürgen Groß
Quoted 10 lines · Julien Grall
Quoted 8 lines · Jan Beulich
Quoted 2 lines · Julien Grall

I think we should consider Juergen's series because the locking for the event channel is easier to understand.

We should, yes. The one thing I'm a little uneasy with is the new lock "variant" that gets introduced. Custom locking methods also are a common source of problems (which isn't to say I see any here).

Quoted 7 lines
Quoted 4 lines

I am also unease with a new lock "variant". However, this is the best proposal I have seen so far to unblock the issue.

I am open to other suggestion with simple locking discipline.

In theory my new lock variant could easily be replaced by a rwlock and using the try-variant for the readers only.

Well, only until we would add check_lock() there, which I think we should really have (not just on the slow paths, thanks to the use of spin_lock() there). The read-vs-write properties you're utilizing aren't applicable in the general case afaict, and hence such checking would get in the way.

Quoted 8 lines
Quoted 4 lines

No, I don't think so.

As long as there is no read_lock() user with interrupts off we should be fine. read_trylock() is no problem as it can't block.

How would check_lock() notice the difference? It would be all the same for read and write acquires of the lock, I think, and hence it would still get unhappy about uses from IRQ paths afaict.

check_lock() isn't applicable here, at least not without modification.

I think our spinlock implementation is wrong in this regard in case a lock is entered via spin_trylock(), BTW. Using spin_trylock() with interrupts off for a lock normally taken with interrupts enabled is perfectly fine IMO.

Juergen

#32 lore raw

Re: [PATCH v2 5/8] evtchn: drop acquiring of per-channel lock from send_guest_{global,vcpu}_virq()

Quoted 38 lines · Jürgen Groß
Quoted 28 lines · Jan Beulich
Quoted 27 lines · Jürgen Groß
Quoted 25 lines · Jan Beulich
Quoted 11 lines · Jürgen Groß
Quoted 10 lines · Julien Grall
Quoted 8 lines · Jan Beulich
Quoted 2 lines · Julien Grall

I think we should consider Juergen's series because the locking for the event channel is easier to understand.

We should, yes. The one thing I'm a little uneasy with is the new lock "variant" that gets introduced. Custom locking methods also are a common source of problems (which isn't to say I see any here).

Quoted 7 lines
Quoted 4 lines

I am also unease with a new lock "variant". However, this is the best proposal I have seen so far to unblock the issue.

I am open to other suggestion with simple locking discipline.

In theory my new lock variant could easily be replaced by a rwlock and using the try-variant for the readers only.

Well, only until we would add check_lock() there, which I think we should really have (not just on the slow paths, thanks to the use of spin_lock() there). The read-vs-write properties you're utilizing aren't applicable in the general case afaict, and hence such checking would get in the way.

Quoted 8 lines
Quoted 4 lines

No, I don't think so.

As long as there is no read_lock() user with interrupts off we should be fine. read_trylock() is no problem as it can't block.

How would check_lock() notice the difference? It would be all the same for read and write acquires of the lock, I think, and hence it would still get unhappy about uses from IRQ paths afaict.

Quoted 6 lines

check_lock() isn't applicable here, at least not without modification.

I think our spinlock implementation is wrong in this regard in case a lock is entered via spin_trylock(), BTW. Using spin_trylock() with interrupts off for a lock normally taken with interrupts enabled is perfectly fine IMO.

Hmm, I think you're right, in which I case guess we ought to relax this.

Jan

#33 lore raw

Re: [PATCH v2 5/8] evtchn: drop acquiring of per-channel lock from send_guest_{global,vcpu}_virq()

Quoted 41 lines · Jan Beulich
Quoted 40 lines · Jürgen Groß
Quoted 28 lines · Jan Beulich
Quoted 27 lines · Jürgen Groß
Quoted 25 lines · Jan Beulich
Quoted 11 lines · Jürgen Groß
Quoted 10 lines · Julien Grall
Quoted 8 lines · Jan Beulich
Quoted 2 lines · Julien Grall

I think we should consider Juergen's series because the locking for the event channel is easier to understand.

We should, yes. The one thing I'm a little uneasy with is the new lock "variant" that gets introduced. Custom locking methods also are a common source of problems (which isn't to say I see any here).

Quoted 7 lines
Quoted 4 lines

I am also unease with a new lock "variant". However, this is the best proposal I have seen so far to unblock the issue.

I am open to other suggestion with simple locking discipline.

In theory my new lock variant could easily be replaced by a rwlock and using the try-variant for the readers only.

Well, only until we would add check_lock() there, which I think we should really have (not just on the slow paths, thanks to the use of spin_lock() there). The read-vs-write properties you're utilizing aren't applicable in the general case afaict, and hence such checking would get in the way.

Quoted 8 lines
Quoted 4 lines

No, I don't think so.

As long as there is no read_lock() user with interrupts off we should be fine. read_trylock() is no problem as it can't block.

How would check_lock() notice the difference? It would be all the same for read and write acquires of the lock, I think, and hence it would still get unhappy about uses from IRQ paths afaict.

check_lock() isn't applicable here, at least not without modification.

Quoted 7 lines
Quoted 4 lines

I think our spinlock implementation is wrong in this regard in case a lock is entered via spin_trylock(), BTW. Using spin_trylock() with interrupts off for a lock normally taken with interrupts enabled is perfectly fine IMO.

Hmm, I think you're right, in which I case guess we ought to relax this.

Just writing a patch. :-)

And one for adding check_lock() to rwlocks, too.

Juergen

#34 lore raw

PATCH v2 6/8 evtchn: convert vIRQ lock to an r/w one

In reply to Jan Beulich

There's no need to serialize all sending of vIRQ-s; all that's needed is serialization against the closing of the respective event channels (so far by means of a barrier). To facilitate the conversion, switch to an ordinary write locked region in evtchn_close().

---
v2: Don't introduce/use rw_barrier() here. Add comment to
evtchn_bind_virq(). Re-base.

xen/common/domain.c +1 -1
@@ -160,7 +160,7 @@ struct vcpu *vcpu_create(struct domain *
     v->vcpu_id = vcpu_id;
     v->dirty_cpu = VCPU_CPU_CLEAN;
 
-    spin_lock_init(&v->virq_lock);
+    rwlock_init(&v->virq_lock);
 
     tasklet_init(&v->continue_hypercall_tasklet, NULL, NULL);
 
xen/common/event_channel.c +15 -8
@@ -449,6 +449,13 @@ int evtchn_bind_virq(evtchn_bind_virq_t
 
     spin_unlock_irqrestore(&chn->lock, flags);
 
+    /*
+     * If by any, the update of virq_to_evtchn[] would need guarding by
+     * virq_lock, but since this is the last action here, there's no strict
+     * need to acquire the lock. Hnece holding event_lock isn't helpful
+     * anymore at this point, but utilize that its unlocking acts as the
+     * otherwise necessary smp_wmb() here.
+     */
     v->virq_to_evtchn[virq] = bind->port = port;
 
  out:
@@ -638,10 +645,10 @@ int evtchn_close(struct domain *d1, int
     case ECS_VIRQ:
         for_each_vcpu ( d1, v )
         {
-            if ( v->virq_to_evtchn[chn1->u.virq] != port1 )
-                continue;
-            v->virq_to_evtchn[chn1->u.virq] = 0;
-            spin_barrier(&v->virq_lock);
+            write_lock_irqsave(&v->virq_lock, flags);
+            if ( v->virq_to_evtchn[chn1->u.virq] == port1 )
+                v->virq_to_evtchn[chn1->u.virq] = 0;
+            write_unlock_irqrestore(&v->virq_lock, flags);
         }
         break;
 
@@ -797,7 +804,7 @@ void send_guest_vcpu_virq(struct vcpu *v
 
     ASSERT(!virq_is_global(virq));
 
-    spin_lock_irqsave(&v->virq_lock, flags);
+    read_lock_irqsave(&v->virq_lock, flags);
 
     port = v->virq_to_evtchn[virq];
     if ( unlikely(port == 0) )
@@ -807,7 +814,7 @@ void send_guest_vcpu_virq(struct vcpu *v
     evtchn_port_set_pending(d, v->vcpu_id, evtchn_from_port(d, port));
 
  out:
-    spin_unlock_irqrestore(&v->virq_lock, flags);
+    read_unlock_irqrestore(&v->virq_lock, flags);
 }
 
 void send_guest_global_virq(struct domain *d, uint32_t virq)
@@ -826,7 +833,7 @@ void send_guest_global_virq(struct domai
     if ( unlikely(v == NULL) )
         return;
 
-    spin_lock_irqsave(&v->virq_lock, flags);
+    read_lock_irqsave(&v->virq_lock, flags);
 
     port = v->virq_to_evtchn[virq];
     if ( unlikely(port == 0) )
@@ -836,7 +843,7 @@ void send_guest_global_virq(struct domai
     evtchn_port_set_pending(d, chn->notify_vcpu_id, chn);
 
  out:
-    spin_unlock_irqrestore(&v->virq_lock, flags);
+    read_unlock_irqrestore(&v->virq_lock, flags);
 }
 
 void send_guest_pirq(struct domain *d, const struct pirq *pirq)
xen/include/xen/sched.h +1 -1
@@ -235,7 +235,7 @@ struct vcpu
 
     /* IRQ-safe virq_lock protects against delivering VIRQ to stale evtchn. */
     evtchn_port_t    virq_to_evtchn[NR_VIRQS];
-    spinlock_t       virq_lock;
+    rwlock_t         virq_lock;
 
     /* Tasklet for continue_hypercall_on_cpu(). */
     struct tasklet   continue_hypercall_tasklet;
#35 lore raw

Re: [PATCH v2 6/8] evtchn: convert vIRQ lock to an r/w one

Hi Jan,

Quoted 24 lines · Jan Beulich

There's no need to serialize all sending of vIRQ-s; all that's needed is serialization against the closing of the respective event channels (so far by means of a barrier). To facilitate the conversion, switch to an ordinary write locked region in evtchn_close().

---
v2: Don't introduce/use rw_barrier() here. Add comment to
evtchn_bind_virq(). Re-base.

xen/common/domain.c +1 -1
@@ -160,7 +160,7 @@ struct vcpu *vcpu_create(struct domain *
      v->vcpu_id = vcpu_id;
      v->dirty_cpu = VCPU_CPU_CLEAN;
 
-    spin_lock_init(&v->virq_lock);
+    rwlock_init(&v->virq_lock);
 
      tasklet_init(&v->continue_hypercall_tasklet, NULL, NULL);
 

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t · line 455

Quoted 7 lines
xen/common/event_channel.c +4 -0
@@ -449,6 +449,13 @@ int evtchn_bind_virq(evtchn_bind_virq_t
 
      spin_unlock_irqrestore(&chn->lock, flags);
 
+    /*
+     * If by any, the update of virq_to_evtchn[] would need guarding by
+     * virq_lock, but since this is the last action here, there's no strict
+     * need to acquire the lock. Hnece holding event_lock isn't helpful

s/Hnece/Hence/

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t · line 459

Quoted 4 lines
xen/common/event_channel.c +3 -0
+     * anymore at this point, but utilize that its unlocking acts as the
+     * otherwise necessary smp_wmb() here.
+     */
      v->virq_to_evtchn[virq] = bind->port = port;

I think all access to v->virq_to_evtchn[virq] should use ACCESS_ONCE() or {read, write}_atomic() to avoid any store/load tearing.

Cheers,

Signature · 1 line
Julien Grall
#36 lore raw

Re: [PATCH v2 6/8] evtchn: convert vIRQ lock to an r/w one

Quoted 14 lines · Julien Grall

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t · line 455

Quoted 10 lines · Jan Beulich
xen/common/event_channel.c +4 -0
@@ -449,6 +449,13 @@ int evtchn_bind_virq(evtchn_bind_virq_t
 
      spin_unlock_irqrestore(&chn->lock, flags);
 
+    /*
+     * If by any, the update of virq_to_evtchn[] would need guarding by
+     * virq_lock, but since this is the last action here, there's no strict
+     * need to acquire the lock. Hnece holding event_lock isn't helpful

s/Hnece/Hence/

Quoted 7 lines

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t · line 459

Quoted 4 lines
xen/common/event_channel.c +3 -0
+     * anymore at this point, but utilize that its unlocking acts as the
+     * otherwise necessary smp_wmb() here.
+     */
      v->virq_to_evtchn[virq] = bind->port = port;

I think all access to v->virq_to_evtchn[virq] should use ACCESS_ONCE() or {read, write}_atomic() to avoid any store/load tearing.

IOW you're suggesting this to be the subject of a separate patch? I don't think such a conversion belongs here (nor even in this series, seeing the much wider applicability of such a change throughout the code base). Or are you seeing anything here which would require such a conversion to be done as a prereq?

Jan

#37 lore raw

Re: [PATCH v2 6/8] evtchn: convert vIRQ lock to an r/w one

Quoted 23 lines · Jan Beulich
Quoted 14 lines · Julien Grall

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t · line 455

Quoted 10 lines · Jan Beulich
xen/common/event_channel.c +4 -0
@@ -449,6 +449,13 @@ int evtchn_bind_virq(evtchn_bind_virq_t
 
       spin_unlock_irqrestore(&chn->lock, flags);
 
+    /*
+     * If by any, the update of virq_to_evtchn[] would need guarding by
+     * virq_lock, but since this is the last action here, there's no strict
+     * need to acquire the lock. Hnece holding event_lock isn't helpful

s/Hnece/Hence/

Quoted 7 lines

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t · line 459

Quoted 4 lines
xen/common/event_channel.c +3 -0
+     * anymore at this point, but utilize that its unlocking acts as the
+     * otherwise necessary smp_wmb() here.
+     */
       v->virq_to_evtchn[virq] = bind->port = port;

I think all access to v->virq_to_evtchn[virq] should use ACCESS_ONCE() or {read, write}_atomic() to avoid any store/load tearing.

Quoted 6 lines

IOW you're suggesting this to be the subject of a separate patch? I don't think such a conversion belongs here (nor even in this series, seeing the much wider applicability of such a change throughout the code base).
Or are you seeing anything here which
would require such a conversion to be done as a prereq?

Yes, your comment implies that it is fine to write to virq_to_evtchn[] without the lock taken. However, this is *only* valid if the compiler doesn't tear down load/store.

So this is a pre-req to get your comment valid.

Cheers,

Signature · 1 line
Julien Grall
#38 lore raw

Re: [PATCH v2 6/8] evtchn: convert vIRQ lock to an r/w one

Quoted 31 lines · Julien Grall
Quoted 23 lines · Jan Beulich
Quoted 14 lines · Julien Grall

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t · line 455

Quoted 10 lines · Jan Beulich
xen/common/event_channel.c +4 -0
@@ -449,6 +449,13 @@ int evtchn_bind_virq(evtchn_bind_virq_t
 
       spin_unlock_irqrestore(&chn->lock, flags);
 
+    /*
+     * If by any, the update of virq_to_evtchn[] would need guarding by
+     * virq_lock, but since this is the last action here, there's no strict
+     * need to acquire the lock. Hnece holding event_lock isn't helpful

s/Hnece/Hence/

Quoted 7 lines

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t · line 459

Quoted 4 lines
xen/common/event_channel.c +3 -0
+     * anymore at this point, but utilize that its unlocking acts as the
+     * otherwise necessary smp_wmb() here.
+     */
       v->virq_to_evtchn[virq] = bind->port = port;

I think all access to v->virq_to_evtchn[virq] should use ACCESS_ONCE() or {read, write}_atomic() to avoid any store/load tearing.

Quoted 6 lines

IOW you're suggesting this to be the subject of a separate patch? I don't think such a conversion belongs here (nor even in this series, seeing the much wider applicability of such a change throughout the code base).
Or are you seeing anything here which
would require such a conversion to be done as a prereq?

Quoted 5 lines

Yes, your comment implies that it is fine to write to virq_to_evtchn[] without the lock taken. However, this is *only* valid if the compiler doesn't tear down load/store.

So this is a pre-req to get your comment valid.

That's an interesting way to view it. I'm only adding the comment, without changing the code here. Iirc it was you who asked me to add the comment. And now this is leading to you wanting me to do entirely unrelated changes, when the code base is full of similar assumptions towards no torn accesses? (Yes, I certainly can add such a patch, but no, I don't think this should be a requirement here.)

Jan

#39 lore raw

Re: [PATCH v2 6/8] evtchn: convert vIRQ lock to an r/w one

Quoted 36 lines · Jan Beulich
Quoted 35 lines · Julien Grall
Quoted 23 lines · Jan Beulich
Quoted 14 lines · Julien Grall

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t · line 455

Quoted 10 lines · Jan Beulich
xen/common/event_channel.c +4 -0
@@ -449,6 +449,13 @@ int evtchn_bind_virq(evtchn_bind_virq_t
 
        spin_unlock_irqrestore(&chn->lock, flags);
 
+    /*
+     * If by any, the update of virq_to_evtchn[] would need guarding by
+     * virq_lock, but since this is the last action here, there's no strict
+     * need to acquire the lock. Hnece holding event_lock isn't helpful

s/Hnece/Hence/

Quoted 7 lines

xen/common/event_channel.c int evtchn_bind_virq(evtchn_bind_virq_t · line 459

Quoted 4 lines
xen/common/event_channel.c +3 -0
+     * anymore at this point, but utilize that its unlocking acts as the
+     * otherwise necessary smp_wmb() here.
+     */
        v->virq_to_evtchn[virq] = bind->port = port;

I think all access to v->virq_to_evtchn[virq] should use ACCESS_ONCE() or {read, write}_atomic() to avoid any store/load tearing.

Quoted 6 lines

IOW you're suggesting this to be the subject of a separate patch? I don't think such a conversion belongs here (nor even in this series, seeing the much wider applicability of such a change throughout the code base).
Or are you seeing anything here which
would require such a conversion to be done as a prereq?

Yes, your comment implies that it is fine to write to virq_to_evtchn[] without the lock taken. However, this is *only* valid if the compiler doesn't tear down load/store.

Quoted 5 lines
Quoted 1 line

So this is a pre-req to get your comment valid.

That's an interesting way to view it. I'm only adding the comment, without changing the code here. Iirc it was you who asked me to add the comment.

Yes I asked this comment because it makes easier for reader to figure out how your code works. But this doesn't mean I wanted to have a misleading comment.

If you don't plan to handle the ACCESS_ONCE(), then it is best to be open to say it in the comment.

Quoted 3 lines

And now this is leading to you wanting me to do entirely unrelated changes, when the code base is full of similar assumptions towards no torn accesses?

I was expecting you writing that :). Well, small steps are always easier in order to achieve a consistent code base.

To me, this is similar to some of the reviewers pushing contributors to update the coding style of area touched. The difference here is without following a memory model, you are at the mercy of the compiler and XSAs.

Quoted 3 lines

(Yes, I certainly can add
such a patch, but no, I don't think this should be a requirement here.)

That's ok. But please update the comment to avoid misleading the reader.

An alternative would be to use the write_lock() here. This shouldn't impact that much the performance and nicely fix the issue.

Cheers,

Signature · 1 line
Julien Grall
#40 lore raw

PATCH v2 7/8 evtchn: convert domain event lock to an r/w one

In reply to Jan Beulich

Especially for the use in evtchn_move_pirqs() (called when moving a vCPU across pCPU-s) and the ones in EOI handling in PCI pass-through code, serializing perhaps an entire domain isn't helpful when no state (which isn't e.g. further protected by the per-channel lock) changes.

Unfortunately this implies dropping of lock profiling for this lock, until r/w locks may get enabled for such functionality.

While ->notify_vcpu_id is now meant to be consistently updated with the per-channel lock held, an extension applies to ECS_PIRQ: The field is also guaranteed to not change with the per-domain event lock held for writing. Therefore the unlink_pirq_port() call from evtchn_bind_vcpu() as well as the link_pirq_port() one from evtchn_bind_pirq() could in principle be moved out of the per-channel locked regions, but this further code churn didn't seem worth it.

---
v2: Consistently lock for writing in evtchn_reset(). Fix error path in
pci_clean_dpci_irqs(). Lock for writing in pt_irq_time_out(), hvm_dirq_assist(), hvm_dpci_eoi(), and hvm_dpci_isairq_eoi(). Move rw_barrier() introduction here. Re-base over changes earlier in the series.
---
RFC:
* In evtchn_bind_vcpu() the question is whether limiting the use of write_lock() to just the ECS_PIRQ case is really worth it.
* In flask_get_peer_sid() the question is whether we wouldn't better switch to using the per-channel lock.

xen/arch/x86/domain.c +2 -2
@@ -917,7 +917,7 @@ int arch_domain_soft_reset(struct domain
     if ( !is_hvm_domain(d) )
         return -EINVAL;
 
-    spin_lock(&d->event_lock);
+    write_lock(&d->event_lock);
     for ( i = 0; i < d->nr_pirqs ; i++ )
     {
         if ( domain_pirq_to_emuirq(d, i) != IRQ_UNBOUND )
@@ -927,7 +927,7 @@ int arch_domain_soft_reset(struct domain
                 break;
         }
     }
-    spin_unlock(&d->event_lock);
+    write_unlock(&d->event_lock);
 
     if ( ret )
         return ret;
xen/arch/x86/hvm/hvm.c +2 -2
@@ -528,9 +528,9 @@ void hvm_migrate_pirqs(struct vcpu *v)
     if ( !is_iommu_enabled(d) || !hvm_domain_irq(d)->dpci )
        return;
 
-    spin_lock(&d->event_lock);
+    read_lock(&d->event_lock);
     pt_pirq_iterate(d, migrate_pirq, v);
-    spin_unlock(&d->event_lock);
+    read_unlock(&d->event_lock);
 }
 
 static bool hvm_get_pending_event(struct vcpu *v, struct x86_event *info)
xen/arch/x86/hvm/irq.c +2 -2
@@ -404,9 +404,9 @@ int hvm_inject_msi(struct domain *d, uin
             {
                 int rc;
 
-                spin_lock(&d->event_lock);
+                write_lock(&d->event_lock);
                 rc = map_domain_emuirq_pirq(d, pirq, IRQ_MSI_EMU);
-                spin_unlock(&d->event_lock);
+                write_unlock(&d->event_lock);
                 if ( rc )
                     return rc;
                 info = pirq_info(d, pirq);
xen/arch/x86/hvm/vioapic.c +2 -2
@@ -203,9 +203,9 @@ static int vioapic_hwdom_map_gsi(unsigne
     {
         gprintk(XENLOG_WARNING, "vioapic: error binding GSI %u: %d\n",
                 gsi, ret);
-        spin_lock(&currd->event_lock);
+        write_lock(&currd->event_lock);
         unmap_domain_pirq(currd, pirq);
-        spin_unlock(&currd->event_lock);
+        write_unlock(&currd->event_lock);
     }
     pcidevs_unlock();
 
xen/arch/x86/hvm/vmsi.c +10 -10
@@ -465,7 +465,7 @@ int msixtbl_pt_register(struct domain *d
     int r = -EINVAL;
 
     ASSERT(pcidevs_locked());
-    ASSERT(spin_is_locked(&d->event_lock));
+    ASSERT(rw_is_write_locked(&d->event_lock));
 
     if ( !msixtbl_initialised(d) )
         return -ENODEV;
@@ -535,7 +535,7 @@ void msixtbl_pt_unregister(struct domain
     struct msixtbl_entry *entry;
 
     ASSERT(pcidevs_locked());
-    ASSERT(spin_is_locked(&d->event_lock));
+    ASSERT(rw_is_write_locked(&d->event_lock));
 
     if ( !msixtbl_initialised(d) )
         return;
@@ -589,13 +589,13 @@ void msixtbl_pt_cleanup(struct domain *d
     if ( !msixtbl_initialised(d) )
         return;
 
-    spin_lock(&d->event_lock);
+    write_lock(&d->event_lock);
 
     list_for_each_entry_safe( entry, temp,
                               &d->arch.hvm.msixtbl_list, list )
         del_msixtbl_entry(entry);
 
-    spin_unlock(&d->event_lock);
+    write_unlock(&d->event_lock);
 }
 
 void msix_write_completion(struct vcpu *v)
@@ -719,9 +719,9 @@ int vpci_msi_arch_update(struct vpci_msi
                          msi->arch.pirq, msi->mask);
     if ( rc )
     {
-        spin_lock(&pdev->domain->event_lock);
+        write_lock(&pdev->domain->event_lock);
         unmap_domain_pirq(pdev->domain, msi->arch.pirq);
-        spin_unlock(&pdev->domain->event_lock);
+        write_unlock(&pdev->domain->event_lock);
         pcidevs_unlock();
         msi->arch.pirq = INVALID_PIRQ;
         return rc;
@@ -760,9 +760,9 @@ static int vpci_msi_enable(const struct
     rc = vpci_msi_update(pdev, data, address, vectors, pirq, mask);
     if ( rc )
     {
-        spin_lock(&pdev->domain->event_lock);
+        write_lock(&pdev->domain->event_lock);
         unmap_domain_pirq(pdev->domain, pirq);
-        spin_unlock(&pdev->domain->event_lock);
+        write_unlock(&pdev->domain->event_lock);
         pcidevs_unlock();
         return rc;
     }
@@ -807,9 +807,9 @@ static void vpci_msi_disable(const struc
         ASSERT(!rc);
     }
 
-    spin_lock(&pdev->domain->event_lock);
+    write_lock(&pdev->domain->event_lock);
     unmap_domain_pirq(pdev->domain, pirq);
-    spin_unlock(&pdev->domain->event_lock);
+    write_unlock(&pdev->domain->event_lock);
     pcidevs_unlock();
 }
 
xen/arch/x86/io_apic.c +2 -2
@@ -2413,10 +2413,10 @@ int ioapic_guest_write(unsigned long phy
     }
     if ( pirq >= 0 )
     {
-        spin_lock(&hardware_domain->event_lock);
+        write_lock(&hardware_domain->event_lock);
         ret = map_domain_pirq(hardware_domain, pirq, irq,
                               MAP_PIRQ_TYPE_GSI, NULL);
-        spin_unlock(&hardware_domain->event_lock);
+        write_unlock(&hardware_domain->event_lock);
         if ( ret < 0 )
             return ret;
     }
xen/arch/x86/irq.c +16 -16
@@ -1536,7 +1536,7 @@ int pirq_guest_bind(struct vcpu *v, stru
     irq_guest_action_t *action, *newaction = NULL;
     int                 rc = 0;
 
-    WARN_ON(!spin_is_locked(&v->domain->event_lock));
+    WARN_ON(!rw_is_write_locked(&v->domain->event_lock));
     BUG_ON(!local_irq_is_enabled());
 
  retry:
@@ -1756,7 +1756,7 @@ void pirq_guest_unbind(struct domain *d,
     struct irq_desc *desc;
     int irq = 0;
 
-    WARN_ON(!spin_is_locked(&d->event_lock));
+    WARN_ON(!rw_is_write_locked(&d->event_lock));
 
     BUG_ON(!local_irq_is_enabled());
     desc = pirq_spin_lock_irq_desc(pirq, NULL);
@@ -1793,7 +1793,7 @@ static bool pirq_guest_force_unbind(stru
     unsigned int i;
     bool bound = false;
 
-    WARN_ON(!spin_is_locked(&d->event_lock));
+    WARN_ON(!rw_is_write_locked(&d->event_lock));
 
     BUG_ON(!local_irq_is_enabled());
     desc = pirq_spin_lock_irq_desc(pirq, NULL);
@@ -2037,7 +2037,7 @@ int get_free_pirq(struct domain *d, int
 {
     int i;
 
-    ASSERT(spin_is_locked(&d->event_lock));
+    ASSERT(rw_is_write_locked(&d->event_lock));
 
     if ( type == MAP_PIRQ_TYPE_GSI )
     {
@@ -2062,7 +2062,7 @@ int get_free_pirqs(struct domain *d, uns
 {
     unsigned int i, found = 0;
 
-    ASSERT(spin_is_locked(&d->event_lock));
+    ASSERT(rw_is_write_locked(&d->event_lock));
 
     for ( i = d->nr_pirqs - 1; i >= nr_irqs_gsi; --i )
         if ( is_free_pirq(d, pirq_info(d, i)) )
@@ -2090,7 +2090,7 @@ int map_domain_pirq(
     DECLARE_BITMAP(prepared, MAX_MSI_IRQS) = {};
     DECLARE_BITMAP(granted, MAX_MSI_IRQS) = {};
 
-    ASSERT(spin_is_locked(&d->event_lock));
+    ASSERT(rw_is_write_locked(&d->event_lock));
 
     if ( !irq_access_permitted(current->domain, irq))
         return -EPERM;
@@ -2309,7 +2309,7 @@ int unmap_domain_pirq(struct domain *d,
         return -EINVAL;
 
     ASSERT(pcidevs_locked());
-    ASSERT(spin_is_locked(&d->event_lock));
+    ASSERT(rw_is_write_locked(&d->event_lock));
 
     info = pirq_info(d, pirq);
     if ( !info || (irq = info->arch.irq) <= 0 )
@@ -2436,13 +2436,13 @@ void free_domain_pirqs(struct domain *d)
     int i;
 
     pcidevs_lock();
-    spin_lock(&d->event_lock);
+    write_lock(&d->event_lock);
 
     for ( i = 0; i < d->nr_pirqs; i++ )
         if ( domain_pirq_to_irq(d, i) > 0 )
             unmap_domain_pirq(d, i);
 
-    spin_unlock(&d->event_lock);
+    write_unlock(&d->event_lock);
     pcidevs_unlock();
 }
 
@@ -2685,7 +2685,7 @@ int map_domain_emuirq_pirq(struct domain
     int old_emuirq = IRQ_UNBOUND, old_pirq = IRQ_UNBOUND;
     struct pirq *info;
 
-    ASSERT(spin_is_locked(&d->event_lock));
+    ASSERT(rw_is_write_locked(&d->event_lock));
 
     if ( !is_hvm_domain(d) )
         return -EINVAL;
@@ -2751,7 +2751,7 @@ int unmap_domain_pirq_emuirq(struct doma
     if ( (pirq < 0) || (pirq >= d->nr_pirqs) )
         return -EINVAL;
 
-    ASSERT(spin_is_locked(&d->event_lock));
+    ASSERT(rw_is_write_locked(&d->event_lock));
 
     emuirq = domain_pirq_to_emuirq(d, pirq);
     if ( emuirq == IRQ_UNBOUND )
@@ -2799,7 +2799,7 @@ static int allocate_pirq(struct domain *
 {
     int current_pirq;
 
-    ASSERT(spin_is_locked(&d->event_lock));
+    ASSERT(rw_is_write_locked(&d->event_lock));
     current_pirq = domain_irq_to_pirq(d, irq);
     if ( pirq < 0 )
     {
@@ -2871,7 +2871,7 @@ int allocate_and_map_gsi_pirq(struct dom
     }
 
     /* Verify or get pirq. */
-    spin_lock(&d->event_lock);
+    write_lock(&d->event_lock);
     pirq = allocate_pirq(d, index, *pirq_p, irq, MAP_PIRQ_TYPE_GSI, NULL);
     if ( pirq < 0 )
     {
@@ -2884,7 +2884,7 @@ int allocate_and_map_gsi_pirq(struct dom
         *pirq_p = pirq;
 
  done:
-    spin_unlock(&d->event_lock);
+    write_unlock(&d->event_lock);
 
     return ret;
 }
@@ -2925,7 +2925,7 @@ int allocate_and_map_msi_pirq(struct dom
 
     pcidevs_lock();
     /* Verify or get pirq. */
-    spin_lock(&d->event_lock);
+    write_lock(&d->event_lock);
     pirq = allocate_pirq(d, index, *pirq_p, irq, type, &msi->entry_nr);
     if ( pirq < 0 )
     {
@@ -2938,7 +2938,7 @@ int allocate_and_map_msi_pirq(struct dom
         *pirq_p = pirq;
 
  done:
-    spin_unlock(&d->event_lock);
+    write_unlock(&d->event_lock);
     pcidevs_unlock();
     if ( ret )
     {
xen/arch/x86/physdev.c +11 -11
@@ -34,7 +34,7 @@ static int physdev_hvm_map_pirq(
 
     ASSERT(!is_hardware_domain(d));
 
-    spin_lock(&d->event_lock);
+    write_lock(&d->event_lock);
     switch ( type )
     {
     case MAP_PIRQ_TYPE_GSI: {
@@ -84,7 +84,7 @@ static int physdev_hvm_map_pirq(
         break;
     }
 
-    spin_unlock(&d->event_lock);
+    write_unlock(&d->event_lock);
     return ret;
 }
 
@@ -154,18 +154,18 @@ int physdev_unmap_pirq(domid_t domid, in
 
     if ( is_hvm_domain(d) && has_pirq(d) )
     {
-        spin_lock(&d->event_lock);
+        write_lock(&d->event_lock);
         if ( domain_pirq_to_emuirq(d, pirq) != IRQ_UNBOUND )
             ret = unmap_domain_pirq_emuirq(d, pirq);
-        spin_unlock(&d->event_lock);
+        write_unlock(&d->event_lock);
         if ( domid == DOMID_SELF || ret )
             goto free_domain;
     }
 
     pcidevs_lock();
-    spin_lock(&d->event_lock);
+    write_lock(&d->event_lock);
     ret = unmap_domain_pirq(d, pirq);
-    spin_unlock(&d->event_lock);
+    write_unlock(&d->event_lock);
     pcidevs_unlock();
 
  free_domain:
@@ -192,10 +192,10 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_H
         ret = -EINVAL;
         if ( eoi.irq >= currd->nr_pirqs )
             break;
-        spin_lock(&currd->event_lock);
+        read_lock(&currd->event_lock);
         pirq = pirq_info(currd, eoi.irq);
         if ( !pirq ) {
-            spin_unlock(&currd->event_lock);
+            read_unlock(&currd->event_lock);
             break;
         }
         if ( currd->arch.auto_unmask )
@@ -214,7 +214,7 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_H
                     && hvm_irq->gsi_assert_count[gsi] )
                 send_guest_pirq(currd, pirq);
         }
-        spin_unlock(&currd->event_lock);
+        read_unlock(&currd->event_lock);
         ret = 0;
         break;
     }
@@ -626,7 +626,7 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_H
         if ( copy_from_guest(&out, arg, 1) != 0 )
             break;
 
-        spin_lock(&currd->event_lock);
+        write_lock(&currd->event_lock);
 
         ret = get_free_pirq(currd, out.type);
         if ( ret >= 0 )
@@ -639,7 +639,7 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_H
                 ret = -ENOMEM;
         }
 
-        spin_unlock(&currd->event_lock);
+        write_unlock(&currd->event_lock);
 
         if ( ret >= 0 )
         {
xen/arch/x86/pv/shim.c +5 -5
@@ -448,7 +448,7 @@ static long pv_shim_event_channel_op(int
         if ( rc )                                                           \
             break;                                                          \
                                                                             \
-        spin_lock(&d->event_lock);                                          \
+        write_lock(&d->event_lock);                                         \
         rc = evtchn_allocate_port(d, op.port_field);                        \
         if ( rc )                                                           \
         {                                                                   \
@@ -457,7 +457,7 @@ static long pv_shim_event_channel_op(int
         }                                                                   \
         else                                                                \
             evtchn_reserve(d, op.port_field);                               \
-        spin_unlock(&d->event_lock);                                        \
+        write_unlock(&d->event_lock);                                       \
                                                                             \
         if ( !rc && __copy_to_guest(arg, &op, 1) )                          \
             rc = -EFAULT;                                                   \
@@ -585,11 +585,11 @@ static long pv_shim_event_channel_op(int
         if ( rc )
             break;
 
-        spin_lock(&d->event_lock);
+        write_lock(&d->event_lock);
         rc = evtchn_allocate_port(d, ipi.port);
         if ( rc )
         {
-            spin_unlock(&d->event_lock);
+            write_unlock(&d->event_lock);
 
             close.port = ipi.port;
             BUG_ON(xen_hypercall_event_channel_op(EVTCHNOP_close, &close));
@@ -598,7 +598,7 @@ static long pv_shim_event_channel_op(int
 
         evtchn_assign_vcpu(d, ipi.port, ipi.vcpu);
         evtchn_reserve(d, ipi.port);
-        spin_unlock(&d->event_lock);
+        write_unlock(&d->event_lock);
 
         if ( __copy_to_guest(arg, &ipi, 1) )
             rc = -EFAULT;
xen/common/event_channel.c +57 -46

310 lines not on this page · raw message

xen/common/event_fifo.c +5 -5
@@ -561,7 +561,7 @@ int evtchn_fifo_init_control(struct evtc
     if ( offset & (8 - 1) )
         return -EINVAL;
 
-    spin_lock(&d->event_lock);
+    write_lock(&d->event_lock);
 
     /*
      * If this is the first control block, setup an empty event array
@@ -593,13 +593,13 @@ int evtchn_fifo_init_control(struct evtc
     else
         rc = map_control_block(v, gfn, offset);
 
-    spin_unlock(&d->event_lock);
+    write_unlock(&d->event_lock);
 
     return rc;
 
  error:
     evtchn_fifo_destroy(d);
-    spin_unlock(&d->event_lock);
+    write_unlock(&d->event_lock);
     return rc;
 }
 
@@ -652,9 +652,9 @@ int evtchn_fifo_expand_array(const struc
     if ( !d->evtchn_fifo )
         return -EOPNOTSUPP;
 
-    spin_lock(&d->event_lock);
+    write_lock(&d->event_lock);
     rc = add_page_to_event_array(d, expand_array->array_gfn);
-    spin_unlock(&d->event_lock);
+    write_unlock(&d->event_lock);
 
     return rc;
 }
xen/drivers/passthrough/io.c +30 -30

234 lines not on this page · raw message

xen/common/spinlock.c +10 -0
@@ -2,6 +2,7 @@
 #include <xen/irq.h>
 #include <xen/smp.h>
 #include <xen/time.h>
+#include <xen/rwlock.h>
 #include <xen/spinlock.h>
 #include <xen/guest_access.h>
 #include <xen/preempt.h>
@@ -334,6 +335,15 @@ void _spin_unlock_recursive(spinlock_t *
     }
 }
 
+void _rw_barrier(rwlock_t *lock)
+{
+    check_barrier(&lock->lock.debug);
+    smp_mb();
+    while ( _rw_is_locked(lock) )
+        arch_lock_relax();
+    smp_mb();
+}
+
 #ifdef CONFIG_DEBUG_LOCK_PROFILE
 
 struct lock_profile_anc {
xen/drivers/passthrough/pci.c +3 -3
@@ -883,7 +883,7 @@ static int pci_clean_dpci_irqs(struct do
     if ( !is_hvm_domain(d) )
         return 0;
 
-    spin_lock(&d->event_lock);
+    write_lock(&d->event_lock);
     hvm_irq_dpci = domain_get_irq_dpci(d);
     if ( hvm_irq_dpci != NULL )
     {
@@ -901,14 +901,14 @@ static int pci_clean_dpci_irqs(struct do
             ret = pt_pirq_iterate(d, pci_clean_dpci_irq, NULL);
         if ( ret )
         {
-            spin_unlock(&d->event_lock);
+            write_unlock(&d->event_lock);
             return ret;
         }
 
         hvm_domain_irq(d)->dpci = NULL;
         free_hvm_irq_dpci(hvm_irq_dpci);
     }
-    spin_unlock(&d->event_lock);
+    write_unlock(&d->event_lock);
     return 0;
 }
 
xen/drivers/passthrough/vtd/x86/hvm.c +2 -2
@@ -54,7 +54,7 @@ void hvm_dpci_isairq_eoi(struct domain *
     if ( !is_iommu_enabled(d) )
         return;
 
-    spin_lock(&d->event_lock);
+    write_lock(&d->event_lock);
 
     dpci = domain_get_irq_dpci(d);
 
@@ -63,5 +63,5 @@ void hvm_dpci_isairq_eoi(struct domain *
         /* Multiple mirq may be mapped to one isa irq */
         pt_pirq_iterate(d, _hvm_dpci_isairq_eoi, (void *)(long)isairq);
     }
-    spin_unlock(&d->event_lock);
+    write_unlock(&d->event_lock);
 }
xen/include/xen/rwlock.h +3 -0
@@ -237,6 +237,8 @@ static inline int _rw_is_write_locked(rw
     return (atomic_read(&lock->cnts) & _QW_WMASK) == _QW_LOCKED;
 }
 
+void _rw_barrier(rwlock_t *lock);
+
 #define read_lock(l)                  _read_lock(l)
 #define read_lock_irq(l)              _read_lock_irq(l)
 #define read_lock_irqsave(l, f)                                 \
@@ -266,6 +268,7 @@ static inline int _rw_is_write_locked(rw
 #define rw_is_locked(l)               _rw_is_locked(l)
 #define rw_is_write_locked(l)         _rw_is_write_locked(l)
 
+#define rw_barrier(l)                 _rw_barrier(l)
 
 typedef struct percpu_rwlock percpu_rwlock_t;
 
xen/include/xen/sched.h +1 -1
@@ -373,7 +373,7 @@ struct domain
     unsigned int     xen_evtchns;
     /* Port to resume from in evtchn_reset(), when in a continuation. */
     unsigned int     next_evtchn;
-    spinlock_t       event_lock;
+    rwlock_t         event_lock;
     const struct evtchn_port_ops *evtchn_port_ops;
     struct evtchn_fifo_domain *evtchn_fifo;
 
xen/xsm/flask/flask_op.c +2 -2
@@ -555,7 +555,7 @@ static int flask_get_peer_sid(struct xen
     struct evtchn *chn;
     struct domain_security_struct *dsec;
 
-    spin_lock(&d->event_lock);
+    read_lock(&d->event_lock);
 
     if ( !port_is_valid(d, arg->evtchn) )
         goto out;
@@ -573,7 +573,7 @@ static int flask_get_peer_sid(struct xen
     rv = 0;
 
  out:
-    spin_unlock(&d->event_lock);
+    read_unlock(&d->event_lock);
     return rv;
 }
 
#41 lore raw

RFC PATCH v2 8/8 evtchn: don't call Xen consumer callback with per-channel lock held

In reply to Jan Beulich

While there don't look to be any problems with this right now, the lock order implications from holding the lock can be very difficult to follow (and may be easy to violate unknowingly). The present callbacks don't (and no such callback should) have any need for the lock to be held.

---
TODO: vm_event_disable() frees the structures used by respective
callbacks - need to either use call_rcu() for freeing, or maintain a count of in-progress calls, for evtchn_close() to wait to drop to zero before dropping the lock / returning.

xen/common/event_channel.c +12 -3
@@ -763,9 +763,18 @@ int evtchn_send(struct domain *ld, unsig
         rport = lchn->u.interdomain.remote_port;
         rchn  = evtchn_from_port(rd, rport);
         if ( consumer_is_xen(rchn) )
-            xen_notification_fn(rchn)(rd->vcpu[rchn->notify_vcpu_id], rport);
-        else
-            evtchn_port_set_pending(rd, rchn->notify_vcpu_id, rchn);
+        {
+            /* Don't keep holding the lock for the call below. */
+            xen_event_channel_notification_t fn = xen_notification_fn(rchn);
+            struct vcpu *rv = rd->vcpu[rchn->notify_vcpu_id];
+
+            rcu_lock_domain(rd);
+            spin_unlock_irqrestore(&lchn->lock, flags);
+            fn(rv, rport);
+            rcu_unlock_domain(rd);
+            return 0;
+        }
+        evtchn_port_set_pending(rd, rchn->notify_vcpu_id, rchn);
         break;
     case ECS_IPI:
         evtchn_port_set_pending(ld, lchn->notify_vcpu_id, lchn);
#42 lore raw

Re: [PATCH RFC v2 8/8] evtchn: don't call Xen consumer callback with per-channel lock held

Hi Jan and sorry for the late reply,

Quoted 11 lines · Jan Beulich

While there don't look to be any problems with this right now, the lock order implications from holding the lock can be very difficult to follow (and may be easy to violate unknowingly). The present callbacks don't (and no such callback should) have any need for the lock to be held.

---
TODO: vm_event_disable() frees the structures used by respective
callbacks - need to either use call_rcu() for freeing, or maintain a count of in-progress calls, for evtchn_close() to wait to drop to zero before dropping the lock / returning.

I would go with the second solution and maintain a count of in-progress calls.

Tamas, Petre how does this sound?

Alex

Quoted 26 lines
xen/common/event_channel.c +12 -3
@@ -763,9 +763,18 @@ int evtchn_send(struct domain *ld, unsig
          rport = lchn->u.interdomain.remote_port;
          rchn  = evtchn_from_port(rd, rport);
          if ( consumer_is_xen(rchn) )
-            xen_notification_fn(rchn)(rd->vcpu[rchn->notify_vcpu_id], rport);
-        else
-            evtchn_port_set_pending(rd, rchn->notify_vcpu_id, rchn);
+        {
+            /* Don't keep holding the lock for the call below. */
+            xen_event_channel_notification_t fn = xen_notification_fn(rchn);
+            struct vcpu *rv = rd->vcpu[rchn->notify_vcpu_id];
+
+            rcu_lock_domain(rd);
+            spin_unlock_irqrestore(&lchn->lock, flags);
+            fn(rv, rport);
+            rcu_unlock_domain(rd);
+            return 0;
+        }
+        evtchn_port_set_pending(rd, rchn->notify_vcpu_id, rchn);
          break;
      case ECS_IPI:
          evtchn_port_set_pending(ld, lchn->notify_vcpu_id, lchn);
#43 lore raw

Re: [PATCH RFC v2 8/8] evtchn: don't call Xen consumer callback with per-channel lock held

Quoted 17 lines · Isaila Alexandru

Hi Jan and sorry for the late reply,

Quoted 11 lines · Jan Beulich

While there don't look to be any problems with this right now, the lock order implications from holding the lock can be very difficult to follow (and may be easy to violate unknowingly). The present callbacks don't (and no such callback should) have any need for the lock to be held.

---
TODO: vm_event_disable() frees the structures used by respective
callbacks - need to either use call_rcu() for freeing, or maintain a count of in-progress calls, for evtchn_close() to wait to drop to zero before dropping the lock / returning.

Quoted 4 lines

I would go with the second solution and maintain a count of in-progress calls.

Tamas, Petre how does this sound?

Agree, doing a reference count before freeing is preferred.

Thanks,
Tamas