^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * trace_events_inject - trace event injection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2019 Cong Wang <cwang@twitter.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) trace_inject_entry(struct trace_event_file *file, void *rec, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct trace_event_buffer fbuffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int written = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) void *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) rcu_read_lock_sched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) entry = trace_event_buffer_reserve(&fbuffer, file, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) memcpy(entry, rec, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) written = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) trace_event_buffer_commit(&fbuffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) rcu_read_unlock_sched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) parse_field(char *str, struct trace_event_call *call,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct ftrace_event_field **pf, u64 *pv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct ftrace_event_field *field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) char *field_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int s, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (!str[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* First find the field to associate to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) while (isspace(str[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) s = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) while (isalnum(str[i]) || str[i] == '_')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) len = i - s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) field_name = kmemdup_nul(str + s, len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!field_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) field = trace_find_event_field(call, field_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) kfree(field_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (!field)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *pf = field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) while (isspace(str[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (str[i] != '=')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) while (isspace(str[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) s = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (isdigit(str[i]) || str[i] == '-') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) char *num, c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* Make sure the field is not a string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (is_string_field(field))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (str[i] == '-')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* We allow 0xDEADBEEF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) while (isalnum(str[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) num = str + s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) c = str[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (c != '\0' && !isspace(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) str[i] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* Make sure it is a value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (field->is_signed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ret = kstrtoll(num, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ret = kstrtoull(num, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) str[i] = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) *pv = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) } else if (str[i] == '\'' || str[i] == '"') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) char q = str[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* Make sure the field is OK for strings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (!is_string_field(field))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) for (i++; str[i]; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (str[i] == '\\' && str[i + 1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (str[i] == q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (!str[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* Skip quotes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) len = i - s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (len >= MAX_FILTER_STR_VAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) *pv = (unsigned long)(str + s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) str[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* go past the last quote */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int trace_get_entry_size(struct trace_event_call *call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct ftrace_event_field *field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct list_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) head = trace_get_fields(call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) list_for_each_entry(field, head, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (field->size + field->offset > size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) size = field->size + field->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static void *trace_alloc_entry(struct trace_event_call *call, int *size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int entry_size = trace_get_entry_size(call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct ftrace_event_field *field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct list_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) void *entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /* We need an extra '\0' at the end. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) entry = kzalloc(entry_size + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) head = trace_get_fields(call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) list_for_each_entry(field, head, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (!is_string_field(field))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (field->filter_type == FILTER_STATIC_STRING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (field->filter_type == FILTER_DYN_STRING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) u32 *str_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int str_loc = entry_size & 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) str_item = (u32 *)(entry + field->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) *str_item = str_loc; /* string length is 0. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) char **paddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) paddr = (char **)(entry + field->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) *paddr = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) *size = entry_size + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #define INJECT_STRING "STATIC STRING CAN NOT BE INJECTED"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* Caller is responsible to free the *pentry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static int parse_entry(char *str, struct trace_event_call *call, void **pentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct ftrace_event_field *field;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) unsigned long irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) void *entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) int entry_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) u64 val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) entry = trace_alloc_entry(call, &entry_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) *pentry = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) local_save_flags(irq_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) tracing_generic_entry_update(entry, call->event.type, irq_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) preempt_count());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) while ((len = parse_field(str, call, &field, &val)) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (is_function_field(field))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (is_string_field(field)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) char *addr = (char *)(unsigned long) val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (field->filter_type == FILTER_STATIC_STRING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) strlcpy(entry + field->offset, addr, field->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) } else if (field->filter_type == FILTER_DYN_STRING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) int str_len = strlen(addr) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) int str_loc = entry_size & 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) u32 *str_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) entry_size += str_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) *pentry = krealloc(entry, entry_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!*pentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) kfree(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) entry = *pentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) strlcpy(entry + (entry_size - str_len), addr, str_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) str_item = (u32 *)(entry + field->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) *str_item = (str_len << 16) | str_loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) char **paddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) paddr = (char **)(entry + field->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) *paddr = INJECT_STRING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) switch (field->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) case 1: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) u8 tmp = (u8) val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) memcpy(entry + field->offset, &tmp, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) case 2: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) u16 tmp = (u16) val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) memcpy(entry + field->offset, &tmp, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) case 4: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) u32 tmp = (u32) val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) memcpy(entry + field->offset, &tmp, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) memcpy(entry + field->offset, &val, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) str += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return entry_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) event_inject_write(struct file *filp, const char __user *ubuf, size_t cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct trace_event_call *call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct trace_event_file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) int err = -ENODEV, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) void *entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (cnt >= PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) buf = memdup_user_nul(ubuf, cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (IS_ERR(buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return PTR_ERR(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) strim(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) mutex_lock(&event_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) file = event_file_data(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) call = file->event_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) size = parse_entry(buf, call, &entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (size < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) err = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) err = trace_inject_entry(file, entry, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) mutex_unlock(&event_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) kfree(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) *ppos += err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) event_inject_read(struct file *file, char __user *buf, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) const struct file_operations event_inject_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) .open = tracing_open_generic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) .read = event_inject_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) .write = event_inject_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) };