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