Skip to contentSkip to conversation

CXL Hotness Monitoring Unit perf driver

RFC PATCH In review Jonathan Cameron 6 participants 23 messages Nov 21, 2024 – Aug 8, 2025

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

cover CXL Hotness Monitoring Unit perf driver

The CXL specification release 3.2 is now available under a click through at
https://computeexpresslink.org/cxl-specification/ and it brings new shiny toys.

RFC reason
- Whilst trace capture with a particular configuration is potentially useful the intent is that CXL HMU units will be used to drive various forms of hotpage migration for memory tiering setups. This driver doesn't do this (yet), but rather provides data capture etc for experimentation and for working out how to mostly put the allocations in the right place to start with by tuning applications.

CXL r3.2 introduces a CXL Hotness Monitoring Unit definition. The intent of this is to provide a way to establish which units of memory (typically pages or larger) in CXL attached memory are hot. The implementation details and algorithm are all implementation defined. The specification simply describes the 'interface' which takes the form of ring buffer of hotness records in a PCI BAR and defined capability, configuration and status registers.

The hardware may have constraints on what it can track, granularity etc and on how accurately it tracks (e.g. counter exhaustion, inaccurate trackers). Some of these constraints are discoverable from the hardware registers, others such as loss of accuracy have no universally accepted measures as they are typically access pattern dependent. Sadly it is very unlikely any hardware will implement a truly precise tracker given the large resource requirements for tracking at a useful granularity.

There are two fundamental operation modes:

* Epoch based. Counters are checked after a period of time (Epoch) and if over a threshold added to the hotlist.
* Always on. Counters run until a threshold is reached, after that the hot unit is added to the hotlist and the counter released.

Counting can be filtered on:

* Region of CXL DPA space (256MiB per bit in a bitmap).
* Type of access - Trusted and non trusted or non trusted only, R/W/RW

Sampling can be modified by:

* Downsampling including potentially randomized downsampling.

The driver presented here is intended to be useful in its own right but also to act as the first step of a possible path towards hotness monitoring based hot page migration. Those steps might look like.

1. Gather data - drivers provide telemetry like solutions to get that data. May be enhanced, for example in this driver by providing the HPA address rather than DPA Unit Address. Userspace can access enough information to do this so maybe not.
2. Userspace algorithm development, possibly combined with userspace triggered migration by PA. Working out how to use different levels of constrained hardware resources will be challenging.
3. Move those algorithms in kernel. Will require generalization across different hotpage trackers etc.

So far this driver just gives access to the raw data. I will probably kick of a longer discussion on how to do adaptive sampling needed to actually use these units for tiering etc, sometime soon (if no one one else beats me too it). There is a follow up topic of how to virtualize this stuff for memory stranding cases (VM gets a fixed mixture of fast and slow memory and should do it's own tiering).

More details in the Documentation patch but typical commands are:

$perf record -a -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,\
hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,\
range_size=1024,randomized_downsampling=0,downsampling_factor=32,\
hotness_granual=12

$perf report --dump-raw-traces

Example output. With a counter_width of 16 (0x10) the least significant 4 bytes are the counter value and the unit index is bits 16-63. Here all units are over the threshold and the indexes are 0,1,2 etc.

. ... CXL_HMU data: size 33512 bytes
Header 0: units: 29c counter_width 10
Header 1 : deadbeef
0000000000000283
0000000000010364
0000000000020366
000000000003033c
0000000000040343
00000000000502ff
000000000006030d
000000000007031a

Which will produce a list of hotness entries.
Bits[N-1:0] counter value
Bits[63:N] Unit ID (combine with unit size and DPA base + HDM decoder
config to get to a Host Physical Address)

Specific RFC questions.
- What should be in the header added to the aux buffer. Currently just the minimum is provided. Number of records and the counter width needed to decode them.
- Should we reset the counters when doing sampling "-F X"
If the frequency is higher than the epoch we never see any hot units. If so, when should we reset them?

Note testing has been light and on emulation only + as perf tool is a pain to build on a striped back VM, build testing has all be on arm64 so far. The driver loads though on both arm64 and x86 so any problems are likely in the perf tool arch specific code which is build tested (on wrong machine)

The QEMU emulation needs some cleanup, but I should be able to post that shortly to let people actually play with this. There are lots of open questions there on how 'right' we want the emulation to be and what counting uarch to emulate.

Jonathan Cameron (4):
cxl: Register devices for CXL Hotness Monitoring Units (CHMU)
cxl: Hotness Monitoring Unit via a Perf AUX Buffer.
perf: Add support for CXL Hotness Monitoring Units (CHMU)
hwtrace: Document CXL Hotness Monitoring Unit driver

 Documentation/trace/cxl-hmu.rst     | 197 +++++++
 Documentation/trace/index.rst       |   1 +
 drivers/cxl/Kconfig                 |   6 +
 drivers/cxl/Makefile                |   3 +
 drivers/cxl/core/Makefile           |   1 +
 drivers/cxl/core/core.h             |   1 +
 drivers/cxl/core/hmu.c              |  64 ++
 drivers/cxl/core/port.c             |   2 +
 drivers/cxl/core/regs.c             |  14 +
 drivers/cxl/cxl.h                   |   5 +
 drivers/cxl/cxlpci.h                |   1 +
 drivers/cxl/hmu.c                   | 880 ++++++++++++++++++++++++++++
 drivers/cxl/hmu.h                   |  23 +
 drivers/cxl/pci.c                   |  26 +-
 tools/perf/arch/arm/util/auxtrace.c |  58 ++
 tools/perf/arch/x86/util/auxtrace.c |  76 +++
 tools/perf/util/Build               |   1 +
 tools/perf/util/auxtrace.c          |   4 +
 tools/perf/util/auxtrace.h          |   1 +
 tools/perf/util/cxl-hmu.c           | 367 ++++++++++++
 tools/perf/util/cxl-hmu.h           |  18 +
 21 files changed, 1748 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/trace/cxl-hmu.rst
 create mode 100644 drivers/cxl/core/hmu.c
 create mode 100644 drivers/cxl/hmu.c
 create mode 100644 drivers/cxl/hmu.h
 create mode 100644 tools/perf/util/cxl-hmu.c
 create mode 100644 tools/perf/util/cxl-hmu.h
Signature · 1 line
2.43.0
#1 lore raw

RFC PATCH 1/4 cxl: Register devices for CXL Hotness Monitoring Units (CHMU)

Basic registration using similar approach to how the CPMUs are registered.

---
 drivers/cxl/core/Makefile |  1 +
 drivers/cxl/core/hmu.c    | 64 +++++++++++++++++++++++++++++++++++++++
 drivers/cxl/core/regs.c   | 14 +++++++++
 drivers/cxl/cxl.h         |  4 +++
 drivers/cxl/cxlpci.h      |  1 +
 drivers/cxl/hmu.h         | 23 ++++++++++++++
 drivers/cxl/pci.c         | 26 +++++++++++++++-
 7 files changed, 132 insertions(+), 1 deletion(-)
drivers/cxl/core/Makefile +1 -0
index 9259bcc6773c..d060abb773ae 100644
@@ -12,6 +12,7 @@ cxl_core-y += memdev.o
 cxl_core-y += mbox.o
 cxl_core-y += pci.o
 cxl_core-y += hdm.o
+cxl_core-y += hmu.o
 cxl_core-y += pmu.o
 cxl_core-y += cdat.o
 cxl_core-$(CONFIG_TRACING) += trace.o
drivers/cxl/core/hmu.c new file +64 -0

64 lines not on this page · raw message

drivers/cxl/core/regs.c +14 -0
index e1082e749c69..c12afaa6ef98 100644
@@ -401,6 +401,20 @@ int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs)
 }
 EXPORT_SYMBOL_NS_GPL(cxl_map_pmu_regs, CXL);
 
+int cxl_map_hmu_regs(struct cxl_register_map *map, struct cxl_hmu_regs *regs)
+{
+	struct device *dev = map->host;
+	resource_size_t phys_addr;
+
+	phys_addr = map->resource;
+	regs->hmu = devm_cxl_iomap_block(dev, phys_addr, map->max_size);
+	if (!regs->hmu)
+		return -ENOMEM;
+
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_map_hmu_regs, CXL);
+
 static int cxl_map_regblock(struct cxl_register_map *map)
 {
 	struct device *host = map->host;
drivers/cxl/cxl.h +4 -0
index 5406e3ab3d4a..8172bc1f7a8d 100644
@@ -227,6 +227,9 @@ struct cxl_regs {
 	struct_group_tagged(cxl_pmu_regs, pmu_regs,
 		void __iomem *pmu;
 	);
+	struct_group_tagged(cxl_hmu_regs, hmu_regs,
+		void __iomem *hmu;
+	);
 
 	/*
 	 * RCH downstream port specific RAS register
@@ -292,6 +295,7 @@ int cxl_map_component_regs(const struct cxl_register_map *map,
 			   unsigned long map_mask);
 int cxl_map_device_regs(const struct cxl_register_map *map,
 			struct cxl_device_regs *regs);
+int cxl_map_hmu_regs(struct cxl_register_map *map, struct cxl_hmu_regs *regs);
 int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs);
 
 enum cxl_regloc_type;
drivers/cxl/cxlpci.h +1 -0
index 4da07727ab9c..71f5e9620137 100644
@@ -67,6 +67,7 @@ enum cxl_regloc_type {
 	CXL_REGLOC_RBI_VIRT,
 	CXL_REGLOC_RBI_MEMDEV,
 	CXL_REGLOC_RBI_PMU,
+	CXL_REGLOC_RBI_HMU,
 	CXL_REGLOC_RBI_TYPES
 };
 
drivers/cxl/hmu.h new file +23 -0
new file mode 100644
index 000000000000..c4798ed9764b
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright(c) 2024 Huawei
+ * CXL Specification rev 3.2 Setion 8.2.8 (CHMU Register Interface)
+ */
+#ifndef CXL_HMU_H
+#define CXL_HMU_H
+#include <linux/device.h>
+
+#define CXL_HMU_REGMAP_SIZE 0xe00 /* Table 8-32 CXL 3.0 specification */
+struct cxl_hmu {
+	struct device dev;
+	void __iomem *base;
+	int assoc_id;
+	int index;
+};
+
+#define to_cxl_hmu(dev) container_of(dev, struct cxl_hmu, dev)
+struct cxl_hmu_regs;
+int devm_cxl_hmu_add(struct device *parent, struct cxl_hmu_regs *regs,
+		     int assoc_id, int idx);
+
+#endif
drivers/cxl/pci.c +25 -1
index 188412d45e0d..e89ea9d3f007 100644
@@ -15,6 +15,7 @@
 #include "cxlmem.h"
 #include "cxlpci.h"
 #include "cxl.h"
+#include "hmu.h"
 #include "pmu.h"
 
 /**
@@ -814,7 +815,7 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	struct cxl_dev_state *cxlds;
 	struct cxl_register_map map;
 	struct cxl_memdev *cxlmd;
-	int i, rc, pmu_count;
+	int i, rc, hmu_count, pmu_count;
 	bool irq_avail;
 
 	/*
@@ -938,6 +939,29 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		}
 	}
 
+	hmu_count = cxl_count_regblock(pdev, CXL_REGLOC_RBI_HMU);
+	for (i = 0; i < hmu_count; i++) {
+		struct cxl_hmu_regs hmu_regs;
+
+		rc = cxl_find_regblock_instance(pdev, CXL_REGLOC_RBI_HMU, &map, i);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not find HMU regblock\n");
+			break;
+		}
+
+		rc = cxl_map_hmu_regs(&map, &hmu_regs);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not map HMU regs\n");
+			break;
+		}
+
+		rc = devm_cxl_hmu_add(cxlds->dev, &hmu_regs, cxlmd->id, i);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not add HMU instance\n");
+			break;
+		}
+	}
+
 	rc = cxl_event_config(host_bridge, mds, irq_avail);
 	if (rc)
 		return rc;
Signature · 1 line
2.43.0
#2 lore raw

Re: [RFC PATCH 1/4] cxl: Register devices for CXL Hotness Monitoring Units (CHMU)

Quoted 161 lines · Jonathan Cameron

Basic registration using similar approach to how the CPMUs are registered.

---

drivers/cxl/core/Makefile |  1 +
drivers/cxl/core/hmu.c    | 64 +++++++++++++++++++++++++++++++++++++++
drivers/cxl/core/regs.c   | 14 +++++++++
drivers/cxl/cxl.h         |  4 +++
drivers/cxl/cxlpci.h      |  1 +
drivers/cxl/hmu.h         | 23 ++++++++++++++
drivers/cxl/pci.c         | 26 +++++++++++++++-
7 files changed, 132 insertions(+), 1 deletion(-)
drivers/cxl/core/Makefile +0 -0
index 9259bcc6773c..d060abb773ae 100644
@@ -12,6 +12,7 @@ cxl_core-y += memdev.o

cxl_core-y += mbox.o
cxl_core-y += pci.o
cxl_core-y += hdm.o
+cxl_core-y += hmu.o
cxl_core-y += pmu.o
cxl_core-y += cdat.o
cxl_core-$(CONFIG_TRACING) += trace.o

drivers/cxl/core/hmu.c new file +64 -0
new file mode 100644
index 000000000000..3ee938bb6c05
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright(c) 2024 Huawei. All rights reserved. */
+
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/idr.h>
+#include <cxlmem.h>
+#include <hmu.h>
+#include <cxl.h>
+#include "core.h"
+
+static void cxl_hmu_release(struct device *dev)
+{
+	struct cxl_hmu *hmu = to_cxl_hmu(dev);
+
+	kfree(hmu);
+}
+
+const struct device_type cxl_hmu_type = {
+	.name = "cxl_hmu",
+	.release = cxl_hmu_release,
+};
+
+static void remove_dev(void *dev)
+{
+	device_unregister(dev);
+}
+
+int devm_cxl_hmu_add(struct device *parent, struct cxl_hmu_regs *regs,
+		     int assoc_id, int index)
+{
+	struct cxl_hmu *hmu;
+	struct device *dev;
+	int rc;
+
+	hmu = kzalloc(sizeof(*hmu), GFP_KERNEL);
+	if (!hmu)
+		return -ENOMEM;
+
+	hmu->assoc_id = assoc_id;
+	hmu->index = index;
+	hmu->base = regs->hmu;
+	dev = &hmu->dev;
+	device_initialize(dev);
+	device_set_pm_not_required(dev);
+	dev->parent = parent;
+	dev->bus = &cxl_bus_type;
+	dev->type = &cxl_hmu_type;
+	rc = dev_set_name(dev, "hmu_mem%d.%d", assoc_id, index);
+	if (rc)
+		goto err;
+
+	rc = device_add(dev);
+	if (rc)
+		goto err;
+
+	return devm_add_action_or_reset(parent, remove_dev, dev);
+
+err:
+	put_device(&hmu->dev);
+	return rc;
+}
+EXPORT_SYMBOL_NS_GPL(devm_cxl_hmu_add, CXL);
+
drivers/cxl/core/regs.c +14 -0
index e1082e749c69..c12afaa6ef98 100644
@@ -401,6 +401,20 @@ int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs)
 }
 EXPORT_SYMBOL_NS_GPL(cxl_map_pmu_regs, CXL);
 
+int cxl_map_hmu_regs(struct cxl_register_map *map, struct cxl_hmu_regs *regs)
+{
+	struct device *dev = map->host;
+	resource_size_t phys_addr;
+
+	phys_addr = map->resource;
+	regs->hmu = devm_cxl_iomap_block(dev, phys_addr, map->max_size);
+	if (!regs->hmu)
+		return -ENOMEM;
+
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_map_hmu_regs, CXL);
+
 static int cxl_map_regblock(struct cxl_register_map *map)
 {
 	struct device *host = map->host;
drivers/cxl/cxl.h +4 -0
index 5406e3ab3d4a..8172bc1f7a8d 100644
@@ -227,6 +227,9 @@ struct cxl_regs {
 	struct_group_tagged(cxl_pmu_regs, pmu_regs,
 		void __iomem *pmu;
 	);
+	struct_group_tagged(cxl_hmu_regs, hmu_regs,
+		void __iomem *hmu;
+	);
 
 	/*
 	 * RCH downstream port specific RAS register
@@ -292,6 +295,7 @@ int cxl_map_component_regs(const struct cxl_register_map *map,
 			   unsigned long map_mask);
 int cxl_map_device_regs(const struct cxl_register_map *map,
 			struct cxl_device_regs *regs);
+int cxl_map_hmu_regs(struct cxl_register_map *map, struct cxl_hmu_regs *regs);
 int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs);
 
 enum cxl_regloc_type;
drivers/cxl/cxlpci.h +1 -0
index 4da07727ab9c..71f5e9620137 100644
@@ -67,6 +67,7 @@ enum cxl_regloc_type {
 	CXL_REGLOC_RBI_VIRT,
 	CXL_REGLOC_RBI_MEMDEV,
 	CXL_REGLOC_RBI_PMU,
+	CXL_REGLOC_RBI_HMU,
 	CXL_REGLOC_RBI_TYPES
 };
 
drivers/cxl/hmu.h new file +0 -0
new file mode 100644
index 000000000000..c4798ed9764b

drivers/cxl/hmu.h line 8

Quoted 8 lines
drivers/cxl/hmu.h new file +8 -0
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright(c) 2024 Huawei
+ * CXL Specification rev 3.2 Setion 8.2.8 (CHMU Register Interface)
+ */
+#ifndef CXL_HMU_H
+#define CXL_HMU_H
+#include <linux/device.h>

No compilation errors even by removing this header. I think this inclusion is not required. Also found similar include at drivers/cxl/pmu.h

drivers/cxl/hmu.h line 10

Quoted 2 lines
drivers/cxl/hmu.h new file +2 -0
+
+#define CXL_HMU_REGMAP_SIZE 0xe00 /* Table 8-32 CXL 3.0 specification */

Above Macro CXL_HMU_REGMAP_SIZE is not used, So we should remove it. Its comment is also not appropriate

Quoted 67 lines
drivers/cxl/hmu.h new file +13 -0
+struct cxl_hmu {
+	struct device dev;
+	void __iomem *base;
+	int assoc_id;
+	int index;
+};
+
+#define to_cxl_hmu(dev) container_of(dev, struct cxl_hmu, dev)
+struct cxl_hmu_regs;
+int devm_cxl_hmu_add(struct device *parent, struct cxl_hmu_regs *regs,
+		     int assoc_id, int idx);
+
+#endif
drivers/cxl/pci.c +25 -1
index 188412d45e0d..e89ea9d3f007 100644
@@ -15,6 +15,7 @@
 #include "cxlmem.h"
 #include "cxlpci.h"
 #include "cxl.h"
+#include "hmu.h"
 #include "pmu.h"
 
 /**
@@ -814,7 +815,7 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	struct cxl_dev_state *cxlds;
 	struct cxl_register_map map;
 	struct cxl_memdev *cxlmd;
-	int i, rc, pmu_count;
+	int i, rc, hmu_count, pmu_count;
 	bool irq_avail;
 
 	/*
@@ -938,6 +939,29 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		}
 	}
 
+	hmu_count = cxl_count_regblock(pdev, CXL_REGLOC_RBI_HMU);
+	for (i = 0; i < hmu_count; i++) {
+		struct cxl_hmu_regs hmu_regs;
+
+		rc = cxl_find_regblock_instance(pdev, CXL_REGLOC_RBI_HMU, &map, i);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not find HMU regblock\n");
+			break;
+		}
+
+		rc = cxl_map_hmu_regs(&map, &hmu_regs);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not map HMU regs\n");
+			break;
+		}
+
+		rc = devm_cxl_hmu_add(cxlds->dev, &hmu_regs, cxlmd->id, i);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not add HMU instance\n");
+			break;
+		}
+	}
+
 	rc = cxl_event_config(host_bridge, mds, irq_avail);
 	if (rc)
 		return rc;
Signature · 1 line
2.43.0
#3 lore raw

Re: [RFC PATCH 1/4] cxl: Register devices for CXL Hotness Monitoring Units (CHMU)

Quoted 10 lines
Quoted 10 lines
drivers/cxl/hmu.h new file +4 -0
new file mode 100644
index 000000000000..c4798ed9764b
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright(c) 2024 Huawei
+ * CXL Specification rev 3.2 Setion 8.2.8 (CHMU Register Interface)
Quoted 8 lines

drivers/cxl/hmu.h line 8

Quoted 4 lines
drivers/cxl/hmu.h new file +4 -0
+ */
+#ifndef CXL_HMU_H
+#define CXL_HMU_H
+#include <linux/device.h>  

No compilation errors even by removing this header. I think this inclusion is not required. Also found similar include at drivers/cxl/pmu.h

Kernel generally follows include what you use principles to avoid future issues due to reorganization of headers etc.

Here struct device definition is needed below so this header should be included. If there are other cases that do not do this, they should be fixed (there are ongoing efforts to clean this up btw by adding the missing includes).

Quoted 6 lines

drivers/cxl/hmu.h line 10

Quoted 3 lines
drivers/cxl/hmu.h new file +2 -0
+
+#define CXL_HMU_REGMAP_SIZE 0xe00 /* Table 8-32 CXL 3.0 specification */  

Above Macro CXL_HMU_REGMAP_SIZE is not used, So we should remove it. Its comment is also not appropriate

Not sure on the comment being in appropriate but sure this define can go away (and the comment with it).

Thanks for taking a look.

Jonathan

#4 lore raw

Re: [RFC PATCH 1/4] cxl: Register devices for CXL Hotness Monitoring Units (CHMU)

In reply to Jonathan Cameron
Quoted 101 lines · Jonathan Cameron

Basic registration using similar approach to how the CPMUs are registered.

---
 drivers/cxl/core/Makefile |  1 +
 drivers/cxl/core/hmu.c    | 64 +++++++++++++++++++++++++++++++++++++++
 drivers/cxl/core/regs.c   | 14 +++++++++
 drivers/cxl/cxl.h         |  4 +++
 drivers/cxl/cxlpci.h      |  1 +
 drivers/cxl/hmu.h         | 23 ++++++++++++++
 drivers/cxl/pci.c         | 26 +++++++++++++++-
 7 files changed, 132 insertions(+), 1 deletion(-)
drivers/cxl/core/Makefile +1 -0
index 9259bcc6773c..d060abb773ae 100644
@@ -12,6 +12,7 @@ cxl_core-y += memdev.o
 cxl_core-y += mbox.o
 cxl_core-y += pci.o
 cxl_core-y += hdm.o
+cxl_core-y += hmu.o
 cxl_core-y += pmu.o
 cxl_core-y += cdat.o
 cxl_core-$(CONFIG_TRACING) += trace.o
drivers/cxl/core/hmu.c new file +64 -0
new file mode 100644
index 000000000000..3ee938bb6c05
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright(c) 2024 Huawei. All rights reserved. */
+
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/idr.h>
+#include <cxlmem.h>
+#include <hmu.h>
+#include <cxl.h>
+#include "core.h"
+
+static void cxl_hmu_release(struct device *dev)
+{
+	struct cxl_hmu *hmu = to_cxl_hmu(dev);
+
+	kfree(hmu);
+}
+
+const struct device_type cxl_hmu_type = {
+	.name = "cxl_hmu",
+	.release = cxl_hmu_release,
+};
+
+static void remove_dev(void *dev)
+{
+	device_unregister(dev);
+}
+
+int devm_cxl_hmu_add(struct device *parent, struct cxl_hmu_regs *regs,
+		     int assoc_id, int index)
+{
+	struct cxl_hmu *hmu;
+	struct device *dev;
+	int rc;
+
+	hmu = kzalloc(sizeof(*hmu), GFP_KERNEL);
+	if (!hmu)
+		return -ENOMEM;
+
+	hmu->assoc_id = assoc_id;
+	hmu->index = index;
+	hmu->base = regs->hmu;
+	dev = &hmu->dev;
+	device_initialize(dev);
+	device_set_pm_not_required(dev);
+	dev->parent = parent;
+	dev->bus = &cxl_bus_type;
+	dev->type = &cxl_hmu_type;
+	rc = dev_set_name(dev, "hmu_mem%d.%d", assoc_id, index);
+	if (rc)
+		goto err;
+
+	rc = device_add(dev);
+	if (rc)
+		goto err;
+
+	return devm_add_action_or_reset(parent, remove_dev, dev);
+
+err:
+	put_device(&hmu->dev);
+	return rc;
+}
+EXPORT_SYMBOL_NS_GPL(devm_cxl_hmu_add, CXL);
+
drivers/cxl/core/regs.c +0 -0
index e1082e749c69..c12afaa6ef98 100644

drivers/cxl/core/regs.c int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs) · line 410

Quoted 10 lines
drivers/cxl/core/regs.c +7 -0
@@ -401,6 +401,20 @@ int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs)
 }
 EXPORT_SYMBOL_NS_GPL(cxl_map_pmu_regs, CXL);
 
