Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rv: Add Runtime Verification (RV) interface

RV is a lightweight (yet rigorous) method that complements classical
exhaustive verification techniques (such as model checking and
theorem proving) with a more practical approach to complex systems.

RV works by analyzing the trace of the system's actual execution,
comparing it against a formal specification of the system behavior.
RV can give precise information on the runtime behavior of the
monitored system while enabling the reaction for unexpected
events, avoiding, for example, the propagation of a failure on
safety-critical systems.

The development of this interface roots in the development of the
paper:

De Oliveira, Daniel Bristot; Cucinotta, Tommaso; De Oliveira, Romulo
Silva. Efficient formal verification for the Linux kernel. In:
International Conference on Software Engineering and Formal Methods.
Springer, Cham, 2019. p. 315-332.

And:

De Oliveira, Daniel Bristot. Automata-based formal analysis
and verification of the real-time Linux kernel. PhD Thesis, 2020.

The RV interface resembles the tracing/ interface on purpose. The current
path for the RV interface is /sys/kernel/tracing/rv/.

It presents these files:

"available_monitors"
- List the available monitors, one per line.

For example:
# cat available_monitors
wip
wwnr

"enabled_monitors"
- Lists the enabled monitors, one per line;
- Writing to it enables a given monitor;
- Writing a monitor name with a '!' prefix disables it;
- Truncating the file disables all enabled monitors.

For example:
# cat enabled_monitors
# echo wip > enabled_monitors
# echo wwnr >> enabled_monitors
# cat enabled_monitors
wip
wwnr
# echo '!wip' >> enabled_monitors
# cat enabled_monitors
wwnr
# echo > enabled_monitors
# cat enabled_monitors
#

Note that more than one monitor can be enabled concurrently.

"monitoring_on"
- It is an on/off general switcher for monitoring. Note
that it does not disable enabled monitors or detach events,
but stop the per-entity monitors of monitoring the events
received from the system. It resembles the "tracing_on" switcher.

"monitors/"
Each monitor will have its one directory inside "monitors/". There
the monitor specific files will be presented.
The "monitors/" directory resembles the "events" directory on
tracefs.

For example:
# cd monitors/wip/
# ls
desc enable
# cat desc
wakeup in preemptive per-cpu testing monitor.
# cat enable
0

For further information, see the comments in the header of
kernel/trace/rv/rv.c from this patch.

Link: https://lkml.kernel.org/r/a4bfe038f50cb047bfb343ad0e12b0e646ab308b.1659052063.git.bristot@kernel.org

Cc: Wim Van Sebroeck <wim@linux-watchdog.org>
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Will Deacon <will@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Marco Elver <elver@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Shuah Khan <skhan@linuxfoundation.org>
Cc: Gabriele Paoloni <gpaoloni@redhat.com>
Cc: Juri Lelli <juri.lelli@redhat.com>
Cc: Clark Williams <williams@redhat.com>
Cc: Tao Zhou <tao.zhou@linux.dev>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: linux-doc@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-trace-devel@vger.kernel.org
Signed-off-by: Daniel Bristot de Oliveira <bristot@kernel.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

authored by

Daniel Bristot de Oliveira and committed by
Steven Rostedt (Google)
102227b9 ac6c1b2c

