Skip to content

Commit d04213a

Browse files
author
Daniel Thompson
committed
kdb: Simplify code to fetch characters from console
Currently kdb_read_get_key() contains complex control flow that, on close inspection, turns out to be unnecessary. In particular: 1. It is impossible to enter the branch conditioned on (escape_delay == 1) except when the loop enters with (escape_delay == 2) allowing us to combine the branches. 2. Most of the code conditioned on (escape_delay == 2) simply modifies local data and then breaks out of the loop causing the function to return escape_data[0]. 3. Based on #2 there is not actually any need to ever explicitly set escape_delay to 2 because we it is much simpler to directly return escape_data[0] instead. 4. escape_data[0] is, for all but one exit path, known to be '\e'. Simplify the code based on these observations. There is a subtle (and harmless) change of behaviour resulting from this simplification: instead of letting the escape timeout after ~1998 milliseconds we now timeout after ~2000 milliseconds Signed-off-by: Daniel Thompson <[email protected]> Reviewed-by: Douglas Anderson <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 53b6313 commit d04213a

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

kernel/debug/kdb/kdb_io.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -124,53 +124,43 @@ static int kdb_read_get_key(char *buffer, size_t bufsize)
124124
touch_nmi_watchdog();
125125
f = &kdb_poll_funcs[0];
126126
}
127-
if (escape_delay == 2) {
128-
*ped = '\0';
129-
ped = escape_data;
130-
--escape_delay;
131-
}
132-
if (escape_delay == 1) {
133-
key = *ped++;
134-
if (!*ped)
135-
--escape_delay;
136-
break;
137-
}
127+
138128
key = (*f)();
129+
139130
if (key == -1) {
140131
if (escape_delay) {
141132
udelay(ESCAPE_UDELAY);
142-
--escape_delay;
133+
if (--escape_delay == 0)
134+
return '\e';
143135
}
144136
continue;
145137
}
138+
146139
if (bufsize <= 2) {
147140
if (key == '\r')
148141
key = '\n';
149142
*buffer++ = key;
150143
*buffer = '\0';
151144
return -1;
152145
}
146+
153147
if (escape_delay == 0 && key == '\e') {
154148
escape_delay = ESCAPE_DELAY;
155149
ped = escape_data;
156150
f_escape = f;
157151
}
158152
if (escape_delay) {
159-
*ped++ = key;
160-
if (f_escape != f) {
161-
escape_delay = 2;
162-
continue;
163-
}
153+
if (f_escape != f)
154+
return '\e';
164155

156+
*ped++ = key;
165157
key = kdb_handle_escape(escape_data, ped - escape_data);
166-
if (key > 0) {
167-
escape_data[0] = key;
168-
escape_data[1] = '\0';
169-
}
170-
if (key)
171-
escape_delay = 2;
172-
continue;
158+
if (key < 0)
159+
return '\e';
160+
if (key == 0)
161+
continue;
173162
}
163+
174164
break; /* A key to process */
175165
}
176166
return key;

0 commit comments

Comments
 (0)