mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "David E. Box" <david.e.box@linux.intel.com>
To: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	"Hans de Goede" <hansg@kernel.org>
Cc: "David E. Box" <david.e.box@linux.intel.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	platform-driver-x86@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 02/12] tools/arch/x86/pmtctl: Add PMT telemetry device discovery
Date: Sun, 22 Nov 2026 11:33:57 -0800	[thread overview]
Message-ID: <65c24797e5d631463d0db4316fdb61625f09b3ff.1795374925.git.david.e.box@linux.intel.com> (raw)
In-Reply-To: <cover.1795374925.git.david.e.box@linux.intel.com>

The pmt_telemetry driver binds to Intel VSEC auxiliary devices and
registers one telemetry device, telemN, per telemetry region under the
auxiliary device's intel_pmt directory. Each telemetry device has a guid
attribute that identifies its metric layout and a telem file with the
sample data.

Add discovery of these telemetry devices. telem_scan() walks the
auxiliary devices bound to pmt_telemetry and records the GUID, name and
data file path of each telemetry device. A device whose GUID can't be
read is skipped with a warning. If no devices are found, an error asks
whether the driver is loaded. The devices are returned sorted by GUID
and then by name, so the commands built on them don't depend on the
order of the sysfs directories.

The results are held in struct pmtctl_ctx, which each command owns and
releases with pmtctl_ctx_free(). auto_pmtctl_ctx frees it automatically
at scope exit, and auto_free does the same for heap pointers.

Also add small helpers to lib/common.c, used by this and later patches:
pmt_grow_array() grows a dynamic array by doubling its capacity,
parse_u32() parses a whole string as an unsigned 32-bit number, and
path_printf() builds a path, failing instead of truncating.

Signed-off-by: David E. Box <david.e.box@linux.intel.com>
---
V2 changes:
  - Replaces v1 05, with the context from v1 08 and pmt_grow_array()
    from v1 03.
  - Unused device fields and sysfs helpers dropped.
  - A device whose GUID can't be read is skipped with a warning.
  - Devices are returned sorted by GUID and name, independent of the
    order of the sysfs directories.
  - pmt_grow_array() returns the array and uses reallocarray(), with no
    realloc(+1) growth or void ** cast.
  - parse_u32() rejects a sign, leading whitespace and trailing
    characters; path_printf() fails instead of truncating.

 tools/arch/x86/pmtctl/Makefile             |   3 +
 tools/arch/x86/pmtctl/include/lib/common.h |  64 ++++++
 tools/arch/x86/pmtctl/include/lib/device.h |  18 ++
 tools/arch/x86/pmtctl/include/lib/pmtctl.h |  24 ++
 tools/arch/x86/pmtctl/lib/common.c         |  75 +++++++
 tools/arch/x86/pmtctl/lib/device_telem.c   | 248 +++++++++++++++++++++
 tools/arch/x86/pmtctl/lib/pmtctl.c         |  22 ++
 7 files changed, 454 insertions(+)
 create mode 100644 tools/arch/x86/pmtctl/include/lib/common.h
 create mode 100644 tools/arch/x86/pmtctl/include/lib/device.h
 create mode 100644 tools/arch/x86/pmtctl/include/lib/pmtctl.h
 create mode 100644 tools/arch/x86/pmtctl/lib/common.c
 create mode 100644 tools/arch/x86/pmtctl/lib/device_telem.c
 create mode 100644 tools/arch/x86/pmtctl/lib/pmtctl.c

diff --git a/tools/arch/x86/pmtctl/Makefile b/tools/arch/x86/pmtctl/Makefile
index 0595d6617920..bf422b84fd34 100644
--- a/tools/arch/x86/pmtctl/Makefile
+++ b/tools/arch/x86/pmtctl/Makefile
@@ -8,7 +8,10 @@ CFLAGS = -Wextra
 override CFLAGS += -O2 -Wall -D_GNU_SOURCE -Iinclude -I../../../include
 
 OBJS := \
+	lib/common.o \
+	lib/device_telem.o \
 	lib/log.o \
+	lib/pmtctl.o \
 	src/main.o
 
 pmtctl: $(OBJS)
