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

Dave Jiang
2025-10-21T22:55:03Z

On 10/21/25 11:31 AM, Ben Cheatham wrote:
> 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)

const char *?

Also maybe get_debugfs_dir_path()

> +{
> + char *dev, *dir, *type, *ret = NULL;

'debugfs_dir' rather than 'ret' would be clearer to read.

DJ

> + 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);
> }
>