Skip to contentSkip to conversation

dyndbg: 2 fixes/cleanups for 5.9

PATCH In review Jim Cromie 4 participants 8 messages Sep 21, 2020 – Sep 27, 2020

Subsystems (2)
lore
Share & tools
lore

Jim Cromie

author maintainer
#0 lore raw

cover dyndbg: 2 fixes/cleanups for 5.9

2 things which might qualify as fixes
a bad (reverted) patch hit a BUG_ON, change that to return -EINVAL instead keep useful part of the reverted patch, use keyword, arg varnames

Jim Cromie (2):
dyndbg: dont panic over bad input
dyndbg: use keyword, arg varnames for query term pairs

 lib/dynamic_debug.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
Signature · 1 line
2.26.2
author maintainer
#1 lore raw

PATCH 1/2 dyndbg: dont panic over bad input

This BUG_ON, from 2009, caught the impossible case of a word-char both starting and ending a string (loosely speaking). A bad (reverted) patch finally hit this case, but even "impossibly bad input" is no reason to panic the kernel. Instead pr_err and return -EINVAL.

---
 lib/dynamic_debug.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/dynamic_debug.c +4 -1
index 2d4dfd44b0fa5..90ddf07ce34fe 100644
@@ -259,7 +259,10 @@ static int ddebug_tokenize(char *buf, char *words[], int maxwords)
 		} else {
 			for (end = buf; *end && !isspace(*end); end++)
 				;
-			BUG_ON(end == buf);
+			if (end == buf) {
+				pr_err("expected non-empty bareword");
+				return -EINVAL;
+			}
 		}
 
 		/* `buf' is start of word, `end' is one past its end */
Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH 1/2] dyndbg: dont panic over bad input

Quoted 4 lines · Jim Cromie

This BUG_ON, from 2009, caught the impossible case of a word-char both starting and ending a string (loosely speaking). A bad (reverted) patch finally hit this case, but even "impossibly bad input" is no reason to panic the kernel. Instead pr_err and return -EINVAL.

[]

lib/dynamic_debug.c

Quoted 1 line
lib/dynamic_debug.c +0 -0

[]

lib/dynamic_debug.c static int ddebug_tokenize(char *buf, char *words[], int maxwords) · line 263

