lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Mon, 17 Jun 2024 17:34:42 -0700
From: Douglas Anderson <dianders@...omium.org>
To: Daniel Thompson <daniel.thompson@...aro.org>
Cc: kgdb-bugreport@...ts.sourceforge.net,
	Douglas Anderson <dianders@...omium.org>,
	Christophe JAILLET <christophe.jaillet@...adoo.fr>,
	Jason Wessel <jason.wessel@...driver.com>,
	Thorsten Blum <thorsten.blum@...lux.com>,
	Yuran Pereira <yuran.pereira@...mail.com>,
	linux-kernel@...r.kernel.org
Subject: [PATCH 08/13] kdb: In kdb_md() make `repeat` and `mdcount` calculations more obvious

In kdb_md(), the `mdcount` variable is the number of lines that the
"md" command will output.

In kdb_md(), the `repeat` variable is the number of "words" that the
"md" command will output.

The relationship between these two variables and how they are
specified is a bit convoluted.

You can adjust `mdcount` via the MDCOUNT environment variable. You can
then override the MDCOUNT environment variable by passing a number of
<lines> as an argument to the command.

You can adjust `repeat` using the `mdWcN` variant of the command where
"N" is the number of words to output.

The rules for how these get applied right now:
* By default, we'll the MDCOUNT environment variable.
* If `mdWcN` is used, the repeat will override.
* If <lines> is specified, the mdcount will override.
* When we're "repeating" a previous command (AKA argc is 0) then we'll
  load in the last_repeat.

If you've specified `repeat` then the `mdcount` can be calculated as:
  mdcount = DIV_ROUND_UP(repeat * bytes_per_word, bytes_per_line)

In other words, if you want to display 9 words, each word is 2 bytes,
and you want 16 bytes per line then you'll take up 2 lines. This would
look like:
  [1]kdb> md2c9 0xffffff80e000c340
  0xffffff80e000c340 0204 0000 0000 0000 e000 8235 0000 0000
  0xffffff80e000c350 0003

If you've specified `mdcount` then `repeat` is simply:
  repeat = mdcount * bytes_per_line / bytes_per_word

Let's make all this logic more obvious by initializing `repeat` to 0
and then setting it to non-zero when it should override. Then we can
do all the math at once.

While changing this, use the proper DIV_ROUND_UP() macro and introcue
a constant for KDB_MD_BYTES_PER_LINE. We'll also make and "if else"
more obvious so we know things always get initialized.

Signed-off-by: Douglas Anderson <dianders@...omium.org>
---

 kernel/debug/kdb/kdb_main.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
index 3c6fffa8509a..fcd5292351a7 100644
--- a/kernel/debug/kdb/kdb_main.c
+++ b/kernel/debug/kdb/kdb_main.c
@@ -1589,11 +1589,13 @@ static void kdb_md_line(const char *fmtstr, unsigned long addr,
 		   " ", cbuf);
 }
 
+#define KDB_MD_BYTES_PER_LINE	16
+
 static int kdb_md(int argc, const char **argv)
 {
 	static unsigned long last_addr;
 	static int last_radix, last_bytesperword, last_repeat;
-	int radix = 16, mdcount = 8, bytesperword = KDB_WORD_SIZE, repeat;
+	int radix = 16, mdcount = 8, bytesperword = KDB_WORD_SIZE, repeat = 0;
 	char fmtchar, fmtstr[64];
 	unsigned long addr;
 	unsigned long word;
@@ -1606,18 +1608,13 @@ static int kdb_md(int argc, const char **argv)
 	kdbgetintenv("RADIX", &radix);
 	kdbgetintenv("BYTESPERWORD", &bytesperword);
 
-	/* Assume 'md <addr>' and start with environment values */
-	repeat = mdcount * 16 / bytesperword;
-
 	if (isdigit(argv[0][2])) {
 		bytesperword = (int)(argv[0][2] - '0');
-		repeat = mdcount * 16 / bytesperword;
 		if (!argv[0][3])
 			valid = true;
 		else if (argv[0][3] == 'c' && argv[0][4]) {
 			char *p;
 			repeat = simple_strtoul(argv[0] + 4, &p, 10);
-			mdcount = ((repeat * bytesperword) + 15) / 16;
 			valid = !*p;
 		}
 	} else if (strcmp(argv[0], "md") == 0)
@@ -1637,10 +1634,7 @@ static int kdb_md(int argc, const char **argv)
 		radix = last_radix;
 		bytesperword = last_bytesperword;
 		repeat = last_repeat;
-		mdcount = ((repeat * bytesperword) + 15) / 16;
-	}
-
-	if (argc) {
+	} else {
 		unsigned long val;
 		int diag, nextarg = 1;
 		diag = kdbgetaddrarg(argc, argv, &nextarg, &addr);
@@ -1652,8 +1646,9 @@ static int kdb_md(int argc, const char **argv)
 		if (argc >= nextarg) {
 			diag = kdbgetularg(argv[nextarg], &val);
 			if (!diag) {
-				mdcount = (int) val;
-				repeat = mdcount * 16 / bytesperword;
+				mdcount = val;
+				/* Specifying <lines> overrides repeat count. */
+				repeat = 0;
 			}
 		}
 		if (argc >= nextarg+1) {
@@ -1699,6 +1694,13 @@ static int kdb_md(int argc, const char **argv)
 		return KDB_BADWIDTH;
 	}
 
+	/* If repeat is non-zero then it overrides */
+	if (repeat)
+		mdcount = DIV_ROUND_UP(repeat * bytesperword, KDB_MD_BYTES_PER_LINE);
+	else
+		repeat = mdcount * 16 / bytesperword;
+
+	/* Always just save `repeat` since `mdcount` can be calculated from it */
 	last_repeat = repeat;
 	last_bytesperword = bytesperword;
 
-- 
2.45.2.627.g7a2c4fd464-goog


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