Skip to contentSkip to conversation

Add DPA->HPA translation to dram & general_media

PATCH Superseded [email protected] 3 participants 12 messages Apr 23, 2024 – Apr 25, 2024 CXL

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

v2 cover Add DPA->HPA translation to dram & general_media

From: Alison Schofield <[email protected]>

Changes in v2:
- Fix !CONFIG_CXL_REGION build error in cxl/core.h stub ([email protected])
- Remove 'to dpa' usage when DPA is already adjusted in TP_fast_assign
- Use a common macro for extracting dpa from event record (Ira)
- Return "" instead of NULL for region name in stub (Jonathan)
- Replace store_region_info MACRO w static inline func (Dan)
- Set uuid to uuid_null when no region info available (Ira)
- Remove useless macro wrapping cxl_to_region_name() (Ira)
- Remove extraneous diff in Patch 4's TP_printk (Jonathan)
- Update commit msg patch 1 & 2, s/stubs/stub (Jonathan)
Link to v1: https://lore.kernel.org/[email protected]/

An update to the cxl_events unit test is in review here:
https://lore.kernel.org/[email protected]/

Begin Cover Letter:
Add HPA translations to CXL events: cxl_dram and cxl_general_media

Patches 1 & 2:
Before adding the new support, do some housekeeping and move related helpers to the region driver because there is no looking up region related info without CONFIG_CXL_REGION.

Patch 3:
The new functionality is introduced - cxl_dram & cxl_general_media events will lookup and log the DPA->HPA translation along with the region name and region uuid.

Patch 4:
Apply the new lookup helpers to the existing cxl_poison event, so it can be the same, share in the new goodness, and also tidy up its implementation.

An update to the cxl_event unit test is posted separately.

Alison Schofield (4):
cxl/region: Move cxl_dpa_to_region() work to the region driver cxl/region: Move cxl_trace_hpa() work to the region driver cxl/core: Add region info to cxl_general_media and cxl_dram events cxl/core: Remove cxlr dependency from cxl_poison trace events

 drivers/cxl/core/core.h   |  20 +++++
 drivers/cxl/core/mbox.c   |  22 ++++--
 drivers/cxl/core/memdev.c |  52 +------------
 drivers/cxl/core/region.c | 151 +++++++++++++++++++++++++++++++++++++-
 drivers/cxl/core/trace.c  |  91 -----------------------
 drivers/cxl/core/trace.h  |  81 ++++++++++++++------
 drivers/cxl/cxlmem.h      |   3 +-
 7 files changed, 245 insertions(+), 175 deletions(-)

base-commit: 4cece764965020c22cff7665b18a012006359095

Signature · 1 line
2.37.3
author maintainer
#1 lore raw

PATCH v2 1/4 cxl/region: Move cxl_dpa_to_region() work to the region driver

From: Alison Schofield <[email protected]>

This helper belongs in the region driver as it is only useful with CONFIG_CXL_REGION. Add a stub in core.h for when the region driver is not built.

---
 drivers/cxl/core/core.h   |  7 +++++++
 drivers/cxl/core/memdev.c | 44 ---------------------------------------
 drivers/cxl/core/region.c | 44 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+), 44 deletions(-)
drivers/cxl/core/core.h +7 -0
index bc5a95665aa0..87008505f8a9 100644
@@ -27,7 +27,14 @@ void cxl_decoder_kill_region(struct cxl_endpoint_decoder *cxled);
 int cxl_region_init(void);
 void cxl_region_exit(void);
 int cxl_get_poison_by_endpoint(struct cxl_port *port);
+struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa);
+
 #else
+static inline
+struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa)
+{
+	return NULL;
+}
 static inline int cxl_get_poison_by_endpoint(struct cxl_port *port)
 {
 	return 0;
drivers/cxl/core/memdev.c +0 -44

50 lines not on this page · raw message

drivers/cxl/core/region.c +44 -0

50 lines not on this page · raw message

Signature · 1 line
2.37.3
author maintainer
#2 lore raw

PATCH v2 2/4 cxl/region: Move cxl_trace_hpa() work to the region driver

In reply to [email protected]

From: Alison Schofield <[email protected]>

This work belongs in the region driver as it is only useful with CONFIG_CXL_REGION. Add a stub in core.h for when the region driver is not built.

---
 drivers/cxl/core/core.h   |  7 +++
 drivers/cxl/core/region.c | 91 +++++++++++++++++++++++++++++++++++++++
 drivers/cxl/core/trace.c  | 91 ---------------------------------------
 drivers/cxl/core/trace.h  |  2 -
 4 files changed, 98 insertions(+), 93 deletions(-)
drivers/cxl/core/core.h +7 -0
index 87008505f8a9..625394486459 100644
@@ -28,8 +28,15 @@ int cxl_region_init(void);
 void cxl_region_exit(void);
 int cxl_get_poison_by_endpoint(struct cxl_port *port);
 struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa);
+u64 cxl_trace_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
+		  u64 dpa);
 
 #else
