diff mbox series

[ndctl,v6,03/13] libdaxctl: add an interface to retrieve the device resource

Message ID 20190717225400.9494-4-vishal.l.verma@intel.com (mailing list archive)
State Superseded
Headers show
Series daxctl: add a new reconfigure-device command | expand

Commit Message

Verma, Vishal L July 17, 2019, 10:53 p.m. UTC
Add an interface to retrieve the 'resource' attribute for a dax device.

Attempt to retrieve it as usual via sysfs, but since older kernels may
be missing this attribute, as a fallback, attempt to retrieve it from
/proc/iomem

Cc: Dan Williams <dan.j.williams@intel.com>
[fscanf format string problem and diagnosis]
Reported-by: Tony Luck <tony.luck@intel.com>
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
 Makefile.am                    |  3 ++-
 daxctl/lib/Makefile.am         |  2 ++
 daxctl/lib/libdaxctl-private.h |  1 +
 daxctl/lib/libdaxctl.c         | 12 +++++++++++
 daxctl/lib/libdaxctl.sym       |  1 +
 daxctl/libdaxctl.h             |  1 +
 util/iomem.c                   | 37 ++++++++++++++++++++++++++++++++++
 util/iomem.h                   | 12 +++++++++++
 8 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100644 util/iomem.c
 create mode 100644 util/iomem.h

Comments

Dan Williams July 18, 2019, 1:56 a.m. UTC | #1
On Wed, Jul 17, 2019 at 3:54 PM Vishal Verma <vishal.l.verma@intel.com> wrote:
>
> Add an interface to retrieve the 'resource' attribute for a dax device.
>
> Attempt to retrieve it as usual via sysfs, but since older kernels may
> be missing this attribute, as a fallback, attempt to retrieve it from
> /proc/iomem
>
> Cc: Dan Williams <dan.j.williams@intel.com>
> [fscanf format string problem and diagnosis]
> Reported-by: Tony Luck <tony.luck@intel.com>
> Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>

Looks ok, just wish we had added a dax resource sysfs attribute
earlier. That fscanf() line is a work of art.

Reviewed-by: Dan Williams <dan.j.williams@intel.com>
diff mbox series

Patch

diff --git a/Makefile.am b/Makefile.am
index df8797e..8d10a10 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -74,6 +74,7 @@  libutil_a_SOURCES = \
 	util/wrapper.c \
 	util/filter.c \
 	util/bitmap.c \
-	util/abspath.c
+	util/abspath.c \
+	util/iomem.c
 
 nobase_include_HEADERS = daxctl/libdaxctl.h
diff --git a/daxctl/lib/Makefile.am b/daxctl/lib/Makefile.am
index 9f0e444..7704b1b 100644
--- a/daxctl/lib/Makefile.am
+++ b/daxctl/lib/Makefile.am
@@ -9,6 +9,8 @@  lib_LTLIBRARIES = libdaxctl.la
 libdaxctl_la_SOURCES =\
 	../libdaxctl.h \
 	libdaxctl-private.h \
+	../../util/iomem.c \
+	../../util/iomem.h \
 	../../util/sysfs.c \
 	../../util/sysfs.h \
 	../../util/log.c \
diff --git a/daxctl/lib/libdaxctl-private.h b/daxctl/lib/libdaxctl-private.h
index e64d0a7..eb7c1ec 100644
--- a/daxctl/lib/libdaxctl-private.h
+++ b/daxctl/lib/libdaxctl-private.h
@@ -59,6 +59,7 @@  struct daxctl_dev {
 	size_t buf_len;
 	char *dev_path;
 	struct list_node list;
+	unsigned long long resource;
 	unsigned long long size;
 	struct kmod_module *module;
 	struct kmod_list *kmod_list;
diff --git a/daxctl/lib/libdaxctl.c b/daxctl/lib/libdaxctl.c
index 05f013b..5431063 100644
--- a/daxctl/lib/libdaxctl.c
+++ b/daxctl/lib/libdaxctl.c
@@ -24,6 +24,7 @@ 
 
 #include <util/log.h>
 #include <util/sysfs.h>
+#include <util/iomem.h>
 #include <daxctl/libdaxctl.h>
 #include "libdaxctl-private.h"
 
@@ -369,6 +370,12 @@  static void *add_dax_dev(void *parent, int id, const char *daxdev_base)
 	dev->major = major(st.st_rdev);
 	dev->minor = minor(st.st_rdev);
 
+	sprintf(path, "%s/resource", daxdev_base);
+	if (sysfs_read_attr(ctx, path, buf) == 0)
+		dev->resource = strtoull(buf, NULL, 0);
+	else
+		dev->resource = iomem_get_dev_resource(ctx, daxdev_base);
+
 	sprintf(path, "%s/size", daxdev_base);
 	if (sysfs_read_attr(ctx, path, buf) < 0)
 		goto err_read;
@@ -878,6 +885,11 @@  DAXCTL_EXPORT int daxctl_dev_get_minor(struct daxctl_dev *dev)
 	return dev->minor;
 }
 
