Skip to contentSkip to conversation

Display IO topology when PXM data is available (plus some cleanup)

PATCH Superseded Boris Ostrovsky 5 participants 34 messages Mar 10, 2015 – Mar 13, 2015 Arm/actions semi architecture

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v4 cover Display IO topology when PXM data is available (plus some cleanup)

Changes in v4:
* Split cputopology and NUMA info changes into separate patches
* Added patch#1 (partly because patch#4 needs to know when when distance is invalid, i.e. NUMA_NO_DISTANCE)
* Split sysctl version update into a separate patch
* Other changes are listed in each patch
* NOTE: I did not test python's xc changes since I don't think I know how.

Changes in v3:
* Added patch #1 to more consistently define nodes as a u8 and properly use NUMA_NO_NODE.
* Make changes to xen_sysctl_numainfo, similar to those made to xen_sysctl_topologyinfo. (Q: I kept both sets of changes in the same patch #3 to avoid bumping interface version twice. Perhaps it's better to split it into two?)
* Instead of copying data for each loop index allocate a buffer and copy once for all three queries in sysctl.c.
* Move hypercall buffer management from libxl to libxc (as requested by Dario, patches #5 and #6).
* Report topology info for offlined CPUs as well
* Added LIBXL_HAVE_PCITOPO macro

Changes in v2:
* Split topology sysctls into two --- one for CPU topology and the other for devices
* Avoid long loops in the hypervisor by using continuations. (I am not particularly happy about using first_dev in the interface, suggestions for a better interface would be appreciated)
* Use proper libxl conventions for interfaces
* Avoid hypervisor stack corruption when copying PXM data from guest

A few patches that add interface for querying hypervisor about device topology and allow 'xl info -n' display this information if PXM object is provided by ACPI.

This series also makes some optimizations and cleanup of current CPU topology and NUMA sysctl queries.

Boris Ostrovsky (9):
numa: __node_distance() should return u8
pci: Stash device's PXM information in struct pci_dev
sysctl: Make XEN_SYSCTL_topologyinfo sysctl a little more efficient
sysctl: Make XEN_SYSCTL_numainfo a little more efficient
sysctl: Add sysctl interface for querying PCI topology
sysctl: Update sysctl version to 0x0000000C
libxl/libxc: Move libxl_get_cpu_topology()'s hypercall buffer
management to libxc
libxl/libxc: Move libxl_get_numainfo()'s hypercall buffer management
to libxc
libxl: Add interface for querying hypervisor about PCI topology

 tools/libxc/include/xenctrl.h     |   12 ++-
 tools/libxc/xc_misc.c             |   83 +++++++++++++---
 tools/libxl/libxl.c               |  157 +++++++++++++++--------------
 tools/libxl/libxl.h               |   12 +++
 tools/libxl/libxl_freebsd.c       |   12 +++
 tools/libxl/libxl_internal.h      |    5 +
 tools/libxl/libxl_linux.c         |   69 +++++++++++++
 tools/libxl/libxl_netbsd.c        |   12 +++
 tools/libxl/libxl_types.idl       |    7 ++
 tools/libxl/libxl_utils.c         |    8 ++
 tools/libxl/xl_cmdimpl.c          |   40 ++++++--
 tools/misc/xenpm.c                |   92 +++++++-----------
 tools/python/xen/lowlevel/xc/xc.c |  109 ++++++++-------------
 xen/arch/x86/physdev.c            |   23 ++++-
 xen/arch/x86/srat.c               |   15 ++-
 xen/common/page_alloc.c           |    4 +-
 xen/common/sysctl.c               |  197 +++++++++++++++++++++++++++----------
 xen/drivers/passthrough/pci.c     |   13 ++-
 xen/include/asm-x86/numa.h        |    2 +-
 xen/include/public/physdev.h      |    6 +
 xen/include/public/sysctl.h       |  111 +++++++++++++++------
 xen/include/xen/numa.h            |    3 +-
 xen/include/xen/pci.h             |    5 +-
 23 files changed, 669 insertions(+), 328 deletions(-)
#1 lore raw

PATCH v4 1/9 numa: __node_distance() should return u8

SLIT values are byte-sized and some of them (0-9 and 255) have special meaning. Adjust __node_distance() to reflect this and modify scrub_heap_pages() to deal with __node_distance() returning an invalid SLIT entry.

---
 xen/arch/x86/srat.c        |   15 +++++++++++----
 xen/common/page_alloc.c    |    4 ++--
 xen/include/asm-x86/numa.h |    2 +-
 xen/include/xen/numa.h     |    3 ++-
 4 files changed, 16 insertions(+), 8 deletions(-)
xen/arch/x86/srat.c +11 -4
index dfabba3..aa2eda3 100644
@@ -496,14 +496,21 @@ static unsigned node_to_pxm(nodeid_t n)
 	return 0;
 }
 
-int __node_distance(nodeid_t a, nodeid_t b)
+u8 __node_distance(nodeid_t a, nodeid_t b)
 {
-	int index;
+	u8 slit_val;
 
 	if (!acpi_slit)
 		return a == b ? 10 : 20;
-	index = acpi_slit->locality_count * node_to_pxm(a);
-	return acpi_slit->entry[index + node_to_pxm(b)];
+
+	slit_val = acpi_slit->entry[acpi_slit->locality_count * node_to_pxm(a) +
+				                node_to_pxm(b)];
+
+	/* ACPI defines 0xff as an unreachable node and 0-9 are undefined */
+	if ((slit_val == 0xff) || (slit_val <= 9))
+		return NUMA_NO_DISTANCE;
+	else
+		return slit_val;
 }
 
 EXPORT_SYMBOL(__node_distance);
xen/common/page_alloc.c +2 -2
index d96d25b..0f0ca56 100644
@@ -1435,13 +1435,13 @@ void __init scrub_heap_pages(void)
         /* Figure out which NODE CPUs are close. */
         for_each_online_node ( j )
         {
-            int distance;
+            u8 distance;
 
             if ( cpumask_empty(&node_to_cpumask(j)) )
                 continue;
 
             distance = __node_distance(i, j);
-            if ( distance < last_distance )
+            if ( (distance < last_distance) && (distance != NUMA_NO_DISTANCE) )
             {
                 last_distance = distance;
                 best_node = j;
xen/include/asm-x86/numa.h +1 -1
index cc5b5d1..7a489d3 100644
@@ -85,6 +85,6 @@ extern int valid_numa_range(u64 start, u64 end, nodeid_t node);
 #endif
 
 void srat_parse_regions(u64 addr);
-extern int __node_distance(nodeid_t a, nodeid_t b);
+extern u8 __node_distance(nodeid_t a, nodeid_t b);
 
 #endif
xen/include/xen/numa.h +2 -1
index ac4b391..7aef1a8 100644
@@ -7,7 +7,8 @@
 #define NODES_SHIFT     0
 #endif
 
-#define NUMA_NO_NODE    0xFF
+#define NUMA_NO_NODE     0xFF
+#define NUMA_NO_DISTANCE 0xFF
 
 #define MAX_NUMNODES    (1 << NODES_SHIFT)
 
Signature · 1 line
1.7.1
#2 lore raw

Re: [PATCH v4 1/9] numa: __node_distance() should return u8

Quoted 6 lines · Boris Ostrovsky

SLIT values are byte-sized and some of them (0-9 and 255) have special meaning. Adjust __node_distance() to reflect this and modify scrub_heap_pages() to deal with __node_distance() returning an invalid SLIT entry.

You also need to teach XEN_SYSCTL_numainfo about the new NUMA_NO_DISTANCE.

Quoted 18 lines
---
 xen/arch/x86/srat.c        |   15 +++++++++++----
 xen/common/page_alloc.c    |    4 ++--
 xen/include/asm-x86/numa.h |    2 +-
 xen/include/xen/numa.h     |    3 ++-
 4 files changed, 16 insertions(+), 8 deletions(-)
xen/arch/x86/srat.c +1 -1
index dfabba3..aa2eda3 100644
@@ -496,14 +496,21 @@ static unsigned node_to_pxm(nodeid_t n)
 	return 0;
 }
 
-int __node_distance(nodeid_t a, nodeid_t b)
+u8 __node_distance(nodeid_t a, nodeid_t b)
 {

xen/arch/x86/srat.c static unsigned node_to_pxm(nodeid_t n) · line 507

Quoted 10 lines
xen/arch/x86/srat.c +4 -3
-	int index;
+	u8 slit_val;
 
 	if (!acpi_slit)
 		return a == b ? 10 : 20;
-	index = acpi_slit->locality_count * node_to_pxm(a);
-	return acpi_slit->entry[index + node_to_pxm(b)];
+
+	slit_val = acpi_slit->entry[acpi_slit->locality_count * node_to_pxm(a) +
+				                node_to_pxm(b)];

This would be easier to read if you kept the old index temporary (although making it a u64).

~Andrew

Quoted 55 lines
xen/arch/x86/srat.c +6 -0
+
+	/* ACPI defines 0xff as an unreachable node and 0-9 are undefined */
+	if ((slit_val == 0xff) || (slit_val <= 9))
+		return NUMA_NO_DISTANCE;
+	else
+		return slit_val;
 }
 
 EXPORT_SYMBOL(__node_distance);
xen/common/page_alloc.c +2 -2
index d96d25b..0f0ca56 100644
@@ -1435,13 +1435,13 @@ void __init scrub_heap_pages(void)
         /* Figure out which NODE CPUs are close. */
         for_each_online_node ( j )
         {
-            int distance;
+            u8 distance;
 
             if ( cpumask_empty(&node_to_cpumask(j)) )
                 continue;
 
             distance = __node_distance(i, j);
-            if ( distance < last_distance )
+            if ( (distance < last_distance) && (distance != NUMA_NO_DISTANCE) )
             {
                 last_distance = distance;
                 best_node = j;
xen/include/asm-x86/numa.h +1 -1
index cc5b5d1..7a489d3 100644
@@ -85,6 +85,6 @@ extern int valid_numa_range(u64 start, u64 end, nodeid_t node);
 #endif
 
 void srat_parse_regions(u64 addr);
-extern int __node_distance(nodeid_t a, nodeid_t b);
+extern u8 __node_distance(nodeid_t a, nodeid_t b);
 
 #endif
xen/include/xen/numa.h +2 -1
index ac4b391..7aef1a8 100644
@@ -7,7 +7,8 @@
 #define NODES_SHIFT     0
 #endif
 
-#define NUMA_NO_NODE    0xFF
+#define NUMA_NO_NODE     0xFF
+#define NUMA_NO_DISTANCE 0xFF
 
 #define MAX_NUMNODES    (1 << NODES_SHIFT)
 
#3 lore raw

Re: [PATCH v4 1/9] numa: __node_distance() should return u8

Quoted 8 lines · Andrew Cooper
Quoted 6 lines · Boris Ostrovsky

SLIT values are byte-sized and some of them (0-9 and 255) have special meaning. Adjust __node_distance() to reflect this and modify scrub_heap_pages() to deal with __node_distance() returning an invalid SLIT entry.

You also need to teach XEN_SYSCTL_numainfo about the new NUMA_NO_DISTANCE.

The sysctl is updated later in the series but this may indeed break bisection.

Quoted 22 lines
Quoted 22 lines
---
  xen/arch/x86/srat.c        |   15 +++++++++++----
  xen/common/page_alloc.c    |    4 ++--
  xen/include/asm-x86/numa.h |    2 +-
  xen/include/xen/numa.h     |    3 ++-
  4 files changed, 16 insertions(+), 8 deletions(-)
xen/arch/x86/srat.c +2 -2
index dfabba3..aa2eda3 100644
@@ -496,14 +496,21 @@ static unsigned node_to_pxm(nodeid_t n)
  	return 0;
  }
 
-int __node_distance(nodeid_t a, nodeid_t b)
+u8 __node_distance(nodeid_t a, nodeid_t b)
  {
-	int index;
+	u8 slit_val;
 
Quoted 9 lines

xen/arch/x86/srat.c static unsigned node_to_pxm(nodeid_t n) · line 507

Quoted 7 lines
xen/arch/x86/srat.c +3 -2
  	if (!acpi_slit)
  		return a == b ? 10 : 20;
-	index = acpi_slit->locality_count * node_to_pxm(a);
-	return acpi_slit->entry[index + node_to_pxm(b)];
+
+	slit_val = acpi_slit->entry[acpi_slit->locality_count * node_to_pxm(a) +
+				                node_to_pxm(b)];

This would be easier to read if you kept the old index temporary (although making it a u64).

Yes.

-boris

#4 lore raw

PATCH v4 2/9 pci: Stash device's PXM information in struct pci_dev

In reply to Boris Ostrovsky

If ACPI provides PXM data for IO devices then dom0 will pass it to hypervisor during PHYSDEVOP_pci_device_add call. This information, however, is currently ignored.

We will store this information (in the form of nodeID) in pci_dev structure so that we can provide it, for example, to the toolstack when it adds support (in the following patches) for querying the hypervisor about device topology

We will also print it when user requests device information dump.

---

Changes in v4:
* New commit title

 xen/arch/x86/physdev.c        |   23 ++++++++++++++++++++---
 xen/drivers/passthrough/pci.c |   13 +++++++++----
 xen/include/public/physdev.h  |    6 ++++++
 xen/include/xen/pci.h         |    5 ++++-
 4 files changed, 39 insertions(+), 8 deletions(-)
xen/arch/x86/physdev.c +20 -3
index 1be1d50..8d63da5 100644
@@ -565,7 +565,8 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( copy_from_guest(&manage_pci, arg, 1) != 0 )
             break;
 
-        ret = pci_add_device(0, manage_pci.bus, manage_pci.devfn, NULL);
+        ret = pci_add_device(0, manage_pci.bus, manage_pci.devfn,
+                             NULL, NUMA_NO_NODE);
         break;
     }
 
@@ -597,13 +598,14 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         pdev_info.physfn.devfn = manage_pci_ext.physfn.devfn;
         ret = pci_add_device(0, manage_pci_ext.bus,
                              manage_pci_ext.devfn,
-                             &pdev_info);
+                             &pdev_info, NUMA_NO_NODE);
         break;
     }
 
     case PHYSDEVOP_pci_device_add: {
         struct physdev_pci_device_add add;
         struct pci_dev_info pdev_info;
+        u8 node;
 
         ret = -EFAULT;
         if ( copy_from_guest(&add, arg, 1) != 0 )
@@ -618,7 +620,22 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         }
         else
             pdev_info.is_virtfn = 0;
-        ret = pci_add_device(add.seg, add.bus, add.devfn, &pdev_info);
+
+        if ( add.flags & XEN_PCI_DEV_PXM )
+        {
+            uint32_t pxm;
+            size_t optarr_off = offsetof(struct physdev_pci_device_add, optarr) /
+                                sizeof(add.optarr[0]);
+
+            if ( copy_from_guest_offset(&pxm, arg, optarr_off, 1) )
+                break;
+
+            node = pxm_to_node(pxm);
+        }
+        else
+            node = NUMA_NO_NODE;
+
+        ret = pci_add_device(add.seg, add.bus, add.devfn, &pdev_info, node);
         break;
     }
 
