Skip to contentSkip to conversation

pmstat interface fixes

PATCH Superseded Ross Lagerwall 3 participants 16 messages May 12, 2025 – May 27, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover pmstat interface fixes

Clarify and fix usage of the pmstat hypervisor and tools interfaces regarding sizes of buffers passed in.

Ross Lagerwall (5):
x86/pmstat: Check size of PMSTAT_get_pxstat buffers public/sysctl: Clarify usage of pm_{px,cx}_stat
cpufreq: Avoid potential buffer overrun and leak libxc/PM: Ensure pxstat buffers are correctly sized libxc/PM: Retry get_pxstat if data is incomplete

 tools/libs/ctrl/xc_pm.c       | 20 ++++++--------
 tools/misc/xenpm.c            | 48 ++++++++++++++++++++------------
 xen/drivers/acpi/pmstat.c     |  6 ++--
 xen/drivers/cpufreq/cpufreq.c |  4 ++-
 xen/include/public/sysctl.h   | 52 +++++++++++++++++++++++++++--------
 5 files changed, 87 insertions(+), 43 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH v2 1/5 x86/pmstat: Check size of PMSTAT_get_pxstat buffers

Check that the total number of states passed in and hence the size of buffers is sufficient to avoid writing more than the caller has allocated.

The interface is not explicit about whether getpx.total is expected to be set by the caller in this case but since it is always set in libxenctrl it seems reasonable to check it.

Fixes c06a7db0c547 ("X86 and IA64: Update cpufreq statistic logic for supporting both x86 and ia64")

---

In v2: Rather than erroring out, copy as much as possible.

 xen/drivers/acpi/pmstat.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
xen/drivers/acpi/pmstat.c +4 -2
index c51b9ca358c2..4736a84d1256 100644
@@ -103,8 +103,10 @@ int do_get_pm_info(struct xen_sysctl_get_pmstat *op)
 
         cpufreq_residency_update(op->cpuid, pxpt->u.cur);
 