+static inline u64
+cxl_trace_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd, u64 dpa)
+{
+	return ULLONG_MAX;
+}
 static inline
 struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa)
 {
drivers/cxl/core/region.c +91 -0

97 lines not on this page · raw message

drivers/cxl/core/trace.c +0 -91

94 lines not on this page · raw message

drivers/cxl/core/trace.h +0 -2
index e5f13260fc52..161bdb5734b0 100644
@@ -642,8 +642,6 @@ TRACE_EVENT(cxl_memory_module,
 #define cxl_poison_overflow(flags, time)				\
 	(flags & CXL_POISON_FLAG_OVERFLOW ? le64_to_cpu(time) : 0)
 
-u64 cxl_trace_hpa(struct cxl_region *cxlr, struct cxl_memdev *memdev, u64 dpa);
-
 TRACE_EVENT(cxl_poison,
 
 	TP_PROTO(struct cxl_memdev *cxlmd, struct cxl_region *cxlr,
Signature · 1 line
2.37.3
author maintainer
#3 lore raw

PATCH v2 3/4 cxl/core: Add region info to cxl_general_media and cxl_dram events

In reply to [email protected]

From: Alison Schofield <[email protected]>

User space may need to know which region, if any, maps the DPAs (device physical addresses) reported in a cxl_general_media or cxl_dram event. Since the mapping can change, the kernel provides this information at the time the event occurs. This informs user space that at event <timestamp> this <region> mapped this <DPA> to this <HPA>.

Add the same region info that is included in the cxl_poison trace
event: the DPA->HPA translation, region name, and region uuid. Introduce and use new helpers that lookup that region info using the struct cxl_memdev and a DPA.

The new fields are inserted in the trace event and no existing fields are modified. If the DPA is not mapped, user will see:
hpa=ULLONG_MAX, region="", and uuid=0

This work must be protected by dpa_rwsem & region_rwsem since it is looking up region mappings.

---
 drivers/cxl/core/core.h   |  6 +++++
 drivers/cxl/core/mbox.c   | 17 ++++++++++---
 drivers/cxl/core/region.c |  8 ++++++
 drivers/cxl/core/trace.h  | 52 +++++++++++++++++++++++++++++++++++----
 4 files changed, 74 insertions(+), 9 deletions(-)
drivers/cxl/core/core.h +6 -0
index 625394486459..2fd8d9797f36 100644
@@ -30,8 +30,14 @@ int cxl_get_poison_by_endpoint(struct cxl_port *port);
 struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa);
 u64 cxl_trace_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
 		  u64 dpa);
+const char *cxl_trace_to_region_name(const struct cxl_memdev *cxlmd, u64 dpa);
 
 #else
+static inline
+const char *cxl_trace_to_region_name(const struct cxl_memdev *cxlmd, u64 dpa)
+{
+	return "";
+}
 static inline u64
 cxl_trace_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd, u64 dpa)
 {
drivers/cxl/core/mbox.c +13 -4
index 9adda4795eb7..3c1c37d5fcb0 100644
@@ -842,14 +842,23 @@ void cxl_event_trace_record(const struct cxl_memdev *cxlmd,
 			    enum cxl_event_type event_type,
 			    const uuid_t *uuid, union cxl_event *evt)
 {
+	if (event_type == CXL_CPER_EVENT_MEM_MODULE) {
+		trace_cxl_memory_module(cxlmd, type, &evt->mem_module);
+		return;
+	}
+	if (event_type == CXL_CPER_EVENT_GENERIC) {
+		trace_cxl_generic_event(cxlmd, type, uuid, &evt->generic);
+		return;
+	}
+
+	/* Protect trace events that do DPA->HPA translations */
+	guard(rwsem_read)(&cxl_region_rwsem);
+	guard(rwsem_read)(&cxl_dpa_rwsem);
+
 	if (event_type == CXL_CPER_EVENT_GEN_MEDIA)
 		trace_cxl_general_media(cxlmd, type, &evt->gen_media);
 	else if (event_type == CXL_CPER_EVENT_DRAM)
 		trace_cxl_dram(cxlmd, type, &evt->dram);
-	else if (event_type == CXL_CPER_EVENT_MEM_MODULE)
-		trace_cxl_memory_module(cxlmd, type, &evt->mem_module);
-	else
-		trace_cxl_generic_event(cxlmd, type, uuid, &evt->generic);
 }
 EXPORT_SYMBOL_NS_GPL(cxl_event_trace_record, CXL);
 
drivers/cxl/core/region.c +8 -0
index 45eb9c560fd6..a5b1eaee1e58 100644
@@ -2723,6 +2723,14 @@ struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa)
 	return ctx.cxlr;
 }
 
+const char *cxl_trace_to_region_name(const struct cxl_memdev *cxlmd, u64 dpa)
+{
+	struct cxl_region *cxlr = cxl_dpa_to_region(cxlmd, dpa);
+
+	/* trace __string() assignment requires "", not NULL */
+	return cxlr ? dev_name(&cxlr->dev) : "";
+}
+
 static bool cxl_is_hpa_in_range(u64 hpa, struct cxl_region *cxlr, int pos)
 {
 	struct cxl_region_params *p = &cxlr->params;
drivers/cxl/core/trace.h +47 -5

110 lines not on this page · raw message

Signature · 1 line
2.37.3
author maintainer
#9 lore raw

PATCH v2 4/4 cxl/core: Remove cxlr dependency from cxl_poison trace events

In reply to [email protected]

From: Alison Schofield <[email protected]>

cxl_poison trace events pass a pointer to a struct cxl_region when poison is discovered in a mapped endpoint. This made for easy look up of region name, uuid, and was useful in starting the dpa->hpa translation.

Another set of trace helpers is now available that eliminates the need to pass on that cxlr. (It was passed along a lot!)

In addition to tidying up the cxl_poison calling path, shifting to the new helpers also means all CXL trace events will share the same code in that regard.

Switch from a uuid array to the field_struct type uuid_t to align on one uuid format in all CXL trace events. That is useful when sharing region uuid lookup helpers.

No externally visible naming changes are made to cxl_poison trace events.

---
 drivers/cxl/core/mbox.c   |  5 ++---
 drivers/cxl/core/memdev.c |  8 ++++----
 drivers/cxl/core/region.c |  8 ++++----
 drivers/cxl/core/trace.h  | 27 +++++++++++----------------
 drivers/cxl/cxlmem.h      |  3 +--
 5 files changed, 22 insertions(+), 29 deletions(-)
drivers/cxl/core/mbox.c +2 -3
index 3c1c37d5fcb0..60a51ea3ff25 100644
@@ -1299,8 +1299,7 @@ int cxl_set_timestamp(struct cxl_memdev_state *mds)
 }
 EXPORT_SYMBOL_NS_GPL(cxl_set_timestamp, CXL);
 
-int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
-		       struct cxl_region *cxlr)
+int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len)
 {
 	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds);
 	struct cxl_mbox_poison_out *po;
@@ -1332,7 +1331,7 @@ int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
 			break;
 
 		for (int i = 0; i < le16_to_cpu(po->count); i++)