+DAXCTL_EXPORT unsigned long long daxctl_dev_get_resource(struct daxctl_dev *dev)
+{
+	return dev->resource;
+}
+
 DAXCTL_EXPORT unsigned long long daxctl_dev_get_size(struct daxctl_dev *dev)
 {
 	return dev->size;
diff --git a/daxctl/lib/libdaxctl.sym b/daxctl/lib/libdaxctl.sym
index 19904a2..1692624 100644
--- a/daxctl/lib/libdaxctl.sym
+++ b/daxctl/lib/libdaxctl.sym
@@ -58,4 +58,5 @@  global:
 	daxctl_dev_disable;
 	daxctl_dev_enable_devdax;
 	daxctl_dev_enable_ram;
+	daxctl_dev_get_resource;
 } LIBDAXCTL_5;
diff --git a/daxctl/libdaxctl.h b/daxctl/libdaxctl.h
index b80488e..7214cd3 100644
--- a/daxctl/libdaxctl.h
+++ b/daxctl/libdaxctl.h
@@ -66,6 +66,7 @@  int daxctl_dev_get_id(struct daxctl_dev *dev);
 const char *daxctl_dev_get_devname(struct daxctl_dev *dev);
 int daxctl_dev_get_major(struct daxctl_dev *dev);
 int daxctl_dev_get_minor(struct daxctl_dev *dev);
+unsigned long long daxctl_dev_get_resource(struct daxctl_dev *dev);
 unsigned long long daxctl_dev_get_size(struct daxctl_dev *dev);
 struct daxctl_ctx *daxctl_dev_get_ctx(struct daxctl_dev *dev);
 int daxctl_dev_is_enabled(struct daxctl_dev *dev);
diff --git a/util/iomem.c b/util/iomem.c
new file mode 100644
index 0000000..a3c23f5
--- /dev/null
+++ b/util/iomem.c
@@ -0,0 +1,37 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright(c) 2019 Intel Corporation. All rights reserved. */
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <util/log.h>
+#include <util/iomem.h>
+#include <util/sysfs.h>
+
+unsigned long long __iomem_get_dev_resource(struct log_ctx *ctx,
+		const char *devpath)
+{
+	const char *devname = devpath_to_devname(devpath);
+	FILE *fp = fopen("/proc/iomem", "r");
+	unsigned long long res;
+	char name[256];
+
+	if (fp == NULL) {
+		log_err(ctx, "%s: open /proc/iomem: %s\n", devname,
+				strerror(errno));
+		return 0;
+	}
+
+	while (fscanf(fp, "%llx-%*x : %254[^\n]\n", &res, name) == 2) {
+		if (strcmp(name, devname) == 0) {
+			log_dbg(ctx, "%s: got resource via iomem: %#llx\n",
+					devname, res);
+			fclose(fp);
+			return res;
+		}
+	}
+
+	log_dbg(ctx, "%s: not found in iomem\n", devname);
+	fclose(fp);
+	return 0;
+}
diff --git a/util/iomem.h b/util/iomem.h
new file mode 100644
index 0000000..aaaf6a7
--- /dev/null
+++ b/util/iomem.h
@@ -0,0 +1,12 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright(c) 2019 Intel Corporation. All rights reserved. */
+#ifndef _NDCTL_IOMEM_H_
+#define _NDCTL_IOMEM_H_
+
+struct log_ctx;
+unsigned long long __iomem_get_dev_resource(struct log_ctx *ctx,
+		const char *path);
+
+#define iomem_get_dev_resource(c, p) __iomem_get_dev_resource(&(c)->ctx, (p))
+
+#endif /* _NDCTL_IOMEM_H_ */