diff --git a/tools/arch/x86/pmtctl/include/lib/common.h b/tools/arch/x86/pmtctl/include/lib/common.h
new file mode 100644
index 000000000000..0e1950bffc43
--- /dev/null
+++ b/tools/arch/x86/pmtctl/include/lib/common.h
@@ -0,0 +1,64 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef PMTCTL_COMMON_H
+#define PMTCTL_COMMON_H
+
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include <linux/compiler.h>
+
+static inline void freep(void *p)
+{
+	free(*(void **)p);
+}
+
+/* Free the pointer with free() at scope exit: char *p auto_free = NULL; */
+#define auto_free __attribute__((cleanup(freep)))
+
+/* Take ownership of an auto_free pointer so it is not freed at scope exit */
+#define no_free_ptr(p) \
+	({ typeof(p) __ptr = (p); (p) = NULL; __ptr; })
+
+#define PMT_ARRAY_INITIAL_CAP 16
+
+/**
+ * pmt_grow_array - grow a dynamic array
+ * @array:     array allocated with malloc() and friends, or NULL
+ * @cap:       current capacity in elements, updated on success
+ * @min_cap:   required capacity in elements
+ * @elem_size: element size in bytes
+ *
+ * The capacity starts at PMT_ARRAY_INITIAL_CAP and doubles until it
+ * reaches @min_cap.
+ *
+ * Return: the possibly moved array, or NULL on allocation failure or
+ * overflow. On failure @array and @cap are unchanged.
+ */
+void *pmt_grow_array(void *array, size_t *cap, size_t min_cap, size_t elem_size);
+
+/**
+ * parse_u32 - parse a complete string as an unsigned 32-bit number
+ * @s:    string to parse
+ * @base: number base, as for strtoul()
+ * @val:  returns the parsed value
+ *
+ * Unlike strtoul(), leading whitespace and a sign are not accepted.
+ *
+ * Return: 0 on success, -EINVAL if @s is not a number or has trailing
+ * characters, or -ERANGE if the value does not fit in 32 bits.
+ */
+int parse_u32(const char *s, int base, uint32_t *val);
+
+/**
+ * path_printf - format a path into a buffer
+ * @buf:  buffer for the path
+ * @size: size of @buf in bytes
+ * @fmt:  printf() format of the path
+ * @...:  arguments for @fmt
+ *
+ * Return: 0 on success, or -ENAMETOOLONG if the path does not fit in @buf.
+ */
+int path_printf(char *buf, size_t size, const char *fmt, ...) __printf(3, 4);
+
+#endif
diff --git a/tools/arch/x86/pmtctl/include/lib/device.h b/tools/arch/x86/pmtctl/include/lib/device.h
new file mode 100644
index 000000000000..92d9603d695c
--- /dev/null
+++ b/tools/arch/x86/pmtctl/include/lib/device.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef PMTCTL_DEVICE_H
+#define PMTCTL_DEVICE_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+/* A PMT telemetry device registered by the pmt_telemetry driver */
+struct pmt_device {
+	uint32_t guid;
+	char *name;		/* e.g. "telem0" */
+	char *data_path;	/* telemetry data file */
+};
+
+int telem_scan(struct pmt_device **devices, size_t *num_devices);
+void telem_free(struct pmt_device *devices, size_t num_devices);
+
+#endif
diff --git a/tools/arch/x86/pmtctl/include/lib/pmtctl.h b/tools/arch/x86/pmtctl/include/lib/pmtctl.h
new file mode 100644
index 000000000000..e3a2c862bc78
--- /dev/null
+++ b/tools/arch/x86/pmtctl/include/lib/pmtctl.h
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef PMTCTL_H
+#define PMTCTL_H
+
+#include <stddef.h>
+
+#include "lib/device.h"
+
+/* State for one pmtctl command, owned by the command */
+struct pmtctl_ctx {
+	struct pmt_device *devices;
+	size_t num_devices;
+};
+
+int pmtctl_ctx_init(struct pmtctl_ctx *ctx);
+void pmtctl_ctx_free(struct pmtctl_ctx *ctx);
+
+/*
+ * Free the context with pmtctl_ctx_free() at scope exit:
+ * struct pmtctl_ctx ctx auto_pmtctl_ctx = {};
+ */
+#define auto_pmtctl_ctx __attribute__((cleanup(pmtctl_ctx_free)))
+
+#endif
diff --git a/tools/arch/x86/pmtctl/lib/common.c b/tools/arch/x86/pmtctl/lib/common.c
new file mode 100644
index 000000000000..adf546df0911
--- /dev/null
+++ b/tools/arch/x86/pmtctl/lib/common.c
@@ -0,0 +1,75 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <ctype.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "lib/common.h"
+
+void *pmt_grow_array(void *array, size_t *cap, size_t min_cap, size_t elem_size)
+{
+	size_t new_cap;
+	void *new_array;
+
+	if (*cap >= min_cap)
+		return array;
+
+	new_cap = *cap;
+	if (!new_cap)
+		new_cap = PMT_ARRAY_INITIAL_CAP;
+
+	while (new_cap < min_cap) {
+		if (new_cap > SIZE_MAX / 2)
+			return NULL;
+		new_cap *= 2;
+	}
+
+	new_array = reallocarray(array, new_cap, elem_size);
+	if (!new_array)
+		return NULL;
+
+	*cap = new_cap;
+
+	return new_array;
+}
+
+int parse_u32(const char *s, int base, uint32_t *val)
+{
+	unsigned long v;
+	char *end;
+
+	if (!isxdigit((unsigned char)*s))
+		return -EINVAL;
+
+	errno = 0;
+	v = strtoul(s, &end, base);
+	if (errno)
+		return -errno;
+
+	if (end == s || *end != '\0')
+		return -EINVAL;
+
+	if (v > UINT32_MAX)
+		return -ERANGE;
+
+	*val = v;
+
+	return 0;
+}
+
+int path_printf(char *buf, size_t size, const char *fmt, ...)
+{
+	va_list ap;
+	int len;
+
+	va_start(ap, fmt);
+	len = vsnprintf(buf, size, fmt, ap);
+	va_end(ap);
+
+	if (len < 0 || (size_t)len >= size)
+		return -ENAMETOOLONG;
+
+	return 0;
+}
diff --git a/tools/arch/x86/pmtctl/lib/device_telem.c b/tools/arch/x86/pmtctl/lib/device_telem.c
new file mode 100644
index 000000000000..0e249f16a21a
--- /dev/null
+++ b/tools/arch/x86/pmtctl/lib/device_telem.c
@@ -0,0 +1,248 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <ctype.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "lib/common.h"
+#include "lib/device.h"
+#include "lib/log.h"
+
+#define PMT_TELEM_AUX_DIR	"/sys/bus/auxiliary/drivers/pmt_telemetry"
+#define PMT_TELEM_PMT_DIR	"intel_pmt"
+#define PMT_TELEM_PREFIX	"telem"
+#define PMT_TELEM_DATA_FILE	"telem"
+
+struct telem_scan {
+	struct pmt_device *devices;
+	size_t num_devices;
+	size_t cap;
+};
+
+/* Read sysfs attribute @dir/@name into @buf without trailing whitespace */
+static int read_attr_text(const char *dir, const char *name, char *buf, size_t buflen)
+{
+	char path[PATH_MAX];
+	ssize_t n;
+	int ret;
+	int fd;
+
+	ret = path_printf(path, sizeof(path), "%s/%s", dir, name);
+	if (ret)
+		return ret;
+
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return -errno;
+
+	n = read(fd, buf, buflen - 1);
+	if (n < 0) {
+		ret = -errno;
+		close(fd);
+		return ret;
+	}
+	close(fd);
+
+	while (n > 0 && isspace((unsigned char)buf[n - 1]))
+		n--;
+	buf[n] = '\0';
+
+	return 0;
+}
+
+/* Read sysfs attribute @dir/@name as a number in @base, as for parse_u32() */
+static int read_attr_u32(const char *dir, const char *name, int base, uint32_t *val)
+{
+	char buf[64];
+	int ret;
+
+	ret = read_attr_text(dir, name, buf, sizeof(buf));
+	if (ret)
+		return ret;
+
+	return parse_u32(buf, base, val);
+}
+
+/* Match "telem<N>" where <N> is a decimal number */
+static bool is_telem_name(const char *name)
+{
+	const size_t prefix_len = strlen(PMT_TELEM_PREFIX);
+	const char *num;
+
+	if (strncmp(name, PMT_TELEM_PREFIX, prefix_len))
+		return false;
+
+	num = name + prefix_len;
+
+	return *num != '\0' && num[strspn(num, "0123456789")] == '\0';
+}
+
+static int telem_add_device(struct telem_scan *scan, const char *devpath, const char *name)
+{
+	char *dev_name auto_free = NULL;
+	char *data_path auto_free = NULL;
+	struct pmt_device *devices;
+	uint32_t guid;
+	int ret;
+
+	ret = read_attr_u32(devpath, "guid", 16, &guid);
+	if (ret) {
+		log_warn("skipping %s: unable to read guid: %s", devpath, strerror(-ret));
+		return 0;
+	}
+
+	dev_name = strdup(name);
+	if (!dev_name)
+		return -ENOMEM;
+
+	if (asprintf(&data_path, "%s/%s", devpath, PMT_TELEM_DATA_FILE) < 0) {
+		/* asprintf() leaves the pointer undefined on failure */
+		data_path = NULL;
+		return -ENOMEM;
+	}
+
+	devices = pmt_grow_array(scan->devices, &scan->cap, scan->num_devices + 1,
+				 sizeof(*devices));
+	if (!devices)
+		return -ENOMEM;
+
+	scan->devices = devices;
+
+	devices[scan->num_devices++] = (struct pmt_device) {
+		.guid = guid,
+		.name = no_free_ptr(dev_name),
+		.data_path = no_free_ptr(data_path),
+	};
+
+	return 0;
+}
+
+/* Add the telemetry devices under one auxiliary device's intel_pmt directory */
+static int telem_scan_pmt_dir(struct telem_scan *scan, const char *pmt_path)
+{
+	struct dirent *de;
+	DIR *dir;
+	int ret = 0;
+
+	dir = opendir(pmt_path);
+	if (!dir) {
+		/* Most driver directory entries (bind, uevent, ...) aren't devices */
+		if (errno == ENOENT || errno == ENOTDIR)
+			return 0;
+
+		return -errno;
+	}
+
+	while ((de = readdir(dir))) {
+		char devpath[PATH_MAX];
+
+		if (!is_telem_name(de->d_name))
+			continue;
+
+		ret = path_printf(devpath, sizeof(devpath), "%s/%s", pmt_path, de->d_name);
+		if (ret)
+			break;
+
+		ret = telem_add_device(scan, devpath, de->d_name);
+		if (ret)
+			break;
+	}
+
+	closedir(dir);
+
+	return ret;
+}
+
+/* Walk the auxiliary devices bound to the pmt_telemetry driver */
+static int telem_scan_aux_dir(struct telem_scan *scan)
+{
+	struct dirent *de;
+	DIR *dir;
+	int ret = 0;
+
+	/* Without the driver there are no devices, which telem_scan() reports */
+	dir = opendir(PMT_TELEM_AUX_DIR);
+	if (!dir)
+		return 0;
+
+	while ((de = readdir(dir))) {
+		char pmt_path[PATH_MAX];
+
+		if (de->d_name[0] == '.')
+			continue;
+
+		ret = path_printf(pmt_path, sizeof(pmt_path), "%s/%s/%s",
+				  PMT_TELEM_AUX_DIR, de->d_name, PMT_TELEM_PMT_DIR);
+		if (ret)
+			break;
+
+		ret = telem_scan_pmt_dir(scan, pmt_path);
+		if (ret)
+			break;
+	}
+
+	closedir(dir);
+
+	return ret;
+}
+
+/* Order devices by GUID, then by name, so telem2 comes before telem10 */
+static int device_cmp(const void *a, const void *b)
+{
+	const struct pmt_device *da = a;
+	const struct pmt_device *db = b;
+
+	if (da->guid != db->guid)
+		return da->guid < db->guid ? -1 : 1;
+
+	return strverscmp(da->name, db->name);
+}
+
+/**
+ * telem_scan - discover PMT telemetry devices
+ * @devices:     returns the discovered devices sorted by GUID, then by name.
+ *               Freed with telem_free().
+ * @num_devices: returns the number of entries in @devices
+ *
+ * Return: 0 on success, -ENODEV if no devices were found, or another
+ * negative errno on failure.
+ */
+int telem_scan(struct pmt_device **devices, size_t *num_devices)
+{
+	struct telem_scan scan = {};
+	int ret;
+
+	ret = telem_scan_aux_dir(&scan);
+	if (ret) {
+		telem_free(scan.devices, scan.num_devices);
+		return log_ret(ret, "unable to scan PMT telemetry devices");
+	}
+
+	if (!scan.num_devices) {
+		log_err("no PMT telemetry devices found (is the pmt_telemetry driver loaded?)");
+		return -ENODEV;
+	}
+
+	qsort(scan.devices, scan.num_devices, sizeof(*scan.devices), device_cmp);
+
+	*devices = scan.devices;
+	*num_devices = scan.num_devices;
+
+	return 0;
+}
+
+void telem_free(struct pmt_device *devices, size_t num_devices)
+{
+	for (size_t i = 0; i < num_devices; i++) {
+		free(devices[i].name);
+		free(devices[i].data_path);
+	}
+	free(devices);
+}
diff --git a/tools/arch/x86/pmtctl/lib/pmtctl.c b/tools/arch/x86/pmtctl/lib/pmtctl.c
new file mode 100644
index 000000000000..9a6fd2a303f4
--- /dev/null
+++ b/tools/arch/x86/pmtctl/lib/pmtctl.c
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include "lib/device.h"
+#include "lib/pmtctl.h"
+
+/**
+ * pmtctl_ctx_init - discover PMT devices
+ * @ctx: zero-initialized context
+ *
+ * Return: 0 on success or a negative errno. @ctx must be released with
+ * pmtctl_ctx_free() in either case.
+ */
+int pmtctl_ctx_init(struct pmtctl_ctx *ctx)
+{
+	return telem_scan(&ctx->devices, &ctx->num_devices);
+}
+
+void pmtctl_ctx_free(struct pmtctl_ctx *ctx)
+{
+	telem_free(ctx->devices, ctx->num_devices);
+	ctx->devices = NULL;
+	ctx->num_devices = 0;
+}
-- 
2.53.0


  parent reply	other threads:[~2026-11-22 19:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 19:33 [PATCH v2 00/12] tools/arch/x86/pmtctl: Add Intel PMT command-line tool David E. Box
2026-11-22 19:33 ` [PATCH v2 01/12] tools/arch/x86/pmtctl: Add pmtctl skeleton David E. Box
2026-11-22 19:33 ` David E. Box [this message]
2026-11-22 19:33 ` [PATCH v2 03/12] tools/arch/x86/pmtctl: Add pmtxml2json conversion tool David E. Box
2026-11-22 19:33 ` [PATCH v2 04/12] tools/arch/x86/pmtctl: Add JSON metric definition loading David E. Box
2026-11-22 19:34 ` [PATCH v2 05/12] tools/arch/x86/pmtctl: Add 'list' command David E. Box
2026-11-22 19:34 ` [PATCH v2 06/12] tools/arch/x86/pmtctl: Add 'list --summary' David E. Box
2026-11-22 19:34 ` [PATCH v2 07/12] tools/arch/x86/pmtctl: Add 'stat' command David E. Box
2026-11-22 19:34 ` [PATCH v2 08/12] tools/arch/x86/pmtctl: Add 'stat --hex' David E. Box
2026-11-22 19:34 ` [PATCH v2 09/12] tools/arch/x86/pmtctl: Add 'stat --raw' David E. Box
2026-11-22 19:34 ` [PATCH v2 10/12] tools/arch/x86/pmtctl: Add README David E. Box
2026-11-22 19:34 ` [PATCH v2 11/12] tools/arch/x86/pmtctl: Add man page David E. Box
2026-11-22 19:34 ` [PATCH v2 12/12] tools/arch/x86/pmtctl: Add MAINTAINERS entry David E. Box

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=65c24797e5d631463d0db4316fdb61625f09b3ff.1795374925.git.david.e.box@linux.intel.com \
    --to=david.e.box@linux.intel.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=hansg@kernel.org \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=platform-driver-x86@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®