A CXL memory node comes online without an explicit package association, and
plain NUMA distance does not convey which physical package it belongs to.
Without that association a CXL node cannot be grouped with the CPUs that
front it.

Register a package notifier per CXL region. When the region's memory node
comes online, the notifier resolves an initiator CPU node - the NUMA node
of the first memdev backing the region - and binds the memory node to that
initiator's package. This gives the topology layer the CPU-side association
that plain NUMA distance does not carry.

The initiator nid comes from the firmware and driver description of the
region's endpoint, so the association is only as accurate as that
description; it gives a more direct host-side grouping than flat distance
values.

Signed-off-by: Rakie Kim <[email protected]>
---
 drivers/cxl/core/region.c | 54 +++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h         |  1 +
 drivers/dax/kmem.c        |  3 +++
 3 files changed, 58 insertions(+)

diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index e50dc716d4e8..af66e2e06c62 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -2673,6 +2673,55 @@ static int cxl_region_calculate_adistance(struct 
notifier_block *nb,
        return NOTIFY_STOP;
 }
 
+/*
+ * Find a NUMA node to act as the initiator for this region: scan the
+ * region's endpoint targets and return the first one that resolves to a
+ * valid NUMA node.
+ */
+static int cxl_region_find_nearest_node(struct cxl_region *cxlr)
+{
+       struct cxl_region_params *p = &cxlr->params;
+       struct cxl_endpoint_decoder *cxled = NULL;
+       struct cxl_memdev *cxlmd = NULL;
+       int i, numa_node;
+
+       for (i = 0; i < p->nr_targets; i++) {
+               cxled = p->targets[i];
+               cxlmd = cxled_to_memdev(cxled);
+               numa_node = dev_to_node(&cxlmd->dev);
+               if (numa_node != NUMA_NO_NODE)
+                       return numa_node;
+       }
+       return NUMA_NO_NODE;
+}
+
+/*
+ * Package notifier callback: when a new memory node is onlined via dax
+ * kmem, bind the node this CXL region backs to its memory package, using
+ * the nearest region target as the initiator. Notifications for other
+ * nodes are ignored.
+ */
+static int cxl_region_add_package_node(struct notifier_block *nb,
+                                      unsigned long dax_nid, void *data)
+{
+       int region_nid, nearest_nid, ret;
+       struct cxl_region *cxlr = container_of(nb, struct cxl_region, 
package_notifier);
+
+       region_nid = phys_to_target_node(cxlr->params.res->start);
+       if (region_nid != dax_nid)
+               return NOTIFY_DONE;
+
+       nearest_nid = cxl_region_find_nearest_node(cxlr);
+       if (nearest_nid == NUMA_NO_NODE)
+               return NOTIFY_DONE;
+
+       ret = mp_add_package_node_by_initiator(dax_nid, nearest_nid);
+       if (ret)
+               return NOTIFY_DONE;
+
+       return NOTIFY_OK;
+}
+
 /**
  * devm_cxl_add_region - Adds a region to a decoder
  * @cxlrd: root decoder
@@ -3852,6 +3901,7 @@ static void shutdown_notifiers(void *_cxlr)
 
        unregister_node_notifier(&cxlr->node_notifier);
        unregister_mt_adistance_algorithm(&cxlr->adist_notifier);
+       unregister_mp_package_notifier(&cxlr->package_notifier);
 }
 
 static void remove_debugfs(void *dentry)
@@ -4066,6 +4116,10 @@ static int cxl_region_probe(struct device *dev)
        cxlr->adist_notifier.priority = 100;
        register_mt_adistance_algorithm(&cxlr->adist_notifier);
 
+       cxlr->package_notifier.notifier_call = cxl_region_add_package_node;
+       cxlr->package_notifier.priority = 100;
+       register_mp_package_notifier(&cxlr->package_notifier);
+
        rc = devm_add_action_or_reset(&cxlr->dev, shutdown_notifiers, cxlr);
        if (rc)
                return rc;
diff --git a/drivers/cxl/cxl.h b/drivers/cxl/cxl.h
index 1297594beaec..9281ca3a5f0f 100644
--- a/drivers/cxl/cxl.h
+++ b/drivers/cxl/cxl.h
@@ -477,6 +477,7 @@ struct cxl_region {
        struct access_coordinate coord[ACCESS_COORDINATE_MAX];
        struct notifier_block node_notifier;
        struct notifier_block adist_notifier;
+       struct notifier_block package_notifier;
 };
 
 struct cxl_nvdimm_bridge {
diff --git a/drivers/dax/kmem.c b/drivers/dax/kmem.c
index 2cc8749bc871..1de23f196354 100644
--- a/drivers/dax/kmem.c
+++ b/drivers/dax/kmem.c
@@ -94,6 +94,9 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
        if (IS_ERR(mtype))
                return PTR_ERR(mtype);
 
+       /* Resolve the memory package for this newly onlined kmem node. */
+       mp_probe_package_id(numa_node);
+
        for (i = 0; i < dev_dax->nr_range; i++) {
                struct range range;
 
-- 
2.25.1


Reply via email to