Repository navigation
Expand file tree
/
Copy pathcli.c
More file actions
426 lines (369 loc) · 14.9 KB
/
Copy pathcli.c
File metadata and controls
426 lines (369 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// The interactive driver. Commands arrive on the channel (io.c) one per line; each is answered with one
// JSON reply, {"timestamp":T,...} or {"timestamp":T,"error":"..."}, in the same ordered stream as
// nothing else. Machine state is not in the stream: after every command
// it is rewritten into a region of guest memory (shm.h) whose address the ready line reports, along
// with the trace events (a CPU's balancer looked for work; a task moved).
//
// Tasks are numbered 1, 2, .. in creation order; a number is never reused.
//
// create [n] n new runnable CFS tasks on CPUs 1..N-1 -> {"timestamp":T,"task":N}
// tick [n] advance n ticks (default 1)
// policy-fair <n> <normal|batch|idle> [-20..19] the class and its nice, as sched_setattr
// takes them; the nice left out is kept (and kept, inert, while rt)
// policy-rt <n> <fifo|rr> <1..99> the class and its priority, which the kernel takes together
// affinity <n> <cpulist> e.g. 1-2,4
// pause <n> / wake <n> sleep when the task next runs / wake it
// freeze <n> / thaw <n> freeze the task where it stands, under the freezer / thaw it
// block <n> like pause, in a freezable sleep (nanosleep), which the freezer takes
// chan-read <n> sleep in the channel until there is a byte, and take it
// chan-write <n> put a byte in, which sync-wakes one reader from this task’s CPU
// yield <n> sched_yield(): the task gives the CPU up but stays runnable
// kill <n> exit when the task next runs
// cgroup-create /a the parent must exist; / is the root
// cgroup-weight /a <w> cpu.weight, 1..10000 (default 100)
// cgroup-cpus /a <cpulist> cpuset.cpus
// cgroup-destroy /a it must hold no task and no child
// cgroup-attach /a <n> `cgroup-attach / <n>` moves the task back to the root
// cpu-topo KEY=g|g;... e.g. CPUS=4;CLS=1-2|3-4;CAP=2,4:512: the test CPU count, groups per level and CAP groups
// cpulist:scale; never CPU 0; rebuilds the sched domains (cpu.c)
// cpu-freq <cpu> <scale> current frequency, as cpufreq changes it under a running system
// check <name> enable a checker (checkers/); a rule that fires emits a warn record
// exit end the session (the VM reboots)
#include <linux/cgroup.h>
#include <linux/cpumask.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/version.h>
#include "driver.h"
#include "internal.h"
#include "shm.h"
#define LINE_MAX 128
// The reply to the command being executed: run() opens it, a command adds its fields (none on
// plain success), run() closes it once the shared region is updated.
static struct kstep_json reply;
static void reply_error(const char *msg) { kstep_json_field_str(&reply, "error", msg); }
// EOPNOTSUPP/EBUSY is cgroup v2's no-internal-process rule.
static void reply_errno(int err) {
char msg[48];
if (!err)
return;
if (err == -EOPNOTSUPP || err == -EBUSY)
return reply_error("cgroup cannot hold both tasks and controlled children");
scnprintf(msg, sizeof(msg), "failed (errno %d)", -err);
reply_error(msg);
}
// Consume one argument while preserving the rest for command-specific validation.
static char *take_arg(char **arg) {
char *token = *arg ? strsep(arg, " \t") : NULL;
if (*arg)
*arg = strim(*arg);
return token;
}
// The session's tasks by creation number: tasks[n - 1]. Numbering is this front-end's
// convention (the fuzz executor has its own); a number is never reused after `kill`.
static struct task_struct *tasks[KSTEP_SHM_TASKS];
static int ntasks;
static int task_number(struct task_struct *p) {
for (int i = 0; i < ntasks; i++)
if (tasks[i] == p)
return i + 1;
return 0;
}
// Take the leading task number off *arg (the rest stays in *arg) and look the task up; on
// failure reply with an error and return NULL.
static struct task_struct *parse_task(char **arg, const char *usage) {
char *num_s = take_arg(arg);
int n;
if (!num_s || kstrtoint(num_s, 10, &n) || n <= 0) {
reply_error(usage);
return NULL;
}
if (n > ntasks || tasks[n - 1]->exit_state) {
reply_error("no such task");
return NULL;
}
return tasks[n - 1];
}
static const struct {
const char *name;
int policy;
} fair_policies[] = {{"normal", SCHED_NORMAL}, {"batch", SCHED_BATCH}, {"idle", SCHED_IDLE}},
rt_policies[] = {{"fifo", SCHED_FIFO}, {"rr", SCHED_RR}};
static void cmd_tick(char *arg) {
int n = 1;
if (arg && (kstrtoint(arg, 10, &n) || n < 1 || n > 10000))
return reply_error("usage: tick [1..10000]");
kstep_tick_repeat(n);
}
static void cmd_affinity(char *arg) {
const char *usage = "usage: affinity <n> <cpulist within 1..CPUS>";
struct task_struct *p = parse_task(&arg, usage);
if (!p)
return;
int err = kstep_task_set_affinity(p, arg);
if (err == -ERANGE)
return reply_error(usage);
// sched_setaffinity rejects a mask disjoint from the task's cpuset (cgroup cpuset.cpus) with EINVAL.
if (err == -EINVAL)
return reply_error("cpulist has no CPU in the task's cgroup cpuset");
reply_errno(err);
}
// The two classes' verbs: each names a policy of its class and the one parameter that class
// reads, set together as sched_setattr sets them. A fair nice left out is kept; a real-time
// priority is required, as the kernel requires it.
static void cmd_policy_fair(char *arg) {
const char *usage = "usage: policy-fair <n> <normal|batch|idle> [-20..19]";
struct task_struct *p = parse_task(&arg, usage);
char *name = take_arg(&arg);
int nice;
if (!p)
return;
nice = task_nice(p); // left out: kept
if (arg && (kstrtoint(arg, 10, &nice) || nice < MIN_NICE || nice > MAX_NICE))
return reply_error(usage);
for (int i = 0; i < ARRAY_SIZE(fair_policies); i++)
if (name && !strcmp(name, fair_policies[i].name))
return kstep_task_set_fair(p, fair_policies[i].policy, nice);
reply_error(usage);
}
static void cmd_policy_rt(char *arg) {
const char *usage = "usage: policy-rt <n> <fifo|rr> <1..99>";
struct task_struct *p = parse_task(&arg, usage);
char *name = take_arg(&arg);
int prio;
if (!p)
return;
if (!arg || kstrtoint(arg, 10, &prio) || prio < 1 || prio > MAX_RT_PRIO - 1)
return reply_error(usage);
for (int i = 0; i < ARRAY_SIZE(rt_policies); i++)
if (name && !strcmp(name, rt_policies[i].name))
return kstep_task_set_rt(p, rt_policies[i].policy, prio);
reply_error(usage);
}
// Take the leading cgroup path off *arg; the kstep_cgroup_* helpers want it without the
// leading slash. It must exist unless `create`.
static const char *parse_cgroup(char **arg, const char *usage, bool create) {
char *path = take_arg(arg);
if (!path || path[0] != '/' || strstr(path, "..") || strlen(path) >= 48) {
reply_error(usage);
return NULL;
}
if (create ? (!path[1] || kstep_cgroup_exists(path + 1)) : (path[1] && !kstep_cgroup_exists(path + 1))) {
reply_error(create ? "cgroup exists" : "no such cgroup");
return NULL;
}
return path + 1;
}
// create [n] [/path]: n tasks (default 1), in the cgroup if one is named. The reply names the last
// one, so the caller knows the range. A task is created parked and unqueued, so moving it before
// its first wakeup puts that wakeup on the cgroup's queue: the scheduler never sees it at the root.
static void cmd_create(char *arg) {
const char *usage = "usage: create [n] [/path]";
const char *name = NULL;
int n = 1;
if (arg && arg[0] != '/' && kstrtoint(take_arg(&arg), 10, &n))
return reply_error(usage);
if (arg && !*arg)
arg = NULL;
if (arg && !(name = parse_cgroup(&arg, usage, false)))
return;
if (arg)
return reply_error(usage);
if (n < 1 || ntasks + n > KSTEP_SHM_TASKS)
return reply_error("too many tasks");
for (int i = 0; i < n; i++) {
struct task_struct *p = kstep_task_create();
int err = name ? kstep_cgroup_move_task(name, p->pid) : 0;
if (err) { // a task the caller was refused is not numbered: let it out of its park to exit
kstep_task_exit(p);
return reply_errno(err);
}
tasks[ntasks++] = p; // the task's kSTEP record keeps it referenced; created pinned to 1..N-1
kstep_task_wakeup(p);
}
kstep_json_field_s64(&reply, "task", ntasks);
}
static void cmd_cgroup_create(char *arg) {
const char *name = parse_cgroup(&arg, "usage: cgroup-create /path", true);
if (!name)
return;
reply_errno(kstep_cgroup_create(name));
}
static void cmd_cgroup_weight(char *arg) {
const char *usage = "usage: cgroup-weight /path <1..10000>";
const char *name = parse_cgroup(&arg, usage, false);
int weight;
if (!name)
return;
if (!arg || kstrtoint(arg, 10, &weight) || weight < 1 || weight > 10000)
return reply_error(usage);
reply_errno(kstep_cgroup_set_weight(name, weight));
}
static void cmd_cgroup_cpus(char *arg) {
const char *usage = "usage: cgroup-cpus /path <cpulist within 1..CPUS>";
const char *name = parse_cgroup(&arg, usage, false);
struct cpumask mask;
if (!name)
return;
if (!kstep_parse_cpus(arg, &mask))
return reply_error(usage);
reply_errno(kstep_cgroup_set_cpuset(name, arg));
}
static void cmd_cgroup_destroy(char *arg) {
const char *name = parse_cgroup(&arg, "usage: cgroup-destroy /path", false);
struct cgroup *cgrp;
bool busy;
if (!name)
return;
if (!*name)
return reply_error("cannot destroy the root cgroup");
cgrp = cgroup_get_from_path(name);
if (IS_ERR(cgrp))
return reply_error("no such cgroup");
busy = cgroup_is_populated(cgrp) || cgrp->nr_descendants; // children still offlining are not in the way
cgroup_put(cgrp);
if (busy)
return reply_error("cgroup has tasks or children");
kstep_cgroup_destroy(name);
}
static void cmd_cgroup_attach(char *arg) {
const char *usage = "usage: cgroup-attach /path <n>";
const char *name = parse_cgroup(&arg, usage, false);
struct task_struct *p;
if (!name || !(p = parse_task(&arg, usage)))
return;
reply_errno(kstep_cgroup_move_task(name, p->pid));
}
static void cmd_check(char *arg) {
if (!arg)
return reply_error("usage: check <name>");
if (kstep_check_enable(arg))
reply_error("no such check");
}
// The machine. Topology and capacity are what the hardware is, so a program sets them once,
// before it starts, in one cpu-topo spec (cpu.c); frequency changes under a running system, as
// cpufreq does, so cpu-freq takes one test CPU and a scale in 1..1024, like the task verbs take
// one task.
static void cmd_cpu_scale(char *arg, const char *usage, void (*set)(int cpu, int scale)) {
char *val = arg ? strchr(arg, ' ') : NULL;
int cpu, scale;
if (!val)
return reply_error(usage);
*val++ = '\0';
if (kstrtoint(arg, 10, &cpu) || cpu < 1 || cpu > kstep_test_ncpus || kstrtoint(val, 10, &scale) ||
scale < 1 || scale > SCHED_CAPACITY_SCALE)
return reply_error(usage);
set(cpu, scale);
}
static void cmd_cpu_freq(char *arg) {
cmd_cpu_scale(arg, "usage: cpu-freq <cpu> <1..1024>", kstep_freq_set);
}
static void cmd_cpu_topo(char *arg) {
if (!arg)
return reply_error("usage: cpu-topo KEY=group|group[;...] (levels, CAP and CPUS=K, see cpu.c)");
const char *err = kstep_topo_set(arg);
if (err)
reply_error(err);
}
static const struct {
const char *verb;
void (*fn)(char *arg);
} commands[] = {
{"create", cmd_create}, {"tick", cmd_tick},
{"cpu-topo", cmd_cpu_topo}, {"cpu-freq", cmd_cpu_freq},
{"policy-fair", cmd_policy_fair}, {"policy-rt", cmd_policy_rt}, {"affinity", cmd_affinity},
{"cgroup-create", cmd_cgroup_create}, {"cgroup-weight", cmd_cgroup_weight},
{"cgroup-cpus", cmd_cgroup_cpus}, {"cgroup-destroy", cmd_cgroup_destroy},
{"cgroup-attach", cmd_cgroup_attach},
{"check", cmd_check},
};
// Verbs that act on one task. Most queue an action on its control file (kmod/task.c) and it acts
// when it next reads; wake returns it from whatever it is asleep in; freeze and thaw are done to
// it, by the freezer, where it stands.
static const struct {
const char *verb;
void (*fn)(struct task_struct *p);
} task_verbs[] = {
{"pause", kstep_task_pause}, {"block", kstep_task_block},
{"wake", kstep_task_wakeup}, {"kill", kstep_task_exit},
{"chan-read", kstep_task_chan_read}, {"chan-write", kstep_task_chan_write},
{"freeze", kstep_freeze_task}, {"thaw", kstep_thaw_task},
{"yield", kstep_task_yield},
};
/* Returns false when the session should end. */
static bool execute(char *line) {
char *verb, *arg;
verb = strsep(&line, " \t");
arg = line ? strim(line) : NULL;
if (arg && !*arg)
arg = NULL;
for (int i = 0; i < ARRAY_SIZE(commands); i++)
if (!strcmp(verb, commands[i].verb)) {
commands[i].fn(arg);
return true;
}
for (int i = 0; i < ARRAY_SIZE(task_verbs); i++)
if (!strcmp(verb, task_verbs[i].verb)) {
char usage[32];
struct task_struct *p;
snprintf(usage, sizeof(usage), "usage: %s <n>", verb);
p = parse_task(&arg, usage);
if (p)
task_verbs[i].fn(p);
return true;
}
if (!strcmp(verb, "exit"))
return false;
reply_error("unknown command");
return true;
}
// A task migrate: one of our tasks moved (other processes are not reported)
static void report_migrate(struct task_struct *p, int src_cpu, int dst_cpu) {
int n = task_number(p);
if (n)
kstep_output_migrate(n, src_cpu, dst_cpu);
}
static phys_addr_t shm_phys;
// The session's tasks, for a rule that judges them as a set (checkers/).
struct task_struct **kstep_session_tasks(int *n) {
*n = ntasks;
return tasks;
}
// The session's own reporting. The rules register for what they watch as they are enabled
// (checkers/), so nothing here has to know which of them wants which event.
static void setup(void) {
kstep_on_balance_selected(kstep_output_balance);
kstep_on_task_migrate(report_migrate);
shm_phys = kstep_shm_init();
}
static void run(void) {
struct kstep_json json;
char line[LINE_MAX];
bool more = true;
// From here on, the session's scheduler coverage; 0 where the kernel has none, which is what
// tells a coverage-driven host (crates/fuzz) it is looking at the wrong kernel rather than at a run
// that found no edges.
phys_addr_t cov_phys = kstep_cov_init();
kstep_json_begin(&json);
kstep_json_field_bool(&json, "ready", true);
kstep_json_field_u64(&json, "shm", shm_phys); // guest-physical address of the state region
kstep_json_field_u64(&json, "cov", cov_phys); // and of the coverage map, allocated apart from it
kstep_json_end(&json);
while (more) {
char *l;
kstep_io_readline(line, sizeof(line));
l = strim(line);
if (!*l || *l == '#')
continue;
kstep_json_begin(&reply);
more = execute(l);
kstep_settle(); // the command has taken effect on every CPU before the state is read
kstep_shm_update(tasks, ntasks); // before the reply, so the region is current when it lands
kstep_json_end(&reply);
}
}
KSTEP_DRIVER_DEFINE{
.name = "cli",
.setup = setup,
.run = run,
};