Skip to contentSkip to conversation

xenoprof: XSA-313 follow-up

PATCH In review Jan Beulich 5 participants 12 messages Apr 15, 2020 – Apr 24, 2020

lore
Share & tools
lore
#0 lore raw

cover xenoprof: XSA-313 follow-up

Patch 1 was considered to become part of the XSA, but it was then decided against. The other two are a little bit of cleanup, albeit there's certainly far more room for tidying. Yet then again Paul, in his mail from Mar 13, was asking whether we shouldn't drop xenoprof altogether, at which point cleaning up the code would be wasted effort.

1: adjust ordering of page sharing vs domain type setting 2: drop unused struct xenoprof fields
3: limit scope of types and #define-s

Jan

#1 lore raw
Quoted 9 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 15 April 2020 09:45
To: [email protected]
Cc: Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH 0/3] xenoprof: XSA-313 follow-up

Quoted 7 lines

Patch 1 was considered to become part of the XSA, but it was then decided against. The other two are a little bit of cleanup, albeit there's certainly far more room for tidying. Yet then again Paul, in his mail from Mar 13, was asking whether we shouldn't drop xenoprof altogether, at which point cleaning up the code would be wasted effort.

That's still my opinion. This is a large chunk of (only passively maintained) code which I think is of very limited value (since it relates to an old tool, and it only works for PV domains).

Paul

#2 lore raw
Quoted 9 lines · Paul Durrant
Quoted 9 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 15 April 2020 09:45
To: [email protected]
Cc: Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH 0/3] xenoprof: XSA-313 follow-up

Quoted 8 lines
Quoted 7 lines

Patch 1 was considered to become part of the XSA, but it was then decided against. The other two are a little bit of cleanup, albeit there's certainly far more room for tidying. Yet then again Paul, in his mail from Mar 13, was asking whether we shouldn't drop xenoprof altogether, at which point cleaning up the code would be wasted effort.

That's still my opinion. This is a large chunk of (only passively maintained) code which I think is of very limited value (since it relates to an old tool, and it only works for PV domains).

... and yet, noone has bothered getting any other profiler in to a half-usable state.

You can already Kconfig it out, and yes it is a PITA to use on modern systems where at the minimum, you need to patch the CPU model whitelist, and in some cases extend the MSR whitelist in Xen, but at this point where there are 0 viable alternatives for profiling, removing it would be a deeply short-sighted move.

~Andrew

#3 lore raw
In reply to Paul Durrant

Hi Paul,

Quoted 17 lines · Paul Durrant
Quoted 9 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 15 April 2020 09:45
To: [email protected]
Cc: Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH 0/3] xenoprof: XSA-313 follow-up

Quoted 7 lines

Patch 1 was considered to become part of the XSA, but it was then decided against. The other two are a little bit of cleanup, albeit there's certainly far more room for tidying. Yet then again Paul, in his mail from Mar 13, was asking whether we shouldn't drop xenoprof altogether, at which point cleaning up the code would be wasted effort.

Quoted 1 line

That's still my opinion. This is a large chunk of (only passively maintained) code which I think is of very limited value (since it relates to an old tool, and it only works for PV domains).

While there are no active user we are aware of, this is an example on how to implement a profiler backend with Xen. So I would agree with Andrew here.

IIRC, the reason behind your request is it makes difficult for your xenheap work. Am I correct? If so, do you have a thread explaining the issues?

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw
Quoted 33 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 20 April 2020 15:15
To: [email protected]; 'Jan Beulich' <[email protected]>; [email protected]
Cc: 'Andrew Cooper' <[email protected]>; 'George Dunlap' <[email protected]>; 'Ian Jackson' <[email protected]>; 'Stefano Stabellini' <[email protected]>; 'Wei Liu' <[email protected]>
Subject: Re: [PATCH 0/3] xenoprof: XSA-313 follow-up

Hi Paul,

Quoted 5 lines · Paul Durrant
Quoted 5 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 15 April 2020 09:45
To: [email protected]
Cc: Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian

Jackson

Quoted 12 lines
Quoted 11 lines

<[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH 0/3] xenoprof: XSA-313 follow-up

Patch 1 was considered to become part of the XSA, but it was then decided against. The other two are a little bit of cleanup, albeit there's certainly far more room for tidying. Yet then again Paul, in his mail from Mar 13, was asking whether we shouldn't drop xenoprof altogether, at which point cleaning up the code would be wasted effort.

Quoted 1 line