+int cxl_map_hmu_regs(struct cxl_register_map *map, struct cxl_hmu_regs *regs)
+{
+	struct device *dev = map->host;
+	resource_size_t phys_addr;
+
+	phys_addr = map->resource;
+	regs->hmu = devm_cxl_iomap_block(dev, phys_addr, map->max_size);

I applied CHMU patch on 6.15.0 kernel and I tried to boot the virt with one cxl root port and one device (jic23/cxl-2025-06-10), then the dmesg shows "Failed to request region 0x10210000-0x1023ffff". I guess it is caused by the 'map->max_size'(0x30000) is large and the resource has been allocated by CPMU regs. I tried to change it to 0x10000, the hmu_mem0.0 could be created as normal.

Quoted 127 lines
drivers/cxl/core/regs.c +7 -0
+	if (!regs->hmu)
+		return -ENOMEM;
+
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_map_hmu_regs, CXL);
+
 static int cxl_map_regblock(struct cxl_register_map *map)
 {
 	struct device *host = map->host;
drivers/cxl/cxl.h +4 -0
index 5406e3ab3d4a..8172bc1f7a8d 100644
@@ -227,6 +227,9 @@ struct cxl_regs {
 	struct_group_tagged(cxl_pmu_regs, pmu_regs,
 		void __iomem *pmu;
 	);
+	struct_group_tagged(cxl_hmu_regs, hmu_regs,
+		void __iomem *hmu;
+	);
 
 	/*
 	 * RCH downstream port specific RAS register
@@ -292,6 +295,7 @@ int cxl_map_component_regs(const struct cxl_register_map *map,
 			   unsigned long map_mask);
 int cxl_map_device_regs(const struct cxl_register_map *map,
 			struct cxl_device_regs *regs);
+int cxl_map_hmu_regs(struct cxl_register_map *map, struct cxl_hmu_regs *regs);
 int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs);
 
 enum cxl_regloc_type;
drivers/cxl/cxlpci.h +1 -0
index 4da07727ab9c..71f5e9620137 100644
@@ -67,6 +67,7 @@ enum cxl_regloc_type {
 	CXL_REGLOC_RBI_VIRT,
 	CXL_REGLOC_RBI_MEMDEV,
 	CXL_REGLOC_RBI_PMU,
+	CXL_REGLOC_RBI_HMU,
 	CXL_REGLOC_RBI_TYPES
 };
 
drivers/cxl/hmu.h new file +23 -0
new file mode 100644
index 000000000000..c4798ed9764b
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright(c) 2024 Huawei
+ * CXL Specification rev 3.2 Setion 8.2.8 (CHMU Register Interface)
+ */
+#ifndef CXL_HMU_H
+#define CXL_HMU_H
+#include <linux/device.h>
+
+#define CXL_HMU_REGMAP_SIZE 0xe00 /* Table 8-32 CXL 3.0 specification */
+struct cxl_hmu {
+	struct device dev;
+	void __iomem *base;
+	int assoc_id;
+	int index;
+};
+
+#define to_cxl_hmu(dev) container_of(dev, struct cxl_hmu, dev)
+struct cxl_hmu_regs;
+int devm_cxl_hmu_add(struct device *parent, struct cxl_hmu_regs *regs,
+		     int assoc_id, int idx);
+
+#endif
drivers/cxl/pci.c +25 -1
index 188412d45e0d..e89ea9d3f007 100644
@@ -15,6 +15,7 @@
 #include "cxlmem.h"
 #include "cxlpci.h"
 #include "cxl.h"
+#include "hmu.h"
 #include "pmu.h"
 
 /**
@@ -814,7 +815,7 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	struct cxl_dev_state *cxlds;
 	struct cxl_register_map map;
 	struct cxl_memdev *cxlmd;
-	int i, rc, pmu_count;
+	int i, rc, hmu_count, pmu_count;
 	bool irq_avail;
 
 	/*
@@ -938,6 +939,29 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		}
 	}
 
+	hmu_count = cxl_count_regblock(pdev, CXL_REGLOC_RBI_HMU);
+	for (i = 0; i < hmu_count; i++) {
+		struct cxl_hmu_regs hmu_regs;
+
+		rc = cxl_find_regblock_instance(pdev, CXL_REGLOC_RBI_HMU, &map, i);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not find HMU regblock\n");
+			break;
+		}
+
+		rc = cxl_map_hmu_regs(&map, &hmu_regs);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not map HMU regs\n");
+			break;
+		}
+
+		rc = devm_cxl_hmu_add(cxlds->dev, &hmu_regs, cxlmd->id, i);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not add HMU instance\n");
+			break;
+		}
+	}
+
 	rc = cxl_event_config(host_bridge, mds, irq_avail);
 	if (rc)
 		return rc;
Signature · 1 line
2.43.0
#5 lore raw

Re: [RFC PATCH 1/4] cxl: Register devices for CXL Hotness Monitoring Units (CHMU)

Quoted 105 lines · On Thu, 19 Jun 2025 09:47:00 +0800 Yuquan Wang <[email protected]>…
Quoted 104 lines · Jonathan Cameron

Basic registration using similar approach to how the CPMUs are registered.

---
 drivers/cxl/core/Makefile |  1 +
 drivers/cxl/core/hmu.c    | 64 +++++++++++++++++++++++++++++++++++++++
 drivers/cxl/core/regs.c   | 14 +++++++++
 drivers/cxl/cxl.h         |  4 +++
 drivers/cxl/cxlpci.h      |  1 +
 drivers/cxl/hmu.h         | 23 ++++++++++++++
 drivers/cxl/pci.c         | 26 +++++++++++++++-
 7 files changed, 132 insertions(+), 1 deletion(-)
drivers/cxl/core/Makefile +1 -0
index 9259bcc6773c..d060abb773ae 100644
@@ -12,6 +12,7 @@ cxl_core-y += memdev.o
 cxl_core-y += mbox.o
 cxl_core-y += pci.o
 cxl_core-y += hdm.o
+cxl_core-y += hmu.o
 cxl_core-y += pmu.o
 cxl_core-y += cdat.o
 cxl_core-$(CONFIG_TRACING) += trace.o
drivers/cxl/core/hmu.c new file +64 -0
new file mode 100644
index 000000000000..3ee938bb6c05
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright(c) 2024 Huawei. All rights reserved. */
+
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/idr.h>
+#include <cxlmem.h>
+#include <hmu.h>
+#include <cxl.h>
+#include "core.h"
+
+static void cxl_hmu_release(struct device *dev)
+{
+	struct cxl_hmu *hmu = to_cxl_hmu(dev);
+
+	kfree(hmu);
+}
+
+const struct device_type cxl_hmu_type = {
+	.name = "cxl_hmu",
+	.release = cxl_hmu_release,
+};
+
+static void remove_dev(void *dev)
+{
+	device_unregister(dev);
+}
+
+int devm_cxl_hmu_add(struct device *parent, struct cxl_hmu_regs *regs,
+		     int assoc_id, int index)
+{
+	struct cxl_hmu *hmu;
+	struct device *dev;
+	int rc;
+
+	hmu = kzalloc(sizeof(*hmu), GFP_KERNEL);
+	if (!hmu)
+		return -ENOMEM;
+
+	hmu->assoc_id = assoc_id;
+	hmu->index = index;
+	hmu->base = regs->hmu;
+	dev = &hmu->dev;
+	device_initialize(dev);
+	device_set_pm_not_required(dev);
+	dev->parent = parent;
+	dev->bus = &cxl_bus_type;
+	dev->type = &cxl_hmu_type;
+	rc = dev_set_name(dev, "hmu_mem%d.%d", assoc_id, index);
+	if (rc)
+		goto err;
+
+	rc = device_add(dev);
+	if (rc)
+		goto err;
+
+	return devm_add_action_or_reset(parent, remove_dev, dev);
+
+err:
+	put_device(&hmu->dev);
+	return rc;
+}
+EXPORT_SYMBOL_NS_GPL(devm_cxl_hmu_add, CXL);
+
drivers/cxl/core/regs.c +0 -0
index e1082e749c69..c12afaa6ef98 100644
@@ -401,6 +401,20 @@ int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs)
 }
 EXPORT_SYMBOL_NS_GPL(cxl_map_pmu_regs, CXL);
 
Quoted 12 lines

drivers/cxl/core/regs.c int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs) · line 410