Quoted 7 lines
lib/dynamic_debug.c +2 -1
@@ -259,7 +259,10 @@ static int ddebug_tokenize(char *buf, char *words[], int maxwords)
 		} else {
 			for (end = buf; *end && !isspace(*end); end++)
 				;
-			BUG_ON(end == buf);
+			if (end == buf) {
+				pr_err("expected non-empty bareword");

missing newline

This message is also unintelligible.
What is a non-empty bareword?

author maintainer
#3 lore raw

Re: [PATCH 1/2] dyndbg: dont panic over bad input

Quoted 19 lines · Joe Perches
Quoted 4 lines · Jim Cromie

This BUG_ON, from 2009, caught the impossible case of a word-char both starting and ending a string (loosely speaking). A bad (reverted) patch finally hit this case, but even "impossibly bad input" is no reason to panic the kernel. Instead pr_err and return -EINVAL.

[]

lib/dynamic_debug.c

Quoted 1 line
lib/dynamic_debug.c +0 -0

[]

lib/dynamic_debug.c static int ddebug_tokenize(char *buf, char *words[], int maxwords) · line 263

Quoted 7 lines
lib/dynamic_debug.c +2 -1
@@ -259,7 +259,10 @@ static int ddebug_tokenize(char *buf, char *words[], int maxwords)
              } else {
                      for (end = buf; *end && !isspace(*end); end++)
                              ;
-                     BUG_ON(end == buf);
+                     if (end == buf) {
+                             pr_err("expected non-empty bareword");

missing newline

Quoted 4 lines

This message is also unintelligible.
What is a non-empty bareword?

hmm, I borrowed the term from perl

en.wiktionary.org › wiki › bareword
(programming, chiefly Perl) A sequence of text characters in source code that do not form a keyword nor part of a quoted string, and may potentially be interpreted ...

basically, a not-quoted word, a keyword or not-quoted-value

Im open that there might be better terminology.
have any suggestions ?

#4 lore raw

Re: [PATCH 1/2] dyndbg: dont panic over bad input

In reply to Jim Cromie
Quoted 16 lines · Jim Cromie

This BUG_ON, from 2009, caught the impossible case of a word-char both starting and ending a string (loosely speaking). A bad (reverted) patch finally hit this case, but even "impossibly bad input" is no reason to panic the kernel. Instead pr_err and return -EINVAL.

---
 lib/dynamic_debug.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/dynamic_debug.c +0 -0
index 2d4dfd44b0fa5..90ddf07ce34fe 100644

lib/dynamic_debug.c static int ddebug_tokenize(char *buf, char *words[], int maxwords) · line 264

Quoted 7 lines
lib/dynamic_debug.c +3 -1
@@ -259,7 +259,10 @@ static int ddebug_tokenize(char *buf, char *words[], int maxwords)
 		} else {
 			for (end = buf; *end && !isspace(*end); end++)
 				;
-			BUG_ON(end == buf);
+			if (end == buf) {
+				pr_err("expected non-empty bareword");
+				return -EINVAL;

My understanding is that the BUG_ON() was there to catch eventual bugs in the algorithm.

IMHO, it was never reachable in the original code:

1. The following lines will skip all spaces and bail out when we reached the trailing '\0':

buf = skip_spaces(buf);
if (!*buf)
break; /* oh, it was trailing whitespace */

2. The following code will find the end of a quoted text:

		if (*buf == '"' || *buf == '\'') {
			int quote = *buf++;
			for (end = buf; *end && *end != quote; end++)


    3. The else part will find end of non-quoted word:

		} else {
			for (end = buf; *end && !isspace(*end); end++)

    4. The BUG_ON() will trigger when the above cycle reached the
       trailing '\0' or space.

       This will never happen because this situation was caught
       in the 1st step.

Your patch triggered the BUG_ON() because it wanted to handle '=' as a special character and was incomplete.

If you want to handle '=' special way. You need to do it the same way as with the space. You need to skip it in 1st step. And it must mark the end of the word in 4th step.

But it will be more complicated. You must be able to handle mix of spaces and '='. I mean the following situations:

word=word
word =word
word= word
word = word
word = = word /* failure ? */

Back to the BUG_ON(). It might be changed to something like:

	pr_err("Internal error when parsing dynamic debug query\n");
	return -EINVAL;

Best Regards,
Petr

author maintainer
#5 lore raw

Re: [PATCH 1/2] dyndbg: dont panic over bad input

Quoted 75 lines · Petr Mladek
Quoted 16 lines · Jim Cromie

This BUG_ON, from 2009, caught the impossible case of a word-char both starting and ending a string (loosely speaking). A bad (reverted) patch finally hit this case, but even "impossibly bad input" is no reason to panic the kernel. Instead pr_err and return -EINVAL.

---
 lib/dynamic_debug.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/dynamic_debug.c +0 -0
index 2d4dfd44b0fa5..90ddf07ce34fe 100644

lib/dynamic_debug.c static int ddebug_tokenize(char *buf, char *words[], int maxwords) · line 264

Quoted 7 lines
lib/dynamic_debug.c +3 -1
@@ -259,7 +259,10 @@ static int ddebug_tokenize(char *buf, char *words[], int maxwords)
              } else {
                      for (end = buf; *end && !isspace(*end); end++)
                              ;
-                     BUG_ON(end == buf);
+                     if (end == buf) {
+                             pr_err("expected non-empty bareword");
+                             return -EINVAL;

My understanding is that the BUG_ON() was there to catch eventual bugs in the algorithm.

IMHO, it was never reachable in the original code:

1. The following lines will skip all spaces and bail out when we reached the trailing '\0':

buf = skip_spaces(buf);
if (!*buf)
break; /* oh, it was trailing whitespace */

2. The following code will find the end of a quoted text:

if (*buf == '"' || *buf == '\'') {
int quote = *buf++;
for (end = buf; *end && *end != quote; end++)

3. The else part will find end of non-quoted word:

} else {
for (end = buf; *end && !isspace(*end); end++)

4. The BUG_ON() will trigger when the above cycle reached the trailing '\0' or space.

This will never happen because this situation was caught in the 1st step.

Your patch triggered the BUG_ON() because it wanted to handle '=' as a special character and was incomplete.

If you want to handle '=' special way. You need to do it the same way as with the space. You need to skip it in 1st step. And it must mark the end of the word in 4th step.

But it will be more complicated. You must be able to handle mix of spaces and '='. I mean the following situations:

word=word
word =word
word= word
word = word
word = = word /* failure ? */

Quoted 8 lines

Back to the BUG_ON(). It might be changed to something like:

        pr_err("Internal error when parsing dynamic debug query\n");
        return -EINVAL;

Best Regards,
Petr

yes, the original patch was ill conceived

Im blaming Transient Acute Myopia,
where I couldnt see to the end of the line, where "=flags" is a proper flag setting.

That "interferes" with using '=' to separate keyword=value.

As you outlined, its possible to implement something that handles both, but I decided that handling keyword=value is just a convenience feature, and isnt worth the added corner-cases and explanatory burden.

#6 lore raw

Re: [PATCH 1/2] dyndbg: dont panic over bad input

In reply to Jim Cromie
Quoted 16 lines · Jim Cromie

This BUG_ON, from 2009, caught the impossible case of a word-char both starting and ending a string (loosely speaking). A bad (reverted) patch finally hit this case, but even "impossibly bad input" is no reason to panic the kernel. Instead pr_err and return -EINVAL.

---
 lib/dynamic_debug.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
lib/dynamic_debug.c +0 -0
index 2d4dfd44b0fa5..90ddf07ce34fe 100644

lib/dynamic_debug.c static int ddebug_tokenize(char *buf, char *words[], int maxwords) · line 263

Quoted 6 lines
lib/dynamic_debug.c +2 -1
@@ -259,7 +259,10 @@ static int ddebug_tokenize(char *buf, char *words[], int maxwords)
 		} else {
 			for (end = buf; *end && !isspace(*end); end++)
 				;
-			BUG_ON(end == buf);
+			if (end == buf) {
+				pr_err("expected non-empty bareword");

Need a "\n" here, right?

thanks,

greg k-h

author maintainer
#7 lore raw

PATCH 2/2 dyndbg: use keyword, arg varnames for query term pairs

In reply to Jim Cromie

optimize for clarity by replacing word[i,i+1] refs with temps. no functional changes.

---
 lib/dynamic_debug.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)
lib/dynamic_debug.c +15 -12
index 90ddf07ce34fe..23e7dd967b0e6 100644
@@ -387,10 +387,13 @@ static int ddebug_parse_query(char *words[], int nwords,
 		query->module = modname;
 
 	for (i = 0; i < nwords; i += 2) {
-		if (!strcmp(words[i], "func")) {
-			rc = check_set(&query->function, words[i+1], "func");
-		} else if (!strcmp(words[i], "file")) {
-			if (check_set(&query->filename, words[i+1], "file"))
+		char *keyword = words[i];
+		char *arg = words[i+1];
+
+		if (!strcmp(keyword, "func")) {
+			rc = check_set(&query->function, arg, "func");
+		} else if (!strcmp(keyword, "file")) {
+			if (check_set(&query->filename, arg, "file"))
 				return -EINVAL;
 
 			/* tail :$info is function or line-range */
@@ -406,18 +409,18 @@ static int ddebug_parse_query(char *words[], int nwords,
 				if (parse_linerange(query, fline))
 					return -EINVAL;
 			}
-		} else if (!strcmp(words[i], "module")) {
-			rc = check_set(&query->module, words[i+1], "module");
-		} else if (!strcmp(words[i], "format")) {
-			string_unescape_inplace(words[i+1], UNESCAPE_SPACE |
+		} else if (!strcmp(keyword, "module")) {
+			rc = check_set(&query->module, arg, "module");
+		} else if (!strcmp(keyword, "format")) {
+			string_unescape_inplace(arg, UNESCAPE_SPACE |
 							    UNESCAPE_OCTAL |
 							    UNESCAPE_SPECIAL);
-			rc = check_set(&query->format, words[i+1], "format");
-		} else if (!strcmp(words[i], "line")) {
-			if (parse_linerange(query, words[i+1]))
+			rc = check_set(&query->format, arg, "format");
+		} else if (!strcmp(keyword, "line")) {
+			if (parse_linerange(query, arg))
 				return -EINVAL;
 		} else {
-			pr_err("unknown keyword \"%s\"\n", words[i]);
+			pr_err("unknown keyword \"%s\"\n", keyword);
 			return -EINVAL;
 		}
 		if (rc)
Signature · 1 line
2.26.2