-        ct = pmpt->perf.state_count;
-        if ( copy_to_guest(op->u.getpx.trans_pt, pxpt->u.trans_pt, ct*ct) )
+        ct = min_t(uint32_t, pmpt->perf.state_count, op->u.getpx.total);
+
+        if ( ct <= op->u.getpx.total &&
+             copy_to_guest(op->u.getpx.trans_pt, pxpt->u.trans_pt, ct * ct) )
         {
             spin_unlock(cpufreq_statistic_lock);
             ret = -EFAULT;
Signature · 1 line
2.49.0
#2 lore raw

Re: [PATCH v2 1/5] x86/pmstat: Check size of PMSTAT_get_pxstat buffers

Quoted 7 lines · Ross Lagerwall

Check that the total number of states passed in and hence the size of buffers is sufficient to avoid writing more than the caller has allocated.

The interface is not explicit about whether getpx.total is expected to be set by the caller in this case but since it is always set in libxenctrl it seems reasonable to check it.

Yet if we start checking the value, I think the public header should also be made say so (in a comment).

xen/drivers/acpi/pmstat.c int do_get_pm_info(struct xen_sysctl_get_pmstat *op) · line 106

Quoted 9 lines
xen/drivers/acpi/pmstat.c +1 -2
@@ -103,8 +103,10 @@ int do_get_pm_info(struct xen_sysctl_get_pmstat *op)
 
         cpufreq_residency_update(op->cpuid, pxpt->u.cur);
 
-        ct = pmpt->perf.state_count;
-        if ( copy_to_guest(op->u.getpx.trans_pt, pxpt->u.trans_pt, ct*ct) )
+        ct = min_t(uint32_t, pmpt->perf.state_count, op->u.getpx.total);

With this, ...

xen/drivers/acpi/pmstat.c int do_get_pm_info(struct xen_sysctl_get_pmstat *op) · line 108

Quoted 1 line
xen/drivers/acpi/pmstat.c +1 -0
+        if ( ct <= op->u.getpx.total &&

... this is going to be always true, isn't it? Which constitutes a violation of Misra rule 14.3.

Also it would be nice if the min_t() could become a normal min(), e.g. by "promoting" op->u.getpx.total to unsigned int via adding 0U. This way it's clear there's no hidden truncation (or else there might be an argument for keeping the check above).

xen/drivers/acpi/pmstat.c int do_get_pm_info(struct xen_sysctl_get_pmstat *op) · line 112

Quoted 4 lines
xen/drivers/acpi/pmstat.c +1 -0
+             copy_to_guest(op->u.getpx.trans_pt, pxpt->u.trans_pt, ct * ct) )
         {
             spin_unlock(cpufreq_statistic_lock);
             ret = -EFAULT;

Why would you constrain this copy-out but not the one just out of context below here? (The question is of course moot if the condition was dropped.)

Jan

#3 lore raw

Re: [PATCH v2 1/5] x86/pmstat: Check size of PMSTAT_get_pxstat buffers

Quoted 35 lines · Jan Beulich
Quoted 7 lines · Ross Lagerwall

Check that the total number of states passed in and hence the size of buffers is sufficient to avoid writing more than the caller has allocated.

The interface is not explicit about whether getpx.total is expected to be set by the caller in this case but since it is always set in libxenctrl it seems reasonable to check it.

Yet if we start checking the value, I think the public header should also be made say so (in a comment).

xen/drivers/acpi/pmstat.c int do_get_pm_info(struct xen_sysctl_get_pmstat *op) · line 106

Quoted 9 lines
xen/drivers/acpi/pmstat.c +1 -2
@@ -103,8 +103,10 @@ int do_get_pm_info(struct xen_sysctl_get_pmstat *op)
 
         cpufreq_residency_update(op->cpuid, pxpt->u.cur);
 
-        ct = pmpt->perf.state_count;
-        if ( copy_to_guest(op->u.getpx.trans_pt, pxpt->u.trans_pt, ct*ct) )
+        ct = min_t(uint32_t, pmpt->perf.state_count, op->u.getpx.total);

With this, ...

xen/drivers/acpi/pmstat.c int do_get_pm_info(struct xen_sysctl_get_pmstat *op) · line 108

Quoted 1 line
xen/drivers/acpi/pmstat.c +1 -0
+        if ( ct <= op->u.getpx.total &&

... this is going to be always true, isn't it? Which constitutes a violation of Misra rule 14.3.

Also it would be nice if the min_t() could become a normal min(), e.g. by "promoting" op->u.getpx.total to unsigned int via adding 0U. This way it's clear there's no hidden truncation (or else there might be an argument for keeping the check above).

Quoted 8 lines

xen/drivers/acpi/pmstat.c int do_get_pm_info(struct xen_sysctl_get_pmstat *op) · line 112

Quoted 4 lines
xen/drivers/acpi/pmstat.c +1 -0
+             copy_to_guest(op->u.getpx.trans_pt, pxpt->u.trans_pt, ct * ct) )
         {
             spin_unlock(cpufreq_statistic_lock);
             ret = -EFAULT;

Why would you constrain this copy-out but not the one just out of context below here? (The question is of course moot if the condition was dropped.)

Oh, I had intended this condition to be...

    if ( ct == op->u.getpx.total &&

... based on your previous comment about the difficulties of partially copying a 2d array.

Quoted 2 lines

An option may be to document that this array is copied only when the buffer is large enough.

I left the other alone since partially copying a 1d array makes sense.

If you would prefer, I can drop the condition and just let the caller deal with the partially copied 2d array?

Thanks,
Ross

#4 lore raw

Re: [PATCH v2 1/5] x86/pmstat: Check size of PMSTAT_get_pxstat buffers

Quoted 49 lines · Ross Lagerwall
Quoted 35 lines · Jan Beulich
Quoted 7 lines · Ross Lagerwall

Check that the total number of states passed in and hence the size of buffers is sufficient to avoid writing more than the caller has allocated.

The interface is not explicit about whether getpx.total is expected to be set by the caller in this case but since it is always set in libxenctrl it seems reasonable to check it.

Yet if we start checking the value, I think the public header should also be made say so (in a comment).

xen/drivers/acpi/pmstat.c int do_get_pm_info(struct xen_sysctl_get_pmstat *op) · line 106

Quoted 9 lines
xen/drivers/acpi/pmstat.c +1 -2
@@ -103,8 +103,10 @@ int do_get_pm_info(struct xen_sysctl_get_pmstat *op)
 
         cpufreq_residency_update(op->cpuid, pxpt->u.cur);
 
-        ct = pmpt->perf.state_count;
-        if ( copy_to_guest(op->u.getpx.trans_pt, pxpt->u.trans_pt, ct*ct) )
+        ct = min_t(uint32_t, pmpt->perf.state_count, op->u.getpx.total);

With this, ...

xen/drivers/acpi/pmstat.c int do_get_pm_info(struct xen_sysctl_get_pmstat *op) · line 108

Quoted 1 line
xen/drivers/acpi/pmstat.c +1 -0
+        if ( ct <= op->u.getpx.total &&

... this is going to be always true, isn't it? Which constitutes a violation of Misra rule 14.3.

Also it would be nice if the min_t() could become a normal min(), e.g. by "promoting" op->u.getpx.total to unsigned int via adding 0U. This way it's clear there's no hidden truncation (or else there might be an argument for keeping the check above).

Quoted 8 lines

xen/drivers/acpi/pmstat.c int do_get_pm_info(struct xen_sysctl_get_pmstat *op) · line 112

Quoted 4 lines
xen/drivers/acpi/pmstat.c +1 -0
+             copy_to_guest(op->u.getpx.trans_pt, pxpt->u.trans_pt, ct * ct) )
         {
             spin_unlock(cpufreq_statistic_lock);
             ret = -EFAULT;

Why would you constrain this copy-out but not the one just out of context below here? (The question is of course moot if the condition was dropped.)

Oh, I had intended this condition to be...

    if ( ct == op->u.getpx.total &&
Quoted 7 lines

... based on your previous comment about the difficulties of partially copying a 2d array.

Quoted 2 lines

An option may be to document that this array is copied only when the buffer is large enough.

I left the other alone since partially copying a 1d array makes sense.

Right, if the relation there becomes == it indeed reflects that the 2-D one is different in this regard from the 1-D one.

Quoted 2 lines

If you would prefer, I can drop the condition and just let the caller deal with the partially copied 2d array?

With the adjusted relation I think all is going to be fine as you (otherwise) had it. There may want to be a brief comment on that extra condition you add.

Jan

#5 lore raw

PATCH v2 2/5 public/sysctl: Clarify usage of pm_{px,cx}_stat

In reply to Ross Lagerwall

---

* New in v2.

 xen/include/public/sysctl.h | 52 ++++++++++++++++++++++++++++---------
 1 file changed, 40 insertions(+), 12 deletions(-)
xen/include/public/sysctl.h +40 -12
index b0fec271d36f..449f0161920a 100644
@@ -215,23 +215,51 @@ typedef struct pm_px_val pm_px_val_t;
 DEFINE_XEN_GUEST_HANDLE(pm_px_val_t);
 
 struct pm_px_stat {
-    uint8_t total;        /* total Px states */
-    uint8_t usable;       /* usable Px states */
-    uint8_t last;         /* last Px state */
-    uint8_t cur;          /* current Px state */
-    XEN_GUEST_HANDLE_64(uint64) trans_pt;   /* Px transition table */
+    /*
+     * IN: Number of elements in pt, number of rows/columns in trans_pt
+     *     (PMSTAT_get_pxstat)
+     * OUT: total Px states (PMSTAT_get_max_px, PMSTAT_get_pxstat)
+     */
+    uint8_t total;
+    uint8_t usable;       /* OUT: usable Px states (PMSTAT_get_pxstat) */
+    uint8_t last;         /* OUT: last Px state (PMSTAT_get_pxstat) */
+    uint8_t cur;          /* OUT: current Px state (PMSTAT_get_pxstat) */
+    /*
+     * OUT: Px transition table. This should have total * total elements.
+     *      This will not be copied if it is smaller than the hypervisor's
+     *      Px transition table. (PMSTAT_get_pxstat)
+     */
+    XEN_GUEST_HANDLE_64(uint64) trans_pt;
+    /* OUT: This should have total elements (PMSTAT_get_pxstat) */
     XEN_GUEST_HANDLE_64(pm_px_val_t) pt;
 };
 
 struct pm_cx_stat {
-    uint32_t nr;    /* entry nr in triggers & residencies, including C0 */
-    uint32_t last;  /* last Cx state */
-    uint64_aligned_t idle_time;                 /* idle time from boot */
-    XEN_GUEST_HANDLE_64(uint64) triggers;    /* Cx trigger counts */
-    XEN_GUEST_HANDLE_64(uint64) residencies; /* Cx residencies */
-    uint32_t nr_pc;                          /* entry nr in pc[] */
-    uint32_t nr_cc;                          /* entry nr in cc[] */
     /*
+     * IN:  Number of elements in triggers, residencies (PMSTAT_get_cxstat)
+     * OUT: entry nr in triggers & residencies, including C0
+     *      (PMSTAT_get_cxstat, PMSTAT_get_max_cx)
+     */
+    uint32_t nr;
+    uint32_t last;  /* OUT: last Cx state (PMSTAT_get_cxstat) */
+    /* OUT: idle time from boot (PMSTAT_get_cxstat)*/
+    uint64_aligned_t idle_time;
+    /* OUT: Cx trigger counts, nr elements (PMSTAT_get_cxstat) */
+    XEN_GUEST_HANDLE_64(uint64) triggers;
+    /* OUT: Cx residencies, nr elements (PMSTAT_get_cxstat) */
+    XEN_GUEST_HANDLE_64(uint64) residencies;
+    /*
+     * IN: entry nr in pc[] (PMSTAT_get_cxstat)
+     * OUT: Index of highest non-zero entry set in pc[] (PMSTAT_get_cxstat)
+     */
+    uint32_t nr_pc;
+    /*
+     * IN: entry nr in cc[] (PMSTAT_get_cxstat)
+     * OUT: Index of highest non-zero entry set in cc[] (PMSTAT_get_cxstat)
+     */
+    uint32_t nr_cc;
+    /*
+     * OUT: (PMSTAT_get_cxstat)
      * These two arrays may (and generally will) have unused slots; slots not
      * having a corresponding hardware register will not be written by the
      * hypervisor. It is therefore up to the caller to put a suitable sentinel
Signature · 1 line
2.49.0
#6 lore raw

Re: [PATCH v2 2/5] public/sysctl: Clarify usage of pm_{px,cx}_stat

Quoted 7 lines · Ross Lagerwall
xen/include/public/sysctl.h +0 -1
@@ -215,23 +215,51 @@ typedef struct pm_px_val pm_px_val_t;
 DEFINE_XEN_GUEST_HANDLE(pm_px_val_t);
 
 struct pm_px_stat {
-    uint8_t total;        /* total Px states */

xen/include/public/sysctl.h typedef struct pm_px_val pm_px_val_t; · line 223

Quoted 10 lines
xen/include/public/sysctl.h +6 -4
-    uint8_t usable;       /* usable Px states */
-    uint8_t last;         /* last Px state */
-    uint8_t cur;          /* current Px state */
-    XEN_GUEST_HANDLE_64(uint64) trans_pt;   /* Px transition table */
+    /*
+     * IN: Number of elements in pt, number of rows/columns in trans_pt
+     *     (PMSTAT_get_pxstat)
+     * OUT: total Px states (PMSTAT_get_max_px, PMSTAT_get_pxstat)
+     */
+    uint8_t total;

The part for this field ought to go in patch 1, as already indicated there.

xen/include/public/sysctl.h typedef struct pm_px_val pm_px_val_t; · line 234

Quoted 11 lines
xen/include/public/sysctl.h +10 -0
+    uint8_t usable;       /* OUT: usable Px states (PMSTAT_get_pxstat) */
+    uint8_t last;         /* OUT: last Px state (PMSTAT_get_pxstat) */
+    uint8_t cur;          /* OUT: current Px state (PMSTAT_get_pxstat) */
+    /*
+     * OUT: Px transition table. This should have total * total elements.
+     *      This will not be copied if it is smaller than the hypervisor's
+     *      Px transition table. (PMSTAT_get_pxstat)
+     */
+    XEN_GUEST_HANDLE_64(uint64) trans_pt;
+    /* OUT: This should have total elements (PMSTAT_get_pxstat) */
     XEN_GUEST_HANDLE_64(pm_px_val_t) pt;

As also indicated there, the same constraint as for trans_pt applies to this output buffer, just that it's having only one dimension.

Quoted 22 lines
xen/include/public/sysctl.h +11 -7
 };
 
 struct pm_cx_stat {
-    uint32_t nr;    /* entry nr in triggers & residencies, including C0 */
-    uint32_t last;  /* last Cx state */
-    uint64_aligned_t idle_time;                 /* idle time from boot */
-    XEN_GUEST_HANDLE_64(uint64) triggers;    /* Cx trigger counts */
-    XEN_GUEST_HANDLE_64(uint64) residencies; /* Cx residencies */
-    uint32_t nr_pc;                          /* entry nr in pc[] */
-    uint32_t nr_cc;                          /* entry nr in cc[] */
     /*
+     * IN:  Number of elements in triggers, residencies (PMSTAT_get_cxstat)
+     * OUT: entry nr in triggers & residencies, including C0
+     *      (PMSTAT_get_cxstat, PMSTAT_get_max_cx)
+     */
+    uint32_t nr;
+    uint32_t last;  /* OUT: last Cx state (PMSTAT_get_cxstat) */
+    /* OUT: idle time from boot (PMSTAT_get_cxstat)*/
+    uint64_aligned_t idle_time;
+    /* OUT: Cx trigger counts, nr elements (PMSTAT_get_cxstat) */
+    XEN_GUEST_HANDLE_64(uint64) triggers;
+    /* OUT: Cx residencies, nr elements (PMSTAT_get_cxstat) */

xen/include/public/sysctl.h typedef struct pm_px_val pm_px_val_t; · line 259

Quoted 10 lines
xen/include/public/sysctl.h +10 -0
+    XEN_GUEST_HANDLE_64(uint64) residencies;
+    /*
+     * IN: entry nr in pc[] (PMSTAT_get_cxstat)
+     * OUT: Index of highest non-zero entry set in pc[] (PMSTAT_get_cxstat)
+     */
+    uint32_t nr_pc;
+    /*
+     * IN: entry nr in cc[] (PMSTAT_get_cxstat)
+     * OUT: Index of highest non-zero entry set in cc[] (PMSTAT_get_cxstat)
+     */

For both of these, it's not "highest non-zero" but, according to ...

xen/include/public/sysctl.h typedef struct pm_px_val pm_px_val_t; · line 265

Quoted 6 lines
xen/include/public/sysctl.h +3 -0
+    uint32_t nr_cc;
+    /*
+     * OUT: (PMSTAT_get_cxstat)
      * These two arrays may (and generally will) have unused slots; slots not
      * having a corresponding hardware register will not be written by the
      * hypervisor. It is therefore up to the caller to put a suitable sentinel

... this comment, "highest written by hypervisor". They're also not "index of", but "one higher than the index of" (i.e. counts, not indexes).

Jan

#7 lore raw

Re: [PATCH v2 2/5] public/sysctl: Clarify usage of pm_{px,cx}_stat

Quoted 79 lines · Jan Beulich
Quoted 7 lines · Ross Lagerwall
xen/include/public/sysctl.h +0 -1
@@ -215,23 +215,51 @@ typedef struct pm_px_val pm_px_val_t;
 DEFINE_XEN_GUEST_HANDLE(pm_px_val_t);
 
 struct pm_px_stat {
-    uint8_t total;        /* total Px states */

xen/include/public/sysctl.h typedef struct pm_px_val pm_px_val_t; · line 223

Quoted 10 lines
xen/include/public/sysctl.h +6 -4
-    uint8_t usable;       /* usable Px states */
-    uint8_t last;         /* last Px state */
-    uint8_t cur;          /* current Px state */
-    XEN_GUEST_HANDLE_64(uint64) trans_pt;   /* Px transition table */
+    /*
+     * IN: Number of elements in pt, number of rows/columns in trans_pt
+     *     (PMSTAT_get_pxstat)
+     * OUT: total Px states (PMSTAT_get_max_px, PMSTAT_get_pxstat)
+     */
+    uint8_t total;

The part for this field ought to go in patch 1, as already indicated there.

xen/include/public/sysctl.h typedef struct pm_px_val pm_px_val_t; · line 234

Quoted 11 lines
xen/include/public/sysctl.h +10 -0
+    uint8_t usable;       /* OUT: usable Px states (PMSTAT_get_pxstat) */
+    uint8_t last;         /* OUT: last Px state (PMSTAT_get_pxstat) */
+    uint8_t cur;          /* OUT: current Px state (PMSTAT_get_pxstat) */
+    /*
+     * OUT: Px transition table. This should have total * total elements.
+     *      This will not be copied if it is smaller than the hypervisor's
+     *      Px transition table. (PMSTAT_get_pxstat)
+     */
+    XEN_GUEST_HANDLE_64(uint64) trans_pt;
+    /* OUT: This should have total elements (PMSTAT_get_pxstat) */
     XEN_GUEST_HANDLE_64(pm_px_val_t) pt;

As also indicated there, the same constraint as for trans_pt applies to this output buffer, just that it's having only one dimension.

Quoted 22 lines
xen/include/public/sysctl.h +11 -7
 };
 
 struct pm_cx_stat {
-    uint32_t nr;    /* entry nr in triggers & residencies, including C0 */
-    uint32_t last;  /* last Cx state */
-    uint64_aligned_t idle_time;                 /* idle time from boot */
-    XEN_GUEST_HANDLE_64(uint64) triggers;    /* Cx trigger counts */
-    XEN_GUEST_HANDLE_64(uint64) residencies; /* Cx residencies */
-    uint32_t nr_pc;                          /* entry nr in pc[] */
-    uint32_t nr_cc;                          /* entry nr in cc[] */
     /*
+     * IN:  Number of elements in triggers, residencies (PMSTAT_get_cxstat)
+     * OUT: entry nr in triggers & residencies, including C0
+     *      (PMSTAT_get_cxstat, PMSTAT_get_max_cx)
+     */
+    uint32_t nr;
+    uint32_t last;  /* OUT: last Cx state (PMSTAT_get_cxstat) */
+    /* OUT: idle time from boot (PMSTAT_get_cxstat)*/
+    uint64_aligned_t idle_time;
+    /* OUT: Cx trigger counts, nr elements (PMSTAT_get_cxstat) */
+    XEN_GUEST_HANDLE_64(uint64) triggers;
+    /* OUT: Cx residencies, nr elements (PMSTAT_get_cxstat) */

xen/include/public/sysctl.h typedef struct pm_px_val pm_px_val_t; · line 259

Quoted 10 lines
xen/include/public/sysctl.h +10 -0
+    XEN_GUEST_HANDLE_64(uint64) residencies;
+    /*
+     * IN: entry nr in pc[] (PMSTAT_get_cxstat)
+     * OUT: Index of highest non-zero entry set in pc[] (PMSTAT_get_cxstat)
+     */
+    uint32_t nr_pc;
+    /*
+     * IN: entry nr in cc[] (PMSTAT_get_cxstat)
+     * OUT: Index of highest non-zero entry set in cc[] (PMSTAT_get_cxstat)
+     */

For both of these, it's not "highest non-zero" but, according to ...

xen/include/public/sysctl.h typedef struct pm_px_val pm_px_val_t; · line 265

Quoted 6 lines
xen/include/public/sysctl.h +3 -0
+    uint32_t nr_cc;
+    /*
+     * OUT: (PMSTAT_get_cxstat)
      * These two arrays may (and generally will) have unused slots; slots not
      * having a corresponding hardware register will not be written by the
      * hypervisor. It is therefore up to the caller to put a suitable sentinel
Quoted 3 lines

... this comment, "highest written by hypervisor". They're also not "index of", but "one higher than the index of" (i.e. counts, not indexes).

Looking at this again, I don't think that matches what Xen does (nor does my previous attempt). The code in question:

#define PUT_xC(what, n) do { \
        if ( stat->nr_##what >= n && \
             copy_to_guest_offset(stat->what, n - 1, &hw_res.what##n, 1) ) \
            return -EFAULT; \
        if ( hw_res.what##n ) \
            nr_##what = n; \
    } while ( 0 )

Xen will copy all the hardware registers that it knows about (regardless of whether the hardware actually has them) and will return in nr_pc / nr_cc the index + 1 of the highest non-zero entry it _would_ have written if there is sufficient space.

I could describe it simply as "OUT: Required size of cc[]" ?

Thanks,
Ross

#8 lore raw

Re: [PATCH v2 2/5] public/sysctl: Clarify usage of pm_{px,cx}_stat

Quoted 87 lines · Ross Lagerwall
Quoted 79 lines · Jan Beulich
Quoted 7 lines · Ross Lagerwall
xen/include/public/sysctl.h +0 -1
@@ -215,23 +215,51 @@ typedef struct pm_px_val pm_px_val_t;
 DEFINE_XEN_GUEST_HANDLE(pm_px_val_t);
 
 struct pm_px_stat {
-    uint8_t total;        /* total Px states */

xen/include/public/sysctl.h typedef struct pm_px_val pm_px_val_t; · line 223

Quoted 10 lines
xen/include/public/sysctl.h +6 -4
-    uint8_t usable;       /* usable Px states */
-    uint8_t last;         /* last Px state */
-    uint8_t cur;          /* current Px state */
-    XEN_GUEST_HANDLE_64(uint64) trans_pt;   /* Px transition table */
+    /*
+     * IN: Number of elements in pt, number of rows/columns in trans_pt
+     *     (PMSTAT_get_pxstat)
+     * OUT: total Px states (PMSTAT_get_max_px, PMSTAT_get_pxstat)
+     */
+    uint8_t total;

The part for this field ought to go in patch 1, as already indicated there.

xen/include/public/sysctl.h typedef struct pm_px_val pm_px_val_t; · line 234

Quoted 11 lines
xen/include/public/sysctl.h +10 -0
+    uint8_t usable;       /* OUT: usable Px states (PMSTAT_get_pxstat) */
+    uint8_t last;         /* OUT: last Px state (PMSTAT_get_pxstat) */
+    uint8_t cur;          /* OUT: current Px state (PMSTAT_get_pxstat) */
+    /*
+     * OUT: Px transition table. This should have total * total elements.
+     *      This will not be copied if it is smaller than the hypervisor's
+     *      Px transition table. (PMSTAT_get_pxstat)
+     */
+    XEN_GUEST_HANDLE_64(uint64) trans_pt;
+    /* OUT: This should have total elements (PMSTAT_get_pxstat) */
     XEN_GUEST_HANDLE_64(pm_px_val_t) pt;

As also indicated there, the same constraint as for trans_pt applies to this output buffer, just that it's having only one dimension.

Quoted 22 lines
xen/include/public/sysctl.h +11 -7
 };
 
 struct pm_cx_stat {
-    uint32_t nr;    /* entry nr in triggers & residencies, including C0 */
-    uint32_t last;  /* last Cx state */
-    uint64_aligned_t idle_time;                 /* idle time from boot */
-    XEN_GUEST_HANDLE_64(uint64) triggers;    /* Cx trigger counts */
-    XEN_GUEST_HANDLE_64(uint64) residencies; /* Cx residencies */
-    uint32_t nr_pc;                          /* entry nr in pc[] */
-    uint32_t nr_cc;                          /* entry nr in cc[] */
     /*
+     * IN:  Number of elements in triggers, residencies (PMSTAT_get_cxstat)
+     * OUT: entry nr in triggers & residencies, including C0
+     *      (PMSTAT_get_cxstat, PMSTAT_get_max_cx)
+     */
+    uint32_t nr;
+    uint32_t last;  /* OUT: last Cx state (PMSTAT_get_cxstat) */
+    /* OUT: idle time from boot (PMSTAT_get_cxstat)*/
+    uint64_aligned_t idle_time;
+    /* OUT: Cx trigger counts, nr elements (PMSTAT_get_cxstat) */
+    XEN_GUEST_HANDLE_64(uint64) triggers;
+    /* OUT: Cx residencies, nr elements (PMSTAT_get_cxstat) */

xen/include/public/sysctl.h typedef struct pm_px_val pm_px_val_t; · line 259

Quoted 10 lines
xen/include/public/sysctl.h +10 -0
+    XEN_GUEST_HANDLE_64(uint64) residencies;
+    /*
+     * IN: entry nr in pc[] (PMSTAT_get_cxstat)
+     * OUT: Index of highest non-zero entry set in pc[] (PMSTAT_get_cxstat)
+     */
+    uint32_t nr_pc;
+    /*
+     * IN: entry nr in cc[] (PMSTAT_get_cxstat)
+     * OUT: Index of highest non-zero entry set in cc[] (PMSTAT_get_cxstat)
+     */

For both of these, it's not "highest non-zero" but, according to ...

xen/include/public/sysctl.h typedef struct pm_px_val pm_px_val_t; · line 265

Quoted 6 lines
xen/include/public/sysctl.h +3 -0
+    uint32_t nr_cc;
+    /*
+     * OUT: (PMSTAT_get_cxstat)
      * These two arrays may (and generally will) have unused slots; slots not
      * having a corresponding hardware register will not be written by the
      * hypervisor. It is therefore up to the caller to put a suitable sentinel
Quoted 3 lines

... this comment, "highest written by hypervisor". They're also not "index of", but "one higher than the index of" (i.e. counts, not indexes).

Looking at this again, I don't think that matches what Xen does (nor does my previous attempt). The code in question:

Quoted 7 lines
#define PUT_xC(what, n) do { \
        if ( stat->nr_##what >= n && \
             copy_to_guest_offset(stat->what, n - 1, &hw_res.what##n, 1) ) \
            return -EFAULT; \
        if ( hw_res.what##n ) \
            nr_##what = n; \
    } while ( 0 )

Right. I should have inserted "that could be" in my reply.

Quoted 4 lines

Xen will copy all the hardware registers that it knows about (regardless of whether the hardware actually has them) and will return in nr_pc / nr_cc the index + 1 of the highest non-zero entry it _would_ have written if there is sufficient space.

Which is kind of bogus when the last (or more) of those would merely be zero.

Quoted 1 line

I could describe it simply as "OUT: Required size of cc[]" ?

Maybe append something like "..., for all known to Xen entries to be written"? Provided we don't want to change the behavior anyway.

Jan

#9 lore raw

PATCH v2 3/5 cpufreq: Avoid potential buffer overrun and leak

In reply to Ross Lagerwall

If set_px_pminfo is called a second time with a larger state_count than the first call, calls to PMSTAT_get_pxstat will read beyond the end of the pt and trans_pt buffers allocated in cpufreq_statistic_init() since they would have been allocated with the original state_count.

Secondly, the states array leaks on each subsequent call of set_px_pminfo.

As far as I know, there is no valid reason to call set_px_pminfo multiple times for the same CPU so fix both these issues by disallowing it.

At the same time, fix a leak of the states array on error.

---

* New in v2.

 xen/drivers/cpufreq/cpufreq.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
xen/drivers/cpufreq/cpufreq.c +3 -1
index 19e29923356a..bf65403ff50b 100644
@@ -520,7 +520,7 @@ int set_px_pminfo(uint32_t acpi_id, struct xen_processor_performance *perf)
     if ( perf->flags & XEN_PX_PSS )
     {
         /* capability check */
-        if ( perf->state_count <= 1 )
+        if ( perf->state_count <= 1 || pxpt->states )
         {
             ret = -EINVAL;
             goto out;
@@ -534,6 +534,8 @@ int set_px_pminfo(uint32_t acpi_id, struct xen_processor_performance *perf)
         }
         if ( copy_from_guest(pxpt->states, perf->states, perf->state_count) )
         {
+            xfree(pxpt->states);
+            pxpt->states = NULL;
             ret = -EFAULT;
             goto out;
         }
Signature · 1 line
2.49.0
#10 lore raw

Re: [PATCH v2 3/5] cpufreq: Avoid potential buffer overrun and leak

Quoted 11 lines · Ross Lagerwall

If set_px_pminfo is called a second time with a larger state_count than the first call, calls to PMSTAT_get_pxstat will read beyond the end of the pt and trans_pt buffers allocated in cpufreq_statistic_init() since they would have been allocated with the original state_count.

Secondly, the states array leaks on each subsequent call of set_px_pminfo.

As far as I know, there is no valid reason to call set_px_pminfo multiple times for the same CPU so fix both these issues by disallowing it.

Iirc it's the processor driver in Linux which would invoke this upon being loaded. It can be unloaded and loaded again. Will it ignore the errors in such a case? As suggested to Penny for some of her work in this area, it may be better to return success instead, to avoid the need for following bad practice in drivers by ignoring errors.

xen/drivers/cpufreq/cpufreq.c int set_px_pminfo(uint32_t acpi_id, struct xen_processor_performance *perf) · line 523

Quoted 8 lines
xen/drivers/cpufreq/cpufreq.c +1 -1
@@ -520,7 +520,7 @@ int set_px_pminfo(uint32_t acpi_id, struct xen_processor_performance *perf)
     if ( perf->flags & XEN_PX_PSS )
     {
         /* capability check */
-        if ( perf->state_count <= 1 )
+        if ( perf->state_count <= 1 || pxpt->states )

Even without the remark above, there probably would want to be two separate if()s, each with a distinctive comment. The comment that's there would go partly stale by the change you suggest. Or perhaps the extra condition could move (inverted) into the outer if()'s clause.

xen/drivers/cpufreq/cpufreq.c int set_px_pminfo(uint32_t acpi_id, struct xen_processor_performance *perf) · line 538

Quoted 9 lines
xen/drivers/cpufreq/cpufreq.c +2 -0
         {
             ret = -EINVAL;
             goto out;
@@ -534,6 +534,8 @@ int set_px_pminfo(uint32_t acpi_id, struct xen_processor_performance *perf)
         }
         if ( copy_from_guest(pxpt->states, perf->states, perf->state_count) )
         {
+            xfree(pxpt->states);
+            pxpt->states = NULL;

Please avoid open-coding XFREE().

Further related to the earlier comment: Beyond the processing of PSS there's more processing below here. If the PSS part succeeded and some later part failed, it may actually be necessary to invoke this operation again. I.e. even more so relevant that it won't fail just because PSS was already processed.

Jan

#11 lore raw

PATCH v2 4/5 libxc/PM: Ensure pxstat buffers are correctly sized

In reply to Ross Lagerwall

xc_pm_get_pxstat() requires the caller to allocate the pt and trans_pt buffers but then calls xc_pm_get_max_px() to determine how big they are (and hence how much Xen will copy into them). This is susceptible to races if xc_pm_get_max_px() changes so avoid the problem by requiring the caller to also pass in the size of the buffers.

---

* New in v2.

 tools/libs/ctrl/xc_pm.c | 20 +++++++++-----------
 tools/misc/xenpm.c      |  1 +
 2 files changed, 10 insertions(+), 11 deletions(-)
tools/libs/ctrl/xc_pm.c +9 -11
index ff7b5ada053f..cffbd1b8a955 100644
@@ -46,35 +46,33 @@ int xc_pm_get_pxstat(xc_interface *xch, int cpuid, struct xc_px_stat *pxpt)
 {
     struct xen_sysctl sysctl = {};
     /* Sizes unknown until xc_pm_get_max_px */
-    DECLARE_NAMED_HYPERCALL_BOUNCE(trans, pxpt->trans_pt, 0, XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
-    DECLARE_NAMED_HYPERCALL_BOUNCE(pt, pxpt->pt, 0, XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
+    DECLARE_NAMED_HYPERCALL_BOUNCE(trans, pxpt->trans_pt,
+                                   pxpt->total * pxpt->total,
+                                   XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
+    DECLARE_NAMED_HYPERCALL_BOUNCE(pt, pxpt->pt, pxpt->total,
+                                   XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
 
-    int max_px, ret;
+    int ret;
 
     if ( !pxpt->trans_pt || !pxpt->pt )
     {
         errno = EINVAL;
         return -1;
     }
-    if ( (ret = xc_pm_get_max_px(xch, cpuid, &max_px)) != 0)
-        return ret;
-
-    HYPERCALL_BOUNCE_SET_SIZE(trans, max_px * max_px * sizeof(uint64_t));
-    HYPERCALL_BOUNCE_SET_SIZE(pt, max_px * sizeof(struct xc_px_val));
 
     if ( xc_hypercall_bounce_pre(xch, trans) )
-        return ret;
+        return -1;
 
     if ( xc_hypercall_bounce_pre(xch, pt) )
     {
         xc_hypercall_bounce_post(xch, trans);
-        return ret;
+        return -1;
     }
 
     sysctl.cmd = XEN_SYSCTL_get_pmstat;
     sysctl.u.get_pmstat.type = PMSTAT_get_pxstat;
     sysctl.u.get_pmstat.cpuid = cpuid;
-    sysctl.u.get_pmstat.u.getpx.total = max_px;
+    sysctl.u.get_pmstat.u.getpx.total = pxpt->total;
     set_xen_guest_handle(sysctl.u.get_pmstat.u.getpx.trans_pt, trans);
     set_xen_guest_handle(sysctl.u.get_pmstat.u.getpx.pt, pt);
 
tools/misc/xenpm.c +1 -0
index db658ebaddd5..de319329e6b0 100644
@@ -319,6 +319,7 @@ static int get_pxstat_by_cpuid(xc_interface *xc_handle, int cpuid, struct xc_px_
     if ( !pxstat)
         return -EINVAL;
 
+    pxstat->total = max_px_num;
     pxstat->trans_pt = malloc(max_px_num * max_px_num *
                               sizeof(uint64_t));
     if ( !pxstat->trans_pt )
Signature · 1 line
2.49.0
#12 lore raw

Re: [PATCH v2 4/5] libxc/PM: Ensure pxstat buffers are correctly sized

tools/libs/ctrl/xc_pm.c int xc_pm_get_pxstat(xc_interface *xch, int cpuid, struct xc_px_stat *pxpt) · line 48

Quoted 8 lines · Ross Lagerwall
tools/libs/ctrl/xc_pm.c +0 -0
index ff7b5ada053f..cffbd1b8a955 100644
@@ -46,35 +46,33 @@ int xc_pm_get_pxstat(xc_interface *xch, int cpuid, struct xc_px_stat *pxpt)
 {
     struct xen_sysctl sysctl = {};
     /* Sizes unknown until xc_pm_get_max_px */

This comment can be removed now.

tools/libs/ctrl/xc_pm.c int xc_pm_get_pxstat(xc_interface *xch, int cpuid, struct xc_px_stat *pxpt) · line 53

Quoted 7 lines
tools/libs/ctrl/xc_pm.c +5 -2
-    DECLARE_NAMED_HYPERCALL_BOUNCE(trans, pxpt->trans_pt, 0, XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
-    DECLARE_NAMED_HYPERCALL_BOUNCE(pt, pxpt->pt, 0, XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
+    DECLARE_NAMED_HYPERCALL_BOUNCE(trans, pxpt->trans_pt,
+                                   pxpt->total * pxpt->total,
+                                   XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
+    DECLARE_NAMED_HYPERCALL_BOUNCE(pt, pxpt->pt, pxpt->total,
+                                   XC_HYPERCALL_BUFFER_BOUNCE_BOTH);

I don't think the macro takes the sizeof(*pt) or sizeof(*trans_pt) into account when using the size provided. So it doesn't looks like you can use pxpt->total alone, and you still need to multiple it by sizeof(*) like it was done in the HYPERCALL_BOUNCE_SET_SIZE() call.

[...]

tools/libs/ctrl/xc_pm.c int xc_pm_get_pxstat(xc_interface *xch, int cpuid, struct xc_px_stat *pxpt) · line 63

Quoted 2 lines
tools/libs/ctrl/xc_pm.c +0 -2
-    HYPERCALL_BOUNCE_SET_SIZE(trans, max_px * max_px * sizeof(uint64_t));
-    HYPERCALL_BOUNCE_SET_SIZE(pt, max_px * sizeof(struct xc_px_val));

Thanks,

Signature · 1 line
Anthony PERARD
#13 lore raw

Re: [PATCH v2 4/5] libxc/PM: Ensure pxstat buffers are correctly sized

Quoted 21 lines · Anthony PERARD

tools/libs/ctrl/xc_pm.c int xc_pm_get_pxstat(xc_interface *xch, int cpuid, struct xc_px_stat *pxpt) · line 48

Quoted 8 lines · Ross Lagerwall
tools/libs/ctrl/xc_pm.c +0 -0
index ff7b5ada053f..cffbd1b8a955 100644
@@ -46,35 +46,33 @@ int xc_pm_get_pxstat(xc_interface *xch, int cpuid, struct xc_px_stat *pxpt)
 {
     struct xen_sysctl sysctl = {};
     /* Sizes unknown until xc_pm_get_max_px */

This comment can be removed now.

tools/libs/ctrl/xc_pm.c int xc_pm_get_pxstat(xc_interface *xch, int cpuid, struct xc_px_stat *pxpt) · line 53

Quoted 7 lines
tools/libs/ctrl/xc_pm.c +5 -2
-    DECLARE_NAMED_HYPERCALL_BOUNCE(trans, pxpt->trans_pt, 0, XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
-    DECLARE_NAMED_HYPERCALL_BOUNCE(pt, pxpt->pt, 0, XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
+    DECLARE_NAMED_HYPERCALL_BOUNCE(trans, pxpt->trans_pt,
+                                   pxpt->total * pxpt->total,
+                                   XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
+    DECLARE_NAMED_HYPERCALL_BOUNCE(pt, pxpt->pt, pxpt->total,
+                                   XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
Quoted 4 lines

I don't think the macro takes the sizeof(*pt) or sizeof(*trans_pt) into account when using the size provided. So it doesn't looks like you can use pxpt->total alone, and you still need to multiple it by sizeof(*) like it was done in the HYPERCALL_BOUNCE_SET_SIZE() call.

Indeed I realized this when testing a v3 of this series. I'll send a new version now.

Thanks,
Ross

#14 lore raw

PATCH v2 5/5 libxc/PM: Retry get_pxstat if data is incomplete

In reply to Ross Lagerwall

If the total returned by Xen is more than the number of elements allocated, it means that the buffer was too small and so the data is incomplete. Retry to get all the data.

---

* New in v2.

 tools/misc/xenpm.c | 49 +++++++++++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 18 deletions(-)
tools/misc/xenpm.c +31 -18
index de319329e6b0..d5387f5f0693 100644
@@ -312,29 +312,42 @@ static int get_pxstat_by_cpuid(xc_interface *xc_handle, int cpuid, struct xc_px_
     int ret = 0;
     int max_px_num = 0;
 
-    ret = xc_pm_get_max_px(xc_handle, cpuid, &max_px_num);
-    if ( ret )
-        return -errno;
-
     if ( !pxstat)
         return -EINVAL;
 
-    pxstat->total = max_px_num;
-    pxstat->trans_pt = malloc(max_px_num * max_px_num *
-                              sizeof(uint64_t));
-    if ( !pxstat->trans_pt )
-        return -ENOMEM;
-    pxstat->pt = malloc(max_px_num * sizeof(struct xc_px_val));
-    if ( !pxstat->pt )
+    for ( ; ; )
     {
-        free(pxstat->trans_pt);
-        return -ENOMEM;
-    }
+        ret = xc_pm_get_max_px(xc_handle, cpuid, &max_px_num);
+        if ( ret )
+            return -errno;
 
-    ret = xc_pm_get_pxstat(xc_handle, cpuid, pxstat);
-    if( ret )
-    {
-        ret = -errno;
+        pxstat->total = max_px_num;
+        pxstat->trans_pt = malloc(max_px_num * max_px_num *
+                                  sizeof(uint64_t));
+        if ( !pxstat->trans_pt )
+            return -ENOMEM;
+        pxstat->pt = malloc(max_px_num * sizeof(struct xc_px_val));
+        if ( !pxstat->pt )
+        {
+            free(pxstat->trans_pt);
+            return -ENOMEM;
+        }
+
+        ret = xc_pm_get_pxstat(xc_handle, cpuid, pxstat);
+        if ( ret )
+        {
+            ret = -errno;
+            free(pxstat->trans_pt);
+            free(pxstat->pt);
+            pxstat->trans_pt = NULL;
+            pxstat->pt = NULL;
+            break;
+        }
+
+        if ( pxstat->total <= max_px_num )
+            break;
+
+        /* get_max_px changed under our feet so the data is incomplete. */
         free(pxstat->trans_pt);
         free(pxstat->pt);
         pxstat->trans_pt = NULL;
Signature · 1 line
2.49.0
Reviewed-by on v2 5/5
#15 lore raw

Re: [PATCH v2 5/5] libxc/PM: Retry get_pxstat if data is incomplete

Quoted 5 lines · Ross Lagerwall

If the total returned by Xen is more than the number of elements allocated, it means that the buffer was too small and so the data is incomplete. Retry to get all the data.

Thanks,

Signature · 1 line
Anthony PERARD