linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Cristian Marussi <cristian.marussi@arm.com>
To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org
Cc: sudeep.holla@arm.com, james.quinlan@broadcom.com,
	Jonathan.Cameron@Huawei.com, f.fainelli@gmail.com,
	vincent.guittot@linaro.org, souvik.chakravarty@arm.com,
	peter.hilber@opensynergy.com, cristian.marussi@arm.com,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>
Subject: [RFC PATCH 07/16] debugfs: Add signed versions of debugfs_create_u32/64 helpers
Date: Sun, 27 Feb 2022 20:55:59 +0000	[thread overview]
Message-ID: <20220227205608.30812-8-cristian.marussi@arm.com> (raw)
In-Reply-To: <20220227205608.30812-1-cristian.marussi@arm.com>

Add a few helpers to deal with signed values integers; built on existing
debugfs internal helpers as the existing unsigned functions already do.

Make the simple_attr_write() internal helper detect the sign of the
requested set operation from the related format string: this is needed
to be able to properly parse negatively signed input strings.

Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
Note that in the rest of the series I do NOT need the s64 WRITE/SET
operations, that required the more invasive simple_attr_write() change,
but it seemed odd to implement a get only debug_create_s32/64 API.
---
 fs/debugfs/file.c       | 74 +++++++++++++++++++++++++++++++++++++++++
 fs/libfs.c              | 12 +++++--
 include/linux/debugfs.h |  4 +++
 3 files changed, 87 insertions(+), 3 deletions(-)

diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c
index 950c63fa4d0b..5363b12c3dcb 100644
--- a/fs/debugfs/file.c
+++ b/fs/debugfs/file.c
@@ -556,6 +556,80 @@ void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
 }
 EXPORT_SYMBOL_GPL(debugfs_create_u64);
 