xen/drivers/passthrough/pci.c +9 -4
index 4b83583..7d3590d 100644
@@ -568,7 +568,8 @@ static void pci_enable_acs(struct pci_dev *pdev)
     pci_conf_write16(seg, bus, dev, func, pos + PCI_ACS_CTRL, ctrl);
 }
 
-int pci_add_device(u16 seg, u8 bus, u8 devfn, const struct pci_dev_info *info)
+int pci_add_device(u16 seg, u8 bus, u8 devfn,
+                   const struct pci_dev_info *info, u8 node)
 {
     struct pci_seg *pseg;
     struct pci_dev *pdev;
@@ -586,7 +587,8 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, const struct pci_dev_info *info)
         pdev = pci_get_pdev(seg, info->physfn.bus, info->physfn.devfn);
         spin_unlock(&pcidevs_lock);
         if ( !pdev )
-            pci_add_device(seg, info->physfn.bus, info->physfn.devfn, NULL);
+            pci_add_device(seg, info->physfn.bus, info->physfn.devfn,
+                           NULL, node);
         pdev_type = "virtual function";
     }
     else
@@ -609,6 +611,8 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, const struct pci_dev_info *info)
     if ( !pdev )
         goto out;
 
+    pdev->node = node;
+
     if ( info )
         pdev->info = *info;
     else if ( !pdev->vf_rlen[0] )