That's still my opinion. This is a large chunk of (only passively maintained) code which I think is

of very limited value (since it relates to an old tool, and it only works for PV domains).

Quoted 7 lines

While there are no active user we are aware of, this is an example on how to implement a profiler backend with Xen. So I would agree with Andrew here.

IIRC, the reason behind your request is it makes difficult for your xenheap work. Am I correct? If so, do you have a thread explaining the issues?

After shared info and grant table, it is the only other occurrence of a xenheap page shared with a non-system domain. Also, it cannot be trivially replaced with an 'extra' domheap page because its assignment changes. Thus a whole bunch of cleanup work that I was hoping to do (largely in domain_relinquish_resources and free_domheap_pages) is either ruled out, or would have to special-case this type of page. Also, I am unconvinced that PV guests are sufficiently common these days (apart from dom0) that profiling them is of any real use.

Paul

#5 lore raw

Hi Paul,

Quoted 37 lines · Paul Durrant
Quoted 37 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 20 April 2020 15:15
To: [email protected]; 'Jan Beulich' <[email protected]>; [email protected]
Cc: 'Andrew Cooper' <[email protected]>; 'George Dunlap' <[email protected]>; 'Ian Jackson' <[email protected]>; 'Stefano Stabellini' <[email protected]>; 'Wei Liu' <[email protected]>
Subject: Re: [PATCH 0/3] xenoprof: XSA-313 follow-up

Hi Paul,

Quoted 5 lines · Paul Durrant
Quoted 5 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 15 April 2020 09:45
To: [email protected]
Cc: Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian

Jackson

Quoted 12 lines
Quoted 11 lines

<[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH 0/3] xenoprof: XSA-313 follow-up

Patch 1 was considered to become part of the XSA, but it was then decided against. The other two are a little bit of cleanup, albeit there's certainly far more room for tidying. Yet then again Paul, in his mail from Mar 13, was asking whether we shouldn't drop xenoprof altogether, at which point cleaning up the code would be wasted effort.

Quoted 1 line

That's still my opinion. This is a large chunk of (only passively maintained) code which I think is

of very limited value (since it relates to an old tool, and it only works for PV domains).

While there are no active user we are aware of, this is an example on how to implement a profiler backend with Xen. So I would agree with Andrew here.

Quoted 5 lines
Quoted 3 lines

IIRC, the reason behind your request is it makes difficult for your xenheap work. Am I correct? If so, do you have a thread explaining the issues?

After shared info and grant table, it is the only other occurrence of a xenheap page shared with a non-system domain. Also, it cannot be trivially replaced with an 'extra' domheap page because its assignment changes. Thus a whole bunch of cleanup work that I was hoping to do (largely in domain_relinquish_resources and free_domheap_pages) is either ruled out, or would have to special-case this type of page.

My knowledge of xenoprof is very limited, so I might be wrong.

From my understanding, a buffer can only be shared between two domains:
- When in passive mode, the buffer will be shared with the primary
profiler (always the hwdom per the check in xenoprof_op_init()).
- When in active mode, the buffer will be shared with the domain
requesting to be profiled.

Would it be possible to consider to have a separate buffer for the passive mode and active mode?

Quoted 1 line

Also, I am unconvinced that PV guests are sufficiently common these days (apart from dom0) that profiling them is of any real use.

Even an HVM guest can't profile itself, I think it would be useful to have dom0 to profile an HVM guest. Are you suggesting this doesn't work?

Cheers,

Signature · 1 line
Julien Grall
#6 lore raw
Quoted 62 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 24 April 2020 11:35
To: [email protected]; 'Jan Beulich' <[email protected]>; [email protected]
Cc: 'Andrew Cooper' <[email protected]>; 'George Dunlap' <[email protected]>; 'Ian Jackson' <[email protected]>; 'Stefano Stabellini' <[email protected]>; 'Wei Liu' <[email protected]>
Subject: Re: [PATCH 0/3] xenoprof: XSA-313 follow-up

Hi Paul,

Quoted 30 lines · Paul Durrant
Quoted 30 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 20 April 2020 15:15
To: [email protected]; 'Jan Beulich' <[email protected]>; [email protected]
Cc: 'Andrew Cooper' <[email protected]>; 'George Dunlap' <[email protected]>; 'Ian Jackson' <[email protected]>; 'Stefano Stabellini' <[email protected]>; 'Wei Liu' <[email protected]>
Subject: Re: [PATCH 0/3] xenoprof: XSA-313 follow-up

