From 968212e098e03ebb9185b374bf6e2b49f9267f6a Mon Sep 17 00:00:00 2001
From: James Morse <james.morse@arm.com>
Date: Thu, 22 Dec 2022 17:01:52 +0000
Subject: [PATCH] debugfs: Add helpers for creating cpumask entries in debugfs

debugfs has handy helpers to make a bool, integer or string available
through debugfs. Add helpers to do the same for cpumasks. These are
read only.

Signed-off-by: James Morse <james.morse@arm.com>
---
 fs/debugfs/file.c       | 83 +++++++++++++++++++++++++++++++++++++++++
 include/linux/debugfs.h |  2 +
 2 files changed, 85 insertions(+)

diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c
index 1f971c880dde1..2dadefc9eae4f 100644
--- a/fs/debugfs/file.c
+++ b/fs/debugfs/file.c
@@ -957,6 +957,89 @@ void debugfs_create_str(const char *name, umode_t mode,
 				   &fops_str_ro, &fops_str_wo);
 }
 
+ssize_t debugfs_read_file_cpumask(struct file *file, char __user *user_buf,
+				   size_t count, loff_t *ppos)
+{
+	struct dentry *dentry = F_DENTRY(file);
+	struct cpumask *cpumask;
+	char *kernel_buf;
+	ssize_t ret;
+	int len;
+
+	ret = debugfs_file_get(dentry);
+	if (unlikely(ret))
+		return ret;
+
+	/* How long is a piece of string? */
+	kernel_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
+	if (!kernel_buf) {
+		debugfs_file_put(dentry);
+		return -ENOMEM;
+	}
+
+	cpumask = (struct cpumask *)file->private_data;
+	len = scnprintf(kernel_buf, PAGE_SIZE,
+			"%*pb\n", cpumask_pr_args(cpumask));
+	debugfs_file_put(dentry);
+	if (len + 1 >= PAGE_SIZE) {
+		kfree(kernel_buf);
+		return -EIO;
+	}
+
+	ret = simple_read_from_buffer(user_buf, count, ppos, kernel_buf, len);
+	kfree(kernel_buf);
+
+	return ret;
+}
+
+static const struct file_operations fops_cpumask = {
+	.read =	debugfs_read_file_cpumask,
+	.write =	debugfs_write_file_str,
+	.open =	simple_open,
+	.llseek =	default_llseek,
+};
+
+static const struct file_operations fops_cpumask_ro = {
+	.read =	debugfs_read_file_cpumask,
+	.open =	simple_open,
+	.llseek =	default_llseek,
+};
+
+static const struct file_operations fops_cpumask_wo = {
+	.write =	debugfs_write_file_str,
+	.open =	simple_open,
+	.llseek =	default_llseek,
+};
+
+/**
+ * debugfs_create_cpumask - create a debugfs file that is used to read a cpumask
+ * @name: a pointer to a string containing the name of the file to create.
+ * @mode: the permission that the file should have
+ * @parent: a pointer to the parent dentry for this file.  This should be a
+ *          directory dentry if set.  If this parameter is %NULL, then the
+ *          file will be created in the root of the debugfs filesystem.
+ * @value: a pointer to the variable that the file should read from.
+ *
+ * This function creates a file in debugfs with the given name that
+ * contains the value of the variable @value.  If the @mode variable is so
+ * set, it can be read from, and written to.
+ *
+ * This function will return a pointer to a dentry if it succeeds.  This
+ * pointer must be passed to the debugfs_remove() function when the file is
+ * to be removed (no automatic cleanup happens if your module is unloaded,
+ * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
+ * returned.
+ *
+ * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
+ * be returned.
+ */
+void debugfs_create_cpumask(const char *name, umode_t mode,
+			     struct dentry *parent, struct cpumask *value)
+{
+	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_cpumask,
+				    &fops_cpumask_ro, &fops_cpumask_wo);
+}
+
 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
 			      size_t count, loff_t *ppos)
 {
diff --git a/include/linux/debugfs.h b/include/linux/debugfs.h
index ea2d919fd9c79..585f43776fdeb 100644
--- a/include/linux/debugfs.h
+++ b/include/linux/debugfs.h
@@ -140,6 +140,8 @@ void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
 			 bool *value);
 void debugfs_create_str(const char *name, umode_t mode,
 			struct dentry *parent, char **value);
+void debugfs_create_cpumask(const char *name, umode_t mode,
+			    struct dentry *parent, struct cpumask *value);
 
 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
 				  struct dentry *parent,
-- 
GitLab