+898
+43
include/linux/rv.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* 3 + * Runtime Verification. 4 + * 5 + * For futher information, see: kernel/trace/rv/rv.c. 6 + */ 7 + #ifndef _LINUX_RV_H 8 + #define _LINUX_RV_H 9 + 10 + #ifdef CONFIG_RV 11 + 12 + /* 13 + * Per-task RV monitors count. Nowadays fixed in RV_PER_TASK_MONITORS. 14 + * If we find justification for more monitors, we can think about 15 + * adding more or developing a dynamic method. So far, none of 16 + * these are justified. 17 + */ 18 + #define RV_PER_TASK_MONITORS 1 19 + #define RV_PER_TASK_MONITOR_INIT (RV_PER_TASK_MONITORS) 20 + 21 + /* 22 + * Futher monitor types are expected, so make this a union. 23 + */ 24 + union rv_task_monitor { 25 + }; 26 + 27 + struct rv_monitor { 28 + const char *name; 29 + const char *description; 30 + bool enabled; 31 + int (*enable)(void); 32 + void (*disable)(void); 33 + void (*reset)(void); 34 + }; 35 + 36 + bool rv_monitoring_on(void); 37 + int rv_unregister_monitor(struct rv_monitor *monitor); 38 + int rv_register_monitor(struct rv_monitor *monitor); 39 + int rv_get_task_monitor_slot(void); 40 + void rv_put_task_monitor_slot(int slot); 41 + 42 + #endif /* CONFIG_RV */ 43 + #endif /* _LINUX_RV_H */
+11
include/linux/sched.h
··· 34 34 #include <linux/rseq.h> 35 35 #include <linux/seqlock.h> 36 36 #include <linux/kcsan.h> 37 + #include <linux/rv.h> 37 38 #include <asm/kmap_size.h> 38 39 39 40 /* task_struct member predeclarations (sorted alphabetically): */ ··· 1499 1498 * cores 1500 1499 */ 1501 1500 struct callback_head l1d_flush_kill; 1501 + #endif 1502 + 1503 + #ifdef CONFIG_RV 1504 + /* 1505 + * Per-task RV monitor. Nowadays fixed in RV_PER_TASK_MONITORS. 1506 + * If we find justification for more monitors, we can think 1507 + * about adding more or developing a dynamic method. So far, 1508 + * none of these are justified. 1509 + */ 1510 + union rv_task_monitor rv[RV_PER_TASK_MONITORS]; 1502 1511 #endif 1503 1512 1504 1513 /*
+2
kernel/trace/Kconfig
··· 1106 1106 1107 1107 If unsure, say N. 1108 1108 1109 + source "kernel/trace/rv/Kconfig" 1110 + 1109 1111 endif # FTRACE
+1
kernel/trace/Makefile
··· 106 106 obj-$(CONFIG_RETHOOK) += rethook.o 107 107 108 108 obj-$(CONFIG_TRACEPOINT_BENCHMARK) += trace_benchmark.o 109 + obj-$(CONFIG_RV) += rv/ 109 110 110 111 libftrace-y := ftrace.o
+12
kernel/trace/rv/Kconfig
··· 1 + # SPDX-License-Identifier: GPL-2.0-only 2 + # 3 + menuconfig RV 4 + bool "Runtime Verification" 5 + depends on TRACING 6 + help 7 + Enable the kernel runtime verification infrastructure. RV is a 8 + lightweight (yet rigorous) method that complements classical 9 + exhaustive verification techniques (such as model checking and 10 + theorem proving). RV works by analyzing the trace of the system's 11 + actual execution, comparing it against a formal specification of 12 + the system behavior.
+3
kernel/trace/rv/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + 3 + obj-$(CONFIG_RV) += rv.o
+782
kernel/trace/rv/rv.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Copyright (C) 2019-2022 Red Hat, Inc. Daniel Bristot de Oliveira <bristot@kernel.org> 4 + * 5 + * This is the online Runtime Verification (RV) interface. 6 + * 7 + * RV is a lightweight (yet rigorous) method that complements classical 8 + * exhaustive verification techniques (such as model checking and 9 + * theorem proving) with a more practical approach to complex systems. 10 + * 11 + * RV works by analyzing the trace of the system's actual execution, 12 + * comparing it against a formal specification of the system behavior. 13 + * RV can give precise information on the runtime behavior of the 14 + * monitored system while enabling the reaction for unexpected 15 + * events, avoiding, for example, the propagation of a failure on 16 + * safety-critical systems. 17 + * 18 + * The development of this interface roots in the development of the 19 + * paper: 20 + * 21 + * De Oliveira, Daniel Bristot; Cucinotta, Tommaso; De Oliveira, Romulo 22 + * Silva. Efficient formal verification for the Linux kernel. In: 23 + * International Conference on Software Engineering and Formal Methods. 24 + * Springer, Cham, 2019. p. 315-332. 25 + * 26 + * And: 27 + * 28 + * De Oliveira, Daniel Bristot, et al. Automata-based formal analysis 29 + * and verification of the real-time Linux kernel. PhD Thesis, 2020. 30 + * 31 + * == Runtime monitor interface == 32 + * 33 + * A monitor is the central part of the runtime verification of a system. 34 + * 35 + * The monitor stands in between the formal specification of the desired 36 + * (or undesired) behavior, and the trace of the actual system. 37 + * 38 + * In Linux terms, the runtime verification monitors are encapsulated 39 + * inside the "RV monitor" abstraction. A RV monitor includes a reference 40 + * model of the system, a set of instances of the monitor (per-cpu monitor, 41 + * per-task monitor, and so on), and the helper functions that glue the 42 + * monitor to the system via trace. Generally, a monitor includes some form 43 + * of trace output as a reaction for event parsing and exceptions, 44 + * as depicted bellow: 45 + * 46 + * Linux +----- RV Monitor ----------------------------------+ Formal 47 + * Realm | | Realm 48 + * +-------------------+ +----------------+ +-----------------+ 49 + * | Linux kernel | | Monitor | | Reference | 50 + * | Tracing | -> | Instance(s) | <- | Model | 51 + * | (instrumentation) | | (verification) | | (specification) | 52 + * +-------------------+ +----------------+ +-----------------+ 53 + * | | | 54 + * | V | 55 + * | +----------+ | 56 + * | | Reaction | | 57 + * | +--+--+--+-+ | 58 + * | | | | | 59 + * | | | +-> trace output ? | 60 + * +------------------------|--|----------------------+ 61 + * | +----> panic ? 62 + * +-------> <user-specified> 63 + * 64 + * This file implements the interface for loading RV monitors, and 65 + * to control the verification session. 66 + * 67 + * == Registering monitors == 68 + * 69 + * The struct rv_monitor defines a set of callback functions to control 70 + * a verification session. For instance, when a given monitor is enabled, 71 + * the "enable" callback function is called to hook the instrumentation 72 + * functions to the kernel trace events. The "disable" function is called 73 + * when disabling the verification session. 74 + * 75 + * A RV monitor is registered via: 76 + * int rv_register_monitor(struct rv_monitor *monitor); 77 + * And unregistered via: 78 + * int rv_unregister_monitor(struct rv_monitor *monitor); 79 + * 80 + * == User interface == 81 + * 82 + * The user interface resembles kernel tracing interface. It presents 83 + * these files: 84 + * 85 + * "available_monitors" 86 + * - List the available monitors, one per line. 87 + * 88 + * For example: 89 + * # cat available_monitors 90 + * wip 91 + * wwnr 92 + * 93 + * "enabled_monitors" 94 + * - Lists the enabled monitors, one per line; 95 + * - Writing to it enables a given monitor; 96 + * - Writing a monitor name with a '!' prefix disables it; 97 + * - Truncating the file disables all enabled monitors. 98 + * 99 + * For example: 100 + * # cat enabled_monitors 101 + * # echo wip > enabled_monitors 102 + * # echo wwnr >> enabled_monitors 103 + * # cat enabled_monitors 104 + * wip 105 + * wwnr 106 + * # echo '!wip' >> enabled_monitors 107 + * # cat enabled_monitors 108 + * wwnr 109 + * # echo > enabled_monitors 110 + * # cat enabled_monitors 111 + * # 112 + * 113 + * Note that more than one monitor can be enabled concurrently. 114 + * 115 + * "monitoring_on" 116 + * - It is an on/off general switcher for monitoring. Note 117 + * that it does not disable enabled monitors or detach events, 118 + * but stops the per-entity monitors from monitoring the events 119 + * received from the instrumentation. It resembles the "tracing_on" 120 + * switcher. 121 + * 122 + * "monitors/" 123 + * Each monitor will have its own directory inside "monitors/". There 124 + * the monitor specific files will be presented. 125 + * The "monitors/" directory resembles the "events" directory on 126 + * tracefs. 127 + * 128 + * For example: 129 + * # cd monitors/wip/ 130 + * # ls 131 + * desc enable 132 + * # cat desc 133 + * auto-generated wakeup in preemptive monitor. 134 + * # cat enable 135 + * 0 136 + */ 137 + 138 + #include <linux/kernel.h> 139 + #include <linux/module.h> 140 + #include <linux/init.h> 141 + #include <linux/slab.h> 142 + 143 + #include "rv.h" 144 + 145 + DEFINE_MUTEX(rv_interface_lock); 146 + 147 + static struct rv_interface rv_root; 148 + 149 + struct dentry *get_monitors_root(void) 150 + { 151 + return rv_root.monitors_dir; 152 + } 153 + 154 + /* 155 + * Interface for the monitor register. 156 + */ 157 + static LIST_HEAD(rv_monitors_list); 158 + 159 + static int task_monitor_count; 160 + static bool task_monitor_slots[RV_PER_TASK_MONITORS]; 161 + 162 + int rv_get_task_monitor_slot(void) 163 + { 164 + int i; 165 + 166 + lockdep_assert_held(&rv_interface_lock); 167 + 168 + if (task_monitor_count == RV_PER_TASK_MONITORS) 169 + return -EBUSY; 170 + 171 + task_monitor_count++; 172 + 173 + for (i = 0; i < RV_PER_TASK_MONITORS; i++) { 174 + if (task_monitor_slots[i] == false) { 175 + task_monitor_slots[i] = true; 176 + return i; 177 + } 178 + } 179 + 180 + WARN_ONCE(1, "RV task_monitor_count and slots are out of sync\n"); 181 + 182 + return -EINVAL; 183 + } 184 + 185 + void rv_put_task_monitor_slot(int slot) 186 + { 187 + lockdep_assert_held(&rv_interface_lock); 188 + 189 + if (slot < 0 || slot >= RV_PER_TASK_MONITORS) { 190 + WARN_ONCE(1, "RV releasing an invalid slot!: %d\n", slot); 191 + return; 192 + } 193 + 194 + WARN_ONCE(!task_monitor_slots[slot], "RV releasing unused task_monitor_slots: %d\n", 195 + slot); 196 + 197 + task_monitor_count--; 198 + task_monitor_slots[slot] = false; 199 + } 200 + 201 + /* 202 + * This section collects the monitor/ files and folders. 203 + */ 204 + static ssize_t monitor_enable_read_data(struct file *filp, char __user *user_buf, size_t count, 205 + loff_t *ppos) 206 + { 207 + struct rv_monitor_def *mdef = filp->private_data; 208 + const char *buff; 209 + 210 + buff = mdef->monitor->enabled ? "1\n" : "0\n"; 211 + 212 + return simple_read_from_buffer(user_buf, count, ppos, buff, strlen(buff)+1); 213 + } 214 + 215 + /* 216 + * __rv_disable_monitor - disabled an enabled monitor 217 + */ 218 + static int __rv_disable_monitor(struct rv_monitor_def *mdef, bool sync) 219 + { 220 + lockdep_assert_held(&rv_interface_lock); 221 + 222 + if (mdef->monitor->enabled) { 223 + mdef->monitor->enabled = 0; 224 + mdef->monitor->disable(); 225 + 226 + /* 227 + * Wait for the execution of all events to finish. 228 + * Otherwise, the data used by the monitor could 229 + * be inconsistent. i.e., if the monitor is re-enabled. 230 + */ 231 + if (sync) 232 + tracepoint_synchronize_unregister(); 233 + return 1; 234 + } 235 + return 0; 236 + } 237 + 238 + /** 239 + * rv_disable_monitor - disable a given runtime monitor 240 + * 241 + * Returns 0 on success. 242 + */ 243 + int rv_disable_monitor(struct rv_monitor_def *mdef) 244 + { 245 + __rv_disable_monitor(mdef, true); 246 + return 0; 247 + } 248 + 249 + /** 250 + * rv_enable_monitor - enable a given runtime monitor 251 + * 252 + * Returns 0 on success, error otherwise. 253 + */ 254 + int rv_enable_monitor(struct rv_monitor_def *mdef) 255 + { 256 + int retval; 257 + 258 + lockdep_assert_held(&rv_interface_lock); 259 + 260 + if (mdef->monitor->enabled) 261 + return 0; 262 + 263 + retval = mdef->monitor->enable(); 264 + 265 + if (!retval) 266 + mdef->monitor->enabled = 1; 267 + 268 + return retval; 269 + } 270 + 271 + /* 272 + * interface for enabling/disabling a monitor. 273 + */ 274 + static ssize_t monitor_enable_write_data(struct file *filp, const char __user *user_buf, 275 + size_t count, loff_t *ppos) 276 + { 277 + struct rv_monitor_def *mdef = filp->private_data; 278 + int retval; 279 + bool val; 280 + 281 + retval = kstrtobool_from_user(user_buf, count, &val); 282 + if (retval) 283 + return retval; 284 + 285 + retval = count; 286 + 287 + mutex_lock(&rv_interface_lock); 288 + 289 + if (val) 290 + retval = rv_enable_monitor(mdef); 291 + else 292 + retval = rv_disable_monitor(mdef); 293 + 294 + mutex_unlock(&rv_interface_lock); 295 + 296 + return retval ? : count; 297 + } 298 + 299 + static const struct file_operations interface_enable_fops = { 300 + .open = simple_open, 301 + .llseek = no_llseek, 302 + .write = monitor_enable_write_data, 303 + .read = monitor_enable_read_data, 304 + }; 305 + 306 + /* 307 + * Interface to read monitors description. 308 + */ 309 + static ssize_t monitor_desc_read_data(struct file *filp, char __user *user_buf, size_t count, 310 + loff_t *ppos) 311 + { 312 + struct rv_monitor_def *mdef = filp->private_data; 313 + char buff[256]; 314 + 315 + memset(buff, 0, sizeof(buff)); 316 + 317 + snprintf(buff, sizeof(buff), "%s\n", mdef->monitor->description); 318 + 319 + return simple_read_from_buffer(user_buf, count, ppos, buff, strlen(buff) + 1); 320 + } 321 + 322 + static const struct file_operations interface_desc_fops = { 323 + .open = simple_open, 324 + .llseek = no_llseek, 325 + .read = monitor_desc_read_data, 326 + }; 327 + 328 + /* 329 + * During the registration of a monitor, this function creates 330 + * the monitor dir, where the specific options of the monitor 331 + * are exposed. 332 + */ 333 + static int create_monitor_dir(struct rv_monitor_def *mdef) 334 + { 335 + struct dentry *root = get_monitors_root(); 336 + const char *name = mdef->monitor->name; 337 + struct dentry *tmp; 338 + int retval; 339 + 340 + mdef->root_d = rv_create_dir(name, root); 341 + if (!mdef->root_d) 342 + return -ENOMEM; 343 + 344 + tmp = rv_create_file("enable", RV_MODE_WRITE, mdef->root_d, mdef, &interface_enable_fops); 345 + if (!tmp) { 346 + retval = -ENOMEM; 347 + goto out_remove_root; 348 + } 349 + 350 + tmp = rv_create_file("desc", RV_MODE_READ, mdef->root_d, mdef, &interface_desc_fops); 351 + if (!tmp) { 352 + retval = -ENOMEM; 353 + goto out_remove_root; 354 + } 355 + 356 + return 0; 357 + 358 + out_remove_root: 359 + rv_remove(mdef->root_d); 360 + return retval; 361 + } 362 + 363 + /* 364 + * Available/Enable monitor shared seq functions. 365 + */ 366 + static int monitors_show(struct seq_file *m, void *p) 367 + { 368 + struct rv_monitor_def *mon_def = p; 369 + 370 + seq_printf(m, "%s\n", mon_def->monitor->name); 371 + return 0; 372 + } 373 + 374 + /* 375 + * Used by the seq file operations at the end of a read 376 + * operation. 377 + */ 378 + static void monitors_stop(struct seq_file *m, void *p) 379 + { 380 + mutex_unlock(&rv_interface_lock); 381 + } 382 + 383 + /* 384 + * Available monitor seq functions. 385 + */ 386 + static void *available_monitors_start(struct seq_file *m, loff_t *pos) 387 + { 388 + mutex_lock(&rv_interface_lock); 389 + return seq_list_start(&rv_monitors_list, *pos); 390 + } 391 + 392 + static void *available_monitors_next(struct seq_file *m, void *p, loff_t *pos) 393 + { 394 + return seq_list_next(p, &rv_monitors_list, pos); 395 + } 396 + 397 + /* 398 + * Enable monitor seq functions. 399 + */ 400 + static void *enabled_monitors_next(struct seq_file *m, void *p, loff_t *pos) 401 + { 402 + struct rv_monitor_def *m_def = p; 403 + 404 + (*pos)++; 405 + 406 + list_for_each_entry_continue(m_def, &rv_monitors_list, list) { 407 + if (m_def->monitor->enabled) 408 + return m_def; 409 + } 410 + 411 + return NULL; 412 + } 413 + 414 + static void *enabled_monitors_start(struct seq_file *m, loff_t *pos) 415 + { 416 + struct rv_monitor_def *m_def; 417 + loff_t l; 418 + 419 + mutex_lock(&rv_interface_lock); 420 + 421 + if (list_empty(&rv_monitors_list)) 422 + return NULL; 423 + 424 + m_def = list_entry(&rv_monitors_list, struct rv_monitor_def, list); 425 + 426 + for (l = 0; l <= *pos; ) { 427 + m_def = enabled_monitors_next(m, m_def, &l); 428 + if (!m_def) 429 + break; 430 + } 431 + 432 + return m_def; 433 + } 434 + 435 + /* 436 + * available/enabled monitors seq definition. 437 + */ 438 + static const struct seq_operations available_monitors_seq_ops = { 439 + .start = available_monitors_start, 440 + .next = available_monitors_next, 441 + .stop = monitors_stop, 442 + .show = monitors_show 443 + }; 444 + 445 + static const struct seq_operations enabled_monitors_seq_ops = { 446 + .start = enabled_monitors_start, 447 + .next = enabled_monitors_next, 448 + .stop = monitors_stop, 449 + .show = monitors_show 450 + }; 451 + 452 + /* 453 + * available_monitors interface. 454 + */ 455 + static int available_monitors_open(struct inode *inode, struct file *file) 456 + { 457 + return seq_open(file, &available_monitors_seq_ops); 458 + }; 459 + 460 + static const struct file_operations available_monitors_ops = { 461 + .open = available_monitors_open, 462 + .read = seq_read, 463 + .llseek = seq_lseek, 464 + .release = seq_release 465 + }; 466 + 467 + /* 468 + * enabled_monitors interface. 469 + */ 470 + static void disable_all_monitors(void) 471 + { 472 + struct rv_monitor_def *mdef; 473 + int enabled = 0; 474 + 475 + mutex_lock(&rv_interface_lock); 476 + 477 + list_for_each_entry(mdef, &rv_monitors_list, list) 478 + enabled += __rv_disable_monitor(mdef, false); 479 + 480 + if (enabled) { 481 + /* 482 + * Wait for the execution of all events to finish. 483 + * Otherwise, the data used by the monitor could 484 + * be inconsistent. i.e., if the monitor is re-enabled. 485 + */ 486 + tracepoint_synchronize_unregister(); 487 + } 488 + 489 + mutex_unlock(&rv_interface_lock); 490 + } 491 + 492 + static int enabled_monitors_open(struct inode *inode, struct file *file) 493 + { 494 + if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) 495 + disable_all_monitors(); 496 + 497 + return seq_open(file, &enabled_monitors_seq_ops); 498 + }; 499 + 500 + static ssize_t enabled_monitors_write(struct file *filp, const char __user *user_buf, 501 + size_t count, loff_t *ppos) 502 + { 503 + char buff[MAX_RV_MONITOR_NAME_SIZE + 2]; 504 + struct rv_monitor_def *mdef; 505 + int retval = -EINVAL; 506 + bool enable = true; 507 + char *ptr = buff; 508 + int len; 509 + 510 + if (count < 1 || count > MAX_RV_MONITOR_NAME_SIZE + 1) 511 + return -EINVAL; 512 + 513 + memset(buff, 0, sizeof(buff)); 514 + 515 + retval = simple_write_to_buffer(buff, sizeof(buff) - 1, ppos, user_buf, count); 516 + if (retval < 0) 517 + return -EFAULT; 518 + 519 + ptr = strim(buff); 520 + 521 + if (ptr[0] == '!') { 522 + enable = false; 523 + ptr++; 524 + } 525 + 526 + len = strlen(ptr); 527 + if (!len) 528 + return count; 529 + 530 + mutex_lock(&rv_interface_lock); 531 + 532 + retval = -EINVAL; 533 + 534 + list_for_each_entry(mdef, &rv_monitors_list, list) { 535 + if (strcmp(ptr, mdef->monitor->name) != 0) 536 + continue; 537 + 538 + /* 539 + * Monitor found! 540 + */ 541 + if (enable) 542 + retval = rv_enable_monitor(mdef); 543 + else 544 + retval = rv_disable_monitor(mdef); 545 + 546 + if (!retval) 547 + retval = count; 548 + 549 + break; 550 + } 551 + 552 + mutex_unlock(&rv_interface_lock); 553 + return retval; 554 + } 555 + 556 + static const struct file_operations enabled_monitors_ops = { 557 + .open = enabled_monitors_open, 558 + .read = seq_read, 559 + .write = enabled_monitors_write, 560 + .llseek = seq_lseek, 561 + .release = seq_release, 562 + }; 563 + 564 + /* 565 + * Monitoring on global switcher! 566 + */ 567 + static bool __read_mostly monitoring_on; 568 + 569 + /** 570 + * rv_monitoring_on - checks if monitoring is on 571 + * 572 + * Returns 1 if on, 0 otherwise. 573 + */ 574 + bool rv_monitoring_on(void) 575 + { 576 + /* Ensures that concurrent monitors read consistent monitoring_on */ 577 + smp_rmb(); 578 + return READ_ONCE(monitoring_on); 579 + } 580 + 581 + /* 582 + * monitoring_on general switcher. 583 + */ 584 + static ssize_t monitoring_on_read_data(struct file *filp, char __user *user_buf, 585 + size_t count, loff_t *ppos) 586 + { 587 + const char *buff; 588 + 589 + buff = rv_monitoring_on() ? "1\n" : "0\n"; 590 + 591 + return simple_read_from_buffer(user_buf, count, ppos, buff, strlen(buff) + 1); 592 + } 593 + 594 + static void turn_monitoring_off(void) 595 + { 596 + WRITE_ONCE(monitoring_on, false); 597 + /* Ensures that concurrent monitors read consistent monitoring_on */ 598 + smp_wmb(); 599 + } 600 + 601 + static void reset_all_monitors(void) 602 + { 603 + struct rv_monitor_def *mdef; 604 + 605 + list_for_each_entry(mdef, &rv_monitors_list, list) { 606 + if (mdef->monitor->enabled) 607 + mdef->monitor->reset(); 608 + } 609 + } 610 + 611 + static void turn_monitoring_on(void) 612 + { 613 + WRITE_ONCE(monitoring_on, true); 614 + /* Ensures that concurrent monitors read consistent monitoring_on */ 615 + smp_wmb(); 616 + } 617 + 618 + static void turn_monitoring_on_with_reset(void) 619 + { 620 + lockdep_assert_held(&rv_interface_lock); 621 + 622 + if (rv_monitoring_on()) 623 + return; 624 + 625 + /* 626 + * Monitors might be out of sync with the system if events were not 627 + * processed because of !rv_monitoring_on(). 628 + * 629 + * Reset all monitors, forcing a re-sync. 630 + */ 631 + reset_all_monitors(); 632 + turn_monitoring_on(); 633 + } 634 + 635 + static ssize_t monitoring_on_write_data(struct file *filp, const char __user *user_buf, 636 + size_t count, loff_t *ppos) 637 + { 638 + int retval; 639 + bool val; 640 + 641 + retval = kstrtobool_from_user(user_buf, count, &val); 642 + if (retval) 643 + return retval; 644 + 645 + mutex_lock(&rv_interface_lock); 646 + 647 + if (val) 648 + turn_monitoring_on_with_reset(); 649 + else 650 + turn_monitoring_off(); 651 + 652 + /* 653 + * Wait for the execution of all events to finish 654 + * before returning to user-space. 655 + */ 656 + tracepoint_synchronize_unregister(); 657 + 658 + mutex_unlock(&rv_interface_lock); 659 + 660 + return count; 661 + } 662 + 663 + static const struct file_operations monitoring_on_fops = { 664 + .open = simple_open, 665 + .llseek = no_llseek, 666 + .write = monitoring_on_write_data, 667 + .read = monitoring_on_read_data, 668 + }; 669 + 670 + static void destroy_monitor_dir(struct rv_monitor_def *mdef) 671 + { 672 + rv_remove(mdef->root_d); 673 + } 674 + 675 + /** 676 + * rv_register_monitor - register a rv monitor. 677 + * @monitor: The rv_monitor to be registered. 678 + * 679 + * Returns 0 if successful, error otherwise. 680 + */ 681 + int rv_register_monitor(struct rv_monitor *monitor) 682 + { 683 + struct rv_monitor_def *r; 684 + int retval = 0; 685 + 686 + if (strlen(monitor->name) >= MAX_RV_MONITOR_NAME_SIZE) { 687 + pr_info("Monitor %s has a name longer than %d\n", monitor->name, 688 + MAX_RV_MONITOR_NAME_SIZE); 689 + return -1; 690 + } 691 + 692 + mutex_lock(&rv_interface_lock); 693 + 694 + list_for_each_entry(r, &rv_monitors_list, list) { 695 + if (strcmp(monitor->name, r->monitor->name) == 0) { 696 + pr_info("Monitor %s is already registered\n", monitor->name); 697 + retval = -1; 698 + goto out_unlock; 699 + } 700 + } 701 + 702 + r = kzalloc(sizeof(struct rv_monitor_def), GFP_KERNEL); 703 + if (!r) { 704 + retval = -ENOMEM; 705 + goto out_unlock; 706 + } 707 + 708 + r->monitor = monitor; 709 + 710 + retval = create_monitor_dir(r); 711 + if (retval) { 712 + kfree(r); 713 + goto out_unlock; 714 + } 715 + 716 + list_add_tail(&r->list, &rv_monitors_list); 717 + 718 + out_unlock: 719 + mutex_unlock(&rv_interface_lock); 720 + return retval; 721 + } 722 + 723 + /** 724 + * rv_unregister_monitor - unregister a rv monitor. 725 + * @monitor: The rv_monitor to be unregistered. 726 + * 727 + * Returns 0 if successful, error otherwise. 728 + */ 729 + int rv_unregister_monitor(struct rv_monitor *monitor) 730 + { 731 + struct rv_monitor_def *ptr, *next; 732 + 733 + mutex_lock(&rv_interface_lock); 734 + 735 + list_for_each_entry_safe(ptr, next, &rv_monitors_list, list) { 736 + if (strcmp(monitor->name, ptr->monitor->name) == 0) { 737 + rv_disable_monitor(ptr); 738 + list_del(&ptr->list); 739 + destroy_monitor_dir(ptr); 740 + } 741 + } 742 + 743 + mutex_unlock(&rv_interface_lock); 744 + return 0; 745 + } 746 + 747 + int __init rv_init_interface(void) 748 + { 749 + struct dentry *tmp; 750 + 751 + rv_root.root_dir = rv_create_dir("rv", NULL); 752 + if (!rv_root.root_dir) 753 + goto out_err; 754 + 755 + rv_root.monitors_dir = rv_create_dir("monitors", rv_root.root_dir); 756 + if (!rv_root.monitors_dir) 757 + goto out_err; 758 + 759 + tmp = rv_create_file("available_monitors", RV_MODE_READ, rv_root.root_dir, NULL, 760 + &available_monitors_ops); 761 + if (!tmp) 762 + goto out_err; 763 + 764 + tmp = rv_create_file("enabled_monitors", RV_MODE_WRITE, rv_root.root_dir, NULL, 765 + &enabled_monitors_ops); 766 + if (!tmp) 767 + goto out_err; 768 + 769 + tmp = rv_create_file("monitoring_on", RV_MODE_WRITE, rv_root.root_dir, NULL, 770 + &monitoring_on_fops); 771 + if (!tmp) 772 + goto out_err; 773 + 774 + turn_monitoring_on(); 775 + 776 + return 0; 777 + 778 + out_err: 779 + rv_remove(rv_root.root_dir); 780 + printk(KERN_ERR "RV: Error while creating the RV interface\n"); 781 + return 1; 782 + }
+33
kernel/trace/rv/rv.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #include <linux/mutex.h> 3 + 4 + struct rv_interface { 5 + struct dentry *root_dir; 6 + struct dentry *monitors_dir; 7 + }; 8 + 9 + #include "../trace.h" 10 + #include <linux/tracefs.h> 11 + #include <linux/rv.h> 12 + 13 + #define RV_MODE_WRITE TRACE_MODE_WRITE 14 + #define RV_MODE_READ TRACE_MODE_READ 15 + 16 + #define rv_create_dir tracefs_create_dir 17 + #define rv_create_file tracefs_create_file 18 + #define rv_remove tracefs_remove 19 + 20 + #define MAX_RV_MONITOR_NAME_SIZE 32 21 + 22 + extern struct mutex rv_interface_lock; 23 + 24 + struct rv_monitor_def { 25 + struct list_head list; 26 + struct rv_monitor *monitor; 27 + struct dentry *root_d; 28 + bool task_monitor; 29 + }; 30 + 31 + struct dentry *get_monitors_root(void); 32 + int rv_disable_monitor(struct rv_monitor_def *mdef); 33 + int rv_enable_monitor(struct rv_monitor_def *mdef);
+2
kernel/trace/trace.c
··· 9772 9772 tracer_init_tracefs_work_func(NULL); 9773 9773 } 9774 9774 9775 + rv_init_interface(); 9776 + 9775 9777 return 0; 9776 9778 } 9777 9779
+9
kernel/trace/trace.h
··· 2005 2005 2006 2006 extern const struct file_operations trace_min_max_fops; 2007 2007 2008 + #ifdef CONFIG_RV 2009 + extern int rv_init_interface(void); 2010 + #else 2011 + static inline int rv_init_interface(void) 2012 + { 2013 + return 0; 2014 + } 2015 + #endif 2016 + 2008 2017 #endif /* _LINUX_KERNEL_TRACE_H */