Quoted 7 lines
drivers/cxl/core/regs.c +7 -0
+int cxl_map_hmu_regs(struct cxl_register_map *map, struct cxl_hmu_regs *regs)
+{
+	struct device *dev = map->host;
+	resource_size_t phys_addr;
+
+	phys_addr = map->resource;
+	regs->hmu = devm_cxl_iomap_block(dev, phys_addr, map->max_size);  

I applied CHMU patch on 6.15.0 kernel and I tried to boot the virt with one cxl root port and one device (jic23/cxl-2025-06-10), then the dmesg shows "Failed to request region 0x10210000-0x1023ffff". I guess it is caused by the 'map->max_size'(0x30000) is large and the resource has been allocated by CPMU regs. I tried to change it to 0x10000, the hmu_mem0.0 could be created as normal.

Ah. I was meaning to post an updated version of this series just to fix the bug you've hit here but forgot to do so! Sorry about that.

I need to figure out if there is a more elegant way to do this but in meantime here is what I'm carrying on top of these posted series. It's a fairly horrible bit of layering as we need the generic code to know to poke around inside a temporary mapping just to get the size that it should iomap.

From fadffb32ed302dfb6dec4497214e1d3b39450f8b Mon Sep 17 00:00:00 2001
From: Jonathan Cameron <[email protected]>
Date: Thu, 17 Apr 2025 11:02:07 +0100
Subject: [PATCH] Fix up sizing of CHMU issue

---
 drivers/cxl/core/regs.c | 16 ++++++++++++++--
 drivers/cxl/cxlpci.h    |  7 +++++++
 drivers/cxl/hmu.c       |  6 ------
 3 files changed, 21 insertions(+), 8 deletions(-)
drivers/cxl/core/regs.c +14 -2
index 925870c4e494..5ae31696bf8b 100644
@@ -416,10 +416,22 @@ EXPORT_SYMBOL_NS_GPL(cxl_map_pmu_regs, "CXL");
 int cxl_map_hmu_regs(struct cxl_register_map *map, struct cxl_hmu_regs *regs)
 {
 	struct device *dev = map->host;
-	resource_size_t phys_addr;
+	u64 __iomem *poke;
+	u64 common_cap[2];
+	resource_size_t phys_addr, phys_size;
 
 	phys_addr = map->resource;
-	regs->hmu = devm_cxl_iomap_block(dev, phys_addr, map->max_size);
+	/* Finding out the size of a CHMU means poking around inside */
+	poke = ioremap(phys_addr, sizeof(common_cap));
+	if (!poke) {
+		return -ENOMEM;
+	}
+	common_cap[0] = le64_to_cpu(readq(poke));
+	common_cap[1] = le64_to_cpu(readq(poke + 1));
+	iounmap(poke);
+	phys_size = FIELD_GET(CHMU_COMMON_CAP0_NUMINST_MSK, common_cap[0]) *
+		FIELD_GET(CHMU_COMMON_CAP1_INSTLEN_MSK, common_cap[1]) + 0x10;
+	regs->hmu = devm_cxl_iomap_block(dev, phys_addr, phys_size);
 	if (!regs->hmu)
 		return -ENOMEM;
 
drivers/cxl/cxlpci.h +7 -0
index f7b902eab288..a91407292aea 100644
@@ -77,6 +77,13 @@ enum cxl_regloc_type {
 	CXL_REGLOC_RBI_TYPES
 };
 
+/* A few CHMU registers are needed to establish size */
+#define CHMU_COMMON_CAP0_REG				0x00
+#define   CHMU_COMMON_CAP0_VER_MSK			GENMASK(3, 0)
+#define   CHMU_COMMON_CAP0_NUMINST_MSK			GENMASK(15, 8)
+#define CHMU_COMMON_CAP1_REG				0x08
+#define   CHMU_COMMON_CAP1_INSTLEN_MSK			GENMASK(15, 0)
+
 /*
  * Table Access DOE, CDAT Read Entry Response
  *
drivers/cxl/hmu.c +0 -6
index 1a7a0f60a6ad..a1953e8750c8 100644
@@ -27,12 +27,6 @@
 #include "cxl.h"
 #include "hmu.h"
 
-#define CHMU_COMMON_CAP0_REG				0x00
-#define   CHMU_COMMON_CAP0_VER_MSK			GENMASK(3, 0)
-#define   CHMU_COMMON_CAP0_NUMINST_MSK			GENMASK(15, 8)
-#define CHMU_COMMON_CAP1_REG				0x08
-#define   CHMU_COMMON_CAP1_INSTLEN_MSK			GENMASK(15, 0)
-
 /* Register offsets within instance */
 #define CHMU_INST0_CAP0_REG				0x00
 #define   CHMU_INST0_CAP0_MSI_N_MSK			GENMASK(3, 0)
Signature · 1 line
2.48.1
Quoted 129 lines
Quoted 127 lines
drivers/cxl/core/regs.c +7 -0
+	if (!regs->hmu)
+		return -ENOMEM;
+
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_map_hmu_regs, CXL);
+
 static int cxl_map_regblock(struct cxl_register_map *map)
 {
 	struct device *host = map->host;
drivers/cxl/cxl.h +4 -0
index 5406e3ab3d4a..8172bc1f7a8d 100644
@@ -227,6 +227,9 @@ struct cxl_regs {
 	struct_group_tagged(cxl_pmu_regs, pmu_regs,
 		void __iomem *pmu;
 	);
+	struct_group_tagged(cxl_hmu_regs, hmu_regs,
+		void __iomem *hmu;
+	);
 
 	/*
 	 * RCH downstream port specific RAS register
@@ -292,6 +295,7 @@ int cxl_map_component_regs(const struct cxl_register_map *map,
 			   unsigned long map_mask);
 int cxl_map_device_regs(const struct cxl_register_map *map,
 			struct cxl_device_regs *regs);
+int cxl_map_hmu_regs(struct cxl_register_map *map, struct cxl_hmu_regs *regs);
 int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs);
 
 enum cxl_regloc_type;
drivers/cxl/cxlpci.h +1 -0
index 4da07727ab9c..71f5e9620137 100644
@@ -67,6 +67,7 @@ enum cxl_regloc_type {
 	CXL_REGLOC_RBI_VIRT,
 	CXL_REGLOC_RBI_MEMDEV,
 	CXL_REGLOC_RBI_PMU,
+	CXL_REGLOC_RBI_HMU,
 	CXL_REGLOC_RBI_TYPES
 };
 
drivers/cxl/hmu.h new file +23 -0
new file mode 100644
index 000000000000..c4798ed9764b
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright(c) 2024 Huawei
+ * CXL Specification rev 3.2 Setion 8.2.8 (CHMU Register Interface)
+ */
+#ifndef CXL_HMU_H
+#define CXL_HMU_H
+#include <linux/device.h>
+
+#define CXL_HMU_REGMAP_SIZE 0xe00 /* Table 8-32 CXL 3.0 specification */
+struct cxl_hmu {
+	struct device dev;
+	void __iomem *base;
+	int assoc_id;
+	int index;
+};
+
+#define to_cxl_hmu(dev) container_of(dev, struct cxl_hmu, dev)
+struct cxl_hmu_regs;
+int devm_cxl_hmu_add(struct device *parent, struct cxl_hmu_regs *regs,
+		     int assoc_id, int idx);
+
+#endif
drivers/cxl/pci.c +25 -1
index 188412d45e0d..e89ea9d3f007 100644
@@ -15,6 +15,7 @@
 #include "cxlmem.h"
 #include "cxlpci.h"
 #include "cxl.h"
+#include "hmu.h"
 #include "pmu.h"
 
 /**
@@ -814,7 +815,7 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	struct cxl_dev_state *cxlds;
 	struct cxl_register_map map;
 	struct cxl_memdev *cxlmd;
-	int i, rc, pmu_count;
+	int i, rc, hmu_count, pmu_count;
 	bool irq_avail;
 
 	/*
@@ -938,6 +939,29 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		}
 	}
 
+	hmu_count = cxl_count_regblock(pdev, CXL_REGLOC_RBI_HMU);
+	for (i = 0; i < hmu_count; i++) {
+		struct cxl_hmu_regs hmu_regs;
+
+		rc = cxl_find_regblock_instance(pdev, CXL_REGLOC_RBI_HMU, &map, i);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not find HMU regblock\n");
+			break;
+		}
+
+		rc = cxl_map_hmu_regs(&map, &hmu_regs);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not map HMU regs\n");
+			break;
+		}
+
+		rc = devm_cxl_hmu_add(cxlds->dev, &hmu_regs, cxlmd->id, i);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not add HMU instance\n");
+			break;
+		}
+	}
+
 	rc = cxl_event_config(host_bridge, mds, irq_avail);
 	if (rc)
 		return rc;
Signature · 1 line
2.43.0
#6 lore raw

Re: [RFC PATCH 1/4] cxl: Register devices for CXL Hotness Monitoring Units (CHMU)

In reply to Jonathan Cameron

Sorry for disturbing! I tried to test send more than 10 emails on my smtp server!

Quoted 234 lines

-----原始邮件-----
发件人: wangyuquan <[email protected]>
发送时间:2025-08-08 16:29:53 (星期五)
收件人: [email protected], [email protected], [email protected], [email protected], [email protected]
抄送: "Jonathan Cameron" <[email protected]>, [email protected], [email protected], "Yicong Yang" <[email protected]>, "Niyas Sait" <[email protected]>, [email protected], "Vandana Salve" <[email protected]>, "Davidlohr Bueso" <[email protected]>, "Dave Jiang" <[email protected]>, "Alison Schofield" <[email protected]>, "Ira Weiny" <[email protected]>, "Dan Williams" <[email protected]>, "Alexander Shishkin" <[email protected]>, "Peter Zijlstra" <[email protected]>, "Ingo Molnar" <[email protected]>, "Arnaldo Carvalho de Melo" <[email protected]>, "Mark Rutland" <[email protected]>, "Gregory Price" <[email protected]>, "Huang Ying" <[email protected]> 主题: [RFC PATCH 1/4] cxl: Register devices for CXL Hotness Monitoring Units (CHMU)

From: Jonathan Cameron <[email protected]>

Basic registration using similar approach to how the CPMUs are registered.

---
 drivers/cxl/core/Makefile |  1 +
 drivers/cxl/core/hmu.c    | 64 +++++++++++++++++++++++++++++++++++++++
 drivers/cxl/core/regs.c   | 14 +++++++++
 drivers/cxl/cxl.h         |  4 +++
 drivers/cxl/cxlpci.h      |  1 +
 drivers/cxl/hmu.h         | 23 ++++++++++++++
 drivers/cxl/pci.c         | 26 +++++++++++++++-
 7 files changed, 132 insertions(+), 1 deletion(-)
drivers/cxl/core/Makefile +1 -0
index 9259bcc6773c..d060abb773ae 100644
@@ -12,6 +12,7 @@ cxl_core-y += memdev.o
 cxl_core-y += mbox.o
 cxl_core-y += pci.o
 cxl_core-y += hdm.o
+cxl_core-y += hmu.o
 cxl_core-y += pmu.o
 cxl_core-y += cdat.o
 cxl_core-$(CONFIG_TRACING) += trace.o
drivers/cxl/core/hmu.c new file +64 -0
new file mode 100644
index 000000000000..3ee938bb6c05
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright(c) 2024 Huawei. All rights reserved. */
+
+#include <linux/device.h>
+#include <linux/slab.h>
+#include <linux/idr.h>
+#include <cxlmem.h>
+#include <hmu.h>
+#include <cxl.h>
+#include "core.h"
+
+static void cxl_hmu_release(struct device *dev)
+{
+	struct cxl_hmu *hmu = to_cxl_hmu(dev);
+
+	kfree(hmu);
+}
+
+const struct device_type cxl_hmu_type = {
+	.name = "cxl_hmu",
+	.release = cxl_hmu_release,
+};
+
+static void remove_dev(void *dev)
+{
+	device_unregister(dev);
+}
+
+int devm_cxl_hmu_add(struct device *parent, struct cxl_hmu_regs *regs,
+		     int assoc_id, int index)
+{
+	struct cxl_hmu *hmu;
+	struct device *dev;
+	int rc;
+
+	hmu = kzalloc(sizeof(*hmu), GFP_KERNEL);
+	if (!hmu)
+		return -ENOMEM;
+
+	hmu->assoc_id = assoc_id;
+	hmu->index = index;
+	hmu->base = regs->hmu;
+	dev = &hmu->dev;
+	device_initialize(dev);
+	device_set_pm_not_required(dev);
+	dev->parent = parent;
+	dev->bus = &cxl_bus_type;
+	dev->type = &cxl_hmu_type;
+	rc = dev_set_name(dev, "hmu_mem%d.%d", assoc_id, index);
+	if (rc)
+		goto err;
+
+	rc = device_add(dev);
+	if (rc)
+		goto err;
+
+	return devm_add_action_or_reset(parent, remove_dev, dev);
+
+err:
+	put_device(&hmu->dev);
+	return rc;
+}
+EXPORT_SYMBOL_NS_GPL(devm_cxl_hmu_add, CXL);
+
drivers/cxl/core/regs.c +14 -0
index e1082e749c69..c12afaa6ef98 100644
@@ -401,6 +401,20 @@ int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs)
 }
 EXPORT_SYMBOL_NS_GPL(cxl_map_pmu_regs, CXL);
 
+int cxl_map_hmu_regs(struct cxl_register_map *map, struct cxl_hmu_regs *regs)
+{
+	struct device *dev = map->host;
+	resource_size_t phys_addr;
+
+	phys_addr = map->resource;
+	regs->hmu = devm_cxl_iomap_block(dev, phys_addr, map->max_size);
+	if (!regs->hmu)
+		return -ENOMEM;
+
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_map_hmu_regs, CXL);
+
 static int cxl_map_regblock(struct cxl_register_map *map)
 {
 	struct device *host = map->host;
drivers/cxl/cxl.h +4 -0
index 5406e3ab3d4a..8172bc1f7a8d 100644
@@ -227,6 +227,9 @@ struct cxl_regs {
 	struct_group_tagged(cxl_pmu_regs, pmu_regs,
 		void __iomem *pmu;
 	);
+	struct_group_tagged(cxl_hmu_regs, hmu_regs,
+		void __iomem *hmu;
+	);
 
 	/*
 	 * RCH downstream port specific RAS register
@@ -292,6 +295,7 @@ int cxl_map_component_regs(const struct cxl_register_map *map,
 			   unsigned long map_mask);
 int cxl_map_device_regs(const struct cxl_register_map *map,
 			struct cxl_device_regs *regs);
+int cxl_map_hmu_regs(struct cxl_register_map *map, struct cxl_hmu_regs *regs);
 int cxl_map_pmu_regs(struct cxl_register_map *map, struct cxl_pmu_regs *regs);
 
 enum cxl_regloc_type;
drivers/cxl/cxlpci.h +1 -0
index 4da07727ab9c..71f5e9620137 100644
@@ -67,6 +67,7 @@ enum cxl_regloc_type {
 	CXL_REGLOC_RBI_VIRT,
 	CXL_REGLOC_RBI_MEMDEV,
 	CXL_REGLOC_RBI_PMU,
+	CXL_REGLOC_RBI_HMU,
 	CXL_REGLOC_RBI_TYPES
 };
 
drivers/cxl/hmu.h new file +23 -0
new file mode 100644
index 000000000000..c4798ed9764b
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright(c) 2024 Huawei
+ * CXL Specification rev 3.2 Setion 8.2.8 (CHMU Register Interface)
+ */
+#ifndef CXL_HMU_H
+#define CXL_HMU_H
+#include <linux/device.h>
+
+#define CXL_HMU_REGMAP_SIZE 0xe00 /* Table 8-32 CXL 3.0 specification */
+struct cxl_hmu {
+	struct device dev;
+	void __iomem *base;
+	int assoc_id;
+	int index;
+};
+
+#define to_cxl_hmu(dev) container_of(dev, struct cxl_hmu, dev)
+struct cxl_hmu_regs;
+int devm_cxl_hmu_add(struct device *parent, struct cxl_hmu_regs *regs,
+		     int assoc_id, int idx);
+
+#endif
drivers/cxl/pci.c +18 -1
index 188412d45e0d..e89ea9d3f007 100644
@@ -15,6 +15,7 @@
 #include "cxlmem.h"
 #include "cxlpci.h"
 #include "cxl.h"
+#include "hmu.h"
 #include "pmu.h"
 
 /**
@@ -814,7 +815,7 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	struct cxl_dev_state *cxlds;
 	struct cxl_register_map map;
 	struct cxl_memdev *cxlmd;
-	int i, rc, pmu_count;
+	int i, rc, hmu_count, pmu_count;
 	bool irq_avail;
 
 	/*
@@ -938,6 +939,29 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 		}
 	}
 
+	hmu_count = cxl_count_regblock(pdev, CXL_REGLOC_RBI_HMU);
+	for (i = 0; i < hmu_count; i++) {
+		struct cxl_hmu_regs hmu_regs;
+
+		rc = cxl_find_regblock_instance(pdev, CXL_REGLOC_RBI_HMU, &map, i);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not find HMU regblock\n");
+			break;
+		}
+
+		rc = cxl_map_hmu_regs(&map, &hmu_regs);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not map HMU regs\n");
+			break;
+		}
+

drivers/cxl/pci.c static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 967

Quoted 10 lines
drivers/cxl/pci.c +7 -0
+		rc = devm_cxl_hmu_add(cxlds->dev, &hmu_regs, cxlmd->id, i);
+		if (rc) {
+			dev_dbg(&pdev->dev, "Could not add HMU instance\n");
+			break;
+		}
+	}
+
 	rc = cxl_event_config(host_bridge, mds, irq_avail);
 	if (rc)
 		return rc;

信息安全声明:本邮件包含信息归发件人所在组织所有,发件人所在组织对该邮件拥有所有权利。请接收者注意保密,未经发件人书面许可,不得向任何第三方组织和个人透露本邮件所含信息。
Information Security Notice: The information contained in this mail is solely property of the sender's organization.This mail communication is confidential.Recipients named above are obligated to maintain secrecy and are not permitted to disclose the contents of this communication to others.

#7 lore raw

RFC PATCH 2/4 cxl: Hotness Monitoring Unit via a Perf AUX Buffer.

In reply to Jonathan Cameron

There are many ways that support for the new CXL hotness monitoring unit could be enabled.

The existing infrastructure of perf + auxiliary buffers is used for the similar activity of trace capture.

This driver is based on the existing hisi_ptt (PCI trace and tune) driver and the CXL PMU driver. Testing was done against QEMU emulation of the feature but it's early days and lots more testing is needed as this is a flexible specification with many corner cases.

The raw hotlist elements cannot be interpreted without knowing the counter width. This is unfortunately dependent in an implementation defined way on the unit size used for monitoring.
As such, store this and a count of new hotlist entrees in a header that is inserted at the start of each set of records added to the auxiliary buffer.

TODO: Add capabilities to expose what can be set for at least some of these parameters.

---
 drivers/cxl/Kconfig     |   6 +
 drivers/cxl/Makefile    |   3 +
 drivers/cxl/core/core.h |   1 +
 drivers/cxl/core/port.c |   2 +
 drivers/cxl/cxl.h       |   1 +
 drivers/cxl/hmu.c       | 880 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 893 insertions(+)
drivers/cxl/Kconfig +6 -0
index 876469e23f7a..c420f828fe20 100644
@@ -146,4 +146,10 @@ config CXL_REGION_INVALIDATION_TEST
 	  If unsure, or if this kernel is meant for production environments,
 	  say N.
 
+config CXL_HMU
+       tristate "CXL: Hotness Monitoring Unit Driver"
+       depends on PERF_EVENTS
+       help
+	 Read data out from the CXL hotness units and provide it to userspace
+	 via the perf auxbuffer framework.
 endif
drivers/cxl/Makefile +3 -0
index 2caa90fa4bf2..b678aa927298 100644
@@ -7,15 +7,18 @@
 # - 'mem' and 'pmem' before endpoint drivers so that memdevs are
 #   immediately enabled
 # - 'pci' last, also mirrors the hardware enumeration hierarchy
+# - 'hmu' doesn't matter for now.
 obj-y += core/
 obj-$(CONFIG_CXL_PORT) += cxl_port.o
 obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o
 obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o
 obj-$(CONFIG_CXL_MEM) += cxl_mem.o
 obj-$(CONFIG_CXL_PCI) += cxl_pci.o
+obj-$(CONFIG_CXL_HMU) += cxl_hmu.o
 
 cxl_port-y := port.o
 cxl_acpi-y := acpi.o
 cxl_pmem-y := pmem.o security.o
 cxl_mem-y := mem.o
 cxl_pci-y := pci.o
+cxl_hmu-y := hmu.o
drivers/cxl/core/core.h +1 -0
index 0c62b4069ba0..88c673a4d950 100644
@@ -6,6 +6,7 @@
 
 extern const struct device_type cxl_nvdimm_bridge_type;
 extern const struct device_type cxl_nvdimm_type;
+extern const struct device_type cxl_hmu_type;
 extern const struct device_type cxl_pmu_type;
 
 extern struct attribute_group cxl_base_attribute_group;
drivers/cxl/core/port.c +2 -0
index af92c67bc954..a91712757830 100644
@@ -74,6 +74,8 @@ static int cxl_device_id(const struct device *dev)
 		return CXL_DEVICE_REGION;
 	if (dev->type == &cxl_pmu_type)
 		return CXL_DEVICE_PMU;
+	if (dev->type == &cxl_hmu_type)
+		return CXL_DEVICE_HMU;
 	return 0;
 }
 
drivers/cxl/cxl.h +1 -0
index 8172bc1f7a8d..bd190e2baa1d 100644
@@ -850,6 +850,7 @@ void cxl_driver_unregister(struct cxl_driver *cxl_drv);
 #define CXL_DEVICE_PMEM_REGION		7
 #define CXL_DEVICE_DAX_REGION		8
 #define CXL_DEVICE_PMU			9
+#define CXL_DEVICE_HMU			10
 
 #define MODULE_ALIAS_CXL(type) MODULE_ALIAS("cxl:t" __stringify(type) "*")
 #define CXL_MODALIAS_FMT "cxl:t%d"
drivers/cxl/hmu.c new file +880 -0

880 lines not on this page · raw message

Signature · 1 line
2.43.0
#8 lore raw

RFC PATCH 3/4 perf: Add support for CXL Hotness Monitoring Units (CHMU)

In reply to Jonathan Cameron

Based closely on existing support for hisi_ptt. Provides basic record and report --dump-raw-traces support.

Example output. With a counter_width of 16 (0x10) the least significant 4 bytes are the counter value and the unit index is bits 16-63. Here all units are over the threshold and the indexes are 0,1,2 etc.

. ... CXL_HMU data: size 33512 bytes
Header 0: units: 29c counter_width 10
Header 1 : deadbeef
0000000000000283
0000000000010364
0000000000020366
000000000003033c
0000000000040343
00000000000502ff
000000000006030d
000000000007031a

Note this is definitely RFC quality code. Before merging should reduce the code duplication that already exists and this code makes sorse.

---
 tools/perf/arch/arm/util/auxtrace.c |  58 +++++
 tools/perf/arch/x86/util/auxtrace.c |  76 ++++++
 tools/perf/util/Build               |   1 +
 tools/perf/util/auxtrace.c          |   4 +
 tools/perf/util/auxtrace.h          |   1 +
 tools/perf/util/cxl-hmu.c           | 367 ++++++++++++++++++++++++++++
 tools/perf/util/cxl-hmu.h           |  18 ++
 7 files changed, 525 insertions(+)
tools/perf/arch/arm/util/auxtrace.c +58 -0
index 3b8eca0ffb17..07ff41800808 100644
@@ -18,6 +18,7 @@
 #include "cs-etm.h"
 #include "arm-spe.h"
 #include "hisi-ptt.h"
+#include "cxl-hmu.h"
 
 static struct perf_pmu **find_all_arm_spe_pmus(int *nr_spes, int *err)
 {
@@ -99,6 +100,49 @@ static struct perf_pmu **find_all_hisi_ptt_pmus(int *nr_ptts, int *err)
 	return hisi_ptt_pmus;
 }
 
+static struct perf_pmu **find_all_cxl_hmu_pmus(int *nr_hmus, int *err)
+{
+	struct perf_pmu **cxl_hmu_pmus = NULL;
+	struct dirent *dent;
+	char path[PATH_MAX];
+	DIR *dir = NULL;
+	int idx = 0;
+
+	perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
+	dir = opendir(path);
+	if (!dir) {
+		*err = -EINVAL;
+		return NULL;
+	}
+
+	while ((dent = readdir(dir))) {
+		if (strstr(dent->d_name, "cxl_hmu"))
+			(*nr_hmus)++;
+	}
+
+	if (!(*nr_hmus))
+		goto out;
+
+	cxl_hmu_pmus = zalloc(sizeof(struct perf_pmu *) * (*nr_hmus));
+	if (!cxl_hmu_pmus) {
+		*err = -ENOMEM;
+		goto out;
+	}
+
+	rewinddir(dir);
+	while ((dent = readdir(dir))) {
+		if (strstr(dent->d_name, "cxl_hmu") && idx < *nr_hmus) {
+			cxl_hmu_pmus[idx] = perf_pmus__find(dent->d_name);
+			if (cxl_hmu_pmus[idx])
+				idx++;
+		}
+	}
+
+out:
+	closedir(dir);
+	return cxl_hmu_pmus;
+}
+
 static struct perf_pmu *find_pmu_for_event(struct perf_pmu **pmus,
 					   int pmu_nr, struct evsel *evsel)
 {
@@ -121,13 +165,16 @@ struct auxtrace_record
 	struct perf_pmu	*cs_etm_pmu = NULL;
 	struct perf_pmu **arm_spe_pmus = NULL;
 	struct perf_pmu **hisi_ptt_pmus = NULL;
+	struct perf_pmu **chmu_pmus = NULL;
 	struct evsel *evsel;
 	struct perf_pmu *found_etm = NULL;
 	struct perf_pmu *found_spe = NULL;
 	struct perf_pmu *found_ptt = NULL;
+	struct perf_pmu *found_chmu = NULL;
 	int auxtrace_event_cnt = 0;
 	int nr_spes = 0;
 	int nr_ptts = 0;
+	int nr_chmus = 0;
 
 	if (!evlist)
 		return NULL;
@@ -135,6 +182,7 @@ struct auxtrace_record
 	cs_etm_pmu = perf_pmus__find(CORESIGHT_ETM_PMU_NAME);
 	arm_spe_pmus = find_all_arm_spe_pmus(&nr_spes, err);
 	hisi_ptt_pmus = find_all_hisi_ptt_pmus(&nr_ptts, err);
+	chmu_pmus = find_all_cxl_hmu_pmus(&nr_chmus, err);
 
 	evlist__for_each_entry(evlist, evsel) {
 		if (cs_etm_pmu && !found_etm)
@@ -145,10 +193,14 @@ struct auxtrace_record
 
 		if (hisi_ptt_pmus && !found_ptt)
 			found_ptt = find_pmu_for_event(hisi_ptt_pmus, nr_ptts, evsel);
+
+		if (chmu_pmus && !found_chmu)
+			found_chmu = find_pmu_for_event(chmu_pmus, nr_chmus, evsel);
 	}
 
 	free(arm_spe_pmus);
 	free(hisi_ptt_pmus);
+	free(chmu_pmus);
 
 	if (found_etm)
 		auxtrace_event_cnt++;
@@ -159,6 +211,9 @@ struct auxtrace_record
 	if (found_ptt)
 		auxtrace_event_cnt++;
 
+	if (found_chmu)
+		auxtrace_event_cnt++;
+
 	if (auxtrace_event_cnt > 1) {
 		pr_err("Concurrent AUX trace operation not currently supported\n");
 		*err = -EOPNOTSUPP;
@@ -174,6 +229,9 @@ struct auxtrace_record
 
 	if (found_ptt)
 		return hisi_ptt_recording_init(err, found_ptt);
+
+	if (found_chmu)
+		return chmu_recording_init(err, found_chmu);
 #endif
 
 	/*
tools/perf/arch/x86/util/auxtrace.c +76 -0

102 lines not on this page · raw message

tools/perf/util/Build +1 -0
index dc616292b2dd..40c645fd0cb3 100644
@@ -127,6 +127,7 @@ perf-util-$(CONFIG_AUXTRACE) += arm-spe.o
 perf-util-$(CONFIG_AUXTRACE) += arm-spe-decoder/
 perf-util-$(CONFIG_AUXTRACE) += hisi-ptt.o
 perf-util-$(CONFIG_AUXTRACE) += hisi-ptt-decoder/
+perf-util-$(CONFIG_AUXTRACE) += cxl-hmu.o
 perf-util-$(CONFIG_AUXTRACE) += s390-cpumsf.o
 
 ifdef CONFIG_LIBOPENCSD
tools/perf/util/auxtrace.c +4 -0
index ca8682966fae..0efc15732a03 100644
@@ -53,6 +53,7 @@
 #include "intel-bts.h"
 #include "arm-spe.h"
 #include "hisi-ptt.h"
+#include "cxl-hmu.h"
 #include "s390-cpumsf.h"
 #include "util/mmap.h"
 
@@ -1333,6 +1334,9 @@ int perf_event__process_auxtrace_info(struct perf_session *session,
 	case PERF_AUXTRACE_HISI_PTT:
 		err = hisi_ptt_process_auxtrace_info(event, session);
 		break;
+	case PERF_AUXTRACE_CXL_HMU:
+		err = cxl_hmu_process_auxtrace_info(event, session);
+		break;
 	case PERF_AUXTRACE_UNKNOWN:
 	default:
 		return -EINVAL;
tools/perf/util/auxtrace.h +1 -0
index a1895a4f530b..8a7a5b7dc2d6 100644
@@ -49,6 +49,7 @@ enum auxtrace_type {
 	PERF_AUXTRACE_ARM_SPE,
 	PERF_AUXTRACE_S390_CPUMSF,
 	PERF_AUXTRACE_HISI_PTT,
+	PERF_AUXTRACE_CXL_HMU,
 };
 
 enum itrace_period_type {
tools/perf/util/cxl-hmu.c new file +367 -0

367 lines not on this page · raw message

tools/perf/util/cxl-hmu.h new file +18 -0
new file mode 100644
index 000000000000..9b4d83219711
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * CXL Hotness Monitoring Unit Support
+ */
+
+#ifndef INCLUDE__PERF_CXL_HMU_H__
+#define INCLUDE__PERF_CXL_HMU_H__
+
+#define CXL_HMU_PMU_NAME		"cxl_hmu"
+#define CXL_HMU_AUXTRACE_PRIV_SIZE	sizeof(u64)
+
+struct auxtrace_record *chmu_recording_init(int *err,
+					       struct perf_pmu *cxl_hmu_pmu);
+
+int cxl_hmu_process_auxtrace_info(union perf_event *event,
+				   struct perf_session *session);
+
+#endif
Signature · 1 line
2.43.0
#9 lore raw

RFC PATCH 4/4 hwtrace: Document CXL Hotness Monitoring Unit driver

In reply to Jonathan Cameron

Add basic documentation to describe the CXL HMU and the perf AUX buffer based interfaces.

---
 Documentation/trace/cxl-hmu.rst | 197 ++++++++++++++++++++++++++++++++
 Documentation/trace/index.rst   |   1 +
 2 files changed, 198 insertions(+)
Documentation/trace/cxl-hmu.rst new file +197 -0
new file mode 100644
index 000000000000..f07a50ba608c
@@ -0,0 +1,197 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==================================
+CXL Hotness Monitoring Unit Driver
+==================================
+
+CXL r3.2 introduced the CXL Hotness Monitoring Unit (CHMU). A CHMU allows
+software running on a CXL Host to identify hot memory ranges, that is those with
+higher access frequency relative to other memory ranges.
+
+A given Logical Device (presentation of a CXL memory device seen by a particular
+host) can provide 1 or more CHMU each of which supports 1 or more separately
+programmable CHMU Instances (CHMUI). These CHMUI are mostly independent with
+the exception that there can be restrictions on them tracking the same memory
+regions. The CHMUs are always completely independent.
+The naming of the units is cxl_hmu_memX.Y.Z where memX matches the naming
+of the memory device in /sys/bus/cxl/devices/, Y is the CHMU index and
+Z is the CHMUI index with the CHMU.
+
+Each CHMUI provides a ring buffer structure known as the Hot List from which the
+host an read back entries that describe the hotness of particular region of
+memory (Hot List Units). The Hot List Unit combines a Unit Address and an access
+count for the particular address. Unit address to DPA requires multiplication
+by the unit size. Thus, for large unit sizes the device may support higher
+counts. It is these Hot List Units that the driver provides via a perf AUX
+buffer by copying them from PCI BAR space.
+
+The unit size at which hotness is measured is configurable for each CHMUI and
+all measurement is done in Device Physical Address space. To relate this to
+Host Physical Address space the HDM (Host-Managed Device Memory) decoder
+configuration must be taken into account to reflect the placement in a
+CXL Fixed Memory Window and any interleaving.
+
+The CHMUI can support interrupts on fills above a watermark, or on overflow
+of the hotlist.
+
+A CHMUI can support two different basic modes of operation. Epoch and
+Always On. These affect what is placed on the hotlist. Note that the actual
+implementation of tracking is implementation defined and likely to be
+inherently imprecise in that the hottest pages may not be discovered due to
+resource exhaustion and the hotness counts may not represent accurately how
+hot they are. The specification allows for a very high degree of flexibility
+in implementation, important as it is likely that a number of different
+hardware implementations will be chosen to suit particular silicon and accuracy
+budgets.
+
+Operation and configuration
+===========================
+
+An example command line is::
+
+  $perf record -a  -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,\
+  hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,\
+  range_size=1024,randomized_downsampling=0,downsampling_factor=32,\
+  hotness_granual=12
+
+  $perf report --dump-raw-traces
+
+which will produce a list of hotlist entries, one per line with a short header
+to provide sufficient information to interpret the entries::
+
+  . ... CXL_HMU data: size 33512 bytes
+  Header 0: units: 29c counter_width 10
+  Header 1 : deadbeef
+  0000000000000283
+  0000000000010364
+  0000000000020366
+  000000000003033c
+  0000000000040343
+  00000000000502ff
+  000000000006030d
+  000000000007031a
+  ...
+
+The least significant counter_width bits (here 16, hex 10) are the counter
+value, all higher bits are the unit index.  Multiply by the unit size
+to get a Device Physical Address.
+
+The parameters are as follows:
+
+epoch_type
+----------
+
+Two values may be supported::
+
+  0 - Epoch based operation
+  1 - Always on operation
+
+
+0. Epoch Based Operation
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+An Epoch is a period of time after which a counter is assessed for hotness.
+
+The device may have a global sense of an Epoch but it may also operate them on
+a per counter, or per region of device basis. This is a function of the
+implementation and is not controllable, but is discoverable. In a global Epoch
+scheme at start of each Epoch all counters are zeroed / deallocated. Counters
+are then allocated in a hardware specific manner and accesses counted. At the
+completion of the Epoch the counters are compared with a threshold and entries
+with a count above a configurable threshold are added to the hotlist. A new
+Epoch is then begun with all counters cleared.
+
+In non-global Epoch scheme, when the Epoch of a given counter begins is not
+specified. An example might be an Epoch for counter only starting on first
+touch to the relevant memory region.  When a local Epoch ends the counter is
+compared to the threshold and if appropriate added to the hotlist.
+
+Note, in Epoch Based Operation, the counter in the hotlist entry provides
+information on how hot the memory is as the counter for the full Epoch is
+provided.
+
+1. Always on Operation
+~~~~~~~~~~~~~~~~~~~~~~
+
+In this mode, counters may all be reset before enabling the CHMUI. Then
+counters are allocated to particular memory units via an hardware specific
+method, perhaps on first touch.  When a counter passes the configurable
+hotness threshold an entry is added to the hotlist and that counter is freed
+for reuse.
+
+In this scheme the count provided in the hotlist entry is not useful as it will
+depend only on the configured threshold.
+
+access_type
+-----------
+
+The parameter controls which access are counted::
+
+  1 - Non-TEE read only
+  2 - Non-TEE write only
+  3 - Non-TEE read and write
+  4 - TEE and Non-TEE read only
+  5 - TEE and Non-TEE write only
+  6 - TEE and Non-tee read and write
+
+
+TEE here refers to a trusted execution environment, specifically one that
+results in the T bit being set in the CXL transactions.
+
+
+hotness_granual
+---------------
+
+Unit size at which tracking is performed.  Must be at least 256 bytes but
+hardware may only support some sizes. Expressed as a power of 2. e.g. 12 = 4kiB.
+
+hotness_threshold
+-----------------
+
+This is the minimum counter value that must be reached for the unit to count as
+hot and be added to the hotlist.
+
+The possible range may be dependent on the unit size as a larger unit size
+requires more bits on the hotlist entry leaving fewer available for the hotness
+counter.
+
+epoch_multiplier and epoch_scale
+--------------------------------
+
+The length of an epoch (in epoch mode) is controlled by these two parameters
+with the decoded epoch_scale multiplied by the epoch_multiplier to give the
+overall epoch length.
+
+epoch_scale::
+
+  1 - 100 usecs
+  2 - 1 msec
+  3 - 10 msecs
+  4 - 100 msecs
+  5 - 1 second
+
+range_base and range_scale
+--------------------------
+
+Expressed in terms of the unit size set via hotness_granual. Each CHMUI has a
+bitmap that controls what Device Physical Address spaces is tracked. Each bit
+represents 256MiB of DPA space.
+
+This interface provides a simple base and size in units of 256MiB to configure
+this bitmap. All bits in the specified range will be set.
+
+downsampling_factor
+-------------------
+
+Hardware may be incapable of counting accesses at full speed or it may be
+desirable to count over a longer period during which the counters would
+overflow.  This control allows selection of a down sampling factor expressed
+as a power of 2 between 1 and 32768.  Default is minimum supported downsampling
+factor.
+
+randomized_downsampling
+-----------------------
+
+To avoid problems with downsampling when accesses are periodic this option
+allows for an implementation defined randomization of the sampling interval,
+whilst remaining close to the specified downsampling_factor.
Documentation/trace/index.rst +1 -0
index 0b300901fd75..b35ed8e9dfa9 100644
@@ -36,3 +36,4 @@ Linux Tracing Technologies
    user_events
    rv/index
    hisi-ptt
+   cxl-hmu
Signature · 1 line
2.43.0
#10 lore raw

Re: [RFC PATCH 4/4] hwtrace: Document CXL Hotness Monitoring Unit driver

Quoted 62 lines · Jonathan Cameron

Add basic documentation to describe the CXL HMU and the perf AUX buffer based interfaces.

---
Documentation/trace/cxl-hmu.rst | 197 ++++++++++++++++++++++++++++++++
Documentation/trace/index.rst | 1 +

2 files changed, 198 insertions(+)
Documentation/trace/cxl-hmu.rst new file +47 -0
new file mode 100644
index 000000000000..f07a50ba608c
@@ -0,0 +1,197 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==================================
+CXL Hotness Monitoring Unit Driver
+==================================
+
+CXL r3.2 introduced the CXL Hotness Monitoring Unit (CHMU). A CHMU allows
+software running on a CXL Host to identify hot memory ranges, that is those with
+higher access frequency relative to other memory ranges.
+
+A given Logical Device (presentation of a CXL memory device seen by a particular
+host) can provide 1 or more CHMU each of which supports 1 or more separately
+programmable CHMU Instances (CHMUI). These CHMUI are mostly independent with
+the exception that there can be restrictions on them tracking the same memory
+regions. The CHMUs are always completely independent.
+The naming of the units is cxl_hmu_memX.Y.Z where memX matches the naming
+of the memory device in /sys/bus/cxl/devices/, Y is the CHMU index and
+Z is the CHMUI index with the CHMU.
+
+Each CHMUI provides a ring buffer structure known as the Hot List from which the
+host an read back entries that describe the hotness of particular region of
+memory (Hot List Units). The Hot List Unit combines a Unit Address and an access
+count for the particular address. Unit address to DPA requires multiplication
+by the unit size. Thus, for large unit sizes the device may support higher
+counts. It is these Hot List Units that the driver provides via a perf AUX
+buffer by copying them from PCI BAR space.
+
+The unit size at which hotness is measured is configurable for each CHMUI and
+all measurement is done in Device Physical Address space. To relate this to
+Host Physical Address space the HDM (Host-Managed Device Memory) decoder
+configuration must be taken into account to reflect the placement in a
+CXL Fixed Memory Window and any interleaving.
+
+The CHMUI can support interrupts on fills above a watermark, or on overflow
+of the hotlist.
+
+A CHMUI can support two different basic modes of operation. Epoch and
+Always On. These affect what is placed on the hotlist. Note that the actual
+implementation of tracking is implementation defined and likely to be
+inherently imprecise in that the hottest pages may not be discovered due to
+resource exhaustion and the hotness counts may not represent accurately how
+hot they are. The specification allows for a very high degree of flexibility
+in implementation, important as it is likely that a number of different
+hardware implementations will be chosen to suit particular silicon and accuracy
+budgets.
+
+Operation and configuration

Documentation/trace/cxl-hmu.rst line 57

Quoted 10 lines
Documentation/trace/cxl-hmu.rst new file +10 -0
+===========================
+
+An example command line is::
+
+  $perf record -a  -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,\
+  hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,\
+  range_size=1024,randomized_downsampling=0,downsampling_factor=32,\
+  hotness_granual=12
+
+  $perf report --dump-raw-traces

Typo: --dump-raw-trace

Quoted 152 lines
Documentation/trace/cxl-hmu.rst new file +140 -0
+
+which will produce a list of hotlist entries, one per line with a short header
+to provide sufficient information to interpret the entries::
+
+  . ... CXL_HMU data: size 33512 bytes
+  Header 0: units: 29c counter_width 10
+  Header 1 : deadbeef
+  0000000000000283
+  0000000000010364
+  0000000000020366
+  000000000003033c
+  0000000000040343
+  00000000000502ff
+  000000000006030d
+  000000000007031a
+  ...
+
+The least significant counter_width bits (here 16, hex 10) are the counter
+value, all higher bits are the unit index.  Multiply by the unit size
+to get a Device Physical Address.
+
+The parameters are as follows:
+
+epoch_type
+----------
+
+Two values may be supported::
+
+  0 - Epoch based operation
+  1 - Always on operation
+
+
+0. Epoch Based Operation
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+An Epoch is a period of time after which a counter is assessed for hotness.
+
+The device may have a global sense of an Epoch but it may also operate them on
+a per counter, or per region of device basis. This is a function of the
+implementation and is not controllable, but is discoverable. In a global Epoch
+scheme at start of each Epoch all counters are zeroed / deallocated. Counters
+are then allocated in a hardware specific manner and accesses counted. At the
+completion of the Epoch the counters are compared with a threshold and entries
+with a count above a configurable threshold are added to the hotlist. A new
+Epoch is then begun with all counters cleared.
+
+In non-global Epoch scheme, when the Epoch of a given counter begins is not
+specified. An example might be an Epoch for counter only starting on first
+touch to the relevant memory region.  When a local Epoch ends the counter is
+compared to the threshold and if appropriate added to the hotlist.
+
+Note, in Epoch Based Operation, the counter in the hotlist entry provides
+information on how hot the memory is as the counter for the full Epoch is
+provided.
+
+1. Always on Operation
+~~~~~~~~~~~~~~~~~~~~~~
+
+In this mode, counters may all be reset before enabling the CHMUI. Then
+counters are allocated to particular memory units via an hardware specific
+method, perhaps on first touch.  When a counter passes the configurable
+hotness threshold an entry is added to the hotlist and that counter is freed
+for reuse.
+
+In this scheme the count provided in the hotlist entry is not useful as it will
+depend only on the configured threshold.
+
+access_type
+-----------
+
+The parameter controls which access are counted::
+
+  1 - Non-TEE read only
+  2 - Non-TEE write only
+  3 - Non-TEE read and write
+  4 - TEE and Non-TEE read only
+  5 - TEE and Non-TEE write only
+  6 - TEE and Non-tee read and write
+
+
+TEE here refers to a trusted execution environment, specifically one that
+results in the T bit being set in the CXL transactions.
+
+
+hotness_granual
+---------------
+
+Unit size at which tracking is performed.  Must be at least 256 bytes but
+hardware may only support some sizes. Expressed as a power of 2. e.g. 12 = 4kiB.
+
+hotness_threshold
+-----------------
+
+This is the minimum counter value that must be reached for the unit to count as
+hot and be added to the hotlist.
+
+The possible range may be dependent on the unit size as a larger unit size
+requires more bits on the hotlist entry leaving fewer available for the hotness
+counter.
+
+epoch_multiplier and epoch_scale
+--------------------------------
+
+The length of an epoch (in epoch mode) is controlled by these two parameters
+with the decoded epoch_scale multiplied by the epoch_multiplier to give the
+overall epoch length.
+
+epoch_scale::
+
+  1 - 100 usecs
+  2 - 1 msec
+  3 - 10 msecs
+  4 - 100 msecs
+  5 - 1 second
+
+range_base and range_scale
+--------------------------
+
+Expressed in terms of the unit size set via hotness_granual. Each CHMUI has a
+bitmap that controls what Device Physical Address spaces is tracked. Each bit
+represents 256MiB of DPA space.
+
+This interface provides a simple base and size in units of 256MiB to configure
+this bitmap. All bits in the specified range will be set.
+
+downsampling_factor
+-------------------
+
+Hardware may be incapable of counting accesses at full speed or it may be
+desirable to count over a longer period during which the counters would
+overflow.  This control allows selection of a down sampling factor expressed
+as a power of 2 between 1 and 32768.  Default is minimum supported downsampling
+factor.
+
+randomized_downsampling
+-----------------------
+
+To avoid problems with downsampling when accesses are periodic this option
+allows for an implementation defined randomization of the sampling interval,
+whilst remaining close to the specified downsampling_factor.
Documentation/trace/index.rst +1 -0
index 0b300901fd75..b35ed8e9dfa9 100644
@@ -36,3 +36,4 @@ Linux Tracing Technologies
   user_events
   rv/index
   hisi-ptt
+   cxl-hmu
Signature · 1 line
2.43.0
#11 lore raw
In reply to Jonathan Cameron
Quoted 103 lines · On Thu, 21 Nov 2024 10:18:41 +0000 Jonathan Cameron…

The CXL specification release 3.2 is now available under a click through at
https://computeexpresslink.org/cxl-specification/ and it brings new shiny toys.

RFC reason
- Whilst trace capture with a particular configuration is potentially useful the intent is that CXL HMU units will be used to drive various forms of hotpage migration for memory tiering setups. This driver doesn't do this (yet), but rather provides data capture etc for experimentation and for working out how to mostly put the allocations in the right place to start with by tuning applications.

CXL r3.2 introduces a CXL Hotness Monitoring Unit definition. The intent of this is to provide a way to establish which units of memory (typically pages or larger) in CXL attached memory are hot. The implementation details and algorithm are all implementation defined. The specification simply describes the 'interface' which takes the form of ring buffer of hotness records in a PCI BAR and defined capability, configuration and status registers.

The hardware may have constraints on what it can track, granularity etc and on how accurately it tracks (e.g. counter exhaustion, inaccurate trackers). Some of these constraints are discoverable from the hardware registers, others such as loss of accuracy have no universally accepted measures as they are typically access pattern dependent. Sadly it is very unlikely any hardware will implement a truly precise tracker given the large resource requirements for tracking at a useful granularity.

There are two fundamental operation modes:

* Epoch based. Counters are checked after a period of time (Epoch) and if over a threshold added to the hotlist.
* Always on. Counters run until a threshold is reached, after that the hot unit is added to the hotlist and the counter released.

Counting can be filtered on:

* Region of CXL DPA space (256MiB per bit in a bitmap).
* Type of access - Trusted and non trusted or non trusted only, R/W/RW

Sampling can be modified by:

* Downsampling including potentially randomized downsampling.

The driver presented here is intended to be useful in its own right but also to act as the first step of a possible path towards hotness monitoring based hot page migration. Those steps might look like.

1. Gather data - drivers provide telemetry like solutions to get that data. May be enhanced, for example in this driver by providing the HPA address rather than DPA Unit Address. Userspace can access enough information to do this so maybe not.
2. Userspace algorithm development, possibly combined with userspace triggered migration by PA. Working out how to use different levels of constrained hardware resources will be challenging.
3. Move those algorithms in kernel. Will require generalization across different hotpage trackers etc.

So far this driver just gives access to the raw data. I will probably kick of a longer discussion on how to do adaptive sampling needed to actually use these units for tiering etc, sometime soon (if no one one else beats me too it). There is a follow up topic of how to virtualize this stuff for memory stranding cases (VM gets a fixed mixture of fast and slow memory and should do it's own tiering).

More details in the Documentation patch but typical commands are:

$perf record -a -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,\
hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,\
range_size=1024,randomized_downsampling=0,downsampling_factor=32,\
hotness_granual=12

$perf report --dump-raw-traces

Example output. With a counter_width of 16 (0x10) the least significant 4 bytes are the counter value and the unit index is bits 16-63. Here all units are over the threshold and the indexes are 0,1,2 etc.

. ... CXL_HMU data: size 33512 bytes
Header 0: units: 29c counter_width 10
Header 1 : deadbeef
0000000000000283
0000000000010364
0000000000020366
000000000003033c
0000000000040343
00000000000502ff
000000000006030d
000000000007031a

Which will produce a list of hotness entries.
Bits[N-1:0] counter value
Bits[63:N] Unit ID (combine with unit size and DPA base + HDM decoder
config to get to a Host Physical Address)

Specific RFC questions.
- What should be in the header added to the aux buffer. Currently just the minimum is provided. Number of records and the counter width needed to decode them.
- Should we reset the counters when doing sampling "-F X"
If the frequency is higher than the epoch we never see any hot units. If so, when should we reset them?

Quoted 5 lines

Note testing has been light and on emulation only + as perf tool is a pain to build on a striped back VM, build testing has all be on arm64 so far. The driver loads though on both arm64 and x86 so any problems are likely in the perf tool arch specific code which is build tested (on wrong machine)

FWIW, runs on x86. However, it triggers a lockdep warning in both start and stop due to the spin lock. Something to tidy up for RFCv2.

J

Quoted 41 lines

The QEMU emulation needs some cleanup, but I should be able to post that shortly to let people actually play with this. There are lots of open questions there on how 'right' we want the emulation to be and what counting uarch to emulate.

Jonathan Cameron (4):
cxl: Register devices for CXL Hotness Monitoring Units (CHMU)
cxl: Hotness Monitoring Unit via a Perf AUX Buffer.
perf: Add support for CXL Hotness Monitoring Units (CHMU)
hwtrace: Document CXL Hotness Monitoring Unit driver

 Documentation/trace/cxl-hmu.rst     | 197 +++++++
 Documentation/trace/index.rst       |   1 +
 drivers/cxl/Kconfig                 |   6 +
 drivers/cxl/Makefile                |   3 +
 drivers/cxl/core/Makefile           |   1 +
 drivers/cxl/core/core.h             |   1 +
 drivers/cxl/core/hmu.c              |  64 ++
 drivers/cxl/core/port.c             |   2 +
 drivers/cxl/core/regs.c             |  14 +
 drivers/cxl/cxl.h                   |   5 +
 drivers/cxl/cxlpci.h                |   1 +
 drivers/cxl/hmu.c                   | 880 ++++++++++++++++++++++++++++
 drivers/cxl/hmu.h                   |  23 +
 drivers/cxl/pci.c                   |  26 +-
 tools/perf/arch/arm/util/auxtrace.c |  58 ++
 tools/perf/arch/x86/util/auxtrace.c |  76 +++
 tools/perf/util/Build               |   1 +
 tools/perf/util/auxtrace.c          |   4 +
 tools/perf/util/auxtrace.h          |   1 +
 tools/perf/util/cxl-hmu.c           | 367 ++++++++++++
 tools/perf/util/cxl-hmu.h           |  18 +
 21 files changed, 1748 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/trace/cxl-hmu.rst
 create mode 100644 drivers/cxl/core/hmu.c
 create mode 100644 drivers/cxl/hmu.c
 create mode 100644 drivers/cxl/hmu.h
 create mode 100644 tools/perf/util/cxl-hmu.c
 create mode 100644 tools/perf/util/cxl-hmu.h
#12 lore raw
In reply to Jonathan Cameron
Quoted 48 lines · Jonathan Cameron

The CXL specification release 3.2 is now available under a click through at
https://computeexpresslink.org/cxl-specification/ and it brings new shiny toys.

RFC reason
- Whilst trace capture with a particular configuration is potentially useful the intent is that CXL HMU units will be used to drive various forms of hotpage migration for memory tiering setups. This driver doesn't do this (yet), but rather provides data capture etc for experimentation and for working out how to mostly put the allocations in the right place to start with by tuning applications.

CXL r3.2 introduces a CXL Hotness Monitoring Unit definition. The intent of this is to provide a way to establish which units of memory (typically pages or larger) in CXL attached memory are hot. The implementation details and algorithm are all implementation defined. The specification simply describes the 'interface' which takes the form of ring buffer of hotness records in a PCI BAR and defined capability, configuration and status registers.

The hardware may have constraints on what it can track, granularity etc and on how accurately it tracks (e.g. counter exhaustion, inaccurate trackers). Some of these constraints are discoverable from the hardware registers, others such as loss of accuracy have no universally accepted measures as they are typically access pattern dependent. Sadly it is very unlikely any hardware will implement a truly precise tracker given the large resource requirements for tracking at a useful granularity.

There are two fundamental operation modes:

* Epoch based. Counters are checked after a period of time (Epoch) and if over a threshold added to the hotlist.
* Always on. Counters run until a threshold is reached, after that the hot unit is added to the hotlist and the counter released.

Counting can be filtered on:

* Region of CXL DPA space (256MiB per bit in a bitmap).
* Type of access - Trusted and non trusted or non trusted only, R/W/RW

Sampling can be modified by:

* Downsampling including potentially randomized downsampling.

The driver presented here is intended to be useful in its own right but also to act as the first step of a possible path towards hotness monitoring based hot page migration. Those steps might look like.

Quoted 7 lines

1. Gather data - drivers provide telemetry like solutions to get that data. May be enhanced, for example in this driver by providing the HPA address rather than DPA Unit Address. Userspace can access enough information to do this so maybe not.
2. Userspace algorithm development, possibly combined with userspace triggered migration by PA. Working out how to use different levels of constrained hardware resources will be challenging.

FWIW this is what i was thinking about for this extension:

https://lore.kernel.org/all/[email protected]/

At least for testing CHMU stuff. So if anyone is poking at testing such things, they can feel free to use that for prototyping. However, I think there is general discomfort around userspace handling HPA/DPA.

So it might look more like

echo nr_pages > /sys/.../tiering/nodeN/promote_pages

rather than handling the raw data from the CHMU to make decisions.

Quoted 3 lines

3. Move those algorithms in kernel. Will require generalization across different hotpage trackers etc.

In a longer discussion with Dan, we considered something a little more abstract - like a system that monitors bandwidth and memory access stalls and decide to promote X pages from Y device. This carries a pretty tall generalization cost, but it's pretty exciting to say the least.

Definitely worth a discussion for later.

Quoted 8 lines

So far this driver just gives access to the raw data. I will probably kick of a longer discussion on how to do adaptive sampling needed to actually use these units for tiering etc, sometime soon (if no one one else beats me too it). There is a follow up topic of how to virtualize this stuff for memory stranding cases (VM gets a fixed mixture of fast and slow memory and should do it's own tiering).

Without having looked at the patches yet, I would presume this interface is at least gated to admin/root? (raw data is physical address info)

~Gregory

#13 lore raw
Quoted 57 lines · On Thu, 21 Nov 2024 09:24:43 -0500 Gregory Price <[email protected]> wrote:
Quoted 48 lines · Jonathan Cameron

The CXL specification release 3.2 is now available under a click through at
https://computeexpresslink.org/cxl-specification/ and it brings new shiny toys.

RFC reason
- Whilst trace capture with a particular configuration is potentially useful the intent is that CXL HMU units will be used to drive various forms of hotpage migration for memory tiering setups. This driver doesn't do this (yet), but rather provides data capture etc for experimentation and for working out how to mostly put the allocations in the right place to start with by tuning applications.

CXL r3.2 introduces a CXL Hotness Monitoring Unit definition. The intent of this is to provide a way to establish which units of memory (typically pages or larger) in CXL attached memory are hot. The implementation details and algorithm are all implementation defined. The specification simply describes the 'interface' which takes the form of ring buffer of hotness records in a PCI BAR and defined capability, configuration and status registers.

The hardware may have constraints on what it can track, granularity etc and on how accurately it tracks (e.g. counter exhaustion, inaccurate trackers). Some of these constraints are discoverable from the hardware registers, others such as loss of accuracy have no universally accepted measures as they are typically access pattern dependent. Sadly it is very unlikely any hardware will implement a truly precise tracker given the large resource requirements for tracking at a useful granularity.

There are two fundamental operation modes:

* Epoch based. Counters are checked after a period of time (Epoch) and if over a threshold added to the hotlist.
* Always on. Counters run until a threshold is reached, after that the hot unit is added to the hotlist and the counter released.

Counting can be filtered on:

* Region of CXL DPA space (256MiB per bit in a bitmap).
* Type of access - Trusted and non trusted or non trusted only, R/W/RW

Sampling can be modified by:

* Downsampling including potentially randomized downsampling.

The driver presented here is intended to be useful in its own right but also to act as the first step of a possible path towards hotness monitoring based hot page migration. Those steps might look like.

Quoted 7 lines

1. Gather data - drivers provide telemetry like solutions to get that data. May be enhanced, for example in this driver by providing the HPA address rather than DPA Unit Address. Userspace can access enough information to do this so maybe not.
2. Userspace algorithm development, possibly combined with userspace triggered migration by PA. Working out how to use different levels of constrained hardware resources will be challenging.

Quoted 3 lines

FWIW this is what i was thinking about for this extension:

https://lore.kernel.org/all/[email protected]/

Yup. I had that in mind. Forgot to actually add a link.

Quoted 10 lines

At least for testing CHMU stuff. So if anyone is poking at testing such things, they can feel free to use that for prototyping. However, I think there is general discomfort around userspace handling HPA/DPA.

So it might look more like

echo nr_pages > /sys/.../tiering/nodeN/promote_pages

rather than handling the raw data from the CHMU to make decisions.

Agreed, but I think we are far away from a point where we can implement that.

Just working out how to tune the hardware to grab useful data is going to take a while to figure out, let alone doing anything much with it.

Without care you won't get a meaningful signal for what is actually hot out of the box. Lots of reasons why including:
a) Exhaustion of tracking resources, due to looking at too large a window or for too long. Will probably need some form of auto updating of what is being scanning (coarse to fine might work though I'm doubtful, scanning across small regions maybe).
b) Threshold too high, no detections.
c) Threshold too low, everything hot.
d) Wrong timescales. Hot is not a well defined thing.
e) Hardware that won't do tracking at fine enough granularity.

Quoted 10 lines
Quoted 5 lines

3. Move those algorithms in kernel. Will require generalization across different hotpage trackers etc.

In a longer discussion with Dan, we considered something a little more abstract - like a system that monitors bandwidth and memory access stalls and decide to promote X pages from Y device. This carries a pretty tall generalization cost, but it's pretty exciting to say the least.

Agreed that ultimately we'll end up somewhere like that. These units are just a small part of what is needed in total.

Quoted 12 lines

Definitely worth a discussion for later.

Quoted 8 lines

So far this driver just gives access to the raw data. I will probably kick of a longer discussion on how to do adaptive sampling needed to actually use these units for tiering etc, sometime soon (if no one one else beats me too it). There is a follow up topic of how to virtualize this stuff for memory stranding cases (VM gets a fixed mixture of fast and slow memory and should do it's own tiering).

Quoted 2 lines

Without having looked at the patches yet, I would presume this interface is at least gated to admin/root? (raw data is physical address info)

That's certainly the intent. It's not going upstream in this form so I haven't actually checked yet :) Uses similar infrastructure to ARM SPE which can also give physical address info + a lot more than that.

Jonathan

Quoted 3 lines

~Gregory

#14 lore raw
Quoted 82 lines · Jonathan Cameron
Quoted 57 lines · On Thu, 21 Nov 2024 09:24:43 -0500 Gregory Price <[email protected]> wrote:
Quoted 48 lines · Jonathan Cameron

The CXL specification release 3.2 is now available under a click through at
https://computeexpresslink.org/cxl-specification/ and it brings new shiny toys.

RFC reason
- Whilst trace capture with a particular configuration is potentially useful the intent is that CXL HMU units will be used to drive various forms of hotpage migration for memory tiering setups. This driver doesn't do this (yet), but rather provides data capture etc for experimentation and for working out how to mostly put the allocations in the right place to start with by tuning applications.

CXL r3.2 introduces a CXL Hotness Monitoring Unit definition. The intent of this is to provide a way to establish which units of memory (typically pages or larger) in CXL attached memory are hot. The implementation details and algorithm are all implementation defined. The specification simply describes the 'interface' which takes the form of ring buffer of hotness records in a PCI BAR and defined capability, configuration and status registers.

The hardware may have constraints on what it can track, granularity etc and on how accurately it tracks (e.g. counter exhaustion, inaccurate trackers). Some of these constraints are discoverable from the hardware registers, others such as loss of accuracy have no universally accepted measures as they are typically access pattern dependent. Sadly it is very unlikely any hardware will implement a truly precise tracker given the large resource requirements for tracking at a useful granularity.

There are two fundamental operation modes:

* Epoch based. Counters are checked after a period of time (Epoch) and if over a threshold added to the hotlist.
* Always on. Counters run until a threshold is reached, after that the hot unit is added to the hotlist and the counter released.

Counting can be filtered on:

* Region of CXL DPA space (256MiB per bit in a bitmap).
* Type of access - Trusted and non trusted or non trusted only, R/W/RW

Sampling can be modified by:

* Downsampling including potentially randomized downsampling.

The driver presented here is intended to be useful in its own right but also to act as the first step of a possible path towards hotness monitoring based hot page migration. Those steps might look like.

Quoted 7 lines

1. Gather data - drivers provide telemetry like solutions to get that data. May be enhanced, for example in this driver by providing the HPA address rather than DPA Unit Address. Userspace can access enough information to do this so maybe not.
2. Userspace algorithm development, possibly combined with userspace triggered migration by PA. Working out how to use different levels of constrained hardware resources will be challenging.

Quoted 3 lines

FWIW this is what i was thinking about for this extension:

https://lore.kernel.org/all/[email protected]/

Yup. I had that in mind. Forgot to actually add a link.

Quoted 10 lines

At least for testing CHMU stuff. So if anyone is poking at testing such things, they can feel free to use that for prototyping. However, I think there is general discomfort around userspace handling HPA/DPA.

So it might look more like

echo nr_pages > /sys/.../tiering/nodeN/promote_pages

rather than handling the raw data from the CHMU to make decisions.

Agreed, but I think we are far away from a point where we can implement that.

Just working out how to tune the hardware to grab useful data is going to take a while to figure out, let alone doing anything much with it.

Quoted 11 lines

Without care you won't get a meaningful signal for what is actually hot out of the box. Lots of reasons why including:
a) Exhaustion of tracking resources, due to looking at too large a window or for too long. Will probably need some form of auto updating of what is being scanning (coarse to fine might work though I'm doubtful, scanning across small regions maybe).
b) Threshold too high, no detections.
c) Threshold too low, everything hot.
d) Wrong timescales. Hot is not a well defined thing.
e) Hardware that won't do tracking at fine enough granularity.