@@ -1191,10 +1195,11 @@ static int _dump_pci_devices(struct pci_seg *pseg, void *arg)
 
     list_for_each_entry ( pdev, &pseg->alldevs_list, alldevs_list )
     {
-        printk("%04x:%02x:%02x.%u - dom %-3d - MSIs < ",
+        printk("%04x:%02x:%02x.%u - dom %-3d - node %-3d - MSIs < ",
                pseg->nr, pdev->bus,
                PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
-               pdev->domain ? pdev->domain->domain_id : -1);
+               pdev->domain ? pdev->domain->domain_id : -1,
+               (pdev->node != NUMA_NO_NODE) ? pdev->node : -1);
         list_for_each_entry ( msi, &pdev->msi_list, list )
                printk("%d ", msi->irq);
         printk(">\n");
xen/include/public/physdev.h +6 -0
index 2683719..f33845d 100644
@@ -293,6 +293,12 @@ struct physdev_pci_device_add {
         uint8_t bus;
         uint8_t devfn;
     } physfn;
+
+    /*
+     * Optional parameters array.
+     * First element ([0]) is PXM domain associated with the device (if
+     * XEN_PCI_DEV_PXM is set)
+     */
 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
     uint32_t optarr[];
 #elif defined(__GNUC__)
xen/include/xen/pci.h +4 -1
index 3988ee6..daece64 100644
@@ -57,6 +57,8 @@ struct pci_dev {
 
     u8 phantom_stride;
 
+    u8 node; /* NUMA node */
+
     enum pdev_type {
         DEV_TYPE_PCI_UNKNOWN,
         DEV_TYPE_PCIe_ENDPOINT,
@@ -103,7 +105,8 @@ void setup_hwdom_pci_devices(struct domain *,
 int pci_release_devices(struct domain *d);
 int pci_add_segment(u16 seg);
 const unsigned long *pci_get_ro_map(u16 seg);
-int pci_add_device(u16 seg, u8 bus, u8 devfn, const struct pci_dev_info *);
+int pci_add_device(u16 seg, u8 bus, u8 devfn,
+                   const struct pci_dev_info *, u8 node);
 int pci_remove_device(u16 seg, u8 bus, u8 devfn);
 int pci_ro_device(int seg, int bus, int devfn);
 void arch_pci_ro_device(int seg, int bdf);
Signature · 1 line
1.7.1
#5 lore raw

Re: [PATCH v4 2/9] pci: Stash device's PXM information in struct pci_dev

xen/include/xen/pci.h struct pci_dev { · line 60

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 10.03.15 at 03:27, <[email protected]> wrote:
xen/include/xen/pci.h +1 -0
@@ -57,6 +57,8 @@ struct pci_dev {
 
     u8 phantom_stride;
 
+    u8 node; /* NUMA node */

I was about to commit this when I noticed that you use u8 instead of nodeid_t (which you had introduced yourself) throughout this patch.

Jan

#6 lore raw

PATCH v4 3/9 sysctl: Make XEN_SYSCTL_topologyinfo sysctl a little more efficient

In reply to Boris Ostrovsky

Instead of copying data for each field in xen_sysctl_topologyinfo separately put cpu/socket/node into a single structure and do a single copy for each processor.

Do not use max_cpu_index, which is almost always used for calculating number CPUs (thus requiring adding or subtracting one), replace it with num_cpus.

There is no need to copy whole op in sysctl to user at the end, we only need num_cpus.

Rename xen_sysctl_topologyinfo and XEN_SYSCTL_topologyinfo to reflect the fact that these are used for CPU topology. Subsequent patch will add support for PCI topology sysctl.

Replace INVALID_TOPOLOGY_ID with "XEN_"-prefixed macros for each invalid type (core, socket, node).

---

Changes in v4:
* Split v3's patch into two --- one for CPU topology and one for NUMA info
* Replaced max_cpu_index with num_cpus to avoid always adding/subtracting one
* Replaced INVALID_TOPOLOGY_ID with separate macros for core/socket/node
* No buffer allocation in sysctl, copy data cpu-by-cpu
* Check for BAD_APICID/NUMA_NO_NODE in sysctl

 tools/libxc/include/xenctrl.h     |    4 +-
 tools/libxc/xc_misc.c             |   10 ++--
 tools/libxl/libxl.c               |   46 +++++++-----------
 tools/misc/xenpm.c                |   93 +++++++++++++++---------------------
 tools/python/xen/lowlevel/xc/xc.c |   60 ++++++++++--------------
 xen/common/sysctl.c               |   66 +++++++++++++++++---------
 xen/include/public/sysctl.h       |   48 +++++++++++--------
 7 files changed, 160 insertions(+), 167 deletions(-)
tools/libxc/include/xenctrl.h +2 -2
index 09d819f..fef4cca 100644
@@ -1226,7 +1226,7 @@ int xc_readconsolering(xc_interface *xch,
 int xc_send_debug_keys(xc_interface *xch, char *keys);
 
 typedef xen_sysctl_physinfo_t xc_physinfo_t;
-typedef xen_sysctl_topologyinfo_t xc_topologyinfo_t;
+typedef xen_sysctl_cputopoinfo_t xc_cputopoinfo_t;
 typedef xen_sysctl_numainfo_t xc_numainfo_t;
 
 typedef uint32_t xc_cpu_to_node_t;
@@ -1237,7 +1237,7 @@ typedef uint64_t xc_node_to_memfree_t;
 typedef uint32_t xc_node_to_node_dist_t;
 
 int xc_physinfo(xc_interface *xch, xc_physinfo_t *info);
-int xc_topologyinfo(xc_interface *xch, xc_topologyinfo_t *info);
+int xc_cputopoinfo(xc_interface *xch, xc_cputopoinfo_t *info);
 int xc_numainfo(xc_interface *xch, xc_numainfo_t *info);
 
 int xc_sched_id(xc_interface *xch,
tools/libxc/xc_misc.c +5 -5
index e253a58..be68291 100644
@@ -177,20 +177,20 @@ int xc_physinfo(xc_interface *xch,
     return 0;
 }
 
-int xc_topologyinfo(xc_interface *xch,
-                xc_topologyinfo_t *put_info)
+int xc_cputopoinfo(xc_interface *xch,
+                   xc_cputopoinfo_t *put_info)
 {
     int ret;
     DECLARE_SYSCTL;
 
-    sysctl.cmd = XEN_SYSCTL_topologyinfo;
+    sysctl.cmd = XEN_SYSCTL_cputopoinfo;
 
-    memcpy(&sysctl.u.topologyinfo, put_info, sizeof(*put_info));
+    memcpy(&sysctl.u.cputopoinfo, put_info, sizeof(*put_info));
 
     if ( (ret = do_sysctl(xch, &sysctl)) != 0 )
         return ret;
 
-    memcpy(put_info, &sysctl.u.topologyinfo, sizeof(*put_info));
+    memcpy(put_info, &sysctl.u.cputopoinfo, sizeof(*put_info));
 
     return 0;
 }
tools/libxl/libxl.c +18 -28

73 lines not on this page · raw message

tools/misc/xenpm.c +39 -54

180 lines not on this page · raw message

tools/python/xen/lowlevel/xc/xc.c +25 -35

119 lines not on this page · raw message

xen/common/sysctl.c +44 -22

83 lines not on this page · raw message

xen/include/public/sysctl.h +27 -21

72 lines not on this page · raw message

Signature · 1 line
1.7.1
#7 lore raw

Re: [PATCH v4 3/9] sysctl: Make XEN_SYSCTL_topologyinfo sysctl a little more efficient

Quoted 10 lines · Boris Ostrovsky

Instead of copying data for each field in xen_sysctl_topologyinfo separately put cpu/socket/node into a single structure and do a single copy for each processor.

Do not use max_cpu_index, which is almost always used for calculating number CPUs (thus requiring adding or subtracting one), replace it with num_cpus.

There is no need to copy whole op in sysctl to user at the end, we only need num_cpus.

Quoted 8 lines

Rename xen_sysctl_topologyinfo and XEN_SYSCTL_topologyinfo to reflect the fact that these are used for CPU topology. Subsequent patch will add support for PCI topology sysctl.

Replace INVALID_TOPOLOGY_ID with "XEN_"-prefixed macros for each invalid type (core, socket, node).

In principle, a good improvement, but I have some specific issues.

Quoted 7 lines

---

tools/python/xen/lowlevel/xc/xc.c +0 -0
index 2aa0dc7..2fd93e0 100644

tools/python/xen/lowlevel/xc/xc.c static PyObject *pyxc_numainfo(XcObject *self) · line 1371

Quoted 8 lines
tools/python/xen/lowlevel/xc/xc.c +1 -1
@@ -1375,7 +1365,7 @@ static PyObject *pyxc_numainfo(XcObject *self)
         for ( j = 0; j <= max_node_index; j++ )
         {
             uint32_t dist = nodes_dist[i*(max_node_index+1) + j];
-            if ( dist == INVALID_TOPOLOGY_ID )
+            if ( dist == ~0u )
             {
                 PyList_Append(node_to_node_dist_obj, Py_None);
             }

This looks like a spurious hunk (perhaps from patch 9?)

Quoted 14 lines
xen/common/sysctl.c +2 -3
index 0cb6ee1..fe48ee8 100644
@@ -320,39 +320,61 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl)
     }
     break;
 
-    case XEN_SYSCTL_topologyinfo:
+    case XEN_SYSCTL_cputopoinfo:
     {
-        uint32_t i, max_cpu_index, last_online_cpu;
-        xen_sysctl_topologyinfo_t *ti = &op->u.topologyinfo;
+        uint32_t i, num_cpus;

xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 332

Quoted 10 lines
xen/common/sysctl.c +6 -3
+        xen_sysctl_cputopoinfo_t *ti = &op->u.cputopoinfo;
 
-        last_online_cpu = cpumask_last(&cpu_online_map);
-        max_cpu_index = min_t(uint32_t, ti->max_cpu_index, last_online_cpu);
-        ti->max_cpu_index = last_online_cpu;
+        if ( guest_handle_is_null(ti->cputopo) )
+        {
+            ret = -EINVAL;
+            break;
+        }

The prevailing hypervisor style is to use a NULL guest handle as an explicit request for size. i.e. write back num_cpus in ti and return success.

Quoted 102 lines
xen/common/sysctl.c +36 -16
-        for ( i = 0; i <= max_cpu_index; i++ )
+        num_cpus = cpumask_last(&cpu_online_map) + 1;
+        if ( ti->num_cpus != num_cpus )
         {
-            if ( !guest_handle_is_null(ti->cpu_to_core) )
+            uint32_t array_sz = ti->num_cpus;
+
+            ti->num_cpus = num_cpus;
+            if ( __copy_field_to_guest(u_sysctl, op,
+                                       u.cputopoinfo.num_cpus) )
             {
-                uint32_t core = cpu_online(i) ? cpu_to_core(i) : ~0u;
-                if ( copy_to_guest_offset(ti->cpu_to_core, i, &core, 1) )
-                    break;
+                ret = -EFAULT;
+                break;
+            }
+            num_cpus = min_t(uint32_t, array_sz, num_cpus);
+        }
+
+        for ( i = 0; i < num_cpus; i++ )
+        {
+            xen_sysctl_cputopo_t cputopo;
+
+            if ( cpu_present(i) )
+            {
+                cputopo.core = cpu_to_core(i);
+                if ( cputopo.core == BAD_APICID )
+                    cputopo.core = XEN_INVALID_CORE_ID;
+                cputopo.socket = cpu_to_socket(i);
+                if ( cputopo.socket == BAD_APICID )
+                    cputopo.socket = XEN_INVALID_SOCKET_ID;
+                cputopo.node = cpu_to_node(i);
+                if ( cputopo.node == NUMA_NO_NODE )
+                    cputopo.node = XEN_INVALID_NODE_ID;
             }
-            if ( !guest_handle_is_null(ti->cpu_to_socket) )
+            else
             {
-                uint32_t socket = cpu_online(i) ? cpu_to_socket(i) : ~0u;
-                if ( copy_to_guest_offset(ti->cpu_to_socket, i, &socket, 1) )
-                    break;
+                cputopo.core = XEN_INVALID_CORE_ID;
+                cputopo.socket = XEN_INVALID_SOCKET_ID;
+                cputopo.node = XEN_INVALID_NODE_ID;
             }
-            if ( !guest_handle_is_null(ti->cpu_to_node) )
+
+            if ( copy_to_guest_offset(ti->cputopo, i, &cputopo, 1) )
             {
-                uint32_t node = cpu_online(i) ? cpu_to_node(i) : ~0u;
-                if ( copy_to_guest_offset(ti->cpu_to_node, i, &node, 1) )
-                    break;
+                ret = -EFAULT;
+                break;
             }
         }
-
-        ret = ((i <= max_cpu_index) || copy_to_guest(u_sysctl, op, 1))
-            ? -EFAULT : 0;
     }
     break;
 
xen/include/public/sysctl.h +17 -9
index 8552dc6..f20da69 100644
@@ -462,31 +462,37 @@ struct xen_sysctl_lockprof_op {
 typedef struct xen_sysctl_lockprof_op xen_sysctl_lockprof_op_t;
 DEFINE_XEN_GUEST_HANDLE(xen_sysctl_lockprof_op_t);
 
-/* XEN_SYSCTL_topologyinfo */
-#define INVALID_TOPOLOGY_ID  (~0U)
-struct xen_sysctl_topologyinfo {
+/* XEN_SYSCTL_cputopoinfo */
+#define XEN_INVALID_CORE_ID     (~0U)
+#define XEN_INVALID_SOCKET_ID   (~0U)
+#define XEN_INVALID_NODE_ID     ((uint8_t)~0)
+
+struct xen_sysctl_cputopo {
+    uint32_t core;
+    uint32_t socket;
+    uint8_t node;
+};
+typedef struct xen_sysctl_cputopo xen_sysctl_cputopo_t;
+DEFINE_XEN_GUEST_HANDLE(xen_sysctl_cputopo_t);
+
+struct xen_sysctl_cputopoinfo {
     /*
-     * IN: maximum addressable entry in the caller-provided arrays.
-     * OUT: largest cpu identifier in the system.
-     * If OUT is greater than IN then the arrays are truncated!
-     * If OUT is leass than IN then the array tails are not written by sysctl.
+     * IN: size of caller-provided cputopo array.
+     * OUT: Number of CPUs in the system.
      */
-    uint32_t max_cpu_index;
+    uint32_t num_cpus;
 
     /*
-     * If not NULL, these arrays are filled with core/socket/node identifier

xen/include/public/sysctl.h struct xen_sysctl_lockprof_op { · line 490

Quoted 10 lines
xen/include/public/sysctl.h +5 -5
-     * for each cpu.
-     * If a cpu has no core/socket/node information (e.g., cpu not present) 
-     * then the sentinel value ~0u is written to each array.
-     * The number of array elements written by the sysctl is:
-     *   min(@max_cpu_index_IN,@max_cpu_index_OUT)+1
+     * If not NULL, filled with core/socket/node identifier for each cpu
+     * If information for a particular entry is not avalable it is set to
+     * XEN_INVALID_<xxx>_ID.
+     * The number of array elements for CPU topology written by the sysctl is:
+     *   min(@num_cpus_IN,@num_cpus_OUT)+1.

This is also a problematic interface as it clobbers the actual length of the valid array. strace/valgrind have no way of validating the state after the hypercall, and the user of the interface has to remember the number they passed in to start with.

It is also prevailing style to return the number of entries actually written to, and fail the hypercall with -ENOBUFS if there is insufficient space in the destination array.

See the implementation of XEN_DOMCTL_get_vcpu_msrs as an example which handles each of these corner cases.

~Andrew

Quoted 31 lines
xen/include/public/sysctl.h +5 -7
      */
-    XEN_GUEST_HANDLE_64(uint32) cpu_to_core;
-    XEN_GUEST_HANDLE_64(uint32) cpu_to_socket;
-    XEN_GUEST_HANDLE_64(uint32) cpu_to_node;
+    XEN_GUEST_HANDLE_64(xen_sysctl_cputopo_t) cputopo;
 };
-typedef struct xen_sysctl_topologyinfo xen_sysctl_topologyinfo_t;
-DEFINE_XEN_GUEST_HANDLE(xen_sysctl_topologyinfo_t);
+typedef struct xen_sysctl_cputopoinfo xen_sysctl_cputopoinfo_t;
+DEFINE_XEN_GUEST_HANDLE(xen_sysctl_cputopoinfo_t);
 
 /* XEN_SYSCTL_numainfo */
 #define INVALID_NUMAINFO_ID (~0U)
@@ -672,7 +678,7 @@ struct xen_sysctl {
 #define XEN_SYSCTL_pm_op                         12
 #define XEN_SYSCTL_page_offline_op               14
 #define XEN_SYSCTL_lockprof_op                   15
-#define XEN_SYSCTL_topologyinfo                  16 
+#define XEN_SYSCTL_cputopoinfo                   16
 #define XEN_SYSCTL_numainfo                      17
 #define XEN_SYSCTL_cpupool_op                    18
 #define XEN_SYSCTL_scheduler_op                  19
@@ -683,7 +689,7 @@ struct xen_sysctl {
         struct xen_sysctl_readconsole       readconsole;
         struct xen_sysctl_tbuf_op           tbuf_op;
         struct xen_sysctl_physinfo          physinfo;
-        struct xen_sysctl_topologyinfo      topologyinfo;
+        struct xen_sysctl_cputopoinfo       cputopoinfo;
         struct xen_sysctl_numainfo          numainfo;
         struct xen_sysctl_sched_id          sched_id;
         struct xen_sysctl_perfc_op          perfc_op;
#8 lore raw

Re: [PATCH v4 3/9] sysctl: Make XEN_SYSCTL_topologyinfo sysctl a little more efficient

Quoted 23 lines · Andrew Cooper
Quoted 10 lines · Boris Ostrovsky

Instead of copying data for each field in xen_sysctl_topologyinfo separately put cpu/socket/node into a single structure and do a single copy for each processor.

Do not use max_cpu_index, which is almost always used for calculating number CPUs (thus requiring adding or subtracting one), replace it with num_cpus.

There is no need to copy whole op in sysctl to user at the end, we only need num_cpus.

Quoted 8 lines

Rename xen_sysctl_topologyinfo and XEN_SYSCTL_topologyinfo to reflect the fact that these are used for CPU topology. Subsequent patch will add support for PCI topology sysctl.

Replace INVALID_TOPOLOGY_ID with "XEN_"-prefixed macros for each invalid type (core, socket, node).

In principle, a good improvement, but I have some specific issues.

Quoted 2 lines

---

Quoted 14 lines

tools/python/xen/lowlevel/xc/xc.c static PyObject *pyxc_numainfo(XcObject *self) · line 1371

Quoted 13 lines
tools/python/xen/lowlevel/xc/xc.c +1 -1
index 2aa0dc7..2fd93e0 100644
@@ -1375,7 +1365,7 @@ static PyObject *pyxc_numainfo(XcObject *self)
          for ( j = 0; j <= max_node_index; j++ )
          {
              uint32_t dist = nodes_dist[i*(max_node_index+1) + j];
-            if ( dist == INVALID_TOPOLOGY_ID )
+            if ( dist == ~0u )
              {
                  PyList_Append(node_to_node_dist_obj, Py_None);
              }

This looks like a spurious hunk (perhaps from patch 9?)

No, this is sort of an intermediate step: INVALID_TOPOLOGY_ID is no longer defined and a new macro for distance will show up in the next patch.

And, in fact, it's wrong anyway since the sysctl never sets distance to INVALID_TOPOLOGY_ID, it sets it explicitly to ~0u. It just so happens that INVALID_TOPOLOGY_ID is also ~0U.

I thought about moving macro definition into this patch but then logically it wouldn't belong here so I figured I'd do what the hypervisor does, which is use a constant. Until the next patch.

Quoted 17 lines
Quoted 17 lines
xen/common/sysctl.c +3 -3
index 0cb6ee1..fe48ee8 100644
@@ -320,39 +320,61 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl)
      }
      break;
 
-    case XEN_SYSCTL_topologyinfo:
+    case XEN_SYSCTL_cputopoinfo:
      {
-        uint32_t i, max_cpu_index, last_online_cpu;
-        xen_sysctl_topologyinfo_t *ti = &op->u.topologyinfo;
+        uint32_t i, num_cpus;
+        xen_sysctl_cputopoinfo_t *ti = &op->u.cputopoinfo;
 
Quoted 11 lines

xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 332

Quoted 8 lines
xen/common/sysctl.c +5 -3
-        last_online_cpu = cpumask_last(&cpu_online_map);
-        max_cpu_index = min_t(uint32_t, ti->max_cpu_index, last_online_cpu);
-        ti->max_cpu_index = last_online_cpu;
+        if ( guest_handle_is_null(ti->cputopo) )
+        {
+            ret = -EINVAL;
+            break;
+        }

The prevailing hypervisor style is to use a NULL guest handle as an explicit request for size. i.e. write back num_cpus in ti and return success.

Yes, this would look better from the interface POV.

-boris

Quoted 159 lines
Quoted 103 lines
xen/common/sysctl.c +36 -16
-        for ( i = 0; i <= max_cpu_index; i++ )
+        num_cpus = cpumask_last(&cpu_online_map) + 1;
+        if ( ti->num_cpus != num_cpus )
          {
-            if ( !guest_handle_is_null(ti->cpu_to_core) )
+            uint32_t array_sz = ti->num_cpus;
+
+            ti->num_cpus = num_cpus;
+            if ( __copy_field_to_guest(u_sysctl, op,
+                                       u.cputopoinfo.num_cpus) )
              {
-                uint32_t core = cpu_online(i) ? cpu_to_core(i) : ~0u;
-                if ( copy_to_guest_offset(ti->cpu_to_core, i, &core, 1) )
-                    break;
+                ret = -EFAULT;
+                break;
+            }
+            num_cpus = min_t(uint32_t, array_sz, num_cpus);
+        }
+
+        for ( i = 0; i < num_cpus; i++ )
+        {
+            xen_sysctl_cputopo_t cputopo;
+
+            if ( cpu_present(i) )
+            {
+                cputopo.core = cpu_to_core(i);
+                if ( cputopo.core == BAD_APICID )
+                    cputopo.core = XEN_INVALID_CORE_ID;
+                cputopo.socket = cpu_to_socket(i);
+                if ( cputopo.socket == BAD_APICID )
+                    cputopo.socket = XEN_INVALID_SOCKET_ID;
+                cputopo.node = cpu_to_node(i);
+                if ( cputopo.node == NUMA_NO_NODE )
+                    cputopo.node = XEN_INVALID_NODE_ID;
              }
-            if ( !guest_handle_is_null(ti->cpu_to_socket) )
+            else
              {
-                uint32_t socket = cpu_online(i) ? cpu_to_socket(i) : ~0u;
-                if ( copy_to_guest_offset(ti->cpu_to_socket, i, &socket, 1) )
-                    break;
+                cputopo.core = XEN_INVALID_CORE_ID;
+                cputopo.socket = XEN_INVALID_SOCKET_ID;
+                cputopo.node = XEN_INVALID_NODE_ID;
              }
-            if ( !guest_handle_is_null(ti->cpu_to_node) )
+
+            if ( copy_to_guest_offset(ti->cputopo, i, &cputopo, 1) )
              {
-                uint32_t node = cpu_online(i) ? cpu_to_node(i) : ~0u;
-                if ( copy_to_guest_offset(ti->cpu_to_node, i, &node, 1) )
-                    break;
+                ret = -EFAULT;
+                break;
              }
          }
-
-        ret = ((i <= max_cpu_index) || copy_to_guest(u_sysctl, op, 1))
-            ? -EFAULT : 0;
      }
      break;
 
xen/include/public/sysctl.h +17 -9
index 8552dc6..f20da69 100644
@@ -462,31 +462,37 @@ struct xen_sysctl_lockprof_op {
  typedef struct xen_sysctl_lockprof_op xen_sysctl_lockprof_op_t;
  DEFINE_XEN_GUEST_HANDLE(xen_sysctl_lockprof_op_t);
 
-/* XEN_SYSCTL_topologyinfo */
-#define INVALID_TOPOLOGY_ID  (~0U)
-struct xen_sysctl_topologyinfo {
+/* XEN_SYSCTL_cputopoinfo */
+#define XEN_INVALID_CORE_ID     (~0U)
+#define XEN_INVALID_SOCKET_ID   (~0U)
+#define XEN_INVALID_NODE_ID     ((uint8_t)~0)
+
+struct xen_sysctl_cputopo {
+    uint32_t core;
+    uint32_t socket;
+    uint8_t node;
+};
+typedef struct xen_sysctl_cputopo xen_sysctl_cputopo_t;
+DEFINE_XEN_GUEST_HANDLE(xen_sysctl_cputopo_t);
+
+struct xen_sysctl_cputopoinfo {
      /*
-     * IN: maximum addressable entry in the caller-provided arrays.
-     * OUT: largest cpu identifier in the system.
-     * If OUT is greater than IN then the arrays are truncated!
-     * If OUT is leass than IN then the array tails are not written by sysctl.
+     * IN: size of caller-provided cputopo array.
+     * OUT: Number of CPUs in the system.
       */
-    uint32_t max_cpu_index;
+    uint32_t num_cpus;
 
      /*
-     * If not NULL, these arrays are filled with core/socket/node identifier

xen/include/public/sysctl.h struct xen_sysctl_lockprof_op { · line 490

Quoted 10 lines
xen/include/public/sysctl.h +5 -5
-     * for each cpu.
-     * If a cpu has no core/socket/node information (e.g., cpu not present)
-     * then the sentinel value ~0u is written to each array.
-     * The number of array elements written by the sysctl is:
-     *   min(@max_cpu_index_IN,@max_cpu_index_OUT)+1
+     * If not NULL, filled with core/socket/node identifier for each cpu
+     * If information for a particular entry is not avalable it is set to
+     * XEN_INVALID_<xxx>_ID.
+     * The number of array elements for CPU topology written by the sysctl is:
+     *   min(@num_cpus_IN,@num_cpus_OUT)+1.

This is also a problematic interface as it clobbers the actual length of the valid array. strace/valgrind have no way of validating the state after the hypercall, and the user of the interface has to remember the number they passed in to start with.

It is also prevailing style to return the number of entries actually written to, and fail the hypercall with -ENOBUFS if there is insufficient space in the destination array.

See the implementation of XEN_DOMCTL_get_vcpu_msrs as an example which handles each of these corner cases.

~Andrew

Quoted 31 lines
xen/include/public/sysctl.h +5 -7
       */
-    XEN_GUEST_HANDLE_64(uint32) cpu_to_core;
-    XEN_GUEST_HANDLE_64(uint32) cpu_to_socket;
-    XEN_GUEST_HANDLE_64(uint32) cpu_to_node;
+    XEN_GUEST_HANDLE_64(xen_sysctl_cputopo_t) cputopo;
  };
-typedef struct xen_sysctl_topologyinfo xen_sysctl_topologyinfo_t;
-DEFINE_XEN_GUEST_HANDLE(xen_sysctl_topologyinfo_t);
+typedef struct xen_sysctl_cputopoinfo xen_sysctl_cputopoinfo_t;
+DEFINE_XEN_GUEST_HANDLE(xen_sysctl_cputopoinfo_t);
 
  /* XEN_SYSCTL_numainfo */
  #define INVALID_NUMAINFO_ID (~0U)
@@ -672,7 +678,7 @@ struct xen_sysctl {
  #define XEN_SYSCTL_pm_op                         12
  #define XEN_SYSCTL_page_offline_op               14
  #define XEN_SYSCTL_lockprof_op                   15
-#define XEN_SYSCTL_topologyinfo                  16
+#define XEN_SYSCTL_cputopoinfo                   16
  #define XEN_SYSCTL_numainfo                      17
  #define XEN_SYSCTL_cpupool_op                    18
  #define XEN_SYSCTL_scheduler_op                  19
@@ -683,7 +689,7 @@ struct xen_sysctl {
          struct xen_sysctl_readconsole       readconsole;
          struct xen_sysctl_tbuf_op           tbuf_op;
          struct xen_sysctl_physinfo          physinfo;
-        struct xen_sysctl_topologyinfo      topologyinfo;
+        struct xen_sysctl_cputopoinfo       cputopoinfo;
          struct xen_sysctl_numainfo          numainfo;
          struct xen_sysctl_sched_id          sched_id;
          struct xen_sysctl_perfc_op          perfc_op;
#9 lore raw

Re: [PATCH v4 3/9] sysctl: Make XEN_SYSCTL_topologyinfo sysctl a little more efficient

In reply to Boris Ostrovsky
Quoted 14 lines · Boris Ostrovsky

Instead of copying data for each field in xen_sysctl_topologyinfo separately put cpu/socket/node into a single structure and do a single copy for each processor.

Do not use max_cpu_index, which is almost always used for calculating number CPUs (thus requiring adding or subtracting one), replace it with num_cpus.

There is no need to copy whole op in sysctl to user at the end, we only need num_cpus.

Rename xen_sysctl_topologyinfo and XEN_SYSCTL_topologyinfo to reflect the fact that these are used for CPU topology. Subsequent patch will add support for PCI topology sysctl.

Quoted 8 lines

Replace INVALID_TOPOLOGY_ID with "XEN_"-prefixed macros for each invalid type (core, socket, node).

---

Changes in v4:
* Split v3's patch into two --- one for CPU topology and one for NUMA info

I think this means this is now back to how v2 looked, in which case you may feel free to reinstate my ack. I only glanced through this version but it looks ok. Let me know if it is actually different to v2 and I'll have a closer look.

#10 lore raw

Re: [PATCH v4 3/9] sysctl: Make XEN_SYSCTL_topologyinfo sysctl a little more efficient

Quoted 21 lines · Ian Campbell
Quoted 20 lines · Boris Ostrovsky

Instead of copying data for each field in xen_sysctl_topologyinfo separately put cpu/socket/node into a single structure and do a single copy for each processor.

Do not use max_cpu_index, which is almost always used for calculating number CPUs (thus requiring adding or subtracting one), replace it with num_cpus.

There is no need to copy whole op in sysctl to user at the end, we only need num_cpus.

Rename xen_sysctl_topologyinfo and XEN_SYSCTL_topologyinfo to reflect the fact that these are used for CPU topology. Subsequent patch will add support for PCI topology sysctl.

Replace INVALID_TOPOLOGY_ID with "XEN_"-prefixed macros for each invalid type (core, socket, node).

---

Quoted 6 lines
Quoted 2 lines

Changes in v4:
* Split v3's patch into two --- one for CPU topology and one for NUMA info

I think this means this is now back to how v2 looked, in which case you may feel free to reinstate my ack. I only glanced through this version but it looks ok. Let me know if it is actually different to v2 and I'll have a closer look.

Yes, this is close to v2. The differences are that we are now sizing everything with max number of CPUs vs max CPU index (so a review for off-by-one errors would be useful) plus new INVALID macros that resulted in some (fairly minor) changes.

However, Andrew asked for more changes in sysctl implementation that may affect the interface (for both this and NUMA patches) so it's probably better to wait until v5.

-boris

#11 lore raw

Re: [PATCH v4 3/9] sysctl: Make XEN_SYSCTL_topologyinfo sysctl a little more efficient

Quoted 1 line · Boris Ostrovsky

it's probably better to wait until v5.

Done!

#12 lore raw

Re: [PATCH v4 3/9] sysctl: Make XEN_SYSCTL_topologyinfo sysctl a little more efficient

In reply to Boris Ostrovsky

xen/include/public/sysctl.h struct xen_sysctl_lockprof_op { · line 474

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 10.03.15 at 03:27, <[email protected]> wrote:
xen/include/public/sysctl.h +5 -0
+struct xen_sysctl_cputopo {
+    uint32_t core;
+    uint32_t socket;
+    uint8_t node;
+};

Let's avoid me asking for explicit padding as well a future problems (it's sysctl and hence can be changed, but anyway) by making the node a uint32_t too (especially if you already think socket and core IDs could get this large).

Jan

#13 lore raw

PATCH v4 4/9 sysctl: Make XEN_SYSCTL_numainfo a little more efficient

In reply to Boris Ostrovsky

Make sysctl NUMA topology query use fewer copies by combining some fields into a single structure and copying distances for each node in a single copy.

Instead of using max_node_index for passing number of nodes keep this value in num_nodes: almost all uses of max_node_index required adding or subtracting one to eventually get to number of nodes anyway.

Replace INVALID_NUMAINFO_ID with XEN_INVALID_MEM_SZ and add XEN_INVALID_NODE_DIST.

---

Changes in v4:
* Split this patch from CPU topology changes
* Replace max_node_index with num_nodes (for the same reason as in previous patch)
* No buffer allocation in sysctl, copy data node-by-node
* Replaced INVALID_NUMAINFO_ID with XEN_INVALID_MEM_SZ, added XEN_INVALID_NODE_DIST

 tools/libxl/libxl.c               |   50 ++++++++++++-------------
 tools/python/xen/lowlevel/xc/xc.c |   56 ++++++++++++++---------------
 xen/common/sysctl.c               |   72 +++++++++++++++++++++----------------
 xen/include/public/sysctl.h       |   32 +++++++++++------
 4 files changed, 113 insertions(+), 97 deletions(-)
tools/libxl/libxl.c +24 -26
index 6660133..8a6f979 100644
@@ -5093,9 +5093,8 @@ libxl_numainfo *libxl_get_numainfo(libxl_ctx *ctx, int *nr)
 {
     GC_INIT(ctx);
     xc_numainfo_t ninfo;
-    DECLARE_HYPERCALL_BUFFER(xc_node_to_memsize_t, memsize);
-    DECLARE_HYPERCALL_BUFFER(xc_node_to_memfree_t, memfree);
-    DECLARE_HYPERCALL_BUFFER(uint32_t, node_dists);
+    DECLARE_HYPERCALL_BUFFER(xen_sysctl_meminfo_t, meminfo);
+    DECLARE_HYPERCALL_BUFFER(uint8_t, distance);
     libxl_numainfo *ret = NULL;
     int i, j, max_nodes;
 
@@ -5107,51 +5106,50 @@ libxl_numainfo *libxl_get_numainfo(libxl_ctx *ctx, int *nr)
         goto out;
     }
 
-    memsize = xc_hypercall_buffer_alloc
-        (ctx->xch, memsize, sizeof(*memsize) * max_nodes);
-    memfree = xc_hypercall_buffer_alloc
-        (ctx->xch, memfree, sizeof(*memfree) * max_nodes);
-    node_dists = xc_hypercall_buffer_alloc
-        (ctx->xch, node_dists, sizeof(*node_dists) * max_nodes * max_nodes);
-    if ((memsize == NULL) || (memfree == NULL) || (node_dists == NULL)) {
+    meminfo = xc_hypercall_buffer_alloc(ctx->xch, meminfo,
+                                        sizeof(*meminfo) * max_nodes);
+    distance = xc_hypercall_buffer_alloc(ctx->xch, distance,
+                                         sizeof(*distance) *
+                                         max_nodes * max_nodes);
+    if ((meminfo == NULL) || (distance == NULL)) {
         LIBXL__LOG_ERRNOVAL(ctx, XTL_ERROR, ENOMEM,
                             "Unable to allocate hypercall arguments");
         goto fail;
     }
 
-    set_xen_guest_handle(ninfo.node_to_memsize, memsize);
-    set_xen_guest_handle(ninfo.node_to_memfree, memfree);
-    set_xen_guest_handle(ninfo.node_to_node_distance, node_dists);
-    ninfo.max_node_index = max_nodes - 1;
+    set_xen_guest_handle(ninfo.meminfo, meminfo);
+    set_xen_guest_handle(ninfo.distance, distance);
+    ninfo.num_nodes = max_nodes;
     if (xc_numainfo(ctx->xch, &ninfo) != 0) {
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "getting numainfo");
         goto fail;
     }
 
-    if (ninfo.max_node_index < max_nodes - 1)
-        max_nodes = ninfo.max_node_index + 1;
+    if (ninfo.num_nodes < max_nodes)
+        max_nodes = ninfo.num_nodes;
 
     *nr = max_nodes;
 
     ret = libxl__zalloc(NOGC, sizeof(libxl_numainfo) * max_nodes);
     for (i = 0; i < max_nodes; i++)
-        ret[i].dists = libxl__calloc(NOGC, max_nodes, sizeof(*node_dists));
+        ret[i].dists = libxl__calloc(NOGC, max_nodes, sizeof(*distance));
 
     for (i = 0; i < max_nodes; i++) {
-#define V(mem, i) (mem[i] == INVALID_NUMAINFO_ID) ? \
-    LIBXL_NUMAINFO_INVALID_ENTRY : mem[i]
-        ret[i].size = V(memsize, i);
-        ret[i].free = V(memfree, i);
+#define V(val, invalid) (val == invalid) ? \
+       LIBXL_NUMAINFO_INVALID_ENTRY : val
+        ret[i].size = V(meminfo[i].memsize, XEN_INVALID_MEM_SZ);
+        ret[i].free = V(meminfo[i].memfree, XEN_INVALID_MEM_SZ);
         ret[i].num_dists = max_nodes;
-        for (j = 0; j < ret[i].num_dists; j++)
-            ret[i].dists[j] = V(node_dists, i * max_nodes + j);
+        for (j = 0; j < ret[i].num_dists; j++) {
+            unsigned idx = i * max_nodes + j;
+            ret[i].dists[j] = V(distance[idx], XEN_INVALID_NODE_DIST);
+        }
 #undef V
     }
 
  fail:
-    xc_hypercall_buffer_free(ctx->xch, memsize);
-    xc_hypercall_buffer_free(ctx->xch, memfree);
-    xc_hypercall_buffer_free(ctx->xch, node_dists);
+    xc_hypercall_buffer_free(ctx->xch, meminfo);
+    xc_hypercall_buffer_free(ctx->xch, distance);
 
  out:
     GC_FREE;
tools/python/xen/lowlevel/xc/xc.c +27 -29

108 lines not on this page · raw message

xen/common/sysctl.c +41 -31

90 lines not on this page · raw message

xen/include/public/sysctl.h +21 -11
index f20da69..c544c76 100644
@@ -495,23 +495,33 @@ typedef struct xen_sysctl_cputopoinfo xen_sysctl_cputopoinfo_t;
 DEFINE_XEN_GUEST_HANDLE(xen_sysctl_cputopoinfo_t);
 
 /* XEN_SYSCTL_numainfo */
-#define INVALID_NUMAINFO_ID (~0U)
+#define XEN_INVALID_MEM_SZ     (~0U)
+#define XEN_INVALID_NODE_DIST  ((uint8_t)~0)
+
+struct xen_sysctl_meminfo {
+    uint64_t memsize;
+    uint64_t memfree;
+};
+typedef struct xen_sysctl_meminfo xen_sysctl_meminfo_t;
+DEFINE_XEN_GUEST_HANDLE(xen_sysctl_meminfo_t);
+
 struct xen_sysctl_numainfo {
     /*
-     * IN: maximum addressable entry in the caller-provided arrays.
-     * OUT: largest node identifier in the system.
-     * If OUT is greater than IN then the arrays are truncated!
+     * IN: size of caller-provided arrays.
+     * OUT: number of nodes in the system.
      */
-    uint32_t max_node_index;
+    uint32_t num_nodes;
 
-    /* NB. Entries are 0 if node is not present. */
-    XEN_GUEST_HANDLE_64(uint64) node_to_memsize;
-    XEN_GUEST_HANDLE_64(uint64) node_to_memfree;
+    /*
+     * OUT: max_node_index-sized array. Entries are XEN_INVALID_MEM_SZ
+     * if node is not present.
+     */
+    XEN_GUEST_HANDLE_64(xen_sysctl_meminfo_t) meminfo;
 
     /*
-     * Array, of size (max_node_index+1)^2, listing memory access distances
+     * OUT: Array, of size (max_node_index+1)^2, listing memory access distances
      * between nodes. If an entry has no node distance information (e.g., node 
-     * not present) then the value ~0u is written.
+     * not present) then the value XEN_INVALID_NODE_DIST is written.
      * 
      * Note that the array rows must be indexed by multiplying by the minimum 
      * of the caller-provided max_node_index and the returned value of
@@ -522,7 +532,7 @@ struct xen_sysctl_numainfo {
      * in the system is larger than the caller can handle, then a 2-d array of
      * the maximum size handleable by the caller is constructed.
      */
-    XEN_GUEST_HANDLE_64(uint32) node_to_node_distance;
+    XEN_GUEST_HANDLE_64(uint8) distance;
 };
 typedef struct xen_sysctl_numainfo xen_sysctl_numainfo_t;
 DEFINE_XEN_GUEST_HANDLE(xen_sysctl_numainfo_t);
Signature · 1 line
1.7.1
#14 lore raw

Re: [PATCH v4 4/9] sysctl: Make XEN_SYSCTL_numainfo a little more efficient

Quoted 4 lines · Boris Ostrovsky

Make sysctl NUMA topology query use fewer copies by combining some fields into a single structure and copying distances for each node in a single copy.

Quoted 8 lines

Instead of using max_node_index for passing number of nodes keep this value in num_nodes: almost all uses of max_node_index required adding or subtracting one to eventually get to number of nodes anyway.

Replace INVALID_NUMAINFO_ID with XEN_INVALID_MEM_SZ and add XEN_INVALID_NODE_DIST.

I have the same concerns with the interface as patch 3, but other comments.

~Andrew

Acked-by on v4 4/9
#15 lore raw

Re: [PATCH v4 4/9] sysctl: Make XEN_SYSCTL_numainfo a little more efficient

In reply to Boris Ostrovsky
Quoted 4 lines · Boris Ostrovsky

Make sysctl NUMA topology query use fewer copies by combining some fields into a single structure and copying distances for each node in a single copy.

Quoted 8 lines

Instead of using max_node_index for passing number of nodes keep this value in num_nodes: almost all uses of max_node_index required adding or subtracting one to eventually get to number of nodes anyway.

Replace INVALID_NUMAINFO_ID with XEN_INVALID_MEM_SZ and add XEN_INVALID_NODE_DIST.

Assuming the h/v guys are happy with the interface the tools side looks like a valid wrapper to it:

Ian

#16 lore raw

Re: [PATCH v4 4/9] sysctl: Make XEN_SYSCTL_numainfo a little more efficient

In reply to Boris Ostrovsky

xen/include/public/sysctl.h struct xen_sysctl_numainfo { · line 535

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 10.03.15 at 03:27, <[email protected]> wrote:
xen/include/public/sysctl.h +1 -1
@@ -522,7 +532,7 @@ struct xen_sysctl_numainfo {
      * in the system is larger than the caller can handle, then a 2-d array of
      * the maximum size handleable by the caller is constructed.
      */
-    XEN_GUEST_HANDLE_64(uint32) node_to_node_distance;
+    XEN_GUEST_HANDLE_64(uint8) distance;

For an architecture neutral interface this shouldn't be artificially constrained to 8 bits.

Jan

#17 lore raw

PATCH v4 5/9 sysctl: Add sysctl interface for querying PCI topology

In reply to Boris Ostrovsky

---

Changes in v4:
* No buffer allocation in sysctl, copy data device-by-device
* Replace INVALID_TOPOLOGY_ID with XEN_INVALID_NODE_ID

 xen/common/sysctl.c         |   59 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/public/sysctl.h |   29 +++++++++++++++++++++
 2 files changed, 88 insertions(+), 0 deletions(-)
xen/common/sysctl.c +59 -0
index 2d11a76..9cd6321 100644
@@ -387,7 +387,66 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl)
         }
     }
     break;
+#ifdef HAS_PCI
+    case XEN_SYSCTL_pcitopoinfo:
+    {
+        xen_sysctl_pcitopoinfo_t *ti = &op->u.pcitopoinfo;
+
+        if ( guest_handle_is_null(ti->devs) ||
+             guest_handle_is_null(ti->nodes) ||
+             (ti->first_dev > ti->num_devs) )
+        {
+            ret = -EINVAL;
+            break;
+        }
+
+        for ( ; ti->first_dev < ti->num_devs; ti->first_dev++ )
+        {
+            physdev_pci_device_t dev;
+            uint8_t node;
+            struct pci_dev *pdev;
+
+            if ( copy_from_guest_offset(&dev, ti->devs, ti->first_dev, 1) )
+            {
+                ret = -EFAULT;
+                break;
+            }
+
+            spin_lock(&pcidevs_lock);
+            pdev = pci_get_pdev(dev.seg, dev.bus, dev.devfn);
+            if ( !pdev || (pdev->node == NUMA_NO_NODE) )
+                node = XEN_INVALID_NODE_ID;
+            else
+                node = pdev->node;
+            spin_unlock(&pcidevs_lock);
+
+            if ( copy_to_guest_offset(ti->nodes, ti->first_dev, &node, 1) )
+            {
+                ret = -EFAULT;
+                break;
+            }
 
+            if ( hypercall_preempt_check() )
+                break;
+        }
+
+        if ( !ret )
+        {
+            ti->first_dev++;
+
+            if ( __copy_field_to_guest(u_sysctl, op, u.pcitopoinfo.first_dev) )
+            {
+                ret = -EFAULT;
+                break;
+            }
+
+            if ( ti->first_dev < ti->num_devs )
+                ret = hypercall_create_continuation(__HYPERVISOR_sysctl,
+                                                    "h", u_sysctl);
+        }
+    }
+    break;
+#endif
 #ifdef TEST_COVERAGE
     case XEN_SYSCTL_coverage_op:
         ret = sysctl_coverage_op(&op->u.coverage_op);
xen/include/public/sysctl.h +29 -0
index c544c76..a224951 100644
@@ -33,6 +33,7 @@
 
 #include "xen.h"
 #include "domctl.h"
+#include "physdev.h"
 
 #define XEN_SYSCTL_INTERFACE_VERSION 0x0000000B
 
@@ -494,6 +495,32 @@ struct xen_sysctl_cputopoinfo {
 typedef struct xen_sysctl_cputopoinfo xen_sysctl_cputopoinfo_t;
 DEFINE_XEN_GUEST_HANDLE(xen_sysctl_cputopoinfo_t);
 
+/* XEN_SYSCTL_pcitopoinfo */
+struct xen_sysctl_pcitopoinfo {
+    /* IN: Size of pcitopo array */
+    uint32_t num_devs;
+
+    /*
+     * IN/OUT: First element of pcitopo array that needs to be processed by
+     * hypervisor.
+     * This is used primarily by hypercall continuations and callers will
+     * typically set it to zero.
+     */
+    uint32_t first_dev;
+
+    /* IN: list of devices */
+    XEN_GUEST_HANDLE_64(physdev_pci_device_t) devs;
+
+    /*
+     * OUT: node identifier for each device.
+     * If information for a particular device is not avalable then set
+     * to XEN_INVALID_NODE_ID.
+     */
+    XEN_GUEST_HANDLE_64(uint8) nodes;
+};
+typedef struct xen_sysctl_pcitopoinfo xen_sysctl_pcitopoinfo_t;
+DEFINE_XEN_GUEST_HANDLE(xen_sysctl_pcitopoinfo_t);
+
 /* XEN_SYSCTL_numainfo */
 #define XEN_INVALID_MEM_SZ     (~0U)
 #define XEN_INVALID_NODE_DIST  ((uint8_t)~0)
@@ -694,12 +721,14 @@ struct xen_sysctl {
 #define XEN_SYSCTL_scheduler_op                  19
 #define XEN_SYSCTL_coverage_op                   20
 #define XEN_SYSCTL_psr_cmt_op                    21
+#define XEN_SYSCTL_pcitopoinfo                   22
     uint32_t interface_version; /* XEN_SYSCTL_INTERFACE_VERSION */
     union {
         struct xen_sysctl_readconsole       readconsole;
         struct xen_sysctl_tbuf_op           tbuf_op;
         struct xen_sysctl_physinfo          physinfo;
         struct xen_sysctl_cputopoinfo       cputopoinfo;
+        struct xen_sysctl_pcitopoinfo       pcitopoinfo;
         struct xen_sysctl_numainfo          numainfo;
         struct xen_sysctl_sched_id          sched_id;
         struct xen_sysctl_perfc_op          perfc_op;
Signature · 1 line
1.7.1
#18 lore raw

Re: [PATCH v4 5/9] sysctl: Add sysctl interface for querying PCI topology

Quoted 16 lines · Boris Ostrovsky

---

Changes in v4:
* No buffer allocation in sysctl, copy data device-by-device
* Replace INVALID_TOPOLOGY_ID with XEN_INVALID_NODE_ID

 xen/common/sysctl.c         |   59 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/public/sysctl.h |   29 +++++++++++++++++++++
 2 files changed, 88 insertions(+), 0 deletions(-)
xen/common/sysctl.c +0 -0
index 2d11a76..9cd6321 100644

xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 389

Quoted 3 lines
xen/common/sysctl.c +0 -0
@@ -387,7 +387,66 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl)
         }
     }
     break;

Blank line here.

xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 391

Quoted 2 lines
xen/common/sysctl.c +2 -0
+#ifdef HAS_PCI
+    case XEN_SYSCTL_pcitopoinfo:

Please try and keep the SYSCTL implementations in numerical order, which should put this new block below the pcr and coverage blocks.

Quoted 48 lines
xen/common/sysctl.c +47 -0
+    {
+        xen_sysctl_pcitopoinfo_t *ti = &op->u.pcitopoinfo;
+
+        if ( guest_handle_is_null(ti->devs) ||
+             guest_handle_is_null(ti->nodes) ||
+             (ti->first_dev > ti->num_devs) )
+        {
+            ret = -EINVAL;
+            break;
+        }
+
+        for ( ; ti->first_dev < ti->num_devs; ti->first_dev++ )
+        {
+            physdev_pci_device_t dev;
+            uint8_t node;
+            struct pci_dev *pdev;
+
+            if ( copy_from_guest_offset(&dev, ti->devs, ti->first_dev, 1) )
+            {
+                ret = -EFAULT;
+                break;
+            }
+
+            spin_lock(&pcidevs_lock);
+            pdev = pci_get_pdev(dev.seg, dev.bus, dev.devfn);
+            if ( !pdev || (pdev->node == NUMA_NO_NODE) )
+                node = XEN_INVALID_NODE_ID;
+            else
+                node = pdev->node;
+            spin_unlock(&pcidevs_lock);
+
+            if ( copy_to_guest_offset(ti->nodes, ti->first_dev, &node, 1) )
+            {
+                ret = -EFAULT;
+                break;
+            }
 
+            if ( hypercall_preempt_check() )
+                break;
+        }
+
+        if ( !ret )
+        {
+            ti->first_dev++;
+
+            if ( __copy_field_to_guest(u_sysctl, op, u.pcitopoinfo.first_dev) )
+            {
+                ret = -EFAULT;

xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 449

Quoted 10 lines
xen/common/sysctl.c +10 -0
+                break;
+            }
+
+            if ( ti->first_dev < ti->num_devs )
+                ret = hypercall_create_continuation(__HYPERVISOR_sysctl,
+                                                    "h", u_sysctl);
+        }
+    }
+    break;
+#endif

And here.

Quoted 16 lines
xen/common/sysctl.c +0 -0
 #ifdef TEST_COVERAGE
     case XEN_SYSCTL_coverage_op:
         ret = sysctl_coverage_op(&op->u.coverage_op);
xen/include/public/sysctl.h +1 -0
index c544c76..a224951 100644
@@ -33,6 +33,7 @@
 
 #include "xen.h"
 #include "domctl.h"
+#include "physdev.h"
 
 #define XEN_SYSCTL_INTERFACE_VERSION 0x0000000B
 

xen/include/public/sysctl.h struct xen_sysctl_cputopoinfo { · line 500

Quoted 6 lines
xen/include/public/sysctl.h +3 -0
@@ -494,6 +495,32 @@ struct xen_sysctl_cputopoinfo {
 typedef struct xen_sysctl_cputopoinfo xen_sysctl_cputopoinfo_t;
 DEFINE_XEN_GUEST_HANDLE(xen_sysctl_cputopoinfo_t);
 
+/* XEN_SYSCTL_pcitopoinfo */
+struct xen_sysctl_pcitopoinfo {
+    /* IN: Size of pcitopo array */

Number of elements (as the two arrays are different sized structures) of both the devs and nodes array. It is not sensible for a caller to ever pass arrays of different lengths.

xen/include/public/sysctl.h struct xen_sysctl_cputopoinfo { · line 507

Quoted 7 lines
xen/include/public/sysctl.h +7 -0
+    uint32_t num_devs;
+
+    /*
+     * IN/OUT: First element of pcitopo array that needs to be processed by
+     * hypervisor.
+     * This is used primarily by hypercall continuations and callers will
+     * typically set it to zero.

I would word this more strongly and state that callers must set this to 0 and that it is an internal implementation detail of Xen.

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

Quoted 34 lines
xen/include/public/sysctl.h +18 -0
+     */
+    uint32_t first_dev;
+
+    /* IN: list of devices */
+    XEN_GUEST_HANDLE_64(physdev_pci_device_t) devs;
+
+    /*
+     * OUT: node identifier for each device.
+     * If information for a particular device is not avalable then set
+     * to XEN_INVALID_NODE_ID.
+     */
+    XEN_GUEST_HANDLE_64(uint8) nodes;
+};
+typedef struct xen_sysctl_pcitopoinfo xen_sysctl_pcitopoinfo_t;
+DEFINE_XEN_GUEST_HANDLE(xen_sysctl_pcitopoinfo_t);
+
 /* XEN_SYSCTL_numainfo */
 #define XEN_INVALID_MEM_SZ     (~0U)
 #define XEN_INVALID_NODE_DIST  ((uint8_t)~0)
@@ -694,12 +721,14 @@ struct xen_sysctl {
 #define XEN_SYSCTL_scheduler_op                  19
 #define XEN_SYSCTL_coverage_op                   20
 #define XEN_SYSCTL_psr_cmt_op                    21
+#define XEN_SYSCTL_pcitopoinfo                   22
     uint32_t interface_version; /* XEN_SYSCTL_INTERFACE_VERSION */
     union {
         struct xen_sysctl_readconsole       readconsole;
         struct xen_sysctl_tbuf_op           tbuf_op;
         struct xen_sysctl_physinfo          physinfo;
         struct xen_sysctl_cputopoinfo       cputopoinfo;
+        struct xen_sysctl_pcitopoinfo       pcitopoinfo;
         struct xen_sysctl_numainfo          numainfo;
         struct xen_sysctl_sched_id          sched_id;
         struct xen_sysctl_perfc_op          perfc_op;
#19 lore raw

Re: [PATCH v4 5/9] sysctl: Add sysctl interface for querying PCI topology

In reply to Boris Ostrovsky
Quoted 36 lines
Quoted 1 line
Quoted 1 line · On 10.03.15 at 03:27, <[email protected]> wrote:
xen/common/sysctl.c +35 -0
+    case XEN_SYSCTL_pcitopoinfo:
+    {
+        xen_sysctl_pcitopoinfo_t *ti = &op->u.pcitopoinfo;
+
+        if ( guest_handle_is_null(ti->devs) ||
+             guest_handle_is_null(ti->nodes) ||
+             (ti->first_dev > ti->num_devs) )
+        {
+            ret = -EINVAL;
+            break;
+        }
+
+        for ( ; ti->first_dev < ti->num_devs; ti->first_dev++ )
+        {
+            physdev_pci_device_t dev;
+            uint8_t node;
+            struct pci_dev *pdev;
+
+            if ( copy_from_guest_offset(&dev, ti->devs, ti->first_dev, 1) )
+            {
+                ret = -EFAULT;
+                break;
+            }
+
+            spin_lock(&pcidevs_lock);
+            pdev = pci_get_pdev(dev.seg, dev.bus, dev.devfn);
+            if ( !pdev || (pdev->node == NUMA_NO_NODE) )
+                node = XEN_INVALID_NODE_ID;
+            else
+                node = pdev->node;
+            spin_unlock(&pcidevs_lock);
+
+            if ( copy_to_guest_offset(ti->nodes, ti->first_dev, &node, 1) )
+            {
+                ret = -EFAULT;

xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 435

Quoted 10 lines
xen/common/sysctl.c +9 -0
+                break;
+            }
 
+            if ( hypercall_preempt_check() )
+                break;
+        }
+
+        if ( !ret )
+        {
+            ti->first_dev++;

This is correct in the preempt case, but it seems wrong to me in the completely-done one. Perhaps it would be better to put the increment right before the preempt check?

Quoted 11 lines
xen/include/public/sysctl.h +11 -0
+struct xen_sysctl_pcitopoinfo {
+    /* IN: Size of pcitopo array */
+    uint32_t num_devs;
+
+    /*
+     * IN/OUT: First element of pcitopo array that needs to be processed by
+     * hypervisor.
+     * This is used primarily by hypercall continuations and callers will
+     * typically set it to zero.
+     */
+    uint32_t first_dev;

xen/include/public/sysctl.h struct xen_sysctl_cputopoinfo { · line 519

Quoted 10 lines
xen/include/public/sysctl.h +10 -0
+
+    /* IN: list of devices */
+    XEN_GUEST_HANDLE_64(physdev_pci_device_t) devs;
+
+    /*
+     * OUT: node identifier for each device.
+     * If information for a particular device is not avalable then set
+     * to XEN_INVALID_NODE_ID.
+     */
+    XEN_GUEST_HANDLE_64(uint8) nodes;

As said in earlier patches - let's not expose the 8-bit limitation to user space, avoiding needless changes going forward.

Jan

#20 lore raw

Re: [PATCH v4 5/9] sysctl: Add sysctl interface for querying PCI topology

Quoted 39 lines · Jan Beulich
Quoted 39 lines
Quoted 1 line
Quoted 1 line · On 10.03.15 at 03:27, <[email protected]> wrote:
xen/common/sysctl.c +37 -0
+    case XEN_SYSCTL_pcitopoinfo:
+    {
+        xen_sysctl_pcitopoinfo_t *ti = &op->u.pcitopoinfo;
+
+        if ( guest_handle_is_null(ti->devs) ||
+             guest_handle_is_null(ti->nodes) ||
+             (ti->first_dev > ti->num_devs) )
+        {
+            ret = -EINVAL;
+            break;
+        }
+
+        for ( ; ti->first_dev < ti->num_devs; ti->first_dev++ )
+        {
+            physdev_pci_device_t dev;
+            uint8_t node;
+            struct pci_dev *pdev;
+
+            if ( copy_from_guest_offset(&dev, ti->devs, ti->first_dev, 1) )
+            {
+                ret = -EFAULT;
+                break;
+            }
+
+            spin_lock(&pcidevs_lock);
+            pdev = pci_get_pdev(dev.seg, dev.bus, dev.devfn);
+            if ( !pdev || (pdev->node == NUMA_NO_NODE) )
+                node = XEN_INVALID_NODE_ID;
+            else
+                node = pdev->node;
+            spin_unlock(&pcidevs_lock);
+
+            if ( copy_to_guest_offset(ti->nodes, ti->first_dev, &node, 1) )
+            {
+                ret = -EFAULT;
+                break;
+            }
Quoted 10 lines

xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 435

Quoted 7 lines
xen/common/sysctl.c +7 -0
+            if ( hypercall_preempt_check() )
+                break;
+        }
+
+        if ( !ret )
+        {
+            ti->first_dev++;

This is correct in the preempt case, but it seems wrong to me in the completely-done one. Perhaps it would be better to put the increment right before the preempt check?

Callers are really not supposed to use this field as it is useless as output.

But for consistency (and to make this look more natural) --- yes, moving it to the loop above (which will become a 'while') would be better.

-boris

#21 lore raw

PATCH v4 6/9 sysctl: Update sysctl version to 0x0000000C

In reply to Boris Ostrovsky

Need to bump sysctl version due to changes to CPU and NUMA topology data structures

---
 xen/include/public/sysctl.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
xen/include/public/sysctl.h +1 -1
index a224951..c3446e2 100644
@@ -35,7 +35,7 @@
 #include "domctl.h"
 #include "physdev.h"
 
-#define XEN_SYSCTL_INTERFACE_VERSION 0x0000000B
+#define XEN_SYSCTL_INTERFACE_VERSION 0x0000000C
 
 /*
  * Read console content from Xen buffer ring.
Signature · 1 line
1.7.1
#22 lore raw

PATCH v4 7/9 libxl/libxc: Move libxl_get_cpu_topology()'s hypercall buffer management to libxc

In reply to Boris Ostrovsky

xc_cputopoinfo() is not expected to be used on a hot path and therefore hypercall buffer management can be pushed into libxc. This will simplify life for callers.

Also update error reporting macros.

---

Changes in v4:
* Update commit message
* Make max_cpus argument in xc_cputopoinfo an unsigned
* Replace error logging macros

 tools/libxc/include/xenctrl.h     |    5 ++-
 tools/libxc/xc_misc.c             |   23 +++++++++++-----
 tools/libxl/libxl.c               |   33 ++++++++----------------
 tools/misc/xenpm.c                |   51 ++++++++++++++++---------------------
 tools/python/xen/lowlevel/xc/xc.c |   16 +++--------
 5 files changed, 57 insertions(+), 71 deletions(-)
tools/libxc/include/xenctrl.h +3 -2
index fef4cca..5550916 100644
@@ -1226,7 +1226,7 @@ int xc_readconsolering(xc_interface *xch,
 int xc_send_debug_keys(xc_interface *xch, char *keys);
 
 typedef xen_sysctl_physinfo_t xc_physinfo_t;
-typedef xen_sysctl_cputopoinfo_t xc_cputopoinfo_t;
+typedef xen_sysctl_cputopo_t xc_cputopo_t;
 typedef xen_sysctl_numainfo_t xc_numainfo_t;
 
 typedef uint32_t xc_cpu_to_node_t;
@@ -1237,7 +1237,8 @@ typedef uint64_t xc_node_to_memfree_t;
 typedef uint32_t xc_node_to_node_dist_t;
 
 int xc_physinfo(xc_interface *xch, xc_physinfo_t *info);
-int xc_cputopoinfo(xc_interface *xch, xc_cputopoinfo_t *info);
+int xc_cputopoinfo(xc_interface *xch, unsigned *max_cpus,
+                   xc_cputopo_t *cputopo);
 int xc_numainfo(xc_interface *xch, xc_numainfo_t *info);
 
 int xc_sched_id(xc_interface *xch,
tools/libxc/xc_misc.c +16 -7
index be68291..bc6eed2 100644
@@ -177,22 +177,31 @@ int xc_physinfo(xc_interface *xch,
     return 0;
 }
 
-int xc_cputopoinfo(xc_interface *xch,
-                   xc_cputopoinfo_t *put_info)
+int xc_cputopoinfo(xc_interface *xch, unsigned *max_cpus,
+                   xc_cputopo_t *cputopo)
 {
     int ret;
     DECLARE_SYSCTL;
+    DECLARE_HYPERCALL_BOUNCE(cputopo, *max_cpus * sizeof(*cputopo),
+                             XC_HYPERCALL_BUFFER_BOUNCE_OUT);
 
-    sysctl.cmd = XEN_SYSCTL_cputopoinfo;
+    if ((ret = xc_hypercall_bounce_pre(xch, cputopo)))
+        goto out;
 
-    memcpy(&sysctl.u.cputopoinfo, put_info, sizeof(*put_info));
+    sysctl.u.cputopoinfo.num_cpus = *max_cpus;
+    set_xen_guest_handle(sysctl.u.cputopoinfo.cputopo, cputopo);
+
+    sysctl.cmd = XEN_SYSCTL_cputopoinfo;
 
     if ( (ret = do_sysctl(xch, &sysctl)) != 0 )
-        return ret;
+        goto out;
 
-    memcpy(put_info, &sysctl.u.cputopoinfo, sizeof(*put_info));
+    *max_cpus = sysctl.u.cputopoinfo.num_cpus;
 
-    return 0;
+out:
+    xc_hypercall_bounce_post(xch, cputopo);
+
+    return ret;
 }
 
 int xc_numainfo(xc_interface *xch,
tools/libxl/libxl.c +11 -22
index 8a6f979..9d7d9f5 100644
@@ -5036,37 +5036,29 @@ int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
 libxl_cputopology *libxl_get_cpu_topology(libxl_ctx *ctx, int *nb_cpu_out)
 {
     GC_INIT(ctx);
-    xc_cputopoinfo_t tinfo;
-    DECLARE_HYPERCALL_BUFFER(xen_sysctl_cputopo_t, cputopo);
+    xc_cputopo_t *cputopo;
     libxl_cputopology *ret = NULL;
     int i;
     int max_cpus;
+    unsigned num_cpus;
 
     max_cpus = libxl_get_max_cpus(ctx);
     if (max_cpus < 0)
     {
-        LIBXL__LOG(ctx, XTL_ERROR, "Unable to determine number of CPUS");
-        ret = NULL;
+        LOG(ERROR, "Unable to determine number of CPUS");
         goto out;
     }
 
-    cputopo = xc_hypercall_buffer_alloc(ctx->xch, cputopo,
-                                        sizeof(*cputopo) * max_cpus);
-    if (cputopo == NULL) {
-        LIBXL__LOG_ERRNOVAL(ctx, XTL_ERROR, ENOMEM,
-                            "Unable to allocate hypercall arguments");
-        goto fail;
-    }
-    set_xen_guest_handle(tinfo.cputopo, cputopo);
-    tinfo.num_cpus = max_cpus;
+    cputopo = libxl__zalloc(gc, sizeof(*cputopo) * max_cpus);
 
-    if (xc_cputopoinfo(ctx->xch, &tinfo) != 0) {
-        LIBXL__LOG_ERRNO(ctx, XTL_ERROR, "CPU topology info hypercall failed");
-        goto fail;
+    num_cpus = max_cpus;
+    if (xc_cputopoinfo(ctx->xch, &num_cpus, cputopo) != 0) {
+        LOGE(ERROR, "CPU topology info hypercall failed");
+        goto out;
     }
 
-    if (tinfo.num_cpus < max_cpus)
-        max_cpus = tinfo.num_cpus;
+    if (num_cpus < max_cpus)
+        max_cpus = num_cpus;
 
     ret = libxl__zalloc(NOGC, sizeof(libxl_cputopology) * max_cpus);
 
@@ -5079,11 +5071,8 @@ libxl_cputopology *libxl_get_cpu_topology(libxl_ctx *ctx, int *nb_cpu_out)
 #undef V
     }
 
- fail:
-    xc_hypercall_buffer_free(ctx->xch, cputopo);
+    *nb_cpu_out = max_cpus;
 
-    if (ret)
-        *nb_cpu_out = max_cpus;
  out:
     GC_FREE;
     return ret;
tools/misc/xenpm.c +22 -29

124 lines not on this page · raw message

tools/python/xen/lowlevel/xc/xc.c +5 -11
index edbaf60..20c4d39 100644
@@ -1221,25 +1221,19 @@ static PyObject *pyxc_getcpuinfo(XcObject *self, PyObject *args, PyObject *kwds)
 static PyObject *pyxc_topologyinfo(XcObject *self)
 {
 #define MAX_CPUS 256
-    xc_cputopoinfo_t tinfo = { 0 };
+    xc_cputopo_t *cputopo;
     unsigned i, num_cpus;
     PyObject *ret_obj = NULL;
     PyObject *cpu_to_core_obj, *cpu_to_socket_obj, *cpu_to_node_obj;
 
-    DECLARE_HYPERCALL_BUFFER(xen_sysctl_cputopo_t, cputopo);
-
-    cputopo = xc_hypercall_buffer_alloc(self->xc_handle, cputopo,
-                                        sizeof(*cputopo) * (MAX_CPUS));
+    num_cpus = MAX_CPUS;
+    cputopo = calloc(num_cpus, sizeof(*cputopo));
     if ( cputopo == NULL )
     	goto out;
 
-    set_xen_guest_handle(tinfo.cputopo, cputopo);
-    tinfo.num_cpus = MAX_CPUS;
-
-    if ( xc_cputopoinfo(self->xc_handle, &tinfo) != 0 )
+    if ( xc_cputopoinfo(self->xc_handle, &num_cpus, cputopo) != 0 )
         goto out;
 
-    num_cpus = tinfo.num_cpus;
     if ( num_cpus > MAX_CPUS )
         num_cpus = MAX_CPUS;
 
@@ -1295,7 +1289,7 @@ static PyObject *pyxc_topologyinfo(XcObject *self)
     Py_DECREF(cpu_to_node_obj);
 
 out:
-    xc_hypercall_buffer_free(self->xc_handle, cputopo);
+    free(cputopo);
     return ret_obj ? ret_obj : pyxc_error_to_exception(self->xc_handle);
 #undef MAX_CPU_INDEX
 }
Signature · 1 line
1.7.1
Reviewed-by on v4 7/9
#23 lore raw

Re: [PATCH v4 7/9] libxl/libxc: Move libxl_get_cpu_topology()'s hypercall buffer management to libxc

Quoted 8 lines · Boris Ostrovsky

xc_cputopoinfo() is not expected to be used on a hot path and therefore hypercall buffer management can be pushed into libxc. This will simplify life for callers.

Also update error reporting macros.

Regards,
Dario

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Acked-by
#24 lore raw

Re: [PATCH v4 7/9] libxl/libxc: Move libxl_get_cpu_topology()'s hypercall buffer management to libxc

Quoted 10 lines · Dario Faggioli
Quoted 8 lines · Boris Ostrovsky

xc_cputopoinfo() is not expected to be used on a hot path and therefore hypercall buffer management can be pushed into libxc. This will simplify life for callers.

Also update error reporting macros.

(without close review of my own, thanks Dario).

#25 lore raw

PATCH v4 8/9 libxl/libxc: Move libxl_get_numainfo()'s hypercall buffer management to libxc

In reply to Boris Ostrovsky

xc_numainfo() is not expected to be used on a hot path and therefore hypercall buffer management can be pushed into libxc. This will simplify life for callers.

Also update error logging macros.

---

Changes in v4:
* Update commit message
* Make max_nodes argument in xc_numainfo an unsigned
* Replace error logging macros

 tools/libxc/include/xenctrl.h     |    4 ++-
 tools/libxc/xc_misc.c             |   29 +++++++++++++++++++------
 tools/libxl/libxl.c               |   42 +++++++++++-------------------------
 tools/python/xen/lowlevel/xc/xc.c |   27 ++++++++---------------
 4 files changed, 47 insertions(+), 55 deletions(-)
tools/libxc/include/xenctrl.h +3 -1
index 5550916..09b0ec2 100644
@@ -1228,6 +1228,7 @@ int xc_send_debug_keys(xc_interface *xch, char *keys);
 typedef xen_sysctl_physinfo_t xc_physinfo_t;
 typedef xen_sysctl_cputopo_t xc_cputopo_t;
 typedef xen_sysctl_numainfo_t xc_numainfo_t;
+typedef xen_sysctl_meminfo_t xc_meminfo_t;
 
 typedef uint32_t xc_cpu_to_node_t;
 typedef uint32_t xc_cpu_to_socket_t;
@@ -1239,7 +1240,8 @@ typedef uint32_t xc_node_to_node_dist_t;
 int xc_physinfo(xc_interface *xch, xc_physinfo_t *info);
 int xc_cputopoinfo(xc_interface *xch, unsigned *max_cpus,
                    xc_cputopo_t *cputopo);
-int xc_numainfo(xc_interface *xch, xc_numainfo_t *info);
+int xc_numainfo(xc_interface *xch, unsigned *max_nodes,
+                xc_meminfo_t *meminfo, uint8_t *distance);
 
 int xc_sched_id(xc_interface *xch,
                 int *sched_id);
tools/libxc/xc_misc.c +22 -7
index bc6eed2..58d3883 100644
@@ -204,22 +204,37 @@ out:
     return ret;
 }
 
-int xc_numainfo(xc_interface *xch,
-                xc_numainfo_t *put_info)
+int xc_numainfo(xc_interface *xch, unsigned *max_nodes,
+                xc_meminfo_t *meminfo, uint8_t *distance)
 {
     int ret;
     DECLARE_SYSCTL;
+    DECLARE_HYPERCALL_BOUNCE(meminfo, *max_nodes * sizeof(*meminfo),
+                             XC_HYPERCALL_BUFFER_BOUNCE_OUT);
+    DECLARE_HYPERCALL_BOUNCE(distance,
+                             *max_nodes * *max_nodes * sizeof(*distance),
+                             XC_HYPERCALL_BUFFER_BOUNCE_OUT);
 
-    sysctl.cmd = XEN_SYSCTL_numainfo;
+    if ((ret = xc_hypercall_bounce_pre(xch, meminfo)))
+        goto out;
+    if ((ret = xc_hypercall_bounce_pre(xch, distance)))
+        goto out;
 
-    memcpy(&sysctl.u.numainfo, put_info, sizeof(*put_info));
+    sysctl.u.numainfo.num_nodes = *max_nodes;
+    set_xen_guest_handle(sysctl.u.numainfo.meminfo, meminfo);
+    set_xen_guest_handle(sysctl.u.numainfo.distance, distance);
 
+    sysctl.cmd = XEN_SYSCTL_numainfo;
     if ((ret = do_sysctl(xch, &sysctl)) != 0)
-        return ret;
+        goto out;
 
-    memcpy(put_info, &sysctl.u.numainfo, sizeof(*put_info));
+    *max_nodes = sysctl.u.numainfo.num_nodes;
 
-    return 0;
+out:
+    xc_hypercall_bounce_post(xch, meminfo);
+    xc_hypercall_bounce_post(xch, distance);
+
+    return ret;
 }
 
 
tools/libxl/libxl.c +13 -29

64 lines not on this page · raw message

tools/python/xen/lowlevel/xc/xc.c +9 -18
index 20c4d39..dacf49e 100644
@@ -1297,32 +1297,23 @@ out:
 static PyObject *pyxc_numainfo(XcObject *self)
 {
 #define MAX_NODES 32
-    xc_numainfo_t ninfo = { 0 };
     unsigned i, j, num_nodes;
     uint64_t free_heap;
     PyObject *ret_obj = NULL, *node_to_node_dist_list_obj;
     PyObject *node_to_memsize_obj, *node_to_memfree_obj;
     PyObject *node_to_dma32_mem_obj, *node_to_node_dist_obj;
-    DECLARE_HYPERCALL_BUFFER(xen_sysctl_meminfo_t, meminfo);
-    DECLARE_HYPERCALL_BUFFER(uint8_t, distance);
+    xc_meminfo_t *meminfo;
+    uint8_t *distance;
 
-    meminfo = xc_hypercall_buffer_alloc(self->xc_handle, meminfo,
-                                        sizeof(*meminfo) * MAX_NODES);
-    if ( meminfo == NULL )
+    num_nodes = MAX_NODES;
+    meminfo = calloc(num_nodes, sizeof(*meminfo));
+    distance = calloc(num_nodes * num_nodes, sizeof(*distance));
+    if ( meminfo == NULL || distance == NULL)
         goto out;
-    distance = xc_hypercall_buffer_alloc(self->xc_handle, distance,
-                                         sizeof(*distance) * MAX_NODES * MAX_NODES);
-    if ( distance == NULL )
-        goto out;
-
-    set_xen_guest_handle(ninfo.meminfo, meminfo);
-    set_xen_guest_handle(ninfo.distance, distance);
-    ninfo.num_nodes = MAX_NODES;
 
-    if ( xc_numainfo(self->xc_handle, &ninfo) != 0 )
+    if ( xc_numainfo(self->xc_handle, &num_nodes, meminfo, distance) != 0 )
         goto out;
 
-    num_nodes = ninfo.num_nodes;
     if ( num_nodes > MAX_NODES )
         num_nodes = MAX_NODES;
 
@@ -1389,8 +1380,8 @@ static PyObject *pyxc_numainfo(XcObject *self)
     Py_DECREF(node_to_node_dist_list_obj);
 
 out:
-    xc_hypercall_buffer_free(self->xc_handle, meminfo);
-    xc_hypercall_buffer_free(self->xc_handle, distance);
+    free(meminfo);
+    free(distance);
     return ret_obj ? ret_obj : pyxc_error_to_exception(self->xc_handle);
 #undef MAX_NODE_INDEX
 }
Signature · 1 line
1.7.1
Reviewed-by on v4 8/9
#26 lore raw

Re: [PATCH v4 8/9] libxl/libxc: Move libxl_get_numainfo()'s hypercall buffer management to libxc

Quoted 8 lines · Boris Ostrovsky

xc_numainfo() is not expected to be used on a hot path and therefore hypercall buffer management can be pushed into libxc. This will simplify life for callers.

Also update error logging macros.

Regards,
Dario

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Acked-by
#27 lore raw

Re: [PATCH v4 8/9] libxl/libxc: Move libxl_get_numainfo()'s hypercall buffer management to libxc

Quoted 10 lines · Dario Faggioli
Quoted 8 lines · Boris Ostrovsky

xc_numainfo() is not expected to be used on a hot path and therefore hypercall buffer management can be pushed into libxc. This will simplify life for callers.

Also update error logging macros.

(again without close review, thanks Dario)

#28 lore raw

PATCH v4 9/9 libxl: Add interface for querying hypervisor about PCI topology

In reply to Boris Ostrovsky

.. and use this new interface to display it along with CPU topology and NUMA information when 'xl info -n' command is issued

The output will look like
...
cpu_topology :
cpu: core socket node
0: 0 0 0
...
device topology :
device node
0000:00:00.0 0
0000:00:01.0 0
...

---

Changes in v4:
* Rename LIBXL_HAVE_PCITOPO to LIBXL_HAVE_PCITOPOLOGY, drop ifdefs from code
* Don't treat no PCI devices as an error in libxl_get_pci_topology()
* Drop unnecessary comments
* Make num_devs argument in xc_pcitopoinfo an unsigned

 tools/libxc/include/xenctrl.h |    3 ++
 tools/libxc/xc_misc.c         |   31 ++++++++++++++++++
 tools/libxl/libxl.c           |   42 +++++++++++++++++++++++++
 tools/libxl/libxl.h           |   12 +++++++
 tools/libxl/libxl_freebsd.c   |   12 +++++++
 tools/libxl/libxl_internal.h  |    5 +++
 tools/libxl/libxl_linux.c     |   69 +++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_netbsd.c    |   12 +++++++
 tools/libxl/libxl_types.idl   |    7 ++++
 tools/libxl/libxl_utils.c     |    8 +++++
 tools/libxl/xl_cmdimpl.c      |   40 +++++++++++++++++++----
 11 files changed, 234 insertions(+), 7 deletions(-)
tools/libxc/include/xenctrl.h +3 -0
index 09b0ec2..c08f253 100644
@@ -1229,6 +1229,7 @@ typedef xen_sysctl_physinfo_t xc_physinfo_t;
 typedef xen_sysctl_cputopo_t xc_cputopo_t;
 typedef xen_sysctl_numainfo_t xc_numainfo_t;
 typedef xen_sysctl_meminfo_t xc_meminfo_t;
+typedef xen_sysctl_pcitopoinfo_t xc_pcitopoinfo_t;
 
 typedef uint32_t xc_cpu_to_node_t;
 typedef uint32_t xc_cpu_to_socket_t;
@@ -1242,6 +1243,8 @@ int xc_cputopoinfo(xc_interface *xch, unsigned *max_cpus,
                    xc_cputopo_t *cputopo);
 int xc_numainfo(xc_interface *xch, unsigned *max_nodes,
                 xc_meminfo_t *meminfo, uint8_t *distance);
+int xc_pcitopoinfo(xc_interface *xch, unsigned num_devs,
+                   physdev_pci_device_t *devs, uint8_t *nodes);
 
 int xc_sched_id(xc_interface *xch,
                 int *sched_id);
tools/libxc/xc_misc.c +31 -0
index 58d3883..f6cb05b 100644
@@ -237,6 +237,37 @@ out:
     return ret;
 }
 
+int xc_pcitopoinfo(xc_interface *xch, unsigned num_devs,
+                   physdev_pci_device_t *devs,
+                   uint8_t *nodes)
+{
+    int ret;
+    DECLARE_SYSCTL;
+    DECLARE_HYPERCALL_BOUNCE(devs, num_devs * sizeof(*devs),
+                             XC_HYPERCALL_BUFFER_BOUNCE_IN);
+    DECLARE_HYPERCALL_BOUNCE(nodes, num_devs* sizeof(*nodes),
+                             XC_HYPERCALL_BUFFER_BOUNCE_OUT);
+
+    if ((ret = xc_hypercall_bounce_pre(xch, devs)))
+        goto out;
+    if ((ret = xc_hypercall_bounce_pre(xch, nodes)))
+        goto out;
+
+    sysctl.u.pcitopoinfo.first_dev = 0;
+    sysctl.u.pcitopoinfo.num_devs = num_devs;
+    set_xen_guest_handle(sysctl.u.pcitopoinfo.devs, devs);
+    set_xen_guest_handle(sysctl.u.pcitopoinfo.nodes, nodes);
+
+    sysctl.cmd = XEN_SYSCTL_pcitopoinfo;
+
+    ret = do_sysctl(xch, &sysctl);
+
+ out:
+    xc_hypercall_bounce_post(xch, devs);
+    xc_hypercall_bounce_post(xch, nodes);
+
+    return ret;
+}
 
 int xc_sched_id(xc_interface *xch,
                 int *sched_id)
tools/libxl/libxl.c +42 -0

48 lines not on this page · raw message

tools/libxl/libxl.h +12 -0
index f784df5..a4c1bf5 100644
@@ -714,6 +714,14 @@ void libxl_mac_copy(libxl_ctx *ctx, libxl_mac *dst, libxl_mac *src);
 #define LIBXL_HAVE_PSR_CMT 1
 #endif
 
+/*
+ * LIBXL_HAVE_PCITOPOLOGY
+ *
+ * If this is defined, then interface to query hypervisor about PCI device
+ * topology is available.
+ */
+#define LIBXL_HAVE_PCITOPOLOGY 1
+
 typedef char **libxl_string_list;
 void libxl_string_list_dispose(libxl_string_list *sl);
 int libxl_string_list_length(const libxl_string_list *sl);
@@ -1092,6 +1100,10 @@ void libxl_vminfo_list_free(libxl_vminfo *list, int nb_vm);
 libxl_cputopology *libxl_get_cpu_topology(libxl_ctx *ctx, int *nb_cpu_out);
 void libxl_cputopology_list_free(libxl_cputopology *, int nb_cpu);
 
+#define LIBXL_PCITOPOLOGY_INVALID_ENTRY (~(uint32_t)0)
+libxl_pcitopology *libxl_get_pci_topology(libxl_ctx *ctx, int *num_devs);
+void libxl_pcitopology_list_free(libxl_pcitopology *, int num_devs);
+
 #define LIBXL_NUMAINFO_INVALID_ENTRY (~(uint32_t)0)
 libxl_numainfo *libxl_get_numainfo(libxl_ctx *ctx, int *nr);
 void libxl_numainfo_list_free(libxl_numainfo *, int nr);
tools/libxl/libxl_freebsd.c +12 -0
index e8b88b3..47c3391 100644
@@ -131,3 +131,15 @@ libxl_device_model_version libxl__default_device_model(libxl__gc *gc)
 {
     return LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN;
 }
+
+int libxl__pci_numdevs(libxl__gc *gc)
+{
+    return ERROR_NI;
+}
+
+int libxl__pci_topology_init(libxl__gc *gc,
+                             physdev_pci_device_t *devs,
+                             int num_devs)
+{
+    return ERROR_NI;
+}
tools/libxl/libxl_internal.h +5 -0
index 934465a..42942b2 100644
@@ -1170,6 +1170,11 @@ _hidden int libxl__try_phy_backend(mode_t st_mode);
 
 _hidden char *libxl__devid_to_localdev(libxl__gc *gc, int devid);
 
+_hidden int libxl__pci_numdevs(libxl__gc *gc);
+_hidden int libxl__pci_topology_init(libxl__gc *gc,
+                                     physdev_pci_device_t *devs,
+                                     int num_devs);
+
 /* from libxl_pci */
 
 _hidden int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcidev, int starting);
tools/libxl/libxl_linux.c +69 -0

72 lines not on this page · raw message

tools/libxl/libxl_netbsd.c +12 -0
index 898e160..a2a962e 100644
@@ -95,3 +95,15 @@ libxl_device_model_version libxl__default_device_model(libxl__gc *gc)
 {
     return LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL;
 }
+
+int libxl__pci_numdevs(libxl__gc *gc)
+{
+    return ERROR_NI;
+}
+
+int libxl__pci_topology_init(libxl__gc *gc,
+                             physdev_pci_device_t *devs,
+                             int num_devs)
+{
+    return ERROR_NI;
+}
tools/libxl/libxl_types.idl +7 -0
index 02be466..2accda3 100644
@@ -645,6 +645,13 @@ libxl_cputopology = Struct("cputopology", [
     ("node", uint32),
     ], dir=DIR_OUT)
 
+libxl_pcitopology = Struct("pcitopology", [
+    ("seg", uint16),
+    ("bus", uint8),
+    ("devfn", uint8),
+    ("node", uint32),
+    ], dir=DIR_OUT)
+
 libxl_sched_credit_params = Struct("sched_credit_params", [
     ("tslice_ms", integer),
     ("ratelimit_us", integer),
tools/libxl/libxl_utils.c +8 -0
index 7095b58..c31407f 100644
@@ -877,6 +877,14 @@ void libxl_cputopology_list_free(libxl_cputopology *list, int nr)
     free(list);
 }
 
+void libxl_pcitopology_list_free(libxl_pcitopology *list, int nr)
+{
+    int i;
+    for (i = 0; i < nr; i++)
+        libxl_pcitopology_dispose(&list[i]);
+    free(list);
+}
+
 void libxl_numainfo_list_free(libxl_numainfo *list, int nr)
 {
     int i;
tools/libxl/xl_cmdimpl.c +33 -7
index e41f633..b9fd8cf 100644
@@ -5214,12 +5214,15 @@ static void output_numainfo(void)
 
 static void output_topologyinfo(void)
 {
-    libxl_cputopology *info;
+    libxl_cputopology *cpuinfo;
     int i, nr;
+    libxl_pcitopology *pciinfo;
+    int valid_devs;
 
-    info = libxl_get_cpu_topology(ctx, &nr);
-    if (info == NULL) {
-        fprintf(stderr, "libxl_get_topologyinfo failed.\n");
+
+    cpuinfo = libxl_get_cpu_topology(ctx, &nr);
+    if (cpuinfo == NULL) {
+        fprintf(stderr, "libxl_get_cpu_topology failed.\n");
         return;
     }
 
@@ -5227,12 +5230,35 @@ static void output_topologyinfo(void)
     printf("cpu:    core    socket     node\n");
 
     for (i = 0; i < nr; i++) {
-        if (info[i].core != LIBXL_CPUTOPOLOGY_INVALID_ENTRY)
+        if (cpuinfo[i].core != LIBXL_CPUTOPOLOGY_INVALID_ENTRY)
             printf("%3d:    %4d     %4d     %4d\n", i,
-                   info[i].core, info[i].socket, info[i].node);
+                   cpuinfo[i].core, cpuinfo[i].socket, cpuinfo[i].node);
+    }
+
+    libxl_cputopology_list_free(cpuinfo, nr);
+
+    pciinfo = libxl_get_pci_topology(ctx, &nr);
+    if (cpuinfo == NULL) {
+        fprintf(stderr, "libxl_get_pci_topology failed.\n");
+        return;
     }
 
-    libxl_cputopology_list_free(info, nr);
+    printf("device topology        :\n");
+    printf("device           node\n");
+    for (i = 0; i < nr; i++) {
+        if (pciinfo[i].node != LIBXL_PCITOPOLOGY_INVALID_ENTRY) {
+            printf("%04x:%02x:%02x.%01x      %d\n", pciinfo[i].seg,
+                   pciinfo[i].bus,
+                   ((pciinfo[i].devfn >> 3) & 0x1f), (pciinfo[i].devfn & 7),
+                   pciinfo[i].node);
+            valid_devs++;
+        }
+    }
+
+    if (valid_devs == 0)
+        printf("No device topology data available\n");
+
+    libxl_pcitopology_list_free(pciinfo, nr);
 
     return;
 }
Signature · 1 line
1.7.1
Reviewed-by on v4 9/9
#29 lore raw

Re: [PATCH v4 9/9] libxl: Add interface for querying hypervisor about PCI topology

Quoted 15 lines · Boris Ostrovsky

.. and use this new interface to display it along with CPU topology and NUMA information when 'xl info -n' command is issued

The output will look like
...
cpu_topology :
cpu: core socket node
0: 0 0 0
...
device topology :
device node
0000:00:00.0 0
0000:00:01.0 0
...

Quoted 2 lines

Regards,
Dario

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Acked-by
#30 lore raw

Re: [PATCH v4 9/9] libxl: Add interface for querying hypervisor about PCI topology

Quoted 16 lines · Dario Faggioli
Quoted 15 lines · Boris Ostrovsky

.. and use this new interface to display it along with CPU topology and NUMA information when 'xl info -n' command is issued

The output will look like
...
cpu_topology :
cpu: core socket node
0: 0 0 0
...
device topology :
device node
0000:00:00.0 0
0000:00:01.0 0
...

Quoted 3 lines
Quoted 2 lines

(I only looked closely at the libxl API changes)

#31 lore raw
In reply to Boris Ostrovsky
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 10.03.15 at 03:27, <[email protected]> wrote:

Changes in v4:
* Split cputopology and NUMA info changes into separate patches
* Added patch#1 (partly because patch#4 needs to know when when distance is invalid, i.e. NUMA_NO_DISTANCE)
* Split sysctl version update into a separate patch

Why?

Jan

#32 lore raw
Quoted 7 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 10.03.15 at 03:27, <[email protected]> wrote:

Changes in v4:
* Split cputopology and NUMA info changes into separate patches
* Added patch#1 (partly because patch#4 needs to know when when distance is invalid, i.e. NUMA_NO_DISTANCE)
* Split sysctl version update into a separate patch

Why?

Which patch should it go to then? The first that changed the interfaces (patch#3) or the second (#4)?

-boris

#33 lore raw
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 10.03.15 at 14:39, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 10.03.15 at 03:27, <[email protected]> wrote:

Changes in v4:
* Split cputopology and NUMA info changes into separate patches
* Added patch#1 (partly because patch#4 needs to know when when distance is invalid, i.e. NUMA_NO_DISTANCE)
* Split sysctl version update into a separate patch

Quoted 4 lines
Quoted 1 line

Why?

Which patch should it go to then? The first that changed the interfaces (patch#3) or the second (#4)?

Whichever first changes the interface in an incompatible way.

Jan