+static int debugfs_s32_set(void *data, u64 val)
+{
+	*(s32 *)data = val;
+	return 0;
+}
+
+static int debugfs_s32_get(void *data, u64 *val)
+{
+	*val = *(s32 *)data;
+	return 0;
+}
+DEFINE_DEBUGFS_ATTRIBUTE(fops_s32, debugfs_s32_get, debugfs_s32_set, "%lld\n");
+DEFINE_DEBUGFS_ATTRIBUTE(fops_s32_ro, debugfs_s32_get, NULL, "%lld\n");
+DEFINE_DEBUGFS_ATTRIBUTE(fops_s32_wo, NULL, debugfs_s32_set, "%lld\n");
+
+/**
+ * debugfs_create_s32 - create a debugfs file that is used to read and write an signed 32-bit value
+ * @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 to and write
+ *         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.
+ */
+void debugfs_create_s32(const char *name, umode_t mode, struct dentry *parent,
+			s32 *value)
+{
+	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_s32,
+				   &fops_s32_ro, &fops_s32_wo);
+}
+EXPORT_SYMBOL_GPL(debugfs_create_s32);
+
+static int debugfs_s64_set(void *data, u64 val)
+{
+	*(s64 *)data = val;
+	return 0;
+}
+
+static int debugfs_s64_get(void *data, u64 *val)
+{
+	*val = *(s64 *)data;
+	return 0;
+}
+DEFINE_DEBUGFS_ATTRIBUTE(fops_s64, debugfs_s64_get, debugfs_s64_set, "%lld\n");
+DEFINE_DEBUGFS_ATTRIBUTE(fops_s64_ro, debugfs_s64_get, NULL, "%lld\n");
+DEFINE_DEBUGFS_ATTRIBUTE(fops_s64_wo, NULL, debugfs_s64_set, "%lld\n");
+
+/**
+ * debugfs_create_s64 - create a debugfs file that is used to read and write a signed 64-bit value
+ * @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 to and write
+ *         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.
+ */
+void debugfs_create_s64(const char *name, umode_t mode, struct dentry *parent,
+			s64 *value)
+{
+	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_s64,
+				   &fops_s64_ro, &fops_s64_wo);
+}
+EXPORT_SYMBOL_GPL(debugfs_create_s64);
+
 static int debugfs_ulong_set(void *data, u64 val)
 {
 	*(unsigned long *)data = val;
diff --git a/fs/libfs.c b/fs/libfs.c
index ba7438ab9371..f5a554ed363b 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -917,8 +917,8 @@ EXPORT_SYMBOL(simple_transaction_release);
 struct simple_attr {
 	int (*get)(void *, u64 *);
 	int (*set)(void *, u64);
-	char get_buf[24];	/* enough to store a u64 and "\n\0" */
-	char set_buf[24];
+	char get_buf[25];	/* enough to store a u64, a sign and "\n\0" */
+	char set_buf[25];
 	void *data;
 	const char *fmt;	/* format for read operation */
 	struct mutex mutex;	/* protects access to these buffers */
@@ -1001,6 +1001,7 @@ ssize_t simple_attr_write(struct file *file, const char __user *buf,
 	unsigned long long val;
 	size_t size;
 	ssize_t ret;
+	char *_fmt;
 
 	attr = file->private_data;
 	if (!attr->set)
@@ -1016,7 +1017,12 @@ ssize_t simple_attr_write(struct file *file, const char __user *buf,
 		goto out;
 
 	attr->set_buf[size] = '\0';
-	ret = kstrtoull(attr->set_buf, 0, &val);
+	_fmt = strchr(attr->fmt, '%');
+	/* Deduce signedness from read format string specifier */
+	if (_fmt &&  (strchr(_fmt, 'd') || strchr(_fmt, 'i')))
+		ret = kstrtoll(attr->set_buf, 0, (long long *)&val);
+	else
+		ret = kstrtoull(attr->set_buf, 0, &val);
 	if (ret)
 		goto out;
 	ret = attr->set(attr->data, val);
diff --git a/include/linux/debugfs.h b/include/linux/debugfs.h
index c869f1e73d75..1b8ea858f261 100644
--- a/include/linux/debugfs.h
+++ b/include/linux/debugfs.h
@@ -114,6 +114,10 @@ void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
 			u64 *value);
 void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
 			  unsigned long *value);
+void debugfs_create_s32(const char *name, umode_t mode, struct dentry *parent,
+			s32 *value);
+void debugfs_create_s64(const char *name, umode_t mode, struct dentry *parent,
+			s64 *value);
 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
 		       u8 *value);
 void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
-- 
2.17.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  parent reply	other threads:[~2022-02-27 20:59 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-27 20:55 [RFC PATCH 00/16] Introduce SCMI test driver and related Kselftest Cristian Marussi
2022-02-27 20:55 ` [RFC PATCH 01/16] firmware: arm_scmi: Simplify scmi_devm_notifier_unregister Cristian Marussi
2022-02-27 20:55 ` [RFC PATCH 02/16] firmware: arm_scmi: Make protocols init fail on basic errors Cristian Marussi
2022-02-27 20:55 ` [RFC PATCH 03/16] firmware: arm_scmi: Add multiple protocols registration support Cristian Marussi
2022-02-27 20:55 ` [RFC PATCH 04/16] firmware: arm_scmi: Add .version_get protocol operation Cristian Marussi
2022-02-27 20:55 ` [RFC PATCH 05/16] firmware: arm_scmi: Expose information on configured transport Cristian Marussi
2022-02-27 20:55 ` [RFC PATCH 06/16] firmware: arm_scmi: Define a common SCMI_MAX_PROTOCOLS value Cristian Marussi
2022-02-27 20:55 ` Cristian Marussi [this message]
2022-03-18 13:21   ` [RFC PATCH 07/16] debugfs: Add signed versions of debugfs_create_u32/64 helpers Greg Kroah-Hartman
2022-03-18 15:39     ` Cristian Marussi
2022-03-18 15:49       ` Greg Kroah-Hartman
2022-02-27 20:56 ` [RFC PATCH 08/16] firmware: arm_scmi: Add SCMI Testing driver Cristian Marussi
2022-02-27 20:56 ` [RFC PATCH 09/16] firmware: arm_scmi: testing: Add Clock protocol full support Cristian Marussi
2022-02-27 20:56 ` [RFC PATCH 10/16] firmware: arm_scmi: testing: Add Sensor protocol basic support Cristian Marussi
2022-02-27 20:56 ` [RFC PATCH 11/16] firmware: arm_scmi: Add test driver generic notification helpers Cristian Marussi
2022-02-27 20:56 ` [RFC PATCH 12/16] firmware: arm_scmi: Add Sensor notifications testing support Cristian Marussi
2022-02-27 20:56 ` [RFC PATCH 13/16] firmware: arm_scmi: Add testing Power protocol support Cristian Marussi
2022-02-27 20:56 ` [RFC PATCH 14/16] firmware: arm_scmi: Add testing Voltage " Cristian Marussi
2022-08-01  4:47   ` Arun KS
2022-08-01  6:44     ` Cristian Marussi
2022-02-27 20:56 ` [RFC PATCH 15/16] selftests: arm64: Add initial SCMI testcases Cristian Marussi
2022-08-01  4:52   ` Arun KS
2022-08-01  6:54     ` Cristian Marussi
     [not found]   ` <CAKZGPAMoBPiikbpNE2SjDxsUQQKZC-Yn5cbn60Sx0ZRqGv47jA@mail.gmail.com>
2022-08-01  7:01     ` Cristian Marussi
2022-02-27 20:56 ` [RFC PATCH 16/16] [DEBUG]: firmware: arm_scmi: Add Kconfig to allow SCMI Testing driver coexistence Cristian Marussi

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=20220227205608.30812-8-cristian.marussi@arm.com \
    --to=cristian.marussi@arm.com \
    --cc=Jonathan.Cameron@Huawei.com \
    --cc=f.fainelli@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=james.quinlan@broadcom.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peter.hilber@opensynergy.com \
    --cc=rafael@kernel.org \
    --cc=souvik.chakravarty@arm.com \
    --cc=sudeep.holla@arm.com \
    --cc=vincent.guittot@linaro.org \
    --cc=viro@zeniv.linux.org.uk \
    /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;
as well as URLs for NNTP newsgroup(s).