Hi Paul,

Quoted 5 lines · Paul Durrant
Quoted 5 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 15 April 2020 09:45
To: [email protected]
Cc: Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian

Jackson

Quoted 12 lines
Quoted 11 lines

<[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH 0/3] xenoprof: XSA-313 follow-up

Patch 1 was considered to become part of the XSA, but it was then decided against. The other two are a little bit of cleanup, albeit there's certainly far more room for tidying. Yet then again Paul, in his mail from Mar 13, was asking whether we shouldn't drop xenoprof altogether, at which point cleaning up the code would be wasted effort.

Quoted 1 line
Quoted 1 line
Quoted 1 line

That's still my opinion. This is a large chunk of (only passively maintained) code which I think

is

Quoted 11 lines
Quoted 9 lines

of very limited value (since it relates to an old tool, and it only works for PV domains).

While there are no active user we are aware of, this is an example on how to implement a profiler backend with Xen. So I would agree with Andrew here.

IIRC, the reason behind your request is it makes difficult for your xenheap work. Am I correct? If so, do you have a thread explaining the issues?

After shared info and grant table, it is the only other occurrence of a xenheap page shared with a

non-system domain. Also, it cannot be trivially replaced with an 'extra' domheap page because its assignment changes. Thus a whole bunch of cleanup work that I was hoping to do (largely in domain_relinquish_resources and free_domheap_pages) is either ruled out, or would have to special-case this type of page.

My knowledge of xenoprof is very limited, so I might be wrong.

Quoted 8 lines

From my understanding, a buffer can only be shared between two domains:
- When in passive mode, the buffer will be shared with the primary
profiler (always the hwdom per the check in xenoprof_op_init()).
- When in active mode, the buffer will be shared with the domain
requesting to be profiled.

Would it be possible to consider to have a separate buffer for the passive mode and active mode?

I think that may well work, yes.

Quoted 7 lines
Quoted 2 lines

Also, I am unconvinced that PV guests are sufficiently common these days (apart from dom0) that

profiling them is of any real use.

Even an HVM guest can't profile itself, I think it would be useful to have dom0 to profile an HVM guest. Are you suggesting this doesn't work?

No. That may work for a PV dom0; I'll have to try it. So, yes, passive profiling may indeed still have value.

Paul

#7 lore raw

PATCH 1/3 xenoprof: adjust ordering of page sharing vs domain type setting

In reply to Jan Beulich

