[ndctl PATCH v3 1/7] libcxl: Add debugfs path to CXL context

Ben Cheatham
2025-10-21T18:32:21Z

Find the CXL debugfs mount point and add it to the CXL library context.
This will be used by poison and procotol error library functions to
access the information presented by the filesystem.

Signed-off-by: Ben Cheatham <Benjamin.Cheatham@amd.com>
---
cxl/lib/libcxl.c | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)

diff --git a/cxl/lib/libcxl.c b/cxl/lib/libcxl.c
index cafde1c..ea5831f 100644
--- a/cxl/lib/libcxl.c
+++ b/cxl/lib/libcxl.c
@@ -54,6 +54,7 @@ struct cxl_ctx {
struct kmod_ctx *kmod_ctx;
struct daxctl_ctx *daxctl_ctx;
void *private_data;
+ const char *debugfs;
};

static void free_pmem(struct cxl_pmem *pmem)
@@ -240,6 +241,43 @@ CXL_EXPORT void *cxl_get_private_data(struct cxl_ctx *ctx)
return ctx->private_data;
}

+static char *get_debugfs_dir(void)
+{
+ char *dev, *dir, *type, *ret = NULL;
+ char line[PATH_MAX + 256 + 1];
+ FILE *fp;
+
+ fp = fopen("/proc/mounts", "r");
+ if (!fp)
+ return ret;
+
+ while (fgets(line, sizeof(line), fp)) {
+ dev = strtok(line, " \t");
+ if (!dev)
+ break;
+
+ dir = strtok(NULL, " \t");
+ if (!dir)
+ break;
+
+ type = strtok(NULL, " \t");
+ if (!type)
+ break;
+
+ if (!strcmp(type, "debugfs")) {
+ ret = calloc(strlen(dir) + 1, 1);
+ if (!ret)
+ break;
+
+ strcpy(ret, dir);
+ break;
+ }
+ }
+
+ fclose(fp);
+ return ret;
+}
+
/**
* cxl_new - instantiate a new library context
* @ctx: context to establish
@@ -295,6 +333,7 @@ CXL_EXPORT int cxl_new(struct cxl_ctx **ctx)
c->udev = udev;
c->udev_queue = udev_queue;
c->timeout = 5000;
+ c->debugfs = get_debugfs_dir();

return 0;

@@ -350,6 +389,7 @@ CXL_EXPORT void cxl_unref(struct cxl_ctx *ctx)
kmod_unref(ctx->kmod_ctx);
daxctl_unref(ctx->daxctl_ctx);
info(ctx, "context %p released\n", ctx);
+ free((void *)ctx->debugfs);
free(ctx);
}

--
2.34.1