-			trace_cxl_poison(cxlmd, cxlr, &po->record[i],
+			trace_cxl_poison(cxlmd, &po->record[i],
 					 po->flags, po->overflow_ts,
 					 CXL_POISON_TRACE_LIST);
 
drivers/cxl/core/memdev.c +4 -4
index 0277726afd04..1a0b596da7b6 100644
@@ -200,14 +200,14 @@ static int cxl_get_poison_by_memdev(struct cxl_memdev *cxlmd)
 	if (resource_size(&cxlds->pmem_res)) {
 		offset = cxlds->pmem_res.start;
 		length = resource_size(&cxlds->pmem_res);
-		rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
+		rc = cxl_mem_get_poison(cxlmd, offset, length);
 		if (rc)
 			return rc;
 	}
 	if (resource_size(&cxlds->ram_res)) {
 		offset = cxlds->ram_res.start;
 		length = resource_size(&cxlds->ram_res);
-		rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
+		rc = cxl_mem_get_poison(cxlmd, offset, length);
 		/*
 		 * Invalid Physical Address is not an error for
 		 * volatile addresses. Device support is optional.
@@ -321,7 +321,7 @@ int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa)
 		.address = cpu_to_le64(dpa),
 		.length = cpu_to_le32(1),
 	};
-	trace_cxl_poison(cxlmd, cxlr, &record, 0, 0, CXL_POISON_TRACE_INJECT);
+	trace_cxl_poison(cxlmd, &record, 0, 0, CXL_POISON_TRACE_INJECT);
 out:
 	up_read(&cxl_dpa_rwsem);
 	up_read(&cxl_region_rwsem);
@@ -385,7 +385,7 @@ int cxl_clear_poison(struct cxl_memdev *cxlmd, u64 dpa)
 		.address = cpu_to_le64(dpa),
 		.length = cpu_to_le32(1),
 	};
-	trace_cxl_poison(cxlmd, cxlr, &record, 0, 0, CXL_POISON_TRACE_CLEAR);
+	trace_cxl_poison(cxlmd, &record, 0, 0, CXL_POISON_TRACE_CLEAR);
 out:
 	up_read(&cxl_dpa_rwsem);
 	up_read(&cxl_region_rwsem);
drivers/cxl/core/region.c +4 -4
index a5b1eaee1e58..8cd057fc212c 100644
@@ -2585,7 +2585,7 @@ static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
 	if (ctx->mode == CXL_DECODER_RAM) {
 		offset = ctx->offset;
 		length = resource_size(&cxlds->ram_res) - offset;
-		rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
+		rc = cxl_mem_get_poison(cxlmd, offset, length);
 		if (rc == -EFAULT)
 			rc = 0;
 		if (rc)
@@ -2603,7 +2603,7 @@ static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
 		return 0;
 	}
 
-	return cxl_mem_get_poison(cxlmd, offset, length, NULL);
+	return cxl_mem_get_poison(cxlmd, offset, length);
 }
 
 static int poison_by_decoder(struct device *dev, void *arg)
@@ -2637,7 +2637,7 @@ static int poison_by_decoder(struct device *dev, void *arg)
 	if (cxled->skip) {
 		offset = cxled->dpa_res->start - cxled->skip;
 		length = cxled->skip;
-		rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
+		rc = cxl_mem_get_poison(cxlmd, offset, length);
 		if (rc == -EFAULT && cxled->mode == CXL_DECODER_RAM)
 			rc = 0;
 		if (rc)
@@ -2646,7 +2646,7 @@ static int poison_by_decoder(struct device *dev, void *arg)
 
 	offset = cxled->dpa_res->start;
 	length = cxled->dpa_res->end - offset + 1;
-	rc = cxl_mem_get_poison(cxlmd, offset, length, cxled->cxld.region);
+	rc = cxl_mem_get_poison(cxlmd, offset, length);
 	if (rc == -EFAULT && cxled->mode == CXL_DECODER_RAM)
 		rc = 0;
 	if (rc)
drivers/cxl/core/trace.h +11 -16
index 2e24364b2b8d..1cfdd1558e6c 100644
@@ -686,23 +686,24 @@ TRACE_EVENT(cxl_memory_module,
 
 TRACE_EVENT(cxl_poison,
 
-	TP_PROTO(struct cxl_memdev *cxlmd, struct cxl_region *cxlr,
+	TP_PROTO(const struct cxl_memdev *cxlmd,
 		 const struct cxl_poison_record *record, u8 flags,
 		 __le64 overflow_ts, enum cxl_poison_trace_type trace_type),
 
-	TP_ARGS(cxlmd, cxlr, record, flags, overflow_ts, trace_type),
+	TP_ARGS(cxlmd, record, flags, overflow_ts, trace_type),
 
 	TP_STRUCT__entry(
 		__string(memdev, dev_name(&cxlmd->dev))
 		__string(host, dev_name(cxlmd->dev.parent))
 		__field(u64, serial)
 		__field(u8, trace_type)
-		__string(region, cxlr ? dev_name(&cxlr->dev) : "")
+		__string(region,
+			 cxl_trace_to_region_name(cxlmd, cxl_poison_record_dpa(record)))
 		__field(u64, overflow_ts)
 		__field(u64, hpa)
 		__field(u64, dpa)
 		__field(u32, dpa_length)
-		__array(char, uuid, 16)
+		__field_struct(uuid_t, uuid)
 		__field(u8, source)
 		__field(u8, flags)
 	    ),
@@ -717,27 +718,21 @@ TRACE_EVENT(cxl_poison,
 		__entry->source = cxl_poison_record_source(record);
 		__entry->trace_type = trace_type;
 		__entry->flags = flags;
-		if (cxlr) {
-			__assign_str(region, dev_name(&cxlr->dev));
-			memcpy(__entry->uuid, &cxlr->params.uuid, 16);
-			__entry->hpa = cxl_trace_hpa(cxlr, cxlmd,
-						     __entry->dpa);
-		} else {
-			__assign_str(region, "");
-			memset(__entry->uuid, 0, 16);
-			__entry->hpa = ULLONG_MAX;
-		}
+		__assign_str(region,
+			     cxl_trace_to_region_name(cxlmd, __entry->dpa));
+		store_region_info(cxlmd, __entry->dpa, &__entry->uuid,
+				  &__entry->hpa);
 	    ),
 
 	TP_printk("memdev=%s host=%s serial=%lld trace_type=%s region=%s "  \
-		"region_uuid=%pU hpa=0x%llx dpa=0x%llx dpa_length=0x%x "    \
+		"region_uuid=%pUb hpa=0x%llx dpa=0x%llx dpa_length=0x%x "   \
 		"source=%s flags=%s overflow_time=%llu",
 		__get_str(memdev),
 		__get_str(host),
 		__entry->serial,
 		show_poison_trace_type(__entry->trace_type),
 		__get_str(region),
-		__entry->uuid,
+		&__entry->uuid,
 		__entry->hpa,
 		__entry->dpa,
 		__entry->dpa_length,
drivers/cxl/cxlmem.h +1 -2
index 20fb3b35e89e..a733b31b7799 100644
@@ -828,8 +828,7 @@ void cxl_event_trace_record(const struct cxl_memdev *cxlmd,
 			    const uuid_t *uuid, union cxl_event *evt);
 int cxl_set_timestamp(struct cxl_memdev_state *mds);
 int cxl_poison_state_init(struct cxl_memdev_state *mds);
-int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,
-		       struct cxl_region *cxlr);
+int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len);
 int cxl_trigger_poison_list(struct cxl_memdev *cxlmd);
 int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa);
 int cxl_clear_poison(struct cxl_memdev *cxlmd, u64 dpa);
Signature · 1 line
2.37.3
Reviewed-by on v2 3/4
#4 lore raw

Re: [PATCH v2 3/4] cxl/core: Add region info to cxl_general_media and cxl_dram events

In reply to [email protected]
Quoted 2 lines · alison.schofield@ wrote:

From: Alison Schofield <[email protected]>

[snip]

Quoted 7 lines
drivers/cxl/core/trace.h +0 -0
index 161bdb5734b0..2e24364b2b8d 100644
@@ -14,6 +14,28 @@
 #include <cxlmem.h>
 #include "core.h"

drivers/cxl/core/trace.h line 25

Quoted 10 lines
drivers/cxl/core/trace.h +9 -0
 
+#ifndef __CXL_EVENTS_DECLARE_ONCE_ONLY
+#define __CXL_EVENTS_DECLARE_ONCE_ONLY
+static inline
+void store_region_info(const struct cxl_memdev *cxlmd, u64 dpa, uuid_t *uuid,
+		       u64 *hpa)
+{
+	struct cxl_region *cxlr;
+
+	cxlr = cxl_dpa_to_region(cxlmd, dpa);

This would normally be: (somewhat nitty though...)

	if (!cxlr) {
		uuid_copy(uuid, &uuid_null);
		*hpa = ULLONG_MAX;
		return;
	}

	uuid_copy(uuid, &cxlr->params.uuid);
	*hpa = cxl_trace_hpa(cxlr, cxlmd, dpa);
}

But in this context I don't think it is critical.

[snip]

author maintainer
#5 lore raw

Re: [PATCH v2 3/4] cxl/core: Add region info to cxl_general_media and cxl_dram events

Quoted 36 lines · Ira Weiny
Quoted 2 lines · alison.schofield@ wrote:

From: Alison Schofield <[email protected]>

[snip]

Quoted 7 lines
drivers/cxl/core/trace.h +0 -0
index 161bdb5734b0..2e24364b2b8d 100644
@@ -14,6 +14,28 @@
 #include <cxlmem.h>
 #include "core.h"

drivers/cxl/core/trace.h line 25

Quoted 10 lines
drivers/cxl/core/trace.h +9 -0
 
+#ifndef __CXL_EVENTS_DECLARE_ONCE_ONLY
+#define __CXL_EVENTS_DECLARE_ONCE_ONLY
+static inline
+void store_region_info(const struct cxl_memdev *cxlmd, u64 dpa, uuid_t *uuid,
+		       u64 *hpa)
+{
+	struct cxl_region *cxlr;
+
+	cxlr = cxl_dpa_to_region(cxlmd, dpa);

This would normally be: (somewhat nitty though...)

	if (!cxlr) {
		uuid_copy(uuid, &uuid_null);
		*hpa = ULLONG_MAX;
		return;
	}

	uuid_copy(uuid, &cxlr->params.uuid);
	*hpa = cxl_trace_hpa(cxlr, cxlmd, dpa);
}
Quoted 1 line

But in this context I don't think it is critical.

Thanks for pointing out. When I switch to inline I didn't 'see' that. I'd like to fix it up. Let me see what else comes in.

--Alison

Quoted 5 lines

[snip]

#6 lore raw

Re: [PATCH v2 3/4] cxl/core: Add region info to cxl_general_media and cxl_dram events

In reply to [email protected]
Quoted 116 lines · alison.schofield@ wrote:

From: Alison Schofield <[email protected]>

User space may need to know which region, if any, maps the DPAs (device physical addresses) reported in a cxl_general_media or cxl_dram event. Since the mapping can change, the kernel provides this information at the time the event occurs. This informs user space that at event <timestamp> this <region> mapped this <DPA> to this <HPA>.

Add the same region info that is included in the cxl_poison trace
event: the DPA->HPA translation, region name, and region uuid. Introduce and use new helpers that lookup that region info using the struct cxl_memdev and a DPA.

The new fields are inserted in the trace event and no existing fields are modified. If the DPA is not mapped, user will see:
hpa=ULLONG_MAX, region="", and uuid=0

This work must be protected by dpa_rwsem & region_rwsem since it is looking up region mappings.

---
 drivers/cxl/core/core.h   |  6 +++++
 drivers/cxl/core/mbox.c   | 17 ++++++++++---
 drivers/cxl/core/region.c |  8 ++++++
 drivers/cxl/core/trace.h  | 52 +++++++++++++++++++++++++++++++++++----
 4 files changed, 74 insertions(+), 9 deletions(-)
drivers/cxl/core/core.h +6 -0
index 625394486459..2fd8d9797f36 100644
@@ -30,8 +30,14 @@ int cxl_get_poison_by_endpoint(struct cxl_port *port);
 struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa);
 u64 cxl_trace_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
 		  u64 dpa);
+const char *cxl_trace_to_region_name(const struct cxl_memdev *cxlmd, u64 dpa);
 
 #else
+static inline
+const char *cxl_trace_to_region_name(const struct cxl_memdev *cxlmd, u64 dpa)
+{
+	return "";
+}
 static inline u64
 cxl_trace_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd, u64 dpa)
 {
drivers/cxl/core/mbox.c +13 -4
index 9adda4795eb7..3c1c37d5fcb0 100644
@@ -842,14 +842,23 @@ void cxl_event_trace_record(const struct cxl_memdev *cxlmd,
 			    enum cxl_event_type event_type,
 			    const uuid_t *uuid, union cxl_event *evt)
 {
+	if (event_type == CXL_CPER_EVENT_MEM_MODULE) {
+		trace_cxl_memory_module(cxlmd, type, &evt->mem_module);
+		return;
+	}
+	if (event_type == CXL_CPER_EVENT_GENERIC) {
+		trace_cxl_generic_event(cxlmd, type, uuid, &evt->generic);
+		return;
+	}
+
+	/* Protect trace events that do DPA->HPA translations */
+	guard(rwsem_read)(&cxl_region_rwsem);
+	guard(rwsem_read)(&cxl_dpa_rwsem);
+
 	if (event_type == CXL_CPER_EVENT_GEN_MEDIA)
 		trace_cxl_general_media(cxlmd, type, &evt->gen_media);
 	else if (event_type == CXL_CPER_EVENT_DRAM)
 		trace_cxl_dram(cxlmd, type, &evt->dram);
-	else if (event_type == CXL_CPER_EVENT_MEM_MODULE)
-		trace_cxl_memory_module(cxlmd, type, &evt->mem_module);
-	else
-		trace_cxl_generic_event(cxlmd, type, uuid, &evt->generic);
 }
 EXPORT_SYMBOL_NS_GPL(cxl_event_trace_record, CXL);
 
drivers/cxl/core/region.c +8 -0
index 45eb9c560fd6..a5b1eaee1e58 100644
@@ -2723,6 +2723,14 @@ struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa)
 	return ctx.cxlr;
 }
 
+const char *cxl_trace_to_region_name(const struct cxl_memdev *cxlmd, u64 dpa)
+{
+	struct cxl_region *cxlr = cxl_dpa_to_region(cxlmd, dpa);
+
+	/* trace __string() assignment requires "", not NULL */
+	return cxlr ? dev_name(&cxlr->dev) : "";
+}
+
 static bool cxl_is_hpa_in_range(u64 hpa, struct cxl_region *cxlr, int pos)
 {
 	struct cxl_region_params *p = &cxlr->params;
drivers/cxl/core/trace.h +8 -0
index 161bdb5734b0..2e24364b2b8d 100644
@@ -14,6 +14,28 @@
 #include <cxlmem.h>
 #include "core.h"
 
+#ifndef __CXL_EVENTS_DECLARE_ONCE_ONLY
+#define __CXL_EVENTS_DECLARE_ONCE_ONLY
+static inline
+void store_region_info(const struct cxl_memdev *cxlmd, u64 dpa, uuid_t *uuid,
+		       u64 *hpa)
+{
+	struct cxl_region *cxlr;
+

drivers/cxl/core/trace.h line 34

Quoted 10 lines
drivers/cxl/core/trace.h +10 -0
+	cxlr = cxl_dpa_to_region(cxlmd, dpa);
+	if (cxlr) {
+		uuid_copy(uuid, &cxlr->params.uuid);
+		*hpa = cxl_trace_hpa(cxlr, cxlmd, dpa);
+	} else {
+		uuid_copy(uuid, &uuid_null);
+		*hpa = ULLONG_MAX;
+	}
+}
+#endif /* __CXL_EVENTS_DECLARE_ONCE_ONLY */

This ifdef usage looks awkward...

Quoted 10 lines
drivers/cxl/core/trace.h +4 -0
+
+#define rec_pa_to_dpa(record)						\
+	(le64_to_cpu(rec->phys_addr) & CXL_DPA_MASK)
+
 #define CXL_RAS_UC_CACHE_DATA_PARITY	BIT(0)
 #define CXL_RAS_UC_CACHE_ADDR_PARITY	BIT(1)
 #define CXL_RAS_UC_CACHE_BE_PARITY	BIT(2)
@@ -330,10 +352,14 @@ TRACE_EVENT(cxl_general_media,
 		__field(u8, channel)
 		__field(u32, device)

drivers/cxl/core/trace.h TRACE_EVENT(cxl_general_media, · line 362

Quoted 10 lines
drivers/cxl/core/trace.h +5 -1
 		__array(u8, comp_id, CXL_EVENT_GEN_MED_COMP_ID_SIZE)
-		__field(u16, validity_flags)
 		/* Following are out of order to pack trace record */
+		__field(u64, hpa)
+		__field_struct(uuid_t, region_uuid)
+		__field(u16, validity_flags)
 		__field(u8, rank)
 		__field(u8, dpa_flags)
+		__string(region_name,
+			 cxl_trace_to_region_name(cxlmd, rec_pa_to_dpa(record)))

...and this looks complicated.

A bit too much dynamic resolution happening within the trace function for my taste. Just do the region lookup in cxl_event_trace_record() and pass it in. That also makes the rwsem usage more apparent rather than digging through trace to find the dependency.

#10 lore raw

Re: [PATCH v2 4/4] cxl/core: Remove cxlr dependency from cxl_poison trace events

In reply to [email protected]
Quoted 24 lines · alison.schofield@ wrote:

From: Alison Schofield <[email protected]>

cxl_poison trace events pass a pointer to a struct cxl_region when poison is discovered in a mapped endpoint. This made for easy look up of region name, uuid, and was useful in starting the dpa->hpa translation.

Another set of trace helpers is now available that eliminates the need to pass on that cxlr. (It was passed along a lot!)

In addition to tidying up the cxl_poison calling path, shifting to the new helpers also means all CXL trace events will share the same code in that regard.

Switch from a uuid array to the field_struct type uuid_t to align on one uuid format in all CXL trace events. That is useful when sharing region uuid lookup helpers.

No externally visible naming changes are made to cxl_poison trace events.

Quoted 7 lines
---
 drivers/cxl/core/mbox.c   |  5 ++---
 drivers/cxl/core/memdev.c |  8 ++++----
 drivers/cxl/core/region.c |  8 ++++----
 drivers/cxl/core/trace.h  | 27 +++++++++++----------------
 drivers/cxl/cxlmem.h      |  3 +--
 5 files changed, 22 insertions(+), 29 deletions(-)

A lot of the thrash here is about removing cxlr as an argument, and per the comment on patch3 I think it is cleaner to go the other way and always pass it in.

author maintainer
#7 lore raw

Re: [PATCH v2 3/4] cxl/core: Add region info to cxl_general_media and cxl_dram events

In reply to Dan Williams
Quoted 121 lines · Dan Williams
Quoted 120 lines · alison.schofield@ wrote:

From: Alison Schofield <[email protected]>

User space may need to know which region, if any, maps the DPAs (device physical addresses) reported in a cxl_general_media or cxl_dram event. Since the mapping can change, the kernel provides this information at the time the event occurs. This informs user space that at event <timestamp> this <region> mapped this <DPA> to this <HPA>.

Add the same region info that is included in the cxl_poison trace
event: the DPA->HPA translation, region name, and region uuid. Introduce and use new helpers that lookup that region info using the struct cxl_memdev and a DPA.

The new fields are inserted in the trace event and no existing fields are modified. If the DPA is not mapped, user will see:
hpa=ULLONG_MAX, region="", and uuid=0

This work must be protected by dpa_rwsem & region_rwsem since it is looking up region mappings.

---
 drivers/cxl/core/core.h   |  6 +++++
 drivers/cxl/core/mbox.c   | 17 ++++++++++---
 drivers/cxl/core/region.c |  8 ++++++
 drivers/cxl/core/trace.h  | 52 +++++++++++++++++++++++++++++++++++----
 4 files changed, 74 insertions(+), 9 deletions(-)
drivers/cxl/core/core.h +6 -0
index 625394486459..2fd8d9797f36 100644
@@ -30,8 +30,14 @@ int cxl_get_poison_by_endpoint(struct cxl_port *port);
 struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa);
 u64 cxl_trace_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
 		  u64 dpa);
+const char *cxl_trace_to_region_name(const struct cxl_memdev *cxlmd, u64 dpa);
 
 #else
+static inline
+const char *cxl_trace_to_region_name(const struct cxl_memdev *cxlmd, u64 dpa)
+{
+	return "";
+}
 static inline u64
 cxl_trace_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd, u64 dpa)
 {
drivers/cxl/core/mbox.c +13 -4
index 9adda4795eb7..3c1c37d5fcb0 100644
@@ -842,14 +842,23 @@ void cxl_event_trace_record(const struct cxl_memdev *cxlmd,
 			    enum cxl_event_type event_type,
 			    const uuid_t *uuid, union cxl_event *evt)
 {
+	if (event_type == CXL_CPER_EVENT_MEM_MODULE) {
+		trace_cxl_memory_module(cxlmd, type, &evt->mem_module);
+		return;
+	}
+	if (event_type == CXL_CPER_EVENT_GENERIC) {
+		trace_cxl_generic_event(cxlmd, type, uuid, &evt->generic);
+		return;
+	}
+
+	/* Protect trace events that do DPA->HPA translations */
+	guard(rwsem_read)(&cxl_region_rwsem);
+	guard(rwsem_read)(&cxl_dpa_rwsem);
+
 	if (event_type == CXL_CPER_EVENT_GEN_MEDIA)
 		trace_cxl_general_media(cxlmd, type, &evt->gen_media);
 	else if (event_type == CXL_CPER_EVENT_DRAM)
 		trace_cxl_dram(cxlmd, type, &evt->dram);
-	else if (event_type == CXL_CPER_EVENT_MEM_MODULE)
-		trace_cxl_memory_module(cxlmd, type, &evt->mem_module);
-	else
-		trace_cxl_generic_event(cxlmd, type, uuid, &evt->generic);
 }
 EXPORT_SYMBOL_NS_GPL(cxl_event_trace_record, CXL);
 
drivers/cxl/core/region.c +8 -0
index 45eb9c560fd6..a5b1eaee1e58 100644
@@ -2723,6 +2723,14 @@ struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa)
 	return ctx.cxlr;
 }
 
+const char *cxl_trace_to_region_name(const struct cxl_memdev *cxlmd, u64 dpa)
+{
+	struct cxl_region *cxlr = cxl_dpa_to_region(cxlmd, dpa);
+
+	/* trace __string() assignment requires "", not NULL */
+	return cxlr ? dev_name(&cxlr->dev) : "";
+}
+
 static bool cxl_is_hpa_in_range(u64 hpa, struct cxl_region *cxlr, int pos)
 {
 	struct cxl_region_params *p = &cxlr->params;
drivers/cxl/core/trace.h +12 -0
index 161bdb5734b0..2e24364b2b8d 100644
@@ -14,6 +14,28 @@
 #include <cxlmem.h>
 #include "core.h"
 
+#ifndef __CXL_EVENTS_DECLARE_ONCE_ONLY
+#define __CXL_EVENTS_DECLARE_ONCE_ONLY
+static inline
+void store_region_info(const struct cxl_memdev *cxlmd, u64 dpa, uuid_t *uuid,
+		       u64 *hpa)
+{
+	struct cxl_region *cxlr;
+
+	cxlr = cxl_dpa_to_region(cxlmd, dpa);
+	if (cxlr) {
+		uuid_copy(uuid, &cxlr->params.uuid);
+		*hpa = cxl_trace_hpa(cxlr, cxlmd, dpa);
Quoted 8 lines

drivers/cxl/core/trace.h line 34

Quoted 6 lines
drivers/cxl/core/trace.h +6 -0
+	} else {
+		uuid_copy(uuid, &uuid_null);
+		*hpa = ULLONG_MAX;
+	}
+}
+#endif /* __CXL_EVENTS_DECLARE_ONCE_ONLY */

This ifdef usage looks awkward...

I only mimic'd others that defined static inline funcs in trace header files. I originally thought it would be protected from this define that already wraps this header file content, but it doesn't.

#if !defined(_CXL_EVENTS_H) || defined(TRACE_HEADER_MULTI_READ) #define _CXL_EVENTS_H

Quoted 24 lines
Quoted 11 lines
drivers/cxl/core/trace.h +4 -0
+
+#define rec_pa_to_dpa(record)						\
+	(le64_to_cpu(rec->phys_addr) & CXL_DPA_MASK)
+
 #define CXL_RAS_UC_CACHE_DATA_PARITY	BIT(0)
 #define CXL_RAS_UC_CACHE_ADDR_PARITY	BIT(1)
 #define CXL_RAS_UC_CACHE_BE_PARITY	BIT(2)
@@ -330,10 +352,14 @@ TRACE_EVENT(cxl_general_media,
 		__field(u8, channel)
 		__field(u32, device)

drivers/cxl/core/trace.h TRACE_EVENT(cxl_general_media, · line 362

Quoted 10 lines
drivers/cxl/core/trace.h +5 -1
 		__array(u8, comp_id, CXL_EVENT_GEN_MED_COMP_ID_SIZE)
-		__field(u16, validity_flags)
 		/* Following are out of order to pack trace record */
+		__field(u64, hpa)
+		__field_struct(uuid_t, region_uuid)
+		__field(u16, validity_flags)
 		__field(u8, rank)
 		__field(u8, dpa_flags)
+		__string(region_name,
+			 cxl_trace_to_region_name(cxlmd, rec_pa_to_dpa(record)))

...and this looks complicated.

Quoted 4 lines

A bit too much dynamic resolution happening within the trace function for my taste. Just do the region lookup in cxl_event_trace_record() and pass it in. That also makes the rwsem usage more apparent rather than digging through trace to find the dependency.

When these cxl_general_media,dram,poison trace events were originally created once of the goals was to push work down *here* so that if the trace events were not enable, no needless work is done.

When you suggest doing the region lookup before calling the trace handler, I'm thinking something like this where the region lookup work still gets skipped if tracing is not enabled:

	if (trace_cxl_general_media_enabled()) {
		cxlr = lookup_region(cxlmd, record);
		trace_cxl_general_media(...., cxlr);

-- Alison

author maintainer
#11 lore raw

Re: [PATCH v2 4/4] cxl/core: Remove cxlr dependency from cxl_poison trace events

In reply to Dan Williams
Quoted 33 lines · Dan Williams
Quoted 24 lines · alison.schofield@ wrote:

From: Alison Schofield <[email protected]>

cxl_poison trace events pass a pointer to a struct cxl_region when poison is discovered in a mapped endpoint. This made for easy look up of region name, uuid, and was useful in starting the dpa->hpa translation.

Another set of trace helpers is now available that eliminates the need to pass on that cxlr. (It was passed along a lot!)

In addition to tidying up the cxl_poison calling path, shifting to the new helpers also means all CXL trace events will share the same code in that regard.

Switch from a uuid array to the field_struct type uuid_t to align on one uuid format in all CXL trace events. That is useful when sharing region uuid lookup helpers.

No externally visible naming changes are made to cxl_poison trace events.

Quoted 7 lines
---
 drivers/cxl/core/mbox.c   |  5 ++---
 drivers/cxl/core/memdev.c |  8 ++++----
 drivers/cxl/core/region.c |  8 ++++----
 drivers/cxl/core/trace.h  | 27 +++++++++++----------------
 drivers/cxl/cxlmem.h      |  3 +--
 5 files changed, 22 insertions(+), 29 deletions(-)
Quoted 3 lines

A lot of the thrash here is about removing cxlr as an argument, and per the comment on patch3 I think it is cleaner to go the other way and always pass it in.

I still think we can mimic what is done for general_media and dram trace events. ie. lookup the region only if trace_cxl_poison_enabled(). as you suggested in previous patch.

I can see why you call it thrash. I'll counter that with the fact that it is a fairly easy to read diff, and the fact that removing the passed along cxlr param, is pretty much 'tested' when it compiles.

-- Alison

#8 lore raw

Re: [PATCH v2 3/4] cxl/core: Add region info to cxl_general_media and cxl_dram events

In reply to Alison Schofield
Quoted 25 lines · Alison Schofield
Quoted 24 lines · Dan Williams
Quoted 23 lines · alison.schofield@ wrote:

From: Alison Schofield <[email protected]>

User space may need to know which region, if any, maps the DPAs (device physical addresses) reported in a cxl_general_media or cxl_dram event. Since the mapping can change, the kernel provides this information at the time the event occurs. This informs user space that at event <timestamp> this <region> mapped this <DPA> to this <HPA>.

Add the same region info that is included in the cxl_poison trace
event: the DPA->HPA translation, region name, and region uuid. Introduce and use new helpers that lookup that region info using the struct cxl_memdev and a DPA.

The new fields are inserted in the trace event and no existing fields are modified. If the DPA is not mapped, user will see:
hpa=ULLONG_MAX, region="", and uuid=0

This work must be protected by dpa_rwsem & region_rwsem since it is looking up region mappings.

[..]

Quoted 29 lines
Quoted 20 lines
Quoted 20 lines
drivers/cxl/core/trace.h +12 -0
index 161bdb5734b0..2e24364b2b8d 100644
@@ -14,6 +14,28 @@
 #include <cxlmem.h>
 #include "core.h"
 
+#ifndef __CXL_EVENTS_DECLARE_ONCE_ONLY
+#define __CXL_EVENTS_DECLARE_ONCE_ONLY
+static inline
+void store_region_info(const struct cxl_memdev *cxlmd, u64 dpa, uuid_t *uuid,
+		       u64 *hpa)
+{
+	struct cxl_region *cxlr;
+
+	cxlr = cxl_dpa_to_region(cxlmd, dpa);
+	if (cxlr) {
+		uuid_copy(uuid, &cxlr->params.uuid);
+		*hpa = cxl_trace_hpa(cxlr, cxlmd, dpa);
Quoted 8 lines

drivers/cxl/core/trace.h line 34

Quoted 6 lines
drivers/cxl/core/trace.h +6 -0
+	} else {
+		uuid_copy(uuid, &uuid_null);
+		*hpa = ULLONG_MAX;
+	}
+}
+#endif /* __CXL_EVENTS_DECLARE_ONCE_ONLY */

This ifdef usage looks awkward...

Quoted 1 line

I only mimic'd others that defined static inline funcs in trace header files.

That's fine, but that awkward trace hack can be skipped altogether if an @region argument is passed to the tracepoints.

Quoted 5 lines

I originally thought it would be protected from this define that already wraps this header file content, but it doesn't.

#if !defined(_CXL_EVENTS_H) || defined(TRACE_HEADER_MULTI_READ) #define _CXL_EVENTS_H

That's what TRACE_HEADER_MULTI_READ allows which is needed for the tricky way that tracepoints are built.

Quoted 23 lines
Quoted 23 lines
Quoted 10 lines
drivers/cxl/core/trace.h +4 -0
+
+#define rec_pa_to_dpa(record)						\
+	(le64_to_cpu(rec->phys_addr) & CXL_DPA_MASK)
+
 #define CXL_RAS_UC_CACHE_DATA_PARITY	BIT(0)
 #define CXL_RAS_UC_CACHE_ADDR_PARITY	BIT(1)
 #define CXL_RAS_UC_CACHE_BE_PARITY	BIT(2)
@@ -330,10 +352,14 @@ TRACE_EVENT(cxl_general_media,
 		__field(u8, channel)
 		__field(u32, device)

drivers/cxl/core/trace.h TRACE_EVENT(cxl_general_media, · line 362

Quoted 10 lines
drivers/cxl/core/trace.h +5 -1
 		__array(u8, comp_id, CXL_EVENT_GEN_MED_COMP_ID_SIZE)
-		__field(u16, validity_flags)
 		/* Following are out of order to pack trace record */
+		__field(u64, hpa)
+		__field_struct(uuid_t, region_uuid)
+		__field(u16, validity_flags)
 		__field(u8, rank)
 		__field(u8, dpa_flags)
+		__string(region_name,
+			 cxl_trace_to_region_name(cxlmd, rec_pa_to_dpa(record)))

...and this looks complicated.

Quoted 8 lines
Quoted 4 lines

A bit too much dynamic resolution happening within the trace function for my taste. Just do the region lookup in cxl_event_trace_record() and pass it in. That also makes the rwsem usage more apparent rather than digging through trace to find the dependency.

When these cxl_general_media,dram,poison trace events were originally created once of the goals was to push work down *here* so that if the trace events were not enable, no needless work is done.

The moment a code path needs to hold a lock, (2 locks in this case!), over a tracepoint it has already incurred significant overhead. So if the name of the game is "skip doing unnecessary work when the tracepoint is disabled", then just check if the tracepoint is disabled, something like:

drivers/cxl/core/mbox.c +17 -8
index 60a51ea3ff25..31f44101582e 100644
@@ -851,14 +851,23 @@ void cxl_event_trace_record(const struct cxl_memdev *cxlmd,
                return;
        }
 
-       /* Protect trace events that do DPA->HPA translations */
-       guard(rwsem_read)(&cxl_region_rwsem);
-       guard(rwsem_read)(&cxl_dpa_rwsem);
-
-       if (event_type == CXL_CPER_EVENT_GEN_MEDIA)
-               trace_cxl_general_media(cxlmd, type, &evt->gen_media);
-       else if (event_type == CXL_CPER_EVENT_DRAM)
-               trace_cxl_dram(cxlmd, type, &evt->dram);
+       if (trace_cxl_general_media_enabled() || trace_cxl_dram_enabled()) {
+               /*
+                * These trace points are annotated with HPA and corresponding
+                * region translation. Take topology mutation locks and lookup
+                * { HPA, REGION } from { DPA, MEMDEV } in the event record.
+                */
+               guard(rwsem_read)(&cxl_region_rwsem);
+               guard(rwsem_read)(&cxl_dpa_rwsem);
+
+               region = ...
+               hpa = ...
+
+               if (event_type == CXL_CPER_EVENT_GEN_MEDIA)
+                       trace_cxl_general_media(cxlmd, type, region, hpa, &evt->gen_media);
+               else if (event_type == CXL_CPER_EVENT_DRAM)
+                       trace_cxl_dram(cxlmd, type, region, hpa, &evt->dram);
+       }
 }
 EXPORT_SYMBOL_NS_GPL(cxl_event_trace_record, CXL);
 
Quoted 8 lines

When you suggest doing the region lookup before calling the trace handler, I'm thinking something like this where the region lookup work still gets skipped if tracing is not enabled:

	if (trace_cxl_general_media_enabled()) {
		cxlr = lookup_region(cxlmd, record);
		trace_cxl_general_media(...., cxlr);

I am chuckling because I wrote all of the above diatribe after finishing that "no needless work is done sentence". After building the example, compile testing a version of it, and pasting it into the mail *then* I read this next paragraph.

So yes, we came to the same conclusion. Please use trace_*_enabled() which makes it clear what the locks are protecting. Some lockdep_assert_held() usage in the lookup helpers would not hurt either.