Buffer pages should be shared with "ignored" or "active" guests only (besides, obviously, the primary profiling domain). Hence domain type should be set to "ignored" before unsharing from the primary domain (which implies even a previously "passive" domain may then access its buffers, albeit that's not very useful unless it gets promoted to "active" subsequently), i.e. such that no further writes of records to the buffer would occur, and (at least for consistency) also before sharing it (with the calling domain) from the XENOPROF_get_buffer path.

xen/common/xenoprof.c +8 -5
@@ -372,8 +372,8 @@ static void reset_passive(struct domain
     if ( x == NULL )
         return;
 
-    unshare_xenoprof_page_with_guest(x);
     x->domain_type = XENOPROF_DOMAIN_IGNORED;
+    unshare_xenoprof_page_with_guest(x);
 }
 
 static void reset_active_list(void)
@@ -654,6 +654,13 @@ static int xenoprof_op_get_buffer(XEN_GU
         if ( ret < 0 )
             return ret;
     }
+    else
+    {
+        d->xenoprof->domain_ready = 0;
+        d->xenoprof->domain_type = XENOPROF_DOMAIN_IGNORED;
+    }
+
+    d->xenoprof->is_primary = (xenoprof_primary_profiler == d);
 
     ret = share_xenoprof_page_with_guest(
         d, virt_to_mfn(d->xenoprof->rawbuf), d->xenoprof->npages);
@@ -662,10 +669,6 @@ static int xenoprof_op_get_buffer(XEN_GU
 
     xenoprof_reset_buf(d);
 
-    d->xenoprof->domain_type  = XENOPROF_DOMAIN_IGNORED;
-    d->xenoprof->domain_ready = 0;
-    d->xenoprof->is_primary   = (xenoprof_primary_profiler == current->domain);
-        
     xenoprof_get_buffer.nbuf = d->xenoprof->nbuf;
     xenoprof_get_buffer.bufsize = d->xenoprof->bufsize;
     if ( !paging_mode_translate(d) )
#8 lore raw

PATCH 2/3 xenoprof: drop unused struct xenoprof fields

In reply to Jan Beulich

Both is_primary and domain_ready are only ever written to. Drop both fields and restrict structure visibility to just the one involved CU. While doing so (and just for starters) make "is_compat" properly bool.

xen/common/xenoprof.c +17 -8
@@ -33,6 +33,23 @@ static DEFINE_SPINLOCK(pmu_owner_lock);
 int pmu_owner = 0;
 int pmu_hvm_refcount = 0;
 
+struct xenoprof_vcpu {
+    int event_size;
+    xenoprof_buf_t *buffer;
+};
+
+struct xenoprof {
+    char *rawbuf;
+    int npages;
+    int nbuf;
+    int bufsize;
+    int domain_type;
+#ifdef CONFIG_COMPAT
+    bool is_compat;
+#endif
+    struct xenoprof_vcpu *vcpu;
+};
+
 static struct domain *active_domains[MAX_OPROF_DOMAINS];
 static int active_ready[MAX_OPROF_DOMAINS];
 static unsigned int adomains;
@@ -259,7 +276,6 @@ static int alloc_xenoprof_struct(
     d->xenoprof->npages = npages;
     d->xenoprof->nbuf = nvcpu;
     d->xenoprof->bufsize = bufsize;
-    d->xenoprof->domain_ready = 0;
     d->xenoprof->domain_type = XENOPROF_DOMAIN_IGNORED;
 
     /* Update buffer pointers for active vcpus */
@@ -327,7 +343,6 @@ static int set_active(struct domain *d)
     if ( x == NULL )
         return -EPERM;
 
-    x->domain_ready = 1;
     x->domain_type = XENOPROF_DOMAIN_ACTIVE;
     active_ready[ind] = 1;
     activated++;
@@ -348,7 +363,6 @@ static int reset_active(struct domain *d
     if ( x == NULL )
         return -EPERM;
 
-    x->domain_ready = 0;
     x->domain_type = XENOPROF_DOMAIN_IGNORED;
     active_ready[ind] = 0;
     active_domains[ind] = NULL;
@@ -655,12 +669,7 @@ static int xenoprof_op_get_buffer(XEN_GU
             return ret;
     }
     else
-    {
-        d->xenoprof->domain_ready = 0;
         d->xenoprof->domain_type = XENOPROF_DOMAIN_IGNORED;
-    }
-
-    d->xenoprof->is_primary = (xenoprof_primary_profiler == d);
 
     ret = share_xenoprof_page_with_guest(
         d, virt_to_mfn(d->xenoprof->rawbuf), d->xenoprof->npages);
xen/include/xen/xenoprof.h +0 -19
@@ -40,25 +40,6 @@ typedef union {
 } xenoprof_buf_t;
 #endif
 
-struct xenoprof_vcpu {
-    int event_size;
-    xenoprof_buf_t *buffer;
-};
-
-struct xenoprof {
-    char *rawbuf;
-    int npages;
-    int nbuf;
-    int bufsize;
-    int domain_type;
-    int domain_ready;
-    int is_primary;
-#ifdef CONFIG_COMPAT
-    int is_compat;
-#endif
-    struct xenoprof_vcpu *vcpu;
-};
-
 #ifndef CONFIG_COMPAT
 #define XENOPROF_COMPAT(x) 0
 #define xenoprof_buf(d, b, field) ACCESS_ONCE((b)->field)
Reviewed-by on v1 2/3
#9 lore raw

Re: [PATCH 2/3] xenoprof: drop unused struct xenoprof fields

Quoted 5 lines · Jan Beulich

Both is_primary and domain_ready are only ever written to. Drop both fields and restrict structure visibility to just the one involved CU. While doing so (and just for starters) make "is_compat" properly bool.

#10 lore raw

PATCH 3/3 xenoprof: limit scope of types and #define-s

In reply to Jan Beulich

Quite a few of the items are used by xenoprof.c only, so move them there to limit their visibility as well as the amount of re-building needed in case of changes. Also drop the inclusion of the public header there.

xen/arch/x86/oprofile/nmi_int.c +1 -1
@@ -19,7 +19,7 @@
 #include <xen/string.h>
 #include <xen/delay.h>
 #include <xen/xenoprof.h>
-#include <public/xen.h>
+#include <public/xenoprof.h>
 #include <asm/msr.h>
 #include <asm/apic.h>
 #include <asm/regs.h>
xen/common/xenoprof.c +26 -0
@@ -23,6 +23,32 @@
 #undef virt_to_mfn
 #define virt_to_mfn(va) _mfn(__virt_to_mfn(va))
 
+#define XENOPROF_DOMAIN_IGNORED    0
+#define XENOPROF_DOMAIN_ACTIVE     1
+#define XENOPROF_DOMAIN_PASSIVE    2
+
+#define XENOPROF_IDLE              0
+#define XENOPROF_INITIALIZED       1
+#define XENOPROF_COUNTERS_RESERVED 2
+#define XENOPROF_READY             3
+#define XENOPROF_PROFILING         4
+
+#ifndef CONFIG_COMPAT
+#define XENOPROF_COMPAT(x) false
+typedef struct xenoprof_buf xenoprof_buf_t;
+#define xenoprof_buf(d, b, field) ACCESS_ONCE((b)->field)
+#else
+#include <compat/xenoprof.h>
+#define XENOPROF_COMPAT(x) ((x)->is_compat)
+typedef union {
+    struct xenoprof_buf native;
+    struct compat_oprof_buf compat;
+} xenoprof_buf_t;
+#define xenoprof_buf(d, b, field) ACCESS_ONCE(*(!(d)->xenoprof->is_compat \
+                                                ? &(b)->native.field \
+                                                : &(b)->compat.field))
+#endif
+
 /* Limit amount of pages used for shared buffer (per domain) */
 #define MAX_OPROF_SHARED_PAGES 32
 
xen/xsm/flask/hooks.c +1 -0
@@ -29,6 +29,7 @@
 #include <public/platform.h>
 #include <public/version.h>
 #include <public/hvm/params.h>
+#include <public/xenoprof.h>
 #include <public/xsm/flask_op.h>
 
 #include <avc.h>
xen/include/asm-x86/xenoprof.h +2 -0
@@ -26,6 +26,8 @@ struct vcpu;
 
 #ifdef CONFIG_XENOPROF
 
+#include <public/xen.h>
+
 int nmi_reserve_counters(void);
 int nmi_setup_events(void);
 int nmi_enable_virq(void);
xen/include/xen/xenoprof.h +2 -31
@@ -11,7 +11,6 @@
 #define __XEN_XENOPROF_H__
 
 #include <xen/inttypes.h>
-#include <public/xenoprof.h>
 #include <asm/xenoprof.h>
 
 #define PMU_OWNER_NONE          0
@@ -20,37 +19,9 @@
 
 #ifdef CONFIG_XENOPROF
 
-#define XENOPROF_DOMAIN_IGNORED    0
-#define XENOPROF_DOMAIN_ACTIVE     1
-#define XENOPROF_DOMAIN_PASSIVE    2
-
-#define XENOPROF_IDLE              0
-#define XENOPROF_INITIALIZED       1
-#define XENOPROF_COUNTERS_RESERVED 2
-#define XENOPROF_READY             3
-#define XENOPROF_PROFILING         4
-
-#ifndef CONFIG_COMPAT
-typedef struct xenoprof_buf xenoprof_buf_t;
-#else
-#include <compat/xenoprof.h>
-typedef union {
-	struct xenoprof_buf native;
-	struct compat_oprof_buf compat;
-} xenoprof_buf_t;
-#endif
-
-#ifndef CONFIG_COMPAT
-#define XENOPROF_COMPAT(x) 0
-#define xenoprof_buf(d, b, field) ACCESS_ONCE((b)->field)
-#else
-#define XENOPROF_COMPAT(x) ((x)->is_compat)
-#define xenoprof_buf(d, b, field) ACCESS_ONCE(*(!(d)->xenoprof->is_compat \
-                                                ? &(b)->native.field \
-                                                : &(b)->compat.field))
-#endif
-
 struct domain;
+struct vcpu;
+struct cpu_user_regs;
 
 int acquire_pmu_ownership(int pmu_ownership);
 void release_pmu_ownership(int pmu_ownership);
Acked-by on v1 3/3
#11 lore raw

Re: [PATCH 3/3] xenoprof: limit scope of types and #define-s

Quoted 5 lines · Jan Beulich

Quite a few of the items are used by xenoprof.c only, so move them there to limit their visibility as well as the amount of re-building needed in case of changes. Also drop the inclusion of the public header there.

I haven't read this patch carefully, but what it does is a good thing in general and I trust our build test can catch any fallout from this sort of change, so: