Skip to contentSkip to conversation

Sunrpc: Supports hexadecimal number for sysctl files of sunrpc debug

PATCH Superseded Kinglong Mee 3 participants 7 messages Sep 10, 2015 – Nov 3, 2015

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Sunrpc: Supports hexadecimal number for sysctl files of sunrpc debug

The sunrpc debug sysctl files only accept decimal number right now. But all the XXXDBUG_XXX macros are defined as hexadecimal. It is not easy to set or check an separate flag.

This patch let those files support accepting hexadecimal number, (decimal number is also supported). Also, display it as hexadecimal.

---
 net/sunrpc/sysctl.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)
net/sunrpc/sysctl.c +15 -11
index 887f018..8e85e6f 100644
@@ -76,7 +76,7 @@ static int
 proc_dodebug(struct ctl_table *table, int write,
 				void __user *buffer, size_t *lenp, loff_t *ppos)
 {
-	char		tmpbuf[20], c, *s;
+	char		tmpbuf[20], c, *s = NULL;
 	char __user *p;
 	unsigned int	value;
 	size_t		left, len;
@@ -103,23 +103,27 @@ proc_dodebug(struct ctl_table *table, int write,
 			return -EFAULT;
 		tmpbuf[left] = '\0';
 
-		for (s = tmpbuf, value = 0; '0' <= *s && *s <= '9'; s++, left--)
-			value = 10 * value + (*s - '0');
-		if (*s && !isspace(*s))
-			return -EINVAL;
-		while (left && isspace(*s))
-			left--, s++;
+		if (tmpbuf[0] == '0' && tmpbuf[1] == 'x')
+			value = simple_strtol(tmpbuf, &s, 16);
+		else
+			value = simple_strtol(tmpbuf, &s, 0);
+		if (s) {
+			if (!isspace(*s))
+				return -EINVAL;
+			left -= (s - tmpbuf);
+			while (left && isspace(*s))
+				left--, s++;
+		} else
+			left = 0;
 		*(unsigned int *) table->data = value;
 		/* Display the RPC tasks on writing to rpc_debug */
 		if (strcmp(table->procname, "rpc_debug") == 0)
 			rpc_show_tasks(&init_net);
 	} else {
-		if (!access_ok(VERIFY_WRITE, buffer, left))
-			return -EFAULT;
-		len = sprintf(tmpbuf, "%d", *(unsigned int *) table->data);
+		len = sprintf(tmpbuf, "0x%04x", *(unsigned int *) table->data);
 		if (len > left)
 			len = left;
-		if (__copy_to_user(buffer, tmpbuf, len))
+		if (copy_to_user(buffer, tmpbuf, len))
 			return -EFAULT;
 		if ((left -= len) > 0) {
 			if (put_user('\n', (char __user *)buffer + len))
Signature · 1 line
2.5.0
#1 lore raw
Quoted 6 lines · Kinglong Mee

The sunrpc debug sysctl files only accept decimal number right now. But all the XXXDBUG_XXX macros are defined as hexadecimal. It is not easy to set or check an separate flag.

This patch let those files support accepting hexadecimal number, (decimal number is also supported). Also, display it as hexadecimal.

That potentially breaks backwards-compatibility if an program that reads this file isn't prepared to accept a hexadecimal value.

That said, rpcdebug seems OK (it uses strtoul(.,.,0), which I think should parse that fine?), and it may well be the only program that actually parses this, so.... I suppose it's OK with me if it's OK with Trond.

--b.

Quoted 59 lines
---
 net/sunrpc/sysctl.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)
net/sunrpc/sysctl.c +15 -11
index 887f018..8e85e6f 100644
@@ -76,7 +76,7 @@ static int
 proc_dodebug(struct ctl_table *table, int write,
 				void __user *buffer, size_t *lenp, loff_t *ppos)
 {
-	char		tmpbuf[20], c, *s;
+	char		tmpbuf[20], c, *s = NULL;
 	char __user *p;
 	unsigned int	value;
 	size_t		left, len;
@@ -103,23 +103,27 @@ proc_dodebug(struct ctl_table *table, int write,
 			return -EFAULT;
 		tmpbuf[left] = '\0';
 
-		for (s = tmpbuf, value = 0; '0' <= *s && *s <= '9'; s++, left--)
-			value = 10 * value + (*s - '0');
-		if (*s && !isspace(*s))
-			return -EINVAL;
-		while (left && isspace(*s))
-			left--, s++;
+		if (tmpbuf[0] == '0' && tmpbuf[1] == 'x')
+			value = simple_strtol(tmpbuf, &s, 16);
+		else
+			value = simple_strtol(tmpbuf, &s, 0);
+		if (s) {
+			if (!isspace(*s))
+				return -EINVAL;
+			left -= (s - tmpbuf);
+			while (left && isspace(*s))
+				left--, s++;
+		} else
+			left = 0;
 		*(unsigned int *) table->data = value;
 		/* Display the RPC tasks on writing to rpc_debug */
 		if (strcmp(table->procname, "rpc_debug") == 0)
 			rpc_show_tasks(&init_net);
 	} else {
-		if (!access_ok(VERIFY_WRITE, buffer, left))
-			return -EFAULT;
-		len = sprintf(tmpbuf, "%d", *(unsigned int *) table->data);
+		len = sprintf(tmpbuf, "0x%04x", *(unsigned int *) table->data);
 		if (len > left)
 			len = left;
-		if (__copy_to_user(buffer, tmpbuf, len))
+		if (copy_to_user(buffer, tmpbuf, len))
 			return -EFAULT;
 		if ((left -= len) > 0) {
 			if (put_user('\n', (char __user *)buffer + len))
Signature · 1 line
2.5.0
#2 lore raw
Quoted 8 lines · J. Bruce Fields
Quoted 6 lines · Kinglong Mee

The sunrpc debug sysctl files only accept decimal number right now. But all the XXXDBUG_XXX macros are defined as hexadecimal. It is not easy to set or check an separate flag.

This patch let those files support accepting hexadecimal number, (decimal number is also supported). Also, display it as hexadecimal.

Quoted 7 lines

That potentially breaks backwards-compatibility if an program that reads this file isn't prepared to accept a hexadecimal value.

That said, rpcdebug seems OK (it uses strtoul(.,.,0), which I think should parse that fine?), and it may well be the only program that actually parses this, so.... I suppose it's OK with me if it's OK with Trond.

Thanks for your comments.
I have test of rpcdebug, it's OK when showing the debug flags.

Quoted 26 lines

--b.

Quoted 23 lines
---
 net/sunrpc/sysctl.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)
net/sunrpc/sysctl.c +1 -1
index 887f018..8e85e6f 100644
@@ -76,7 +76,7 @@ static int
 proc_dodebug(struct ctl_table *table, int write,
 				void __user *buffer, size_t *lenp, loff_t *ppos)
 {
-	char		tmpbuf[20], c, *s;
+	char		tmpbuf[20], c, *s = NULL;
 	char __user *p;
 	unsigned int	value;
 	size_t		left, len;
@@ -103,23 +103,27 @@ proc_dodebug(struct ctl_table *table, int write,
 			return -EFAULT;
 		tmpbuf[left] = '\0';
 
Quoted 8 lines

net/sunrpc/sysctl.c proc_dodebug(struct ctl_table *table, int write, · line 107

Quoted 8 lines
net/sunrpc/sysctl.c +2 -6
-		for (s = tmpbuf, value = 0; '0' <= *s && *s <= '9'; s++, left--)
-			value = 10 * value + (*s - '0');
-		if (*s && !isspace(*s))
-			return -EINVAL;
-		while (left && isspace(*s))
-			left--, s++;
+		if (tmpbuf[0] == '0' && tmpbuf[1] == 'x')
+			value = simple_strtol(tmpbuf, &s, 16);

It's a duplicate of this.

Quoted 4 lines

net/sunrpc/sysctl.c proc_dodebug(struct ctl_table *table, int write, · line 111

Quoted 4 lines
net/sunrpc/sysctl.c +4 -0
+		else
+			value = simple_strtol(tmpbuf, &s, 0);
+		if (s) {
+			if (!isspace(*s))

It's a bug of checking '\0' here.

I will fix those problems in new version.

thanks,
Kinglong Mee

Quoted 25 lines
Quoted 24 lines
net/sunrpc/sysctl.c +8 -4
+				return -EINVAL;
+			left -= (s - tmpbuf);
+			while (left && isspace(*s))
+				left--, s++;
+		} else
+			left = 0;
 		*(unsigned int *) table->data = value;
 		/* Display the RPC tasks on writing to rpc_debug */
 		if (strcmp(table->procname, "rpc_debug") == 0)
 			rpc_show_tasks(&init_net);
 	} else {
-		if (!access_ok(VERIFY_WRITE, buffer, left))
-			return -EFAULT;
-		len = sprintf(tmpbuf, "%d", *(unsigned int *) table->data);
+		len = sprintf(tmpbuf, "0x%04x", *(unsigned int *) table->data);
 		if (len > left)
 			len = left;
-		if (__copy_to_user(buffer, tmpbuf, len))
+		if (copy_to_user(buffer, tmpbuf, len))
 			return -EFAULT;
 		if ((left -= len) > 0) {
 			if (put_user('\n', (char __user *)buffer + len))
Signature · 1 line
2.5.0
#3 lore raw

PATCH v2 1/1 Sunrpc: Supports hexadecimal number for sysctl files of sunrpc debug

The sunrpc debug sysctl files only accept decimal number right now. But all the XXXDBUG_XXX macros are defined as hexadecimal. It is not easy to set or check an separate flag.

This patch let those files support accepting hexadecimal number, (decimal number is also supported). Also, display it as hexadecimal.

v2,
Remove duplicate parsing of '0x...', just using simple_strtol(tmpbuf, &s, 0) Fix a bug of isspace() checking after parsing

---
 net/sunrpc/sysctl.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
net/sunrpc/sysctl.c +12 -11
index 887f018..c88d9bc 100644
@@ -76,7 +76,7 @@ static int
 proc_dodebug(struct ctl_table *table, int write,
 				void __user *buffer, size_t *lenp, loff_t *ppos)
 {
-	char		tmpbuf[20], c, *s;
+	char		tmpbuf[20], c, *s = NULL;
 	char __user *p;
 	unsigned int	value;
 	size_t		left, len;
@@ -103,23 +103,24 @@ proc_dodebug(struct ctl_table *table, int write,
 			return -EFAULT;
 		tmpbuf[left] = '\0';
 
-		for (s = tmpbuf, value = 0; '0' <= *s && *s <= '9'; s++, left--)
-			value = 10 * value + (*s - '0');
-		if (*s && !isspace(*s))
-			return -EINVAL;
-		while (left && isspace(*s))
-			left--, s++;
+		value = simple_strtol(tmpbuf, &s, 0);
+		if (s) {
+			left -= (s - tmpbuf);
+			if (left && !isspace(*s))
+				return -EINVAL;
+			while (left && isspace(*s))
+				left--, s++;
+		} else
+			left = 0;
 		*(unsigned int *) table->data = value;
 		/* Display the RPC tasks on writing to rpc_debug */
 		if (strcmp(table->procname, "rpc_debug") == 0)
 			rpc_show_tasks(&init_net);
 	} else {
-		if (!access_ok(VERIFY_WRITE, buffer, left))
-			return -EFAULT;
-		len = sprintf(tmpbuf, "%d", *(unsigned int *) table->data);
+		len = sprintf(tmpbuf, "0x%04x", *(unsigned int *) table->data);
 		if (len > left)
 			len = left;
-		if (__copy_to_user(buffer, tmpbuf, len))
+		if (copy_to_user(buffer, tmpbuf, len))
 			return -EFAULT;
 		if ((left -= len) > 0) {
 			if (put_user('\n', (char __user *)buffer + len))
Signature · 1 line
2.5.0
#4 lore raw
Quoted 65 lines · Kinglong Mee

The sunrpc debug sysctl files only accept decimal number right now. But all the XXXDBUG_XXX macros are defined as hexadecimal. It is not easy to set or check an separate flag.

This patch let those files support accepting hexadecimal number, (decimal number is also supported). Also, display it as hexadecimal.

v2,
Remove duplicate parsing of '0x...', just using simple_strtol(tmpbuf, &s, 0) Fix a bug of isspace() checking after parsing

---
 net/sunrpc/sysctl.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
net/sunrpc/sysctl.c +12 -12
index 887f018..c88d9bc 100644
@@ -76,7 +76,7 @@ static int
 proc_dodebug(struct ctl_table *table, int write,
                                void __user *buffer, size_t *lenp, loff_t *ppos)
 {
-       char            tmpbuf[20], c, *s;
+       char            tmpbuf[20], c, *s = NULL;
        char __user *p;
        unsigned int    value;
        size_t          left, len;
@@ -103,23 +103,24 @@ proc_dodebug(struct ctl_table *table, int write,
                        return -EFAULT;
                tmpbuf[left] = '\0';
 
-               for (s = tmpbuf, value = 0; '0' <= *s && *s <= '9'; s++, left--)
-                       value = 10 * value + (*s - '0');
-               if (*s && !isspace(*s))
-                       return -EINVAL;
-               while (left && isspace(*s))
-                       left--, s++;
+               value = simple_strtol(tmpbuf, &s, 0);
+               if (s) {
+                       left -= (s - tmpbuf);
+                       if (left && !isspace(*s))
+                               return -EINVAL;
+                       while (left && isspace(*s))
+                               left--, s++;
+               } else
+                       left = 0;
                *(unsigned int *) table->data = value;
                /* Display the RPC tasks on writing to rpc_debug */
                if (strcmp(table->procname, "rpc_debug") == 0)
                        rpc_show_tasks(&init_net);
        } else {
-               if (!access_ok(VERIFY_WRITE, buffer, left))
-                       return -EFAULT;
-               len = sprintf(tmpbuf, "%d", *(unsigned int *) table->data);
+               len = sprintf(tmpbuf, "0x%04x", *(unsigned int *) table->data);
                if (len > left)
                        len = left;
-               if (__copy_to_user(buffer, tmpbuf, len))
+               if (copy_to_user(buffer, tmpbuf, len))
                        return -EFAULT;
                if ((left -= len) > 0) {
                        if (put_user('\n', (char __user *)buffer + len))
--
Quoted 2 lines

2.5.0

Bruce, did you take this patch, or is there an expectation that I should do so?

Cheers
Trond

#5 lore raw
Quoted 62 lines · Trond Myklebust
Quoted 61 lines · Kinglong Mee

The sunrpc debug sysctl files only accept decimal number right now. But all the XXXDBUG_XXX macros are defined as hexadecimal. It is not easy to set or check an separate flag.

This patch let those files support accepting hexadecimal number, (decimal number is also supported). Also, display it as hexadecimal.

v2,
Remove duplicate parsing of '0x...', just using simple_strtol(tmpbuf, &s, 0) Fix a bug of isspace() checking after parsing

---
 net/sunrpc/sysctl.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
net/sunrpc/sysctl.c +12 -11
index 887f018..c88d9bc 100644
@@ -76,7 +76,7 @@ static int
 proc_dodebug(struct ctl_table *table, int write,
                                void __user *buffer, size_t *lenp, loff_t *ppos)
 {
-       char            tmpbuf[20], c, *s;
+       char            tmpbuf[20], c, *s = NULL;
        char __user *p;
        unsigned int    value;
        size_t          left, len;
@@ -103,23 +103,24 @@ proc_dodebug(struct ctl_table *table, int write,
                        return -EFAULT;
                tmpbuf[left] = '\0';
 
-               for (s = tmpbuf, value = 0; '0' <= *s && *s <= '9'; s++, left--)
-                       value = 10 * value + (*s - '0');
-               if (*s && !isspace(*s))
-                       return -EINVAL;
-               while (left && isspace(*s))
-                       left--, s++;
+               value = simple_strtol(tmpbuf, &s, 0);
+               if (s) {
+                       left -= (s - tmpbuf);
+                       if (left && !isspace(*s))
+                               return -EINVAL;
+                       while (left && isspace(*s))
+                               left--, s++;
+               } else
+                       left = 0;
                *(unsigned int *) table->data = value;
                /* Display the RPC tasks on writing to rpc_debug */
                if (strcmp(table->procname, "rpc_debug") == 0)
                        rpc_show_tasks(&init_net);
        } else {
-               if (!access_ok(VERIFY_WRITE, buffer, left))
-                       return -EFAULT;
-               len = sprintf(tmpbuf, "%d", *(unsigned int *) table->data);
+               len = sprintf(tmpbuf, "0x%04x", *(unsigned int *) table->data);
                if (len > left)
                        len = left;
-               if (__copy_to_user(buffer, tmpbuf, len))
+               if (copy_to_user(buffer, tmpbuf, len))
Quoted 8 lines
Quoted 6 lines
net/sunrpc/sysctl.c +0 -1
                        return -EFAULT;
                if ((left -= len) > 0) {
                        if (put_user('\n', (char __user *)buffer + len))
--

2.5.0

Bruce, did you take this patch, or is there an expectation that I should do so?

Patch looked OK to me, but I didn't take it, could you?

--b.

#6 lore raw
Quoted 72 lines · J. Bruce Fields
Quoted 62 lines · Trond Myklebust
Quoted 61 lines · Kinglong Mee

The sunrpc debug sysctl files only accept decimal number right now. But all the XXXDBUG_XXX macros are defined as hexadecimal. It is not easy to set or check an separate flag.

This patch let those files support accepting hexadecimal number, (decimal number is also supported). Also, display it as hexadecimal.

v2,
Remove duplicate parsing of '0x...', just using simple_strtol(tmpbuf, &s, 0) Fix a bug of isspace() checking after parsing

---
 net/sunrpc/sysctl.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
net/sunrpc/sysctl.c +12 -11
index 887f018..c88d9bc 100644
@@ -76,7 +76,7 @@ static int
 proc_dodebug(struct ctl_table *table, int write,
                                void __user *buffer, size_t *lenp, loff_t *ppos)
 {
-       char            tmpbuf[20], c, *s;
+       char            tmpbuf[20], c, *s = NULL;
        char __user *p;
        unsigned int    value;
        size_t          left, len;
@@ -103,23 +103,24 @@ proc_dodebug(struct ctl_table *table, int write,
                        return -EFAULT;
                tmpbuf[left] = '\0';
 
-               for (s = tmpbuf, value = 0; '0' <= *s && *s <= '9'; s++, left--)
-                       value = 10 * value + (*s - '0');
-               if (*s && !isspace(*s))
-                       return -EINVAL;
-               while (left && isspace(*s))
-                       left--, s++;
+               value = simple_strtol(tmpbuf, &s, 0);
+               if (s) {
+                       left -= (s - tmpbuf);
+                       if (left && !isspace(*s))
+                               return -EINVAL;
+                       while (left && isspace(*s))
+                               left--, s++;
+               } else
+                       left = 0;
                *(unsigned int *) table->data = value;
                /* Display the RPC tasks on writing to rpc_debug */
                if (strcmp(table->procname, "rpc_debug") == 0)
                        rpc_show_tasks(&init_net);
        } else {
-               if (!access_ok(VERIFY_WRITE, buffer, left))
-                       return -EFAULT;
-               len = sprintf(tmpbuf, "%d", *(unsigned int *) table->data);
+               len = sprintf(tmpbuf, "0x%04x", *(unsigned int *) table->data);
                if (len > left)
                        len = left;
-               if (__copy_to_user(buffer, tmpbuf, len))
+               if (copy_to_user(buffer, tmpbuf, len))
Quoted 8 lines
Quoted 6 lines
net/sunrpc/sysctl.c +0 -1
                        return -EFAULT;
                if ((left -= len) > 0) {
                        if (put_user('\n', (char __user *)buffer + len))
--

2.5.0

Bruce, did you take this patch, or is there an expectation that I should do so?

Quoted 2 lines

Patch looked OK to me, but I didn't take it, could you?

Sure. I'll pick it up.