f) How does this even work with interleaving on larger pools :B It's pretend-addressing all the way down :D

Lots of conceptually complex and fun questions here.

~Gregory

#15 lore raw
In reply to Jonathan Cameron
Quoted 4 lines · On Thu, 21 Nov 2024 14:58:52 +0000 Jonathan Cameron…
Quoted 1 line · On Thu, 21 Nov 2024 09:24:43 -0500 Gregory Price <[email protected]> wrote:

On Thu, Nov 21, 2024 at 10:18:41AM +0000, Jonathan Cameron wrote:

[...]

Quoted 13 lines

Just working out how to tune the hardware to grab useful data is going to take a while to figure out, let alone doing anything much with it.

Without care you won't get a meaningful signal for what is actually hot out of the box. Lots of reasons why including:
a) Exhaustion of tracking resources, due to looking at too large a window or for too long. Will probably need some form of auto updating of what is being scanning (coarse to fine might work though I'm doubtful, scanning across small regions maybe).
b) Threshold too high, no detections.
c) Threshold too low, everything hot.
d) Wrong timescales. Hot is not a well defined thing.
e) Hardware that won't do tracking at fine enough granularity.

Similar questions can be raised to general hotness monitoring including that for DAMON. I'm trying to summarize[1] rules of thumbs for DAMON tuning based on my humble experiences. Once it is done, I will further try automations of tunings.

In future, hopefully DAMON can be extended to utilize CXL hotness monitoring unit as low level primitive for access check. Then, the guidance and automation of DAMON tuning could be just applied.

Note that I'm not saying DAMON should be the only way to utilize CXL hotness monitoring unit. I'm saying DAMON could be one of the ways :)

[1] https://lore.kernel.org/[email protected]

Thanks,
SJ

[...]

#16 lore raw
In reply to Jonathan Cameron
Quoted 3 lines · On Thu, 21 Nov 2024 10:18:41 +0000 Jonathan Cameron…

The CXL specification release 3.2 is now available under a click through at
https://computeexpresslink.org/cxl-specification/ and it brings new shiny toys.

If anyone wants to play, basic emulation on my CXL QEMU staging tree
https://gitlab.com/jic23/qemu/-/commit/e89b35d264c1bcc04807e7afab1254f35ffc8cb9

Branch with a few other things on top is:
https://gitlab.com/jic23/qemu/-/commits/cxl-2024-11-27

Note that this currently doesn't produce real data. I have a plan / initial PoC / hack to hook that up via an addition to the QEMU cache plugin and an external tool to emulate the hotness tracker counting hardware. Will be a little while before I get that finished, so in a meantime the above exercises the driver.

Jonathan

Quoted 146 lines

RFC reason
- Whilst trace capture with a particular configuration is potentially useful the intent is that CXL HMU units will be used to drive various forms of hotpage migration for memory tiering setups. This driver doesn't do this (yet), but rather provides data capture etc for experimentation and for working out how to mostly put the allocations in the right place to start with by tuning applications.

CXL r3.2 introduces a CXL Hotness Monitoring Unit definition. The intent of this is to provide a way to establish which units of memory (typically pages or larger) in CXL attached memory are hot. The implementation details and algorithm are all implementation defined. The specification simply describes the 'interface' which takes the form of ring buffer of hotness records in a PCI BAR and defined capability, configuration and status registers.

The hardware may have constraints on what it can track, granularity etc and on how accurately it tracks (e.g. counter exhaustion, inaccurate trackers). Some of these constraints are discoverable from the hardware registers, others such as loss of accuracy have no universally accepted measures as they are typically access pattern dependent. Sadly it is very unlikely any hardware will implement a truly precise tracker given the large resource requirements for tracking at a useful granularity.

There are two fundamental operation modes:

* Epoch based. Counters are checked after a period of time (Epoch) and if over a threshold added to the hotlist.
* Always on. Counters run until a threshold is reached, after that the hot unit is added to the hotlist and the counter released.

Counting can be filtered on:

* Region of CXL DPA space (256MiB per bit in a bitmap).
* Type of access - Trusted and non trusted or non trusted only, R/W/RW

Sampling can be modified by:

* Downsampling including potentially randomized downsampling.

The driver presented here is intended to be useful in its own right but also to act as the first step of a possible path towards hotness monitoring based hot page migration. Those steps might look like.

1. Gather data - drivers provide telemetry like solutions to get that data. May be enhanced, for example in this driver by providing the HPA address rather than DPA Unit Address. Userspace can access enough information to do this so maybe not.
2. Userspace algorithm development, possibly combined with userspace triggered migration by PA. Working out how to use different levels of constrained hardware resources will be challenging.
3. Move those algorithms in kernel. Will require generalization across different hotpage trackers etc.

So far this driver just gives access to the raw data. I will probably kick of a longer discussion on how to do adaptive sampling needed to actually use these units for tiering etc, sometime soon (if no one one else beats me too it). There is a follow up topic of how to virtualize this stuff for memory stranding cases (VM gets a fixed mixture of fast and slow memory and should do it's own tiering).

More details in the Documentation patch but typical commands are:

$perf record -a -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,\
hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,\
range_size=1024,randomized_downsampling=0,downsampling_factor=32,\
hotness_granual=12

$perf report --dump-raw-traces

Example output. With a counter_width of 16 (0x10) the least significant 4 bytes are the counter value and the unit index is bits 16-63. Here all units are over the threshold and the indexes are 0,1,2 etc.

. ... CXL_HMU data: size 33512 bytes
Header 0: units: 29c counter_width 10
Header 1 : deadbeef
0000000000000283
0000000000010364
0000000000020366
000000000003033c
0000000000040343
00000000000502ff
000000000006030d
000000000007031a

Which will produce a list of hotness entries.
Bits[N-1:0] counter value
Bits[63:N] Unit ID (combine with unit size and DPA base + HDM decoder
config to get to a Host Physical Address)

Specific RFC questions.
- What should be in the header added to the aux buffer. Currently just the minimum is provided. Number of records and the counter width needed to decode them.
- Should we reset the counters when doing sampling "-F X"
If the frequency is higher than the epoch we never see any hot units. If so, when should we reset them?

Note testing has been light and on emulation only + as perf tool is a pain to build on a striped back VM, build testing has all be on arm64 so far. The driver loads though on both arm64 and x86 so any problems are likely in the perf tool arch specific code which is build tested (on wrong machine)

The QEMU emulation needs some cleanup, but I should be able to post that shortly to let people actually play with this. There are lots of open questions there on how 'right' we want the emulation to be and what counting uarch to emulate.

Jonathan Cameron (4):
cxl: Register devices for CXL Hotness Monitoring Units (CHMU)
cxl: Hotness Monitoring Unit via a Perf AUX Buffer.
perf: Add support for CXL Hotness Monitoring Units (CHMU)
hwtrace: Document CXL Hotness Monitoring Unit driver

 Documentation/trace/cxl-hmu.rst     | 197 +++++++
 Documentation/trace/index.rst       |   1 +
 drivers/cxl/Kconfig                 |   6 +
 drivers/cxl/Makefile                |   3 +
 drivers/cxl/core/Makefile           |   1 +
 drivers/cxl/core/core.h             |   1 +
 drivers/cxl/core/hmu.c              |  64 ++
 drivers/cxl/core/port.c             |   2 +
 drivers/cxl/core/regs.c             |  14 +
 drivers/cxl/cxl.h                   |   5 +
 drivers/cxl/cxlpci.h                |   1 +
 drivers/cxl/hmu.c                   | 880 ++++++++++++++++++++++++++++
 drivers/cxl/hmu.h                   |  23 +
 drivers/cxl/pci.c                   |  26 +-
 tools/perf/arch/arm/util/auxtrace.c |  58 ++
 tools/perf/arch/x86/util/auxtrace.c |  76 +++
 tools/perf/util/Build               |   1 +
 tools/perf/util/auxtrace.c          |   4 +
 tools/perf/util/auxtrace.h          |   1 +
 tools/perf/util/cxl-hmu.c           | 367 ++++++++++++
 tools/perf/util/cxl-hmu.h           |  18 +
 21 files changed, 1748 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/trace/cxl-hmu.rst
 create mode 100644 drivers/cxl/core/hmu.c
 create mode 100644 drivers/cxl/hmu.c
 create mode 100644 drivers/cxl/hmu.h
 create mode 100644 tools/perf/util/cxl-hmu.c
 create mode 100644 tools/perf/util/cxl-hmu.h
#17 lore raw

Micron Confidential

Micron Confidential

Quoted 21 lines

From: Jonathan Cameron <[email protected]>
Sent: Wednesday, November 27, 2024 10:05 PM

Quoted 3 lines · On Thu, 21 Nov 2024 10:18:41 +-0000 Jonathan Cameron…

The CXL specification release 3.2 is now available under a click through at

https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcom
p

Quoted 1 line

uteexpresslink.org%2Fcxl-

specification%2F&data=05%7C02%7Cajayjoshi%40micron.com%7Ce59092c 80eed4878d9cc08dd0f016a78%7Cf38a5ecd28134862b11bac1d563c806f% 7C0%7C0%7C638683221020661525%7CUnknown%7CTWFpbGZsb3d8eyJF bXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiT WFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=A6OYPhky94PnkzYn 4bfB1usIFDQzR1GlY1QFK3hBVtY%3D&reserved=0 and it brings new shiny toys.

Quoted 10 lines

If anyone wants to play, basic emulation on my CXL QEMU staging tree
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitla
b.com%2Fjic23%2Fqemu%2F- %2Fcommit%2Fe89b35d264c1bcc04807e7afab1254f35ffc8cb9&data=05%7 C02%7Cajayjoshi%40micron.com%7Ce59092c80eed4878d9cc08dd0f016a7 8%7Cf38a5ecd28134862b11bac1d563c806f%7C0%7C0%7C638683221020 676260%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYi OiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D %7C0%7C%7C%7C&sdata=Un0fB5v%2BBKTnQPldKKoRwOpw9GrGdDwBrXm JamKEIvA%3D&reserved=0

This is interesting. We are definitely trying this and let you know how it goes.

Quoted 168 lines

Branch with a few other things on top is:
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitla
b.com%2Fjic23%2Fqemu%2F-%2Fcommits%2Fcxl-2024-11- 27&data=05%7C02%7Cajayjoshi%40micron.com%7Ce59092c80eed4878d9 cc08dd0f016a78%7Cf38a5ecd28134862b11bac1d563c806f%7C0%7C0%7C 638683221020684284%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGk iOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIj oyfQ%3D%3D%7C0%7C%7C%7C&sdata=V451%2BM9UKiC0RfBUviNTY3fZH UGHdjJEgGuR0DowJZM%3D&reserved=0

Note that this currently doesn't produce real data. I have a plan / initial PoC / hack to hook that up via an addition to the QEMU cache plugin and an external tool to emulate the hotness tracker counting hardware. Will be a little while before I get that finished, so in a meantime the above exercises the driver.

Jonathan

Quoted 17 lines

RFC reason
- Whilst trace capture with a particular configuration is potentially useful the intent is that CXL HMU units will be used to drive various forms of hotpage migration for memory tiering setups. This driver doesn't do this (yet), but rather provides data capture etc for experimentation and for working out how to mostly put the allocations in the right place to start with by tuning applications.

CXL r3.2 introduces a CXL Hotness Monitoring Unit definition. The intent of this is to provide a way to establish which units of memory (typically pages or larger) in CXL attached memory are hot. The implementation details and algorithm are all implementation defined. The specification simply describes the 'interface' which takes the form of ring buffer of hotness records in a PCI BAR and defined capability, configuration and status registers.

Quoted 7 lines

The hardware may have constraints on what it can track, granularity etc and on how accurately it tracks (e.g. counter exhaustion, inaccurate trackers). Some of these constraints are discoverable from the hardware registers, others such as loss of accuracy have no universally accepted measures as they are typically access pattern dependent. Sadly it is very unlikely any hardware will implement a truly precise tracker given the large resource requirements for tracking at a

useful granularity.

Quoted 38 lines

There are two fundamental operation modes:

* Epoch based. Counters are checked after a period of time (Epoch) and if over a threshold added to the hotlist.
* Always on. Counters run until a threshold is reached, after that the hot unit is added to the hotlist and the counter released.

Counting can be filtered on:

* Region of CXL DPA space (256MiB per bit in a bitmap).
* Type of access - Trusted and non trusted or non trusted only, R/W/RW

Sampling can be modified by:

* Downsampling including potentially randomized downsampling.

The driver presented here is intended to be useful in its own right but also to act as the first step of a possible path towards hotness monitoring based hot page migration. Those steps might look like.

1. Gather data - drivers provide telemetry like solutions to get that data. May be enhanced, for example in this driver by providing the HPA address rather than DPA Unit Address. Userspace can access enough information to do this so maybe not.
2. Userspace algorithm development, possibly combined with userspace triggered migration by PA. Working out how to use different levels of constrained hardware resources will be challenging.
3. Move those algorithms in kernel. Will require generalization across different hotpage trackers etc.

So far this driver just gives access to the raw data. I will probably kick of a longer discussion on how to do adaptive sampling needed to actually use these units for tiering etc, sometime soon (if no one one else beats me too it). There is a follow up topic of how to virtualize this stuff for memory stranding cases (VM gets a fixed mixture of fast and slow memory and should do it's own tiering).

Quoted 5 lines

More details in the Documentation patch but typical commands are:

$perf record -a -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,\

hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,\

Quoted 80 lines

range_size=1024,randomized_downsampling=0,downsampling_factor=32,\
hotness_granual=12

$perf report --dump-raw-traces

Example output. With a counter_width of 16 (0x10) the least significant
4 bytes are the counter value and the unit index is bits 16-63. Here all units are over the threshold and the indexes are 0,1,2 etc.

. ... CXL_HMU data: size 33512 bytes
Header 0: units: 29c counter_width 10
Header 1 : deadbeef
0000000000000283
0000000000010364
0000000000020366
000000000003033c
0000000000040343
00000000000502ff
000000000006030d
000000000007031a

Which will produce a list of hotness entries.
Bits[N-1:0] counter value
Bits[63:N] Unit ID (combine with unit size and DPA base +- HDM decoder
config to get to a Host Physical Address)

Specific RFC questions.
- What should be in the header added to the aux buffer. Currently just the minimum is provided. Number of records and the counter width needed to decode them.
- Should we reset the counters when doing sampling "-F X"
If the frequency is higher than the epoch we never see any hot units. If so, when should we reset them?

Note testing has been light and on emulation only +- as perf tool is a pain to build on a striped back VM, build testing has all be on arm64 so far. The driver loads though on both arm64 and x86 so any problems are likely in the perf tool arch specific code which is build tested (on wrong machine)

The QEMU emulation needs some cleanup, but I should be able to post that shortly to let people actually play with this. There are lots of open questions there on how 'right' we want the emulation to be and what counting uarch to emulate.

Jonathan Cameron (4):
cxl: Register devices for CXL Hotness Monitoring Units (CHMU)
cxl: Hotness Monitoring Unit via a Perf AUX Buffer.
perf: Add support for CXL Hotness Monitoring Units (CHMU)
hwtrace: Document CXL Hotness Monitoring Unit driver

 Documentation/trace/cxl-hmu.rst     | 197 +-+-+-+-+-+-+-
 Documentation/trace/index.rst       |   1 +-
 drivers/cxl/Kconfig                 |   6 +-
 drivers/cxl/Makefile                |   3 +-
 drivers/cxl/core/Makefile           |   1 +-
 drivers/cxl/core/core.h             |   1 +-
 drivers/cxl/core/hmu.c              |  64 +-+-
 drivers/cxl/core/port.c             |   2 +-
 drivers/cxl/core/regs.c             |  14 +-
 drivers/cxl/cxl.h                   |   5 +-
 drivers/cxl/cxlpci.h                |   1 +-
 drivers/cxl/hmu.c                   | 880 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
 drivers/cxl/hmu.h                   |  23 +-
 drivers/cxl/pci.c                   |  26 +--
 tools/perf/arch/arm/util/auxtrace.c |  58 +-+-
tools/perf/arch/x86/util/auxtrace.c |  76 +-+-+-
 tools/perf/util/Build               |   1 +-
 tools/perf/util/auxtrace.c          |   4 +-
 tools/perf/util/auxtrace.h          |   1 +-
 tools/perf/util/cxl-hmu.c           | 367 +-+-+-+-+-+-+-+-+-+-+-+-
 tools/perf/util/cxl-hmu.h           |  18 +-
 21 files changed, 1748 insertions(+-), 1 deletion(-)  create mode

100644 Documentation/trace/cxl-hmu.rst create mode 100644 drivers/cxl/core/hmu.c create mode 100644 drivers/cxl/hmu.c create mode 100644 drivers/cxl/hmu.h create mode 100644 tools/perf/util/cxl-hmu.c create mode 100644 tools/perf/util/cxl-hmu.h

#18 lore raw
In reply to Jonathan Cameron
Quoted 13 lines · Jonathan Cameron
Quoted 3 lines · On Thu, 21 Nov 2024 10:18:41 +0000 Jonathan Cameron…

The CXL specification release 3.2 is now available under a click through at
https://computeexpresslink.org/cxl-specification/ and it brings new shiny toys.

If anyone wants to play, basic emulation on my CXL QEMU staging tree
https://gitlab.com/jic23/qemu/-/commit/e89b35d264c1bcc04807e7afab1254f35ffc8cb9

Branch with a few other things on top is:
https://gitlab.com/jic23/qemu/-/commits/cxl-2024-11-27

Quoted 76 lines

Note that this currently doesn't produce real data. I have a plan / initial PoC / hack to hook that up via an addition to the QEMU cache plugin and an external tool to emulate the hotness tracker counting hardware. Will be a little while before I get that finished, so in a meantime the above exercises the driver.

Jonathan

Quoted 68 lines

RFC reason
- Whilst trace capture with a particular configuration is potentially useful the intent is that CXL HMU units will be used to drive various forms of hotpage migration for memory tiering setups. This driver doesn't do this (yet), but rather provides data capture etc for experimentation and for working out how to mostly put the allocations in the right place to start with by tuning applications.

CXL r3.2 introduces a CXL Hotness Monitoring Unit definition. The intent of this is to provide a way to establish which units of memory (typically pages or larger) in CXL attached memory are hot. The implementation details and algorithm are all implementation defined. The specification simply describes the 'interface' which takes the form of ring buffer of hotness records in a PCI BAR and defined capability, configuration and status registers.

The hardware may have constraints on what it can track, granularity etc and on how accurately it tracks (e.g. counter exhaustion, inaccurate trackers). Some of these constraints are discoverable from the hardware registers, others such as loss of accuracy have no universally accepted measures as they are typically access pattern dependent. Sadly it is very unlikely any hardware will implement a truly precise tracker given the large resource requirements for tracking at a useful granularity.

There are two fundamental operation modes:

* Epoch based. Counters are checked after a period of time (Epoch) and if over a threshold added to the hotlist.
* Always on. Counters run until a threshold is reached, after that the hot unit is added to the hotlist and the counter released.

Counting can be filtered on:

* Region of CXL DPA space (256MiB per bit in a bitmap).
* Type of access - Trusted and non trusted or non trusted only, R/W/RW

Sampling can be modified by:

* Downsampling including potentially randomized downsampling.

The driver presented here is intended to be useful in its own right but also to act as the first step of a possible path towards hotness monitoring based hot page migration. Those steps might look like.

1. Gather data - drivers provide telemetry like solutions to get that data. May be enhanced, for example in this driver by providing the HPA address rather than DPA Unit Address. Userspace can access enough information to do this so maybe not.
2. Userspace algorithm development, possibly combined with userspace triggered migration by PA. Working out how to use different levels of constrained hardware resources will be challenging.
3. Move those algorithms in kernel. Will require generalization across different hotpage trackers etc.

So far this driver just gives access to the raw data. I will probably kick of a longer discussion on how to do adaptive sampling needed to actually use these units for tiering etc, sometime soon (if no one one else beats me too it). There is a follow up topic of how to virtualize this stuff for memory stranding cases (VM gets a fixed mixture of fast and slow memory and should do it's own tiering).

More details in the Documentation patch but typical commands are:

$perf record -a -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,\
hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,\
range_size=1024,randomized_downsampling=0,downsampling_factor=32,\
hotness_granual=12

Facing issue while executing perf record on x86 emulation environment using following steps

1. Tried applying CHMU Patch on branch cxl-for-6.13 using b4 utility. As base commit is not specified, with minor change able to apply patch. Compiled kernel with CONFIG_CXL_HMU

2. Compiled jic23/cxl-2024-11-27 for x86_64-softmmu

3. Launched Qemu with following CXL topology along with compiled kernel

VM="-object memory-backend-ram,id=vmem1,share=on,size=512M \
    -device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1 \
    -device cxl-rp,port=0,bus=cxl.1,id=root_port13,chassis=0,slot=2 \
    -device cxl-type3,bus=root_port13,volatile-memdev=vmem1,id=cxl-vmem1 \
    -M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G,cxl-fmw.0.interleave-granularity=8k"

4. Created region and onlined this memory. Also run top utility on the newly created numa node using numactl -m<node> top

5. Compiled and installed perf utility in qemu environment, and able to see cxl_hmu_mem* entries in perf list

root@QEMUCXL2030mm:~# perf list
<snip>
 cxl_hmu_mem0.0.0/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
 cxl_hmu_mem0.0.1/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
 cxl_hmu_mem0.0.2/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
 cxl_hmu_mem1.0.0/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
 cxl_hmu_mem1.0.1/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
 cxl_hmu_mem1.0.2/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
 cxl_pmu_mem0.0/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
 cxl_pmu_mem0.1/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
 cxl_pmu_mem1.0/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
 cxl_pmu_mem1.1/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
<snip>

6. Tried running perf command mentioned in Documentation/trace/cxl-hmu.rst

root@QEMUCXL2030mm:/home/cxl/cxl-linux-mainline/tools/perf# perf -v
perf version 6.12.rc5.gc198a4f4a356
root@QEMUCXL2030mm:/home/cxl/cxl-linux-mainline/tools/perf# perf record -a  -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,range_size=1024,randomized_downsampling=0,downsampling_factor=32,hotness_granual=12
event syntax error: '..ess_granual=12'
                                 \___ Unrecognized input

Are there any steps i am missing?

Regards,
Neeraj

Quoted 79 lines
Quoted 78 lines

$perf report --dump-raw-traces

Example output. With a counter_width of 16 (0x10) the least significant 4 bytes are the counter value and the unit index is bits 16-63. Here all units are over the threshold and the indexes are 0,1,2 etc.

. ... CXL_HMU data: size 33512 bytes
Header 0: units: 29c counter_width 10
Header 1 : deadbeef
0000000000000283
0000000000010364
0000000000020366
000000000003033c
0000000000040343
00000000000502ff
000000000006030d
000000000007031a

Which will produce a list of hotness entries.
Bits[N-1:0] counter value
Bits[63:N] Unit ID (combine with unit size and DPA base + HDM decoder
config to get to a Host Physical Address)

Specific RFC questions.
- What should be in the header added to the aux buffer. Currently just the minimum is provided. Number of records and the counter width needed to decode them.
- Should we reset the counters when doing sampling "-F X"
If the frequency is higher than the epoch we never see any hot units. If so, when should we reset them?

Note testing has been light and on emulation only + as perf tool is a pain to build on a striped back VM, build testing has all be on arm64 so far. The driver loads though on both arm64 and x86 so any problems are likely in the perf tool arch specific code which is build tested (on wrong machine)

The QEMU emulation needs some cleanup, but I should be able to post that shortly to let people actually play with this. There are lots of open questions there on how 'right' we want the emulation to be and what counting uarch to emulate.

Jonathan Cameron (4):
cxl: Register devices for CXL Hotness Monitoring Units (CHMU)
cxl: Hotness Monitoring Unit via a Perf AUX Buffer.
perf: Add support for CXL Hotness Monitoring Units (CHMU)
hwtrace: Document CXL Hotness Monitoring Unit driver

 Documentation/trace/cxl-hmu.rst     | 197 +++++++
 Documentation/trace/index.rst       |   1 +
 drivers/cxl/Kconfig                 |   6 +
 drivers/cxl/Makefile                |   3 +
 drivers/cxl/core/Makefile           |   1 +
 drivers/cxl/core/core.h             |   1 +
 drivers/cxl/core/hmu.c              |  64 ++
 drivers/cxl/core/port.c             |   2 +
 drivers/cxl/core/regs.c             |  14 +
 drivers/cxl/cxl.h                   |   5 +
 drivers/cxl/cxlpci.h                |   1 +
 drivers/cxl/hmu.c                   | 880 ++++++++++++++++++++++++++++
 drivers/cxl/hmu.h                   |  23 +
 drivers/cxl/pci.c                   |  26 +-
 tools/perf/arch/arm/util/auxtrace.c |  58 ++
 tools/perf/arch/x86/util/auxtrace.c |  76 +++
 tools/perf/util/Build               |   1 +
 tools/perf/util/auxtrace.c          |   4 +
 tools/perf/util/auxtrace.h          |   1 +
 tools/perf/util/cxl-hmu.c           | 367 ++++++++++++
 tools/perf/util/cxl-hmu.h           |  18 +
 21 files changed, 1748 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/trace/cxl-hmu.rst
 create mode 100644 drivers/cxl/core/hmu.c
 create mode 100644 drivers/cxl/hmu.c
 create mode 100644 drivers/cxl/hmu.h
 create mode 100644 tools/perf/util/cxl-hmu.c
 create mode 100644 tools/perf/util/cxl-hmu.h
#19 lore raw
Quoted 126 lines · On Fri, 3 Jan 2025 10:57:22 +0530 Neeraj Kumar <[email protected]> wrote:
Quoted 13 lines · Jonathan Cameron
Quoted 3 lines · On Thu, 21 Nov 2024 10:18:41 +0000 Jonathan Cameron…

The CXL specification release 3.2 is now available under a click through at
https://computeexpresslink.org/cxl-specification/ and it brings new shiny toys.

If anyone wants to play, basic emulation on my CXL QEMU staging tree
https://gitlab.com/jic23/qemu/-/commit/e89b35d264c1bcc04807e7afab1254f35ffc8cb9

Branch with a few other things on top is:
https://gitlab.com/jic23/qemu/-/commits/cxl-2024-11-27

Quoted 76 lines

Note that this currently doesn't produce real data. I have a plan / initial PoC / hack to hook that up via an addition to the QEMU cache plugin and an external tool to emulate the hotness tracker counting hardware. Will be a little while before I get that finished, so in a meantime the above exercises the driver.

Jonathan

Quoted 68 lines

RFC reason
- Whilst trace capture with a particular configuration is potentially useful the intent is that CXL HMU units will be used to drive various forms of hotpage migration for memory tiering setups. This driver doesn't do this (yet), but rather provides data capture etc for experimentation and for working out how to mostly put the allocations in the right place to start with by tuning applications.

CXL r3.2 introduces a CXL Hotness Monitoring Unit definition. The intent of this is to provide a way to establish which units of memory (typically pages or larger) in CXL attached memory are hot. The implementation details and algorithm are all implementation defined. The specification simply describes the 'interface' which takes the form of ring buffer of hotness records in a PCI BAR and defined capability, configuration and status registers.

The hardware may have constraints on what it can track, granularity etc and on how accurately it tracks (e.g. counter exhaustion, inaccurate trackers). Some of these constraints are discoverable from the hardware registers, others such as loss of accuracy have no universally accepted measures as they are typically access pattern dependent. Sadly it is very unlikely any hardware will implement a truly precise tracker given the large resource requirements for tracking at a useful granularity.

There are two fundamental operation modes:

* Epoch based. Counters are checked after a period of time (Epoch) and if over a threshold added to the hotlist.
* Always on. Counters run until a threshold is reached, after that the hot unit is added to the hotlist and the counter released.

Counting can be filtered on:

* Region of CXL DPA space (256MiB per bit in a bitmap).
* Type of access - Trusted and non trusted or non trusted only, R/W/RW

Sampling can be modified by:

* Downsampling including potentially randomized downsampling.

The driver presented here is intended to be useful in its own right but also to act as the first step of a possible path towards hotness monitoring based hot page migration. Those steps might look like.

1. Gather data - drivers provide telemetry like solutions to get that data. May be enhanced, for example in this driver by providing the HPA address rather than DPA Unit Address. Userspace can access enough information to do this so maybe not.
2. Userspace algorithm development, possibly combined with userspace triggered migration by PA. Working out how to use different levels of constrained hardware resources will be challenging.
3. Move those algorithms in kernel. Will require generalization across different hotpage trackers etc.

So far this driver just gives access to the raw data. I will probably kick of a longer discussion on how to do adaptive sampling needed to actually use these units for tiering etc, sometime soon (if no one one else beats me too it). There is a follow up topic of how to virtualize this stuff for memory stranding cases (VM gets a fixed mixture of fast and slow memory and should do it's own tiering).

More details in the Documentation patch but typical commands are:

$perf record -a -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,\
hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,\
range_size=1024,randomized_downsampling=0,downsampling_factor=32,\
hotness_granual=12

Facing issue while executing perf record on x86 emulation environment using following steps

1. Tried applying CHMU Patch on branch cxl-for-6.13 using b4 utility. As base commit is not specified, with minor change able to apply patch. Compiled kernel with CONFIG_CXL_HMU

2. Compiled jic23/cxl-2024-11-27 for x86_64-softmmu

3. Launched Qemu with following CXL topology along with compiled kernel

VM="-object memory-backend-ram,id=vmem1,share=on,size=512M \
     -device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1 \
     -device cxl-rp,port=0,bus=cxl.1,id=root_port13,chassis=0,slot=2 \
     -device cxl-type3,bus=root_port13,volatile-memdev=vmem1,id=cxl-vmem1 \
     -M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G,cxl-fmw.0.interleave-granularity=8k"

4. Created region and onlined this memory. Also run top utility on the newly created numa node using numactl -m<node> top

5. Compiled and installed perf utility in qemu environment, and able to see cxl_hmu_mem* entries in perf list

root@QEMUCXL2030mm:~# perf list
<snip>
  cxl_hmu_mem0.0.0/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem0.0.1/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem0.0.2/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem1.0.0/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem1.0.1/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem1.0.2/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_pmu_mem0.0/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
  cxl_pmu_mem0.1/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
  cxl_pmu_mem1.0/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
  cxl_pmu_mem1.1/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
<snip>
Quoted 7 lines

6. Tried running perf command mentioned in Documentation/trace/cxl-hmu.rst

root@QEMUCXL2030mm:/home/cxl/cxl-linux-mainline/tools/perf# perf -v
perf version 6.12.rc5.gc198a4f4a356
root@QEMUCXL2030mm:/home/cxl/cxl-linux-mainline/tools/perf# perf record -a  -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,range_size=1024,randomized_downsampling=0,downsampling_factor=32,hotness_granual=12
event syntax error: '..ess_granual=12'
                                  \___ Unrecognized input

This is probably my mistake when cutting and pasting the example from a terminal. Add a trailing / and something to run.

perf record -a -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,range_size=1024,randomized_downsampling=0,downsampling_factor=32,hotness_granual=12/ -- sleep 10

Jonathan

Quoted 88 lines

Are there any steps i am missing?

Regards,
Neeraj

Quoted 79 lines
Quoted 78 lines

$perf report --dump-raw-traces

Example output. With a counter_width of 16 (0x10) the least significant 4 bytes are the counter value and the unit index is bits 16-63. Here all units are over the threshold and the indexes are 0,1,2 etc.

. ... CXL_HMU data: size 33512 bytes
Header 0: units: 29c counter_width 10
Header 1 : deadbeef
0000000000000283
0000000000010364
0000000000020366
000000000003033c
0000000000040343
00000000000502ff
000000000006030d
000000000007031a

Which will produce a list of hotness entries.
Bits[N-1:0] counter value
Bits[63:N] Unit ID (combine with unit size and DPA base + HDM decoder
config to get to a Host Physical Address)

Specific RFC questions.
- What should be in the header added to the aux buffer. Currently just the minimum is provided. Number of records and the counter width needed to decode them.
- Should we reset the counters when doing sampling "-F X"
If the frequency is higher than the epoch we never see any hot units. If so, when should we reset them?

Note testing has been light and on emulation only + as perf tool is a pain to build on a striped back VM, build testing has all be on arm64 so far. The driver loads though on both arm64 and x86 so any problems are likely in the perf tool arch specific code which is build tested (on wrong machine)

The QEMU emulation needs some cleanup, but I should be able to post that shortly to let people actually play with this. There are lots of open questions there on how 'right' we want the emulation to be and what counting uarch to emulate.

Jonathan Cameron (4):
cxl: Register devices for CXL Hotness Monitoring Units (CHMU)
cxl: Hotness Monitoring Unit via a Perf AUX Buffer.
perf: Add support for CXL Hotness Monitoring Units (CHMU)
hwtrace: Document CXL Hotness Monitoring Unit driver

 Documentation/trace/cxl-hmu.rst     | 197 +++++++
 Documentation/trace/index.rst       |   1 +
 drivers/cxl/Kconfig                 |   6 +
 drivers/cxl/Makefile                |   3 +
 drivers/cxl/core/Makefile           |   1 +
 drivers/cxl/core/core.h             |   1 +
 drivers/cxl/core/hmu.c              |  64 ++
 drivers/cxl/core/port.c             |   2 +
 drivers/cxl/core/regs.c             |  14 +
 drivers/cxl/cxl.h                   |   5 +
 drivers/cxl/cxlpci.h                |   1 +
 drivers/cxl/hmu.c                   | 880 ++++++++++++++++++++++++++++
 drivers/cxl/hmu.h                   |  23 +
 drivers/cxl/pci.c                   |  26 +-
 tools/perf/arch/arm/util/auxtrace.c |  58 ++
 tools/perf/arch/x86/util/auxtrace.c |  76 +++
 tools/perf/util/Build               |   1 +
 tools/perf/util/auxtrace.c          |   4 +
 tools/perf/util/auxtrace.h          |   1 +
 tools/perf/util/cxl-hmu.c           | 367 ++++++++++++
 tools/perf/util/cxl-hmu.h           |  18 +
 21 files changed, 1748 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/trace/cxl-hmu.rst
 create mode 100644 drivers/cxl/core/hmu.c
 create mode 100644 drivers/cxl/hmu.c
 create mode 100644 drivers/cxl/hmu.h
 create mode 100644 tools/perf/util/cxl-hmu.c
 create mode 100644 tools/perf/util/cxl-hmu.h
#20 lore raw
Quoted 137 lines · Jonathan Cameron
Quoted 126 lines · On Fri, 3 Jan 2025 10:57:22 +0530 Neeraj Kumar <[email protected]> wrote:
Quoted 13 lines · Jonathan Cameron
Quoted 3 lines · On Thu, 21 Nov 2024 10:18:41 +0000 Jonathan Cameron…

The CXL specification release 3.2 is now available under a click through at
https://computeexpresslink.org/cxl-specification/ and it brings new shiny toys.

If anyone wants to play, basic emulation on my CXL QEMU staging tree
https://gitlab.com/jic23/qemu/-/commit/e89b35d264c1bcc04807e7afab1254f35ffc8cb9

Branch with a few other things on top is:
https://gitlab.com/jic23/qemu/-/commits/cxl-2024-11-27

Quoted 76 lines

Note that this currently doesn't produce real data. I have a plan / initial PoC / hack to hook that up via an addition to the QEMU cache plugin and an external tool to emulate the hotness tracker counting hardware. Will be a little while before I get that finished, so in a meantime the above exercises the driver.

Jonathan

Quoted 68 lines

RFC reason
- Whilst trace capture with a particular configuration is potentially useful the intent is that CXL HMU units will be used to drive various forms of hotpage migration for memory tiering setups. This driver doesn't do this (yet), but rather provides data capture etc for experimentation and for working out how to mostly put the allocations in the right place to start with by tuning applications.

CXL r3.2 introduces a CXL Hotness Monitoring Unit definition. The intent of this is to provide a way to establish which units of memory (typically pages or larger) in CXL attached memory are hot. The implementation details and algorithm are all implementation defined. The specification simply describes the 'interface' which takes the form of ring buffer of hotness records in a PCI BAR and defined capability, configuration and status registers.

The hardware may have constraints on what it can track, granularity etc and on how accurately it tracks (e.g. counter exhaustion, inaccurate trackers). Some of these constraints are discoverable from the hardware registers, others such as loss of accuracy have no universally accepted measures as they are typically access pattern dependent. Sadly it is very unlikely any hardware will implement a truly precise tracker given the large resource requirements for tracking at a useful granularity.

There are two fundamental operation modes:

* Epoch based. Counters are checked after a period of time (Epoch) and if over a threshold added to the hotlist.
* Always on. Counters run until a threshold is reached, after that the hot unit is added to the hotlist and the counter released.

Counting can be filtered on:

* Region of CXL DPA space (256MiB per bit in a bitmap).
* Type of access - Trusted and non trusted or non trusted only, R/W/RW

Sampling can be modified by:

* Downsampling including potentially randomized downsampling.

The driver presented here is intended to be useful in its own right but also to act as the first step of a possible path towards hotness monitoring based hot page migration. Those steps might look like.

1. Gather data - drivers provide telemetry like solutions to get that data. May be enhanced, for example in this driver by providing the HPA address rather than DPA Unit Address. Userspace can access enough information to do this so maybe not.
2. Userspace algorithm development, possibly combined with userspace triggered migration by PA. Working out how to use different levels of constrained hardware resources will be challenging.
3. Move those algorithms in kernel. Will require generalization across different hotpage trackers etc.

So far this driver just gives access to the raw data. I will probably kick of a longer discussion on how to do adaptive sampling needed to actually use these units for tiering etc, sometime soon (if no one one else beats me too it). There is a follow up topic of how to virtualize this stuff for memory stranding cases (VM gets a fixed mixture of fast and slow memory and should do it's own tiering).

More details in the Documentation patch but typical commands are:

$perf record -a -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,\
hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,\
range_size=1024,randomized_downsampling=0,downsampling_factor=32,\
hotness_granual=12

Facing issue while executing perf record on x86 emulation environment using following steps

1. Tried applying CHMU Patch on branch cxl-for-6.13 using b4 utility. As base commit is not specified, with minor change able to apply patch. Compiled kernel with CONFIG_CXL_HMU

2. Compiled jic23/cxl-2024-11-27 for x86_64-softmmu

3. Launched Qemu with following CXL topology along with compiled kernel

VM="-object memory-backend-ram,id=vmem1,share=on,size=512M \
     -device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1 \
     -device cxl-rp,port=0,bus=cxl.1,id=root_port13,chassis=0,slot=2 \
     -device cxl-type3,bus=root_port13,volatile-memdev=vmem1,id=cxl-vmem1 \
     -M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G,cxl-fmw.0.interleave-granularity=8k"

4. Created region and onlined this memory. Also run top utility on the newly created numa node using numactl -m<node> top

5. Compiled and installed perf utility in qemu environment, and able to see cxl_hmu_mem* entries in perf list

root@QEMUCXL2030mm:~# perf list
<snip>
  cxl_hmu_mem0.0.0/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem0.0.1/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem0.0.2/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem1.0.0/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem1.0.1/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem1.0.2/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_pmu_mem0.0/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
  cxl_pmu_mem0.1/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
  cxl_pmu_mem1.0/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
  cxl_pmu_mem1.1/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
<snip>
Quoted 7 lines

6. Tried running perf command mentioned in Documentation/trace/cxl-hmu.rst

root@QEMUCXL2030mm:/home/cxl/cxl-linux-mainline/tools/perf# perf -v
perf version 6.12.rc5.gc198a4f4a356
root@QEMUCXL2030mm:/home/cxl/cxl-linux-mainline/tools/perf# perf record -a  -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,range_size=1024,randomized_downsampling=0,downsampling_factor=32,hotness_granual=12
event syntax error: '..ess_granual=12'
                                  \___ Unrecognized input
Quoted 4 lines

This is probably my mistake when cutting and pasting the example from a terminal. Add a trailing / and something to run.

perf record -a -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,range_size=1024,randomized_downsampling=0,downsampling_factor=32,hotness_granual=12/ -- sleep 10

Hi Jonathan,

I tried to use this new command but perf shows error.

Based on the change of hmu iomap_block size[1], my steps are like below:

step1: Create cxl region and online the numa node

root@ubuntu-jammy-arm64:~/tools# numactl -H
available: 3 nodes (0-2)
node 0 cpus: 0 1
node 0 size: 1972 MB
node 0 free: 1694 MB
node 1 cpus: 2 3
node 1 size: 1942 MB
node 1 free: 1690 MB
node 2 cpus:
node 2 size: 256 MB
node 2 free: 256 MB
node distances:
node   0   1   2
  0:  10  20  20
  1:  20  10  20
  2:  20  20  10

step2: Bind this numa node to run 'ls'

root@ubuntu-jammy-arm64:~/tools# numactl -m 2 ls
build   perf.data  ndctl  perf     
root@ubuntu-jammy-arm64:~/tools# numastat 
                           node0           node1           node2
numa_hit                  109323          141170              77
numa_miss                      0               0               0
numa_foreign                   0               0               0
interleave_hit               519             591               0
local_node                108810          139591               0
other_node                   513            1579              77

step3: Use perf tool

root@ubuntu-jammy-arm64:~/tools# perf -v
perf version 6.15.rc5.g2c3e6f60f5cf
root@ubuntu-jammy-arm64:~/tools# perf list | grep -i hmu
  cxl_hmu_mem0.0.0/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw event descriptor]
root@ubuntu-jammy-arm64:~/tools# perf record -a  -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,hotness_threshold=1024,epoperf record -a  -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,range_size=1024,randomized_downsampling=0,downsampling_factor=32,hotness_granual=12/ -- sleep 10

Error:
cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,range_size=1024,randomized_downsampling=0,downsampling_factor=32,hotness_granual=12/H: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'

[1]:https://lore.kernel.org/linux-cxl/[email protected]/T/#u

Is something wrong on the CHMU interrupts?

Quoted 92 lines

Jonathan

Quoted 88 lines

Are there any steps i am missing?

Regards,
Neeraj

Quoted 79 lines
Quoted 78 lines

$perf report --dump-raw-traces

Example output. With a counter_width of 16 (0x10) the least significant 4 bytes are the counter value and the unit index is bits 16-63. Here all units are over the threshold and the indexes are 0,1,2 etc.

. ... CXL_HMU data: size 33512 bytes
Header 0: units: 29c counter_width 10
Header 1 : deadbeef
0000000000000283
0000000000010364
0000000000020366
000000000003033c
0000000000040343
00000000000502ff
000000000006030d
000000000007031a

Which will produce a list of hotness entries.
Bits[N-1:0] counter value
Bits[63:N] Unit ID (combine with unit size and DPA base + HDM decoder
config to get to a Host Physical Address)

Specific RFC questions.
- What should be in the header added to the aux buffer. Currently just the minimum is provided. Number of records and the counter width needed to decode them.
- Should we reset the counters when doing sampling "-F X"
If the frequency is higher than the epoch we never see any hot units. If so, when should we reset them?

Note testing has been light and on emulation only + as perf tool is a pain to build on a striped back VM, build testing has all be on arm64 so far. The driver loads though on both arm64 and x86 so any problems are likely in the perf tool arch specific code which is build tested (on wrong machine)

The QEMU emulation needs some cleanup, but I should be able to post that shortly to let people actually play with this. There are lots of open questions there on how 'right' we want the emulation to be and what counting uarch to emulate.

Jonathan Cameron (4):
cxl: Register devices for CXL Hotness Monitoring Units (CHMU)
cxl: Hotness Monitoring Unit via a Perf AUX Buffer.
perf: Add support for CXL Hotness Monitoring Units (CHMU)
hwtrace: Document CXL Hotness Monitoring Unit driver

 Documentation/trace/cxl-hmu.rst     | 197 +++++++
 Documentation/trace/index.rst       |   1 +
 drivers/cxl/Kconfig                 |   6 +
 drivers/cxl/Makefile                |   3 +
 drivers/cxl/core/Makefile           |   1 +
 drivers/cxl/core/core.h             |   1 +
 drivers/cxl/core/hmu.c              |  64 ++
 drivers/cxl/core/port.c             |   2 +
 drivers/cxl/core/regs.c             |  14 +
 drivers/cxl/cxl.h                   |   5 +
 drivers/cxl/cxlpci.h                |   1 +
 drivers/cxl/hmu.c                   | 880 ++++++++++++++++++++++++++++
 drivers/cxl/hmu.h                   |  23 +
 drivers/cxl/pci.c                   |  26 +-
 tools/perf/arch/arm/util/auxtrace.c |  58 ++
 tools/perf/arch/x86/util/auxtrace.c |  76 +++
 tools/perf/util/Build               |   1 +
 tools/perf/util/auxtrace.c          |   4 +
 tools/perf/util/auxtrace.h          |   1 +
 tools/perf/util/cxl-hmu.c           | 367 ++++++++++++
 tools/perf/util/cxl-hmu.h           |  18 +
 21 files changed, 1748 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/trace/cxl-hmu.rst
 create mode 100644 drivers/cxl/core/hmu.c
 create mode 100644 drivers/cxl/hmu.c
 create mode 100644 drivers/cxl/hmu.h
 create mode 100644 tools/perf/util/cxl-hmu.c
 create mode 100644 tools/perf/util/cxl-hmu.h
#21 lore raw
Quoted 184 lines · On Thu, 19 Jun 2025 11:59:28 +0800 Yuquan Wang <[email protected]>…
Quoted 137 lines · Jonathan Cameron
Quoted 126 lines · On Fri, 3 Jan 2025 10:57:22 +0530 Neeraj Kumar <[email protected]> wrote:
Quoted 13 lines · Jonathan Cameron
Quoted 3 lines · On Thu, 21 Nov 2024 10:18:41 +0000 Jonathan Cameron…

The CXL specification release 3.2 is now available under a click through at
https://computeexpresslink.org/cxl-specification/ and it brings new shiny toys.

If anyone wants to play, basic emulation on my CXL QEMU staging tree
https://gitlab.com/jic23/qemu/-/commit/e89b35d264c1bcc04807e7afab1254f35ffc8cb9

Branch with a few other things on top is:
https://gitlab.com/jic23/qemu/-/commits/cxl-2024-11-27

Quoted 76 lines

Note that this currently doesn't produce real data. I have a plan / initial PoC / hack to hook that up via an addition to the QEMU cache plugin and an external tool to emulate the hotness tracker counting hardware. Will be a little while before I get that finished, so in a meantime the above exercises the driver.

Jonathan

Quoted 68 lines

RFC reason
- Whilst trace capture with a particular configuration is potentially useful the intent is that CXL HMU units will be used to drive various forms of hotpage migration for memory tiering setups. This driver doesn't do this (yet), but rather provides data capture etc for experimentation and for working out how to mostly put the allocations in the right place to start with by tuning applications.

CXL r3.2 introduces a CXL Hotness Monitoring Unit definition. The intent of this is to provide a way to establish which units of memory (typically pages or larger) in CXL attached memory are hot. The implementation details and algorithm are all implementation defined. The specification simply describes the 'interface' which takes the form of ring buffer of hotness records in a PCI BAR and defined capability, configuration and status registers.

The hardware may have constraints on what it can track, granularity etc and on how accurately it tracks (e.g. counter exhaustion, inaccurate trackers). Some of these constraints are discoverable from the hardware registers, others such as loss of accuracy have no universally accepted measures as they are typically access pattern dependent. Sadly it is very unlikely any hardware will implement a truly precise tracker given the large resource requirements for tracking at a useful granularity.

There are two fundamental operation modes:

* Epoch based. Counters are checked after a period of time (Epoch) and if over a threshold added to the hotlist.
* Always on. Counters run until a threshold is reached, after that the hot unit is added to the hotlist and the counter released.

Counting can be filtered on:

* Region of CXL DPA space (256MiB per bit in a bitmap).
* Type of access - Trusted and non trusted or non trusted only, R/W/RW

Sampling can be modified by:

* Downsampling including potentially randomized downsampling.

The driver presented here is intended to be useful in its own right but also to act as the first step of a possible path towards hotness monitoring based hot page migration. Those steps might look like.

1. Gather data - drivers provide telemetry like solutions to get that data. May be enhanced, for example in this driver by providing the HPA address rather than DPA Unit Address. Userspace can access enough information to do this so maybe not.
2. Userspace algorithm development, possibly combined with userspace triggered migration by PA. Working out how to use different levels of constrained hardware resources will be challenging.
3. Move those algorithms in kernel. Will require generalization across different hotpage trackers etc.

So far this driver just gives access to the raw data. I will probably kick of a longer discussion on how to do adaptive sampling needed to actually use these units for tiering etc, sometime soon (if no one one else beats me too it). There is a follow up topic of how to virtualize this stuff for memory stranding cases (VM gets a fixed mixture of fast and slow memory and should do it's own tiering).

More details in the Documentation patch but typical commands are:

$perf record -a -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,\
hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,\
range_size=1024,randomized_downsampling=0,downsampling_factor=32,\
hotness_granual=12

Facing issue while executing perf record on x86 emulation environment using following steps

1. Tried applying CHMU Patch on branch cxl-for-6.13 using b4 utility. As base commit is not specified, with minor change able to apply patch. Compiled kernel with CONFIG_CXL_HMU

2. Compiled jic23/cxl-2024-11-27 for x86_64-softmmu

3. Launched Qemu with following CXL topology along with compiled kernel

VM="-object memory-backend-ram,id=vmem1,share=on,size=512M \
     -device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1 \
     -device cxl-rp,port=0,bus=cxl.1,id=root_port13,chassis=0,slot=2 \
     -device cxl-type3,bus=root_port13,volatile-memdev=vmem1,id=cxl-vmem1 \
     -M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G,cxl-fmw.0.interleave-granularity=8k"

4. Created region and onlined this memory. Also run top utility on the newly created numa node using numactl -m<node> top

5. Compiled and installed perf utility in qemu environment, and able to see cxl_hmu_mem* entries in perf list

root@QEMUCXL2030mm:~# perf list
<snip>
  cxl_hmu_mem0.0.0/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem0.0.1/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem0.0.2/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem1.0.0/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem1.0.1/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_hmu_mem1.0.2/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw ev>
  cxl_pmu_mem0.0/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
  cxl_pmu_mem0.1/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
  cxl_pmu_mem1.0/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
  cxl_pmu_mem1.1/vid=0..0xffff,edge,mask=0..0xffffffff,.../modifier[Raw event descriptor]
<snip>
Quoted 7 lines

6. Tried running perf command mentioned in Documentation/trace/cxl-hmu.rst

root@QEMUCXL2030mm:/home/cxl/cxl-linux-mainline/tools/perf# perf -v
perf version 6.12.rc5.gc198a4f4a356
root@QEMUCXL2030mm:/home/cxl/cxl-linux-mainline/tools/perf# perf record -a  -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,range_size=1024,randomized_downsampling=0,downsampling_factor=32,hotness_granual=12
event syntax error: '..ess_granual=12'
                                  \___ Unrecognized input  
Quoted 4 lines

This is probably my mistake when cutting and pasting the example from a terminal. Add a trailing / and something to run.

perf record -a -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,range_size=1024,randomized_downsampling=0,downsampling_factor=32,hotness_granual=12/ -- sleep 10

Hi Jonathan,

I tried to use this new command but perf shows error.

Based on the change of hmu iomap_block size[1], my steps are like below:

step1: Create cxl region and online the numa node

root@ubuntu-jammy-arm64:~/tools# numactl -H
available: 3 nodes (0-2)
node 0 cpus: 0 1
node 0 size: 1972 MB
node 0 free: 1694 MB
node 1 cpus: 2 3
node 1 size: 1942 MB
node 1 free: 1690 MB
node 2 cpus:
node 2 size: 256 MB
node 2 free: 256 MB
node distances:
node   0   1   2
  0:  10  20  20
  1:  20  10  20
  2:  20  20  10

step2: Bind this numa node to run 'ls'

root@ubuntu-jammy-arm64:~/tools# numactl -m 2 ls
build   perf.data  ndctl  perf     
root@ubuntu-jammy-arm64:~/tools# numastat 
                           node0           node1           node2
numa_hit                  109323          141170              77
numa_miss                      0               0               0
numa_foreign                   0               0               0
interleave_hit               519             591               0
local_node                108810          139591               0
other_node                   513            1579              77

step3: Use perf tool

Quoted 8 lines
root@ubuntu-jammy-arm64:~/tools# perf -v
perf version 6.15.rc5.g2c3e6f60f5cf
root@ubuntu-jammy-arm64:~/tools# perf list | grep -i hmu
  cxl_hmu_mem0.0.0/hotness_granual=0..0xffffffff,hotness_threshold=0..0xffffffff,downsampling_factor=0..255,.../modifier[Raw event descriptor]
root@ubuntu-jammy-arm64:~/tools# perf record -a  -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,hotness_threshold=1024,epoperf record -a  -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,range_size=1024,randomized_downsampling=0,downsampling_factor=32,hotness_granual=12/ -- sleep 10

This cut and paste seems to have half of two commands.

I'll just grab the second one as what I'm guessing you were running.

perf record -a -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,range_size=1024,randomized_downsampling=0,downsampling_factor=32,hotness_granual=12/ -- sleep 10

Quoted 6 lines

Error:
cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,range_size=1024,randomized_downsampling=0,downsampling_factor=32,hotness_granual=12/H: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'

[1]:https://lore.kernel.org/linux-cxl/[email protected]/T/#u

Is something wrong on the CHMU interrupts?

That error message is annoyingly meaningless and misleading. I think the perf core is assuming that any not supported error means that is the problem rather than something else in the command line might not be supported.

It's failing because we don't support that downsampling_factor. The format does (hence I think the stuff above) but the emulation doesn't.

So for now just don't specify downsampling_factor in the command line. There is a bug in the emulation around setting it to 1 it seems.

val = FIELD_DP64(val, CXL_CHMU0_CAP1, DOWN_SAMPLING_FACTORS, BIT(1))
which should have been bit 0 to indicate no downsampling (comment above that is wrong too).

Looking at the spec is rather confusing on how this is supposed to work. We have a bitmap that has 16 bits, each of which represents a power of 2, but then we have a CHMU configuration register Down-sampling factor that says bits 99:96 are the 'one of the 16 possible values' I assume that means bit offset.

I'll reply to the qemu thread to point out this bug in the capability.

Jonathan

Quoted 95 lines
Quoted 93 lines

Jonathan

Quoted 88 lines

Are there any steps i am missing?

Regards,
Neeraj

Quoted 79 lines
Quoted 78 lines

$perf report --dump-raw-traces

Example output. With a counter_width of 16 (0x10) the least significant 4 bytes are the counter value and the unit index is bits 16-63. Here all units are over the threshold and the indexes are 0,1,2 etc.

. ... CXL_HMU data: size 33512 bytes
Header 0: units: 29c counter_width 10
Header 1 : deadbeef
0000000000000283
0000000000010364
0000000000020366
000000000003033c
0000000000040343
00000000000502ff
000000000006030d
000000000007031a

Which will produce a list of hotness entries.
Bits[N-1:0] counter value
Bits[63:N] Unit ID (combine with unit size and DPA base + HDM decoder
config to get to a Host Physical Address)

Specific RFC questions.
- What should be in the header added to the aux buffer. Currently just the minimum is provided. Number of records and the counter width needed to decode them.
- Should we reset the counters when doing sampling "-F X"
If the frequency is higher than the epoch we never see any hot units. If so, when should we reset them?

Note testing has been light and on emulation only + as perf tool is a pain to build on a striped back VM, build testing has all be on arm64 so far. The driver loads though on both arm64 and x86 so any problems are likely in the perf tool arch specific code which is build tested (on wrong machine)

The QEMU emulation needs some cleanup, but I should be able to post that shortly to let people actually play with this. There are lots of open questions there on how 'right' we want the emulation to be and what counting uarch to emulate.

Jonathan Cameron (4):
cxl: Register devices for CXL Hotness Monitoring Units (CHMU)
cxl: Hotness Monitoring Unit via a Perf AUX Buffer.
perf: Add support for CXL Hotness Monitoring Units (CHMU)
hwtrace: Document CXL Hotness Monitoring Unit driver

 Documentation/trace/cxl-hmu.rst     | 197 +++++++
 Documentation/trace/index.rst       |   1 +
 drivers/cxl/Kconfig                 |   6 +
 drivers/cxl/Makefile                |   3 +
 drivers/cxl/core/Makefile           |   1 +
 drivers/cxl/core/core.h             |   1 +
 drivers/cxl/core/hmu.c              |  64 ++
 drivers/cxl/core/port.c             |   2 +
 drivers/cxl/core/regs.c             |  14 +
 drivers/cxl/cxl.h                   |   5 +
 drivers/cxl/cxlpci.h                |   1 +
 drivers/cxl/hmu.c                   | 880 ++++++++++++++++++++++++++++
 drivers/cxl/hmu.h                   |  23 +
 drivers/cxl/pci.c                   |  26 +-
 tools/perf/arch/arm/util/auxtrace.c |  58 ++
 tools/perf/arch/x86/util/auxtrace.c |  76 +++
 tools/perf/util/Build               |   1 +
 tools/perf/util/auxtrace.c          |   4 +
 tools/perf/util/auxtrace.h          |   1 +
 tools/perf/util/cxl-hmu.c           | 367 ++++++++++++
 tools/perf/util/cxl-hmu.h           |  18 +
 21 files changed, 1748 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/trace/cxl-hmu.rst
 create mode 100644 drivers/cxl/core/hmu.c
 create mode 100644 drivers/cxl/hmu.c
 create mode 100644 drivers/cxl/hmu.h
 create mode 100644 tools/perf/util/cxl-hmu.c
 create mode 100644 tools/perf/util/cxl-hmu.h
#22 lore raw
In reply to Jonathan Cameron
Quoted 3 lines · On Thu, 21 Nov 2024 10:18:41 +0000 Jonathan Cameron…

The CXL specification release 3.2 is now available under a click through at
https://computeexpresslink.org/cxl-specification/ and it brings new shiny toys.

PoC of qemu plugin based approach to getting real data:
https://lore.kernel.org/qemu-devel/[email protected]/

Also available on gitlab.com/jic23/qemu cxl-2025-01-24

I have a minor revision to this driver to post after a spec clarification on units of one parameter. Will do that next week but for now that PoC ignores the parameter anyway :)

Hopefully a cleaned up version of the above will provide us with a useful platform for algorithm and framework development. I've posted it early as there is a fundamental question for the QEMU maintainers on how they would prefer the plugin interaction to be done - once that's resolved it's just a question of wiring up more parameters and providing additional options for the actual counting implementation. For now it's an oracle with 32 bit counters for every page. Hardware folk tell me that might be a 'little too expensive'!

It's not even that slow :)

Jonathan

Quoted 146 lines

RFC reason
- Whilst trace capture with a particular configuration is potentially useful the intent is that CXL HMU units will be used to drive various forms of hotpage migration for memory tiering setups. This driver doesn't do this (yet), but rather provides data capture etc for experimentation and for working out how to mostly put the allocations in the right place to start with by tuning applications.

CXL r3.2 introduces a CXL Hotness Monitoring Unit definition. The intent of this is to provide a way to establish which units of memory (typically pages or larger) in CXL attached memory are hot. The implementation details and algorithm are all implementation defined. The specification simply describes the 'interface' which takes the form of ring buffer of hotness records in a PCI BAR and defined capability, configuration and status registers.

The hardware may have constraints on what it can track, granularity etc and on how accurately it tracks (e.g. counter exhaustion, inaccurate trackers). Some of these constraints are discoverable from the hardware registers, others such as loss of accuracy have no universally accepted measures as they are typically access pattern dependent. Sadly it is very unlikely any hardware will implement a truly precise tracker given the large resource requirements for tracking at a useful granularity.

There are two fundamental operation modes:

* Epoch based. Counters are checked after a period of time (Epoch) and if over a threshold added to the hotlist.
* Always on. Counters run until a threshold is reached, after that the hot unit is added to the hotlist and the counter released.

Counting can be filtered on:

* Region of CXL DPA space (256MiB per bit in a bitmap).
* Type of access - Trusted and non trusted or non trusted only, R/W/RW

Sampling can be modified by:

* Downsampling including potentially randomized downsampling.

The driver presented here is intended to be useful in its own right but also to act as the first step of a possible path towards hotness monitoring based hot page migration. Those steps might look like.

1. Gather data - drivers provide telemetry like solutions to get that data. May be enhanced, for example in this driver by providing the HPA address rather than DPA Unit Address. Userspace can access enough information to do this so maybe not.
2. Userspace algorithm development, possibly combined with userspace triggered migration by PA. Working out how to use different levels of constrained hardware resources will be challenging.
3. Move those algorithms in kernel. Will require generalization across different hotpage trackers etc.

So far this driver just gives access to the raw data. I will probably kick of a longer discussion on how to do adaptive sampling needed to actually use these units for tiering etc, sometime soon (if no one one else beats me too it). There is a follow up topic of how to virtualize this stuff for memory stranding cases (VM gets a fixed mixture of fast and slow memory and should do it's own tiering).

More details in the Documentation patch but typical commands are:

$perf record -a -e cxl_hmu_mem0.0.0/epoch_type=0,access_type=6,\
hotness_threshold=1024,epoch_multiplier=4,epoch_scale=4,range_base=0,\
range_size=1024,randomized_downsampling=0,downsampling_factor=32,\
hotness_granual=12

$perf report --dump-raw-traces

Example output. With a counter_width of 16 (0x10) the least significant 4 bytes are the counter value and the unit index is bits 16-63. Here all units are over the threshold and the indexes are 0,1,2 etc.

. ... CXL_HMU data: size 33512 bytes
Header 0: units: 29c counter_width 10
Header 1 : deadbeef
0000000000000283
0000000000010364
0000000000020366
000000000003033c
0000000000040343
00000000000502ff
000000000006030d
000000000007031a

Which will produce a list of hotness entries.
Bits[N-1:0] counter value
Bits[63:N] Unit ID (combine with unit size and DPA base + HDM decoder
config to get to a Host Physical Address)

Specific RFC questions.
- What should be in the header added to the aux buffer. Currently just the minimum is provided. Number of records and the counter width needed to decode them.
- Should we reset the counters when doing sampling "-F X"
If the frequency is higher than the epoch we never see any hot units. If so, when should we reset them?

Note testing has been light and on emulation only + as perf tool is a pain to build on a striped back VM, build testing has all be on arm64 so far. The driver loads though on both arm64 and x86 so any problems are likely in the perf tool arch specific code which is build tested (on wrong machine)

The QEMU emulation needs some cleanup, but I should be able to post that shortly to let people actually play with this. There are lots of open questions there on how 'right' we want the emulation to be and what counting uarch to emulate.

Jonathan Cameron (4):
cxl: Register devices for CXL Hotness Monitoring Units (CHMU)
cxl: Hotness Monitoring Unit via a Perf AUX Buffer.
perf: Add support for CXL Hotness Monitoring Units (CHMU)
hwtrace: Document CXL Hotness Monitoring Unit driver

 Documentation/trace/cxl-hmu.rst     | 197 +++++++
 Documentation/trace/index.rst       |   1 +
 drivers/cxl/Kconfig                 |   6 +
 drivers/cxl/Makefile                |   3 +
 drivers/cxl/core/Makefile           |   1 +
 drivers/cxl/core/core.h             |   1 +
 drivers/cxl/core/hmu.c              |  64 ++
 drivers/cxl/core/port.c             |   2 +
 drivers/cxl/core/regs.c             |  14 +
 drivers/cxl/cxl.h                   |   5 +
 drivers/cxl/cxlpci.h                |   1 +
 drivers/cxl/hmu.c                   | 880 ++++++++++++++++++++++++++++
 drivers/cxl/hmu.h                   |  23 +
 drivers/cxl/pci.c                   |  26 +-
 tools/perf/arch/arm/util/auxtrace.c |  58 ++
 tools/perf/arch/x86/util/auxtrace.c |  76 +++
 tools/perf/util/Build               |   1 +
 tools/perf/util/auxtrace.c          |   4 +
 tools/perf/util/auxtrace.h          |   1 +
 tools/perf/util/cxl-hmu.c           | 367 ++++++++++++
 tools/perf/util/cxl-hmu.h           |  18 +
 21 files changed, 1748 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/trace/cxl-hmu.rst
 create mode 100644 drivers/cxl/core/hmu.c
 create mode 100644 drivers/cxl/hmu.c
 create mode 100644 drivers/cxl/hmu.h
 create mode 100644 tools/perf/util/cxl-hmu.c
 create mode 100644 tools/perf/util/cxl-hmu.h