| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Copyright (c) 2021, Microsoft Corporation. |
| 4 | * |
| 5 | * Authors: |
| 6 | * Beau Belgrave <beaub@linux.microsoft.com> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/bitmap.h> |
| 10 | #include <linux/cdev.h> |
| 11 | #include <linux/hashtable.h> |
| 12 | #include <linux/list.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/uio.h> |
| 15 | #include <linux/ioctl.h> |
| 16 | #include <linux/jhash.h> |
| 17 | #include <linux/refcount.h> |
| 18 | #include <linux/trace_events.h> |
| 19 | #include <linux/tracefs.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/highmem.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/user_events.h> |
| 25 | #include "trace_dynevent.h" |
| 26 | #include "trace_output.h" |
| 27 | #include "trace.h" |
| 28 | |
| 29 | #define USER_EVENTS_PREFIX_LEN (sizeof(USER_EVENTS_PREFIX)-1) |
| 30 | |
| 31 | #define FIELD_DEPTH_TYPE 0 |
| 32 | #define FIELD_DEPTH_NAME 1 |
| 33 | #define FIELD_DEPTH_SIZE 2 |
| 34 | |
| 35 | /* Limit how long of an event name plus args within the subsystem. */ |
| 36 | #define MAX_EVENT_DESC 512 |
| 37 | #define EVENT_NAME(user_event) ((user_event)->reg_name) |
| 38 | #define EVENT_TP_NAME(user_event) ((user_event)->tracepoint.name) |
| 39 | #define MAX_FIELD_ARRAY_SIZE 1024 |
| 40 | |
| 41 | /* |
| 42 | * Internal bits (kernel side only) to keep track of connected probes: |
| 43 | * These are used when status is requested in text form about an event. These |
| 44 | * bits are compared against an internal byte on the event to determine which |
| 45 | * probes to print out to the user. |
| 46 | * |
| 47 | * These do not reflect the mapped bytes between the user and kernel space. |
| 48 | */ |
| 49 | #define EVENT_STATUS_FTRACE BIT(0) |
| 50 | #define EVENT_STATUS_PERF BIT(1) |
| 51 | #define EVENT_STATUS_OTHER BIT(7) |
| 52 | |
| 53 | /* |
| 54 | * Stores the system name, tables, and locks for a group of events. This |
| 55 | * allows isolation for events by various means. |
| 56 | */ |
| 57 | struct user_event_group { |
| 58 | char *system_name; |
| 59 | char *system_multi_name; |
| 60 | struct hlist_node node; |
| 61 | struct mutex reg_mutex; |
| 62 | DECLARE_HASHTABLE(register_table, 8); |
| 63 | /* ID that moves forward within the group for multi-event names */ |
| 64 | u64 multi_id; |
| 65 | }; |
| 66 | |
| 67 | /* Group for init_user_ns mapping, top-most group */ |
| 68 | static struct user_event_group *init_group; |
| 69 | |
| 70 | /* Max allowed events for the whole system */ |
| 71 | static unsigned int max_user_events = 32768; |
| 72 | |
| 73 | /* Current number of events on the whole system */ |
| 74 | static unsigned int current_user_events; |
| 75 | |
| 76 | /* |
| 77 | * Stores per-event properties, as users register events |
| 78 | * within a file a user_event might be created if it does not |
| 79 | * already exist. These are globally used and their lifetime |
| 80 | * is tied to the refcnt member. These cannot go away until the |
| 81 | * refcnt reaches one. |
| 82 | */ |
| 83 | struct user_event { |
| 84 | struct user_event_group *group; |
| 85 | char *reg_name; |
| 86 | struct tracepoint tracepoint; |
| 87 | struct trace_event_call call; |
| 88 | struct trace_event_class class; |
| 89 | struct dyn_event devent; |
| 90 | struct hlist_node node; |
| 91 | struct list_head fields; |
| 92 | struct list_head validators; |
| 93 | struct work_struct put_work; |
| 94 | refcount_t refcnt; |
| 95 | int min_size; |
| 96 | int reg_flags; |
| 97 | char status; |
| 98 | }; |
| 99 | |
| 100 | /* |
| 101 | * Stores per-mm/event properties that enable an address to be |
| 102 | * updated properly for each task. As tasks are forked, we use |
| 103 | * these to track enablement sites that are tied to an event. |
| 104 | */ |
| 105 | struct user_event_enabler { |
| 106 | struct list_head mm_enablers_link; |
| 107 | struct user_event *event; |
| 108 | unsigned long addr; |
| 109 | |
| 110 | /* Track enable bit, flags, etc. Aligned for bitops. */ |
| 111 | unsigned long values; |
| 112 | }; |
| 113 | |
| 114 | /* Bits 0-5 are for the bit to update upon enable/disable (0-63 allowed) */ |
| 115 | #define ENABLE_VAL_BIT_MASK 0x3F |
| 116 | |
| 117 | /* Bit 6 is for faulting status of enablement */ |
| 118 | #define ENABLE_VAL_FAULTING_BIT 6 |
| 119 | |
| 120 | /* Bit 7 is for freeing status of enablement */ |
| 121 | #define ENABLE_VAL_FREEING_BIT 7 |
| 122 | |
| 123 | /* Bit 8 is for marking 32-bit on 64-bit */ |
| 124 | #define ENABLE_VAL_32_ON_64_BIT 8 |
| 125 | |
| 126 | #define ENABLE_VAL_COMPAT_MASK (1 << ENABLE_VAL_32_ON_64_BIT) |
| 127 | |
| 128 | /* Only duplicate the bit and compat values */ |
| 129 | #define ENABLE_VAL_DUP_MASK (ENABLE_VAL_BIT_MASK | ENABLE_VAL_COMPAT_MASK) |
| 130 | |
| 131 | #define ENABLE_BITOPS(e) (&(e)->values) |
| 132 | |
| 133 | #define ENABLE_BIT(e) ((int)((e)->values & ENABLE_VAL_BIT_MASK)) |
| 134 | |
| 135 | #define EVENT_MULTI_FORMAT(f) ((f) & USER_EVENT_REG_MULTI_FORMAT) |
| 136 | |
| 137 | /* Used for asynchronous faulting in of pages */ |
| 138 | struct user_event_enabler_fault { |
| 139 | struct work_struct work; |
| 140 | struct user_event_mm *mm; |
| 141 | struct user_event_enabler *enabler; |
| 142 | int attempt; |
| 143 | }; |
| 144 | |
| 145 | static struct kmem_cache *fault_cache; |
| 146 | |
| 147 | /* Global list of memory descriptors using user_events */ |
| 148 | static LIST_HEAD(user_event_mms); |
| 149 | static DEFINE_SPINLOCK(user_event_mms_lock); |
| 150 | |
| 151 | /* |
| 152 | * Stores per-file events references, as users register events |
| 153 | * within a file this structure is modified and freed via RCU. |
| 154 | * The lifetime of this struct is tied to the lifetime of the file. |
| 155 | * These are not shared and only accessible by the file that created it. |
| 156 | */ |
| 157 | struct user_event_refs { |
| 158 | struct rcu_head rcu; |
| 159 | int count; |
| 160 | struct user_event *events[]; |
| 161 | }; |
| 162 | |
| 163 | struct user_event_file_info { |
| 164 | struct user_event_group *group; |
| 165 | struct user_event_refs *refs; |
| 166 | }; |
| 167 | |
| 168 | #define VALIDATOR_ENSURE_NULL (1 << 0) |
| 169 | #define VALIDATOR_REL (1 << 1) |
| 170 | |
| 171 | struct user_event_validator { |
| 172 | struct list_head user_event_link; |
| 173 | int offset; |
| 174 | int flags; |
| 175 | }; |
| 176 | |
| 177 | static inline void align_addr_bit(unsigned long *addr, int *bit, |
| 178 | unsigned long *flags) |
| 179 | { |
| 180 | if (IS_ALIGNED(*addr, sizeof(long))) { |
| 181 | #ifdef __BIG_ENDIAN |
| 182 | /* 32 bit on BE 64 bit requires a 32 bit offset when aligned. */ |
| 183 | if (test_bit(ENABLE_VAL_32_ON_64_BIT, flags)) |
| 184 | *bit += 32; |
| 185 | #endif |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | *addr = ALIGN_DOWN(*addr, sizeof(long)); |
| 190 | |
| 191 | /* |
| 192 | * We only support 32 and 64 bit values. The only time we need |
| 193 | * to align is a 32 bit value on a 64 bit kernel, which on LE |
| 194 | * is always 32 bits, and on BE requires no change when unaligned. |
| 195 | */ |
| 196 | #ifdef __LITTLE_ENDIAN |
| 197 | *bit += 32; |
| 198 | #endif |
| 199 | } |
| 200 | |
| 201 | typedef void (*user_event_func_t) (struct user_event *user, struct iov_iter *i, |
| 202 | void *tpdata, bool *faulted); |
| 203 | |
| 204 | static int user_event_parse(struct user_event_group *group, char *name, |
| 205 | char *args, char *flags, |
| 206 | struct user_event **newuser, int reg_flags); |
| 207 | |
| 208 | static struct user_event_mm *user_event_mm_get(struct user_event_mm *mm); |
| 209 | static struct user_event_mm *user_event_mm_get_all(struct user_event *user); |
| 210 | static void user_event_mm_put(struct user_event_mm *mm); |
| 211 | static int destroy_user_event(struct user_event *user); |
| 212 | static bool user_fields_match(struct user_event *user, int argc, |
| 213 | const char **argv); |
| 214 | |
| 215 | static u32 user_event_key(char *name) |
| 216 | { |
| 217 | return jhash(key: name, strlen(name), initval: 0); |
| 218 | } |
| 219 | |
| 220 | static bool user_event_capable(u16 reg_flags) |
| 221 | { |
| 222 | /* Persistent events require CAP_PERFMON / CAP_SYS_ADMIN */ |
| 223 | if (reg_flags & USER_EVENT_REG_PERSIST) { |
| 224 | if (!perfmon_capable()) |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | static struct user_event *user_event_get(struct user_event *user) |
| 232 | { |
| 233 | refcount_inc(r: &user->refcnt); |
| 234 | |
| 235 | return user; |
| 236 | } |
| 237 | |
| 238 | static void delayed_destroy_user_event(struct work_struct *work) |
| 239 | { |
| 240 | struct user_event *user = container_of( |
| 241 | work, struct user_event, put_work); |
| 242 | |
| 243 | mutex_lock(&event_mutex); |
| 244 | |
| 245 | if (!refcount_dec_and_test(r: &user->refcnt)) |
| 246 | goto out; |
| 247 | |
| 248 | if (destroy_user_event(user)) { |
| 249 | /* |
| 250 | * The only reason this would fail here is if we cannot |
| 251 | * update the visibility of the event. In this case the |
| 252 | * event stays in the hashtable, waiting for someone to |
| 253 | * attempt to delete it later. |
| 254 | */ |
| 255 | pr_warn("user_events: Unable to delete event\n" ); |
| 256 | refcount_set(r: &user->refcnt, n: 1); |
| 257 | } |
| 258 | out: |
| 259 | mutex_unlock(lock: &event_mutex); |
| 260 | } |
| 261 | |
| 262 | static void user_event_put(struct user_event *user, bool locked) |
| 263 | { |
| 264 | bool delete; |
| 265 | |
| 266 | if (unlikely(!user)) |
| 267 | return; |
| 268 | |
| 269 | /* |
| 270 | * When the event is not enabled for auto-delete there will always |
| 271 | * be at least 1 reference to the event. During the event creation |
| 272 | * we initially set the refcnt to 2 to achieve this. In those cases |
| 273 | * the caller must acquire event_mutex and after decrement check if |
| 274 | * the refcnt is 1, meaning this is the last reference. When auto |
| 275 | * delete is enabled, there will only be 1 ref, IE: refcnt will be |
| 276 | * only set to 1 during creation to allow the below checks to go |
| 277 | * through upon the last put. The last put must always be done with |
| 278 | * the event mutex held. |
| 279 | */ |
| 280 | if (!locked) { |
| 281 | lockdep_assert_not_held(&event_mutex); |
| 282 | delete = refcount_dec_and_mutex_lock(r: &user->refcnt, lock: &event_mutex); |
| 283 | } else { |
| 284 | lockdep_assert_held(&event_mutex); |
| 285 | delete = refcount_dec_and_test(r: &user->refcnt); |
| 286 | } |
| 287 | |
| 288 | if (!delete) |
| 289 | return; |
| 290 | |
| 291 | /* |
| 292 | * We now have the event_mutex in all cases, which ensures that |
| 293 | * no new references will be taken until event_mutex is released. |
| 294 | * New references come through find_user_event(), which requires |
| 295 | * the event_mutex to be held. |
| 296 | */ |
| 297 | |
| 298 | if (user->reg_flags & USER_EVENT_REG_PERSIST) { |
| 299 | /* We should not get here when persist flag is set */ |
| 300 | pr_alert("BUG: Auto-delete engaged on persistent event\n" ); |
| 301 | goto out; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Unfortunately we have to attempt the actual destroy in a work |
| 306 | * queue. This is because not all cases handle a trace_event_call |
| 307 | * being removed within the class->reg() operation for unregister. |
| 308 | */ |
| 309 | INIT_WORK(&user->put_work, delayed_destroy_user_event); |
| 310 | |
| 311 | /* |
| 312 | * Since the event is still in the hashtable, we have to re-inc |
| 313 | * the ref count to 1. This count will be decremented and checked |
| 314 | * in the work queue to ensure it's still the last ref. This is |
| 315 | * needed because a user-process could register the same event in |
| 316 | * between the time of event_mutex release and the work queue |
| 317 | * running the delayed destroy. If we removed the item now from |
| 318 | * the hashtable, this would result in a timing window where a |
| 319 | * user process would fail a register because the trace_event_call |
| 320 | * register would fail in the tracing layers. |
| 321 | */ |
| 322 | refcount_set(r: &user->refcnt, n: 1); |
| 323 | |
| 324 | if (WARN_ON_ONCE(!schedule_work(&user->put_work))) { |
| 325 | /* |
| 326 | * If we fail we must wait for an admin to attempt delete or |
| 327 | * another register/close of the event, whichever is first. |
| 328 | */ |
| 329 | pr_warn("user_events: Unable to queue delayed destroy\n" ); |
| 330 | } |
| 331 | out: |
| 332 | /* Ensure if we didn't have event_mutex before we unlock it */ |
| 333 | if (!locked) |
| 334 | mutex_unlock(lock: &event_mutex); |
| 335 | } |
| 336 | |
| 337 | static void user_event_group_destroy(struct user_event_group *group) |
| 338 | { |
| 339 | kfree(objp: group->system_name); |
| 340 | kfree(objp: group->system_multi_name); |
| 341 | kfree(objp: group); |
| 342 | } |
| 343 | |
| 344 | static char *user_event_group_system_name(void) |
| 345 | { |
| 346 | char *system_name; |
| 347 | int len = sizeof(USER_EVENTS_SYSTEM) + 1; |
| 348 | |
| 349 | system_name = kmalloc(len, GFP_KERNEL); |
| 350 | |
| 351 | if (!system_name) |
| 352 | return NULL; |
| 353 | |
| 354 | snprintf(buf: system_name, size: len, fmt: "%s" , USER_EVENTS_SYSTEM); |
| 355 | |
| 356 | return system_name; |
| 357 | } |
| 358 | |
| 359 | static char *user_event_group_system_multi_name(void) |
| 360 | { |
| 361 | return kstrdup(USER_EVENTS_MULTI_SYSTEM, GFP_KERNEL); |
| 362 | } |
| 363 | |
| 364 | static struct user_event_group *current_user_event_group(void) |
| 365 | { |
| 366 | return init_group; |
| 367 | } |
| 368 | |
| 369 | static struct user_event_group *user_event_group_create(void) |
| 370 | { |
| 371 | struct user_event_group *group; |
| 372 | |
| 373 | group = kzalloc(sizeof(*group), GFP_KERNEL); |
| 374 | |
| 375 | if (!group) |
| 376 | return NULL; |
| 377 | |
| 378 | group->system_name = user_event_group_system_name(); |
| 379 | |
| 380 | if (!group->system_name) |
| 381 | goto error; |
| 382 | |
| 383 | group->system_multi_name = user_event_group_system_multi_name(); |
| 384 | |
| 385 | if (!group->system_multi_name) |
| 386 | goto error; |
| 387 | |
| 388 | mutex_init(&group->reg_mutex); |
| 389 | hash_init(group->register_table); |
| 390 | |
| 391 | return group; |
| 392 | error: |
| 393 | if (group) |
| 394 | user_event_group_destroy(group); |
| 395 | |
| 396 | return NULL; |
| 397 | }; |
| 398 | |
| 399 | static void user_event_enabler_destroy(struct user_event_enabler *enabler, |
| 400 | bool locked) |
| 401 | { |
| 402 | list_del_rcu(entry: &enabler->mm_enablers_link); |
| 403 | |
| 404 | /* No longer tracking the event via the enabler */ |
| 405 | user_event_put(user: enabler->event, locked); |
| 406 | |
| 407 | kfree(objp: enabler); |
| 408 | } |
| 409 | |
| 410 | static int user_event_mm_fault_in(struct user_event_mm *mm, unsigned long uaddr, |
| 411 | int attempt) |
| 412 | { |
| 413 | bool unlocked; |
| 414 | int ret; |
| 415 | |
| 416 | /* |
| 417 | * Normally this is low, ensure that it cannot be taken advantage of by |
| 418 | * bad user processes to cause excessive looping. |
| 419 | */ |
| 420 | if (attempt > 10) |
| 421 | return -EFAULT; |
| 422 | |
| 423 | mmap_read_lock(mm: mm->mm); |
| 424 | |
| 425 | /* Ensure MM has tasks, cannot use after exit_mm() */ |
| 426 | if (refcount_read(r: &mm->tasks) == 0) { |
| 427 | ret = -ENOENT; |
| 428 | goto out; |
| 429 | } |
| 430 | |
| 431 | ret = fixup_user_fault(mm: mm->mm, address: uaddr, fault_flags: FAULT_FLAG_WRITE | FAULT_FLAG_REMOTE, |
| 432 | unlocked: &unlocked); |
| 433 | out: |
| 434 | mmap_read_unlock(mm: mm->mm); |
| 435 | |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | static int user_event_enabler_write(struct user_event_mm *mm, |
| 440 | struct user_event_enabler *enabler, |
| 441 | bool fixup_fault, int *attempt); |
| 442 | |
| 443 | static void user_event_enabler_fault_fixup(struct work_struct *work) |
| 444 | { |
| 445 | struct user_event_enabler_fault *fault = container_of( |
| 446 | work, struct user_event_enabler_fault, work); |
| 447 | struct user_event_enabler *enabler = fault->enabler; |
| 448 | struct user_event_mm *mm = fault->mm; |
| 449 | unsigned long uaddr = enabler->addr; |
| 450 | int attempt = fault->attempt; |
| 451 | int ret; |
| 452 | |
| 453 | ret = user_event_mm_fault_in(mm, uaddr, attempt); |
| 454 | |
| 455 | if (ret && ret != -ENOENT) { |
| 456 | struct user_event *user = enabler->event; |
| 457 | |
| 458 | pr_warn("user_events: Fault for mm: 0x%p @ 0x%llx event: %s\n" , |
| 459 | mm->mm, (unsigned long long)uaddr, EVENT_NAME(user)); |
| 460 | } |
| 461 | |
| 462 | /* Prevent state changes from racing */ |
| 463 | mutex_lock(&event_mutex); |
| 464 | |
| 465 | /* User asked for enabler to be removed during fault */ |
| 466 | if (test_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(enabler))) { |
| 467 | user_event_enabler_destroy(enabler, locked: true); |
| 468 | goto out; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * If we managed to get the page, re-issue the write. We do not |
| 473 | * want to get into a possible infinite loop, which is why we only |
| 474 | * attempt again directly if the page came in. If we couldn't get |
| 475 | * the page here, then we will try again the next time the event is |
| 476 | * enabled/disabled. |
| 477 | */ |
| 478 | clear_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)); |
| 479 | |
| 480 | if (!ret) { |
| 481 | mmap_read_lock(mm: mm->mm); |
| 482 | user_event_enabler_write(mm, enabler, fixup_fault: true, attempt: &attempt); |
| 483 | mmap_read_unlock(mm: mm->mm); |
| 484 | } |
| 485 | out: |
| 486 | mutex_unlock(lock: &event_mutex); |
| 487 | |
| 488 | /* In all cases we no longer need the mm or fault */ |
| 489 | user_event_mm_put(mm); |
| 490 | kmem_cache_free(s: fault_cache, objp: fault); |
| 491 | } |
| 492 | |
| 493 | static bool user_event_enabler_queue_fault(struct user_event_mm *mm, |
| 494 | struct user_event_enabler *enabler, |
| 495 | int attempt) |
| 496 | { |
| 497 | struct user_event_enabler_fault *fault; |
| 498 | |
| 499 | fault = kmem_cache_zalloc(fault_cache, GFP_NOWAIT); |
| 500 | |
| 501 | if (!fault) |
| 502 | return false; |
| 503 | |
| 504 | INIT_WORK(&fault->work, user_event_enabler_fault_fixup); |
| 505 | fault->mm = user_event_mm_get(mm); |
| 506 | fault->enabler = enabler; |
| 507 | fault->attempt = attempt; |
| 508 | |
| 509 | /* Don't try to queue in again while we have a pending fault */ |
| 510 | set_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)); |
| 511 | |
| 512 | if (!schedule_work(work: &fault->work)) { |
| 513 | /* Allow another attempt later */ |
| 514 | clear_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)); |
| 515 | |
| 516 | user_event_mm_put(mm); |
| 517 | kmem_cache_free(s: fault_cache, objp: fault); |
| 518 | |
| 519 | return false; |
| 520 | } |
| 521 | |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | static int user_event_enabler_write(struct user_event_mm *mm, |
| 526 | struct user_event_enabler *enabler, |
| 527 | bool fixup_fault, int *attempt) |
| 528 | { |
| 529 | unsigned long uaddr = enabler->addr; |
| 530 | unsigned long *ptr; |
| 531 | struct page *page; |
| 532 | void *kaddr; |
| 533 | int bit = ENABLE_BIT(enabler); |
| 534 | int ret; |
| 535 | |
| 536 | lockdep_assert_held(&event_mutex); |
| 537 | mmap_assert_locked(mm: mm->mm); |
| 538 | |
| 539 | *attempt += 1; |
| 540 | |
| 541 | /* Ensure MM has tasks, cannot use after exit_mm() */ |
| 542 | if (refcount_read(r: &mm->tasks) == 0) |
| 543 | return -ENOENT; |
| 544 | |
| 545 | if (unlikely(test_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)) || |
| 546 | test_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(enabler)))) |
| 547 | return -EBUSY; |
| 548 | |
| 549 | align_addr_bit(addr: &uaddr, bit: &bit, ENABLE_BITOPS(enabler)); |
| 550 | |
| 551 | ret = pin_user_pages_remote(mm: mm->mm, start: uaddr, nr_pages: 1, gup_flags: FOLL_WRITE | FOLL_NOFAULT, |
| 552 | pages: &page, NULL); |
| 553 | |
| 554 | if (unlikely(ret <= 0)) { |
| 555 | if (!fixup_fault) |
| 556 | return -EFAULT; |
| 557 | |
| 558 | if (!user_event_enabler_queue_fault(mm, enabler, attempt: *attempt)) |
| 559 | pr_warn("user_events: Unable to queue fault handler\n" ); |
| 560 | |
| 561 | return -EFAULT; |
| 562 | } |
| 563 | |
| 564 | kaddr = kmap_local_page(page); |
| 565 | ptr = kaddr + (uaddr & ~PAGE_MASK); |
| 566 | |
| 567 | /* Update bit atomically, user tracers must be atomic as well */ |
| 568 | if (enabler->event && enabler->event->status) |
| 569 | set_bit(nr: bit, addr: ptr); |
| 570 | else |
| 571 | clear_bit(nr: bit, addr: ptr); |
| 572 | |
| 573 | kunmap_local(kaddr); |
| 574 | unpin_user_pages_dirty_lock(pages: &page, npages: 1, make_dirty: true); |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | static bool user_event_enabler_exists(struct user_event_mm *mm, |
| 580 | unsigned long uaddr, unsigned char bit) |
| 581 | { |
| 582 | struct user_event_enabler *enabler; |
| 583 | |
| 584 | list_for_each_entry(enabler, &mm->enablers, mm_enablers_link) { |
| 585 | if (enabler->addr == uaddr && ENABLE_BIT(enabler) == bit) |
| 586 | return true; |
| 587 | } |
| 588 | |
| 589 | return false; |
| 590 | } |
| 591 | |
| 592 | static void user_event_enabler_update(struct user_event *user) |
| 593 | { |
| 594 | struct user_event_enabler *enabler; |
| 595 | struct user_event_mm *next; |
| 596 | struct user_event_mm *mm; |
| 597 | int attempt; |
| 598 | |
| 599 | lockdep_assert_held(&event_mutex); |
| 600 | |
| 601 | /* |
| 602 | * We need to build a one-shot list of all the mms that have an |
| 603 | * enabler for the user_event passed in. This list is only valid |
| 604 | * while holding the event_mutex. The only reason for this is due |
| 605 | * to the global mm list being RCU protected and we use methods |
| 606 | * which can wait (mmap_read_lock and pin_user_pages_remote). |
| 607 | * |
| 608 | * NOTE: user_event_mm_get_all() increments the ref count of each |
| 609 | * mm that is added to the list to prevent removal timing windows. |
| 610 | * We must always put each mm after they are used, which may wait. |
| 611 | */ |
| 612 | mm = user_event_mm_get_all(user); |
| 613 | |
| 614 | while (mm) { |
| 615 | next = mm->next; |
| 616 | mmap_read_lock(mm: mm->mm); |
| 617 | |
| 618 | list_for_each_entry(enabler, &mm->enablers, mm_enablers_link) { |
| 619 | if (enabler->event == user) { |
| 620 | attempt = 0; |
| 621 | user_event_enabler_write(mm, enabler, fixup_fault: true, attempt: &attempt); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | mmap_read_unlock(mm: mm->mm); |
| 626 | user_event_mm_put(mm); |
| 627 | mm = next; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | static bool user_event_enabler_dup(struct user_event_enabler *orig, |
| 632 | struct user_event_mm *mm) |
| 633 | { |
| 634 | struct user_event_enabler *enabler; |
| 635 | |
| 636 | /* Skip pending frees */ |
| 637 | if (unlikely(test_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(orig)))) |
| 638 | return true; |
| 639 | |
| 640 | enabler = kzalloc(sizeof(*enabler), GFP_NOWAIT | __GFP_ACCOUNT); |
| 641 | |
| 642 | if (!enabler) |
| 643 | return false; |
| 644 | |
| 645 | enabler->event = user_event_get(user: orig->event); |
| 646 | enabler->addr = orig->addr; |
| 647 | |
| 648 | /* Only dup part of value (ignore future flags, etc) */ |
| 649 | enabler->values = orig->values & ENABLE_VAL_DUP_MASK; |
| 650 | |
| 651 | /* Enablers not exposed yet, RCU not required */ |
| 652 | list_add(new: &enabler->mm_enablers_link, head: &mm->enablers); |
| 653 | |
| 654 | return true; |
| 655 | } |
| 656 | |
| 657 | static struct user_event_mm *user_event_mm_get(struct user_event_mm *mm) |
| 658 | { |
| 659 | refcount_inc(r: &mm->refcnt); |
| 660 | |
| 661 | return mm; |
| 662 | } |
| 663 | |
| 664 | static struct user_event_mm *user_event_mm_get_all(struct user_event *user) |
| 665 | { |
| 666 | struct user_event_mm *found = NULL; |
| 667 | struct user_event_enabler *enabler; |
| 668 | struct user_event_mm *mm; |
| 669 | |
| 670 | /* |
| 671 | * We use the mm->next field to build a one-shot list from the global |
| 672 | * RCU protected list. To build this list the event_mutex must be held. |
| 673 | * This lets us build a list without requiring allocs that could fail |
| 674 | * when user based events are most wanted for diagnostics. |
| 675 | */ |
| 676 | lockdep_assert_held(&event_mutex); |
| 677 | |
| 678 | /* |
| 679 | * We do not want to block fork/exec while enablements are being |
| 680 | * updated, so we use RCU to walk the current tasks that have used |
| 681 | * user_events ABI for 1 or more events. Each enabler found in each |
| 682 | * task that matches the event being updated has a write to reflect |
| 683 | * the kernel state back into the process. Waits/faults must not occur |
| 684 | * during this. So we scan the list under RCU for all the mm that have |
| 685 | * the event within it. This is needed because mm_read_lock() can wait. |
| 686 | * Each user mm returned has a ref inc to handle remove RCU races. |
| 687 | */ |
| 688 | rcu_read_lock(); |
| 689 | |
| 690 | list_for_each_entry_rcu(mm, &user_event_mms, mms_link) { |
| 691 | list_for_each_entry_rcu(enabler, &mm->enablers, mm_enablers_link) { |
| 692 | if (enabler->event == user) { |
| 693 | mm->next = found; |
| 694 | found = user_event_mm_get(mm); |
| 695 | break; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | rcu_read_unlock(); |
| 701 | |
| 702 | return found; |
| 703 | } |
| 704 | |
| 705 | static struct user_event_mm *user_event_mm_alloc(struct task_struct *t) |
| 706 | { |
| 707 | struct user_event_mm *user_mm; |
| 708 | |
| 709 | user_mm = kzalloc(sizeof(*user_mm), GFP_KERNEL_ACCOUNT); |
| 710 | |
| 711 | if (!user_mm) |
| 712 | return NULL; |
| 713 | |
| 714 | user_mm->mm = t->mm; |
| 715 | INIT_LIST_HEAD(list: &user_mm->enablers); |
| 716 | refcount_set(r: &user_mm->refcnt, n: 1); |
| 717 | refcount_set(r: &user_mm->tasks, n: 1); |
| 718 | |
| 719 | /* |
| 720 | * The lifetime of the memory descriptor can slightly outlast |
| 721 | * the task lifetime if a ref to the user_event_mm is taken |
| 722 | * between list_del_rcu() and call_rcu(). Therefore we need |
| 723 | * to take a reference to it to ensure it can live this long |
| 724 | * under this corner case. This can also occur in clones that |
| 725 | * outlast the parent. |
| 726 | */ |
| 727 | mmgrab(mm: user_mm->mm); |
| 728 | |
| 729 | return user_mm; |
| 730 | } |
| 731 | |
| 732 | static void user_event_mm_attach(struct user_event_mm *user_mm, struct task_struct *t) |
| 733 | { |
| 734 | unsigned long flags; |
| 735 | |
| 736 | spin_lock_irqsave(&user_event_mms_lock, flags); |
| 737 | list_add_rcu(new: &user_mm->mms_link, head: &user_event_mms); |
| 738 | spin_unlock_irqrestore(lock: &user_event_mms_lock, flags); |
| 739 | |
| 740 | t->user_event_mm = user_mm; |
| 741 | } |
| 742 | |
| 743 | static struct user_event_mm *current_user_event_mm(void) |
| 744 | { |
| 745 | struct user_event_mm *user_mm = current->user_event_mm; |
| 746 | |
| 747 | if (user_mm) |
| 748 | goto inc; |
| 749 | |
| 750 | user_mm = user_event_mm_alloc(current); |
| 751 | |
| 752 | if (!user_mm) |
| 753 | goto error; |
| 754 | |
| 755 | user_event_mm_attach(user_mm, current); |
| 756 | inc: |
| 757 | refcount_inc(r: &user_mm->refcnt); |
| 758 | error: |
| 759 | return user_mm; |
| 760 | } |
| 761 | |
| 762 | static void user_event_mm_destroy(struct user_event_mm *mm) |
| 763 | { |
| 764 | struct user_event_enabler *enabler, *next; |
| 765 | |
| 766 | list_for_each_entry_safe(enabler, next, &mm->enablers, mm_enablers_link) |
| 767 | user_event_enabler_destroy(enabler, locked: false); |
| 768 | |
| 769 | mmdrop(mm: mm->mm); |
| 770 | kfree(objp: mm); |
| 771 | } |
| 772 | |
| 773 | static void user_event_mm_put(struct user_event_mm *mm) |
| 774 | { |
| 775 | if (mm && refcount_dec_and_test(r: &mm->refcnt)) |
| 776 | user_event_mm_destroy(mm); |
| 777 | } |
| 778 | |
| 779 | static void delayed_user_event_mm_put(struct work_struct *work) |
| 780 | { |
| 781 | struct user_event_mm *mm; |
| 782 | |
| 783 | mm = container_of(to_rcu_work(work), struct user_event_mm, put_rwork); |
| 784 | user_event_mm_put(mm); |
| 785 | } |
| 786 | |
| 787 | void user_event_mm_remove(struct task_struct *t) |
| 788 | { |
| 789 | struct user_event_mm *mm; |
| 790 | unsigned long flags; |
| 791 | |
| 792 | might_sleep(); |
| 793 | |
| 794 | mm = t->user_event_mm; |
| 795 | t->user_event_mm = NULL; |
| 796 | |
| 797 | /* Clone will increment the tasks, only remove if last clone */ |
| 798 | if (!refcount_dec_and_test(r: &mm->tasks)) |
| 799 | return; |
| 800 | |
| 801 | /* Remove the mm from the list, so it can no longer be enabled */ |
| 802 | spin_lock_irqsave(&user_event_mms_lock, flags); |
| 803 | list_del_rcu(entry: &mm->mms_link); |
| 804 | spin_unlock_irqrestore(lock: &user_event_mms_lock, flags); |
| 805 | |
| 806 | /* |
| 807 | * We need to wait for currently occurring writes to stop within |
| 808 | * the mm. This is required since exit_mm() snaps the current rss |
| 809 | * stats and clears them. On the final mmdrop(), check_mm() will |
| 810 | * report a bug if these increment. |
| 811 | * |
| 812 | * All writes/pins are done under mmap_read lock, take the write |
| 813 | * lock to ensure in-progress faults have completed. Faults that |
| 814 | * are pending but yet to run will check the task count and skip |
| 815 | * the fault since the mm is going away. |
| 816 | */ |
| 817 | mmap_write_lock(mm: mm->mm); |
| 818 | mmap_write_unlock(mm: mm->mm); |
| 819 | |
| 820 | /* |
| 821 | * Put for mm must be done after RCU delay to handle new refs in |
| 822 | * between the list_del_rcu() and now. This ensures any get refs |
| 823 | * during rcu_read_lock() are accounted for during list removal. |
| 824 | * |
| 825 | * CPU A | CPU B |
| 826 | * --------------------------------------------------------------- |
| 827 | * user_event_mm_remove() | rcu_read_lock(); |
| 828 | * list_del_rcu() | list_for_each_entry_rcu(); |
| 829 | * call_rcu() | refcount_inc(); |
| 830 | * . | rcu_read_unlock(); |
| 831 | * schedule_work() | . |
| 832 | * user_event_mm_put() | . |
| 833 | * |
| 834 | * mmdrop() cannot be called in the softirq context of call_rcu() |
| 835 | * so we use a work queue after call_rcu() to run within. |
| 836 | */ |
| 837 | INIT_RCU_WORK(&mm->put_rwork, delayed_user_event_mm_put); |
| 838 | queue_rcu_work(wq: system_percpu_wq, rwork: &mm->put_rwork); |
| 839 | } |
| 840 | |
| 841 | void user_event_mm_dup(struct task_struct *t, struct user_event_mm *old_mm) |
| 842 | { |
| 843 | struct user_event_mm *mm = user_event_mm_alloc(t); |
| 844 | struct user_event_enabler *enabler; |
| 845 | |
| 846 | if (!mm) |
| 847 | return; |
| 848 | |
| 849 | rcu_read_lock(); |
| 850 | |
| 851 | list_for_each_entry_rcu(enabler, &old_mm->enablers, mm_enablers_link) { |
| 852 | if (!user_event_enabler_dup(orig: enabler, mm)) |
| 853 | goto error; |
| 854 | } |
| 855 | |
| 856 | rcu_read_unlock(); |
| 857 | |
| 858 | user_event_mm_attach(user_mm: mm, t); |
| 859 | return; |
| 860 | error: |
| 861 | rcu_read_unlock(); |
| 862 | user_event_mm_destroy(mm); |
| 863 | } |
| 864 | |
| 865 | static bool current_user_event_enabler_exists(unsigned long uaddr, |
| 866 | unsigned char bit) |
| 867 | { |
| 868 | struct user_event_mm *user_mm = current_user_event_mm(); |
| 869 | bool exists; |
| 870 | |
| 871 | if (!user_mm) |
| 872 | return false; |
| 873 | |
| 874 | exists = user_event_enabler_exists(mm: user_mm, uaddr, bit); |
| 875 | |
| 876 | user_event_mm_put(mm: user_mm); |
| 877 | |
| 878 | return exists; |
| 879 | } |
| 880 | |
| 881 | static struct user_event_enabler |
| 882 | *user_event_enabler_create(struct user_reg *reg, struct user_event *user, |
| 883 | int *write_result) |
| 884 | { |
| 885 | struct user_event_enabler *enabler; |
| 886 | struct user_event_mm *user_mm; |
| 887 | unsigned long uaddr = (unsigned long)reg->enable_addr; |
| 888 | int attempt = 0; |
| 889 | |
| 890 | user_mm = current_user_event_mm(); |
| 891 | |
| 892 | if (!user_mm) |
| 893 | return NULL; |
| 894 | |
| 895 | enabler = kzalloc(sizeof(*enabler), GFP_KERNEL_ACCOUNT); |
| 896 | |
| 897 | if (!enabler) |
| 898 | goto out; |
| 899 | |
| 900 | enabler->event = user; |
| 901 | enabler->addr = uaddr; |
| 902 | enabler->values = reg->enable_bit; |
| 903 | |
| 904 | #if BITS_PER_LONG >= 64 |
| 905 | if (reg->enable_size == 4) |
| 906 | set_bit(ENABLE_VAL_32_ON_64_BIT, ENABLE_BITOPS(enabler)); |
| 907 | #endif |
| 908 | |
| 909 | retry: |
| 910 | /* Prevents state changes from racing with new enablers */ |
| 911 | mutex_lock(&event_mutex); |
| 912 | |
| 913 | /* Attempt to reflect the current state within the process */ |
| 914 | mmap_read_lock(mm: user_mm->mm); |
| 915 | *write_result = user_event_enabler_write(mm: user_mm, enabler, fixup_fault: false, |
| 916 | attempt: &attempt); |
| 917 | mmap_read_unlock(mm: user_mm->mm); |
| 918 | |
| 919 | /* |
| 920 | * If the write works, then we will track the enabler. A ref to the |
| 921 | * underlying user_event is held by the enabler to prevent it going |
| 922 | * away while the enabler is still in use by a process. The ref is |
| 923 | * removed when the enabler is destroyed. This means a event cannot |
| 924 | * be forcefully deleted from the system until all tasks using it |
| 925 | * exit or run exec(), which includes forks and clones. |
| 926 | */ |
| 927 | if (!*write_result) { |
| 928 | user_event_get(user); |
| 929 | list_add_rcu(new: &enabler->mm_enablers_link, head: &user_mm->enablers); |
| 930 | } |
| 931 | |
| 932 | mutex_unlock(lock: &event_mutex); |
| 933 | |
| 934 | if (*write_result) { |
| 935 | /* Attempt to fault-in and retry if it worked */ |
| 936 | if (!user_event_mm_fault_in(mm: user_mm, uaddr, attempt)) |
| 937 | goto retry; |
| 938 | |
| 939 | kfree(objp: enabler); |
| 940 | enabler = NULL; |
| 941 | } |
| 942 | out: |
| 943 | user_event_mm_put(mm: user_mm); |
| 944 | |
| 945 | return enabler; |
| 946 | } |
| 947 | |
| 948 | static __always_inline __must_check |
| 949 | bool user_event_last_ref(struct user_event *user) |
| 950 | { |
| 951 | int last = 0; |
| 952 | |
| 953 | if (user->reg_flags & USER_EVENT_REG_PERSIST) |
| 954 | last = 1; |
| 955 | |
| 956 | return refcount_read(r: &user->refcnt) == last; |
| 957 | } |
| 958 | |
| 959 | static __always_inline __must_check |
| 960 | size_t copy_nofault(void *addr, size_t bytes, struct iov_iter *i) |
| 961 | { |
| 962 | size_t ret; |
| 963 | |
| 964 | pagefault_disable(); |
| 965 | |
| 966 | ret = copy_from_iter_nocache(addr, bytes, i); |
| 967 | |
| 968 | pagefault_enable(); |
| 969 | |
| 970 | return ret; |
| 971 | } |
| 972 | |
| 973 | static struct list_head *user_event_get_fields(struct trace_event_call *call) |
| 974 | { |
| 975 | struct user_event *user = (struct user_event *)call->data; |
| 976 | |
| 977 | return &user->fields; |
| 978 | } |
| 979 | |
| 980 | /* |
| 981 | * Parses a register command for user_events |
| 982 | * Format: event_name[:FLAG1[,FLAG2...]] [field1[;field2...]] |
| 983 | * |
| 984 | * Example event named 'test' with a 20 char 'msg' field with an unsigned int |
| 985 | * 'id' field after: |
| 986 | * test char[20] msg;unsigned int id |
| 987 | * |
| 988 | * NOTE: Offsets are from the user data perspective, they are not from the |
| 989 | * trace_entry/buffer perspective. We automatically add the common properties |
| 990 | * sizes to the offset for the user. |
| 991 | * |
| 992 | * Upon success user_event has its ref count increased by 1. |
| 993 | */ |
| 994 | static int user_event_parse_cmd(struct user_event_group *group, |
| 995 | char *raw_command, struct user_event **newuser, |
| 996 | int reg_flags) |
| 997 | { |
| 998 | char *name = raw_command; |
| 999 | char *args = strpbrk(name, " " ); |
| 1000 | char *flags; |
| 1001 | |
| 1002 | if (args) |
| 1003 | *args++ = '\0'; |
| 1004 | |
| 1005 | flags = strpbrk(name, ":" ); |
| 1006 | |
| 1007 | if (flags) |
| 1008 | *flags++ = '\0'; |
| 1009 | |
| 1010 | return user_event_parse(group, name, args, flags, newuser, reg_flags); |
| 1011 | } |
| 1012 | |
| 1013 | static int user_field_array_size(const char *type) |
| 1014 | { |
| 1015 | const char *start = strchr(type, '['); |
| 1016 | char val[8]; |
| 1017 | char *bracket; |
| 1018 | int size = 0; |
| 1019 | |
| 1020 | if (start == NULL) |
| 1021 | return -EINVAL; |
| 1022 | |
| 1023 | if (strscpy(val, start + 1, sizeof(val)) <= 0) |
| 1024 | return -EINVAL; |
| 1025 | |
| 1026 | bracket = strchr(val, ']'); |
| 1027 | |
| 1028 | if (!bracket) |
| 1029 | return -EINVAL; |
| 1030 | |
| 1031 | *bracket = '\0'; |
| 1032 | |
| 1033 | if (kstrtouint(s: val, base: 0, res: &size)) |
| 1034 | return -EINVAL; |
| 1035 | |
| 1036 | if (size > MAX_FIELD_ARRAY_SIZE) |
| 1037 | return -EINVAL; |
| 1038 | |
| 1039 | return size; |
| 1040 | } |
| 1041 | |
| 1042 | static int user_field_size(const char *type) |
| 1043 | { |
| 1044 | /* long is not allowed from a user, since it's ambiguous in size */ |
| 1045 | if (strcmp(type, "s64" ) == 0) |
| 1046 | return sizeof(s64); |
| 1047 | if (strcmp(type, "u64" ) == 0) |
| 1048 | return sizeof(u64); |
| 1049 | if (strcmp(type, "s32" ) == 0) |
| 1050 | return sizeof(s32); |
| 1051 | if (strcmp(type, "u32" ) == 0) |
| 1052 | return sizeof(u32); |
| 1053 | if (strcmp(type, "int" ) == 0) |
| 1054 | return sizeof(int); |
| 1055 | if (strcmp(type, "unsigned int" ) == 0) |
| 1056 | return sizeof(unsigned int); |
| 1057 | if (strcmp(type, "s16" ) == 0) |
| 1058 | return sizeof(s16); |
| 1059 | if (strcmp(type, "u16" ) == 0) |
| 1060 | return sizeof(u16); |
| 1061 | if (strcmp(type, "short" ) == 0) |
| 1062 | return sizeof(short); |
| 1063 | if (strcmp(type, "unsigned short" ) == 0) |
| 1064 | return sizeof(unsigned short); |
| 1065 | if (strcmp(type, "s8" ) == 0) |
| 1066 | return sizeof(s8); |
| 1067 | if (strcmp(type, "u8" ) == 0) |
| 1068 | return sizeof(u8); |
| 1069 | if (strcmp(type, "char" ) == 0) |
| 1070 | return sizeof(char); |
| 1071 | if (strcmp(type, "unsigned char" ) == 0) |
| 1072 | return sizeof(unsigned char); |
| 1073 | if (str_has_prefix(str: type, prefix: "char[" )) |
| 1074 | return user_field_array_size(type); |
| 1075 | if (str_has_prefix(str: type, prefix: "unsigned char[" )) |
| 1076 | return user_field_array_size(type); |
| 1077 | if (str_has_prefix(str: type, prefix: "__data_loc " )) |
| 1078 | return sizeof(u32); |
| 1079 | if (str_has_prefix(str: type, prefix: "__rel_loc " )) |
| 1080 | return sizeof(u32); |
| 1081 | |
| 1082 | /* Unknown basic type, error */ |
| 1083 | return -EINVAL; |
| 1084 | } |
| 1085 | |
| 1086 | static void user_event_destroy_validators(struct user_event *user) |
| 1087 | { |
| 1088 | struct user_event_validator *validator, *next; |
| 1089 | struct list_head *head = &user->validators; |
| 1090 | |
| 1091 | list_for_each_entry_safe(validator, next, head, user_event_link) { |
| 1092 | list_del(entry: &validator->user_event_link); |
| 1093 | kfree(objp: validator); |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | static void user_event_destroy_fields(struct user_event *user) |
| 1098 | { |
| 1099 | struct ftrace_event_field *field, *next; |
| 1100 | struct list_head *head = &user->fields; |
| 1101 | |
| 1102 | list_for_each_entry_safe(field, next, head, link) { |
| 1103 | list_del(entry: &field->link); |
| 1104 | kfree(objp: field); |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | static int user_event_add_field(struct user_event *user, const char *type, |
| 1109 | const char *name, int offset, int size, |
| 1110 | int is_signed, int filter_type) |
| 1111 | { |
| 1112 | struct user_event_validator *validator; |
| 1113 | struct ftrace_event_field *field; |
| 1114 | int validator_flags = 0; |
| 1115 | |
| 1116 | field = kmalloc(sizeof(*field), GFP_KERNEL_ACCOUNT); |
| 1117 | |
| 1118 | if (!field) |
| 1119 | return -ENOMEM; |
| 1120 | |
| 1121 | if (str_has_prefix(str: type, prefix: "__data_loc " )) |
| 1122 | goto add_validator; |
| 1123 | |
| 1124 | if (str_has_prefix(str: type, prefix: "__rel_loc " )) { |
| 1125 | validator_flags |= VALIDATOR_REL; |
| 1126 | goto add_validator; |
| 1127 | } |
| 1128 | |
| 1129 | goto add_field; |
| 1130 | |
| 1131 | add_validator: |
| 1132 | if (strstr(type, "char" ) != NULL) |
| 1133 | validator_flags |= VALIDATOR_ENSURE_NULL; |
| 1134 | |
| 1135 | validator = kmalloc(sizeof(*validator), GFP_KERNEL_ACCOUNT); |
| 1136 | |
| 1137 | if (!validator) { |
| 1138 | kfree(objp: field); |
| 1139 | return -ENOMEM; |
| 1140 | } |
| 1141 | |
| 1142 | validator->flags = validator_flags; |
| 1143 | validator->offset = offset; |
| 1144 | |
| 1145 | /* Want sequential access when validating */ |
| 1146 | list_add_tail(new: &validator->user_event_link, head: &user->validators); |
| 1147 | |
| 1148 | add_field: |
| 1149 | field->type = type; |
| 1150 | field->name = name; |
| 1151 | field->offset = offset; |
| 1152 | field->size = size; |
| 1153 | field->is_signed = is_signed; |
| 1154 | field->filter_type = filter_type; |
| 1155 | |
| 1156 | if (filter_type == FILTER_OTHER) |
| 1157 | field->filter_type = filter_assign_type(type); |
| 1158 | |
| 1159 | list_add(new: &field->link, head: &user->fields); |
| 1160 | |
| 1161 | /* |
| 1162 | * Min size from user writes that are required, this does not include |
| 1163 | * the size of trace_entry (common fields). |
| 1164 | */ |
| 1165 | user->min_size = (offset + size) - sizeof(struct trace_entry); |
| 1166 | |
| 1167 | return 0; |
| 1168 | } |
| 1169 | |
| 1170 | /* |
| 1171 | * Parses the values of a field within the description |
| 1172 | * Format: type name [size] |
| 1173 | */ |
| 1174 | static int user_event_parse_field(char *field, struct user_event *user, |
| 1175 | u32 *offset) |
| 1176 | { |
| 1177 | char *part, *type, *name; |
| 1178 | u32 depth = 0, saved_offset = *offset; |
| 1179 | int len, size = -EINVAL; |
| 1180 | bool is_struct = false; |
| 1181 | |
| 1182 | field = skip_spaces(field); |
| 1183 | |
| 1184 | if (*field == '\0') |
| 1185 | return 0; |
| 1186 | |
| 1187 | /* Handle types that have a space within */ |
| 1188 | len = str_has_prefix(str: field, prefix: "unsigned " ); |
| 1189 | if (len) |
| 1190 | goto skip_next; |
| 1191 | |
| 1192 | len = str_has_prefix(str: field, prefix: "struct " ); |
| 1193 | if (len) { |
| 1194 | is_struct = true; |
| 1195 | goto skip_next; |
| 1196 | } |
| 1197 | |
| 1198 | len = str_has_prefix(str: field, prefix: "__data_loc unsigned " ); |
| 1199 | if (len) |
| 1200 | goto skip_next; |
| 1201 | |
| 1202 | len = str_has_prefix(str: field, prefix: "__data_loc " ); |
| 1203 | if (len) |
| 1204 | goto skip_next; |
| 1205 | |
| 1206 | len = str_has_prefix(str: field, prefix: "__rel_loc unsigned " ); |
| 1207 | if (len) |
| 1208 | goto skip_next; |
| 1209 | |
| 1210 | len = str_has_prefix(str: field, prefix: "__rel_loc " ); |
| 1211 | if (len) |
| 1212 | goto skip_next; |
| 1213 | |
| 1214 | goto parse; |
| 1215 | skip_next: |
| 1216 | type = field; |
| 1217 | field = strpbrk(field + len, " " ); |
| 1218 | |
| 1219 | if (field == NULL) |
| 1220 | return -EINVAL; |
| 1221 | |
| 1222 | *field++ = '\0'; |
| 1223 | depth++; |
| 1224 | parse: |
| 1225 | name = NULL; |
| 1226 | |
| 1227 | while ((part = strsep(&field, " " )) != NULL) { |
| 1228 | switch (depth++) { |
| 1229 | case FIELD_DEPTH_TYPE: |
| 1230 | type = part; |
| 1231 | break; |
| 1232 | case FIELD_DEPTH_NAME: |
| 1233 | name = part; |
| 1234 | break; |
| 1235 | case FIELD_DEPTH_SIZE: |
| 1236 | if (!is_struct) |
| 1237 | return -EINVAL; |
| 1238 | |
| 1239 | if (kstrtou32(s: part, base: 10, res: &size)) |
| 1240 | return -EINVAL; |
| 1241 | break; |
| 1242 | default: |
| 1243 | return -EINVAL; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | if (depth < FIELD_DEPTH_SIZE || !name) |
| 1248 | return -EINVAL; |
| 1249 | |
| 1250 | if (depth == FIELD_DEPTH_SIZE) |
| 1251 | size = user_field_size(type); |
| 1252 | |
| 1253 | if (size == 0) |
| 1254 | return -EINVAL; |
| 1255 | |
| 1256 | if (size < 0) |
| 1257 | return size; |
| 1258 | |
| 1259 | *offset = saved_offset + size; |
| 1260 | |
| 1261 | return user_event_add_field(user, type, name, offset: saved_offset, size, |
| 1262 | is_signed: type[0] != 'u', filter_type: FILTER_OTHER); |
| 1263 | } |
| 1264 | |
| 1265 | static int user_event_parse_fields(struct user_event *user, char *args) |
| 1266 | { |
| 1267 | char *field; |
| 1268 | u32 offset = sizeof(struct trace_entry); |
| 1269 | int ret = -EINVAL; |
| 1270 | |
| 1271 | if (args == NULL) |
| 1272 | return 0; |
| 1273 | |
| 1274 | while ((field = strsep(&args, ";" )) != NULL) { |
| 1275 | ret = user_event_parse_field(field, user, offset: &offset); |
| 1276 | |
| 1277 | if (ret) |
| 1278 | break; |
| 1279 | } |
| 1280 | |
| 1281 | return ret; |
| 1282 | } |
| 1283 | |
| 1284 | static struct trace_event_fields user_event_fields_array[1]; |
| 1285 | |
| 1286 | static const char *user_field_format(const char *type) |
| 1287 | { |
| 1288 | if (strcmp(type, "s64" ) == 0) |
| 1289 | return "%lld" ; |
| 1290 | if (strcmp(type, "u64" ) == 0) |
| 1291 | return "%llu" ; |
| 1292 | if (strcmp(type, "s32" ) == 0) |
| 1293 | return "%d" ; |
| 1294 | if (strcmp(type, "u32" ) == 0) |
| 1295 | return "%u" ; |
| 1296 | if (strcmp(type, "int" ) == 0) |
| 1297 | return "%d" ; |
| 1298 | if (strcmp(type, "unsigned int" ) == 0) |
| 1299 | return "%u" ; |
| 1300 | if (strcmp(type, "s16" ) == 0) |
| 1301 | return "%d" ; |
| 1302 | if (strcmp(type, "u16" ) == 0) |
| 1303 | return "%u" ; |
| 1304 | if (strcmp(type, "short" ) == 0) |
| 1305 | return "%d" ; |
| 1306 | if (strcmp(type, "unsigned short" ) == 0) |
| 1307 | return "%u" ; |
| 1308 | if (strcmp(type, "s8" ) == 0) |
| 1309 | return "%d" ; |
| 1310 | if (strcmp(type, "u8" ) == 0) |
| 1311 | return "%u" ; |
| 1312 | if (strcmp(type, "char" ) == 0) |
| 1313 | return "%d" ; |
| 1314 | if (strcmp(type, "unsigned char" ) == 0) |
| 1315 | return "%u" ; |
| 1316 | if (strstr(type, "char[" ) != NULL) |
| 1317 | return "%s" ; |
| 1318 | |
| 1319 | /* Unknown, likely struct, allowed treat as 64-bit */ |
| 1320 | return "%llu" ; |
| 1321 | } |
| 1322 | |
| 1323 | static bool user_field_is_dyn_string(const char *type, const char **str_func) |
| 1324 | { |
| 1325 | if (str_has_prefix(str: type, prefix: "__data_loc " )) { |
| 1326 | *str_func = "__get_str" ; |
| 1327 | goto check; |
| 1328 | } |
| 1329 | |
| 1330 | if (str_has_prefix(str: type, prefix: "__rel_loc " )) { |
| 1331 | *str_func = "__get_rel_str" ; |
| 1332 | goto check; |
| 1333 | } |
| 1334 | |
| 1335 | return false; |
| 1336 | check: |
| 1337 | return strstr(type, "char" ) != NULL; |
| 1338 | } |
| 1339 | |
| 1340 | #define LEN_OR_ZERO (len ? len - pos : 0) |
| 1341 | static int user_dyn_field_set_string(int argc, const char **argv, int *iout, |
| 1342 | char *buf, int len, bool *colon) |
| 1343 | { |
| 1344 | int pos = 0, i = *iout; |
| 1345 | |
| 1346 | *colon = false; |
| 1347 | |
| 1348 | for (; i < argc; ++i) { |
| 1349 | if (i != *iout) |
| 1350 | pos += snprintf(buf: buf + pos, LEN_OR_ZERO, fmt: " " ); |
| 1351 | |
| 1352 | pos += snprintf(buf: buf + pos, LEN_OR_ZERO, fmt: "%s" , argv[i]); |
| 1353 | |
| 1354 | if (strchr(argv[i], ';')) { |
| 1355 | ++i; |
| 1356 | *colon = true; |
| 1357 | break; |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | /* Actual set, advance i */ |
| 1362 | if (len != 0) |
| 1363 | *iout = i; |
| 1364 | |
| 1365 | return pos + 1; |
| 1366 | } |
| 1367 | |
| 1368 | static int user_field_set_string(struct ftrace_event_field *field, |
| 1369 | char *buf, int len, bool colon) |
| 1370 | { |
| 1371 | int pos = 0; |
| 1372 | |
| 1373 | pos += snprintf(buf: buf + pos, LEN_OR_ZERO, fmt: "%s" , field->type); |
| 1374 | pos += snprintf(buf: buf + pos, LEN_OR_ZERO, fmt: " " ); |
| 1375 | pos += snprintf(buf: buf + pos, LEN_OR_ZERO, fmt: "%s" , field->name); |
| 1376 | |
| 1377 | if (str_has_prefix(str: field->type, prefix: "struct " )) |
| 1378 | pos += snprintf(buf: buf + pos, LEN_OR_ZERO, fmt: " %d" , field->size); |
| 1379 | |
| 1380 | if (colon) |
| 1381 | pos += snprintf(buf: buf + pos, LEN_OR_ZERO, fmt: ";" ); |
| 1382 | |
| 1383 | return pos + 1; |
| 1384 | } |
| 1385 | |
| 1386 | static int user_event_set_print_fmt(struct user_event *user, char *buf, int len) |
| 1387 | { |
| 1388 | struct ftrace_event_field *field; |
| 1389 | struct list_head *head = &user->fields; |
| 1390 | int pos = 0, depth = 0; |
| 1391 | const char *str_func; |
| 1392 | |
| 1393 | pos += snprintf(buf: buf + pos, LEN_OR_ZERO, fmt: "\"" ); |
| 1394 | |
| 1395 | list_for_each_entry_reverse(field, head, link) { |
| 1396 | if (depth != 0) |
| 1397 | pos += snprintf(buf: buf + pos, LEN_OR_ZERO, fmt: " " ); |
| 1398 | |
| 1399 | pos += snprintf(buf: buf + pos, LEN_OR_ZERO, fmt: "%s=%s" , |
| 1400 | field->name, user_field_format(type: field->type)); |
| 1401 | |
| 1402 | depth++; |
| 1403 | } |
| 1404 | |
| 1405 | pos += snprintf(buf: buf + pos, LEN_OR_ZERO, fmt: "\"" ); |
| 1406 | |
| 1407 | list_for_each_entry_reverse(field, head, link) { |
| 1408 | if (user_field_is_dyn_string(type: field->type, str_func: &str_func)) |
| 1409 | pos += snprintf(buf: buf + pos, LEN_OR_ZERO, |
| 1410 | fmt: ", %s(%s)" , str_func, field->name); |
| 1411 | else |
| 1412 | pos += snprintf(buf: buf + pos, LEN_OR_ZERO, |
| 1413 | fmt: ", REC->%s" , field->name); |
| 1414 | } |
| 1415 | |
| 1416 | return pos + 1; |
| 1417 | } |
| 1418 | #undef LEN_OR_ZERO |
| 1419 | |
| 1420 | static int user_event_create_print_fmt(struct user_event *user) |
| 1421 | { |
| 1422 | char *print_fmt; |
| 1423 | int len; |
| 1424 | |
| 1425 | len = user_event_set_print_fmt(user, NULL, len: 0); |
| 1426 | |
| 1427 | print_fmt = kmalloc(len, GFP_KERNEL_ACCOUNT); |
| 1428 | |
| 1429 | if (!print_fmt) |
| 1430 | return -ENOMEM; |
| 1431 | |
| 1432 | user_event_set_print_fmt(user, buf: print_fmt, len); |
| 1433 | |
| 1434 | user->call.print_fmt = print_fmt; |
| 1435 | |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
| 1439 | static enum print_line_t user_event_print_trace(struct trace_iterator *iter, |
| 1440 | int flags, |
| 1441 | struct trace_event *event) |
| 1442 | { |
| 1443 | return print_event_fields(iter, event); |
| 1444 | } |
| 1445 | |
| 1446 | static struct trace_event_functions user_event_funcs = { |
| 1447 | .trace = user_event_print_trace, |
| 1448 | }; |
| 1449 | |
| 1450 | static int user_event_set_call_visible(struct user_event *user, bool visible) |
| 1451 | { |
| 1452 | CLASS(prepare_creds, cred)(); |
| 1453 | if (!cred) |
| 1454 | return -ENOMEM; |
| 1455 | |
| 1456 | /* |
| 1457 | * While by default tracefs is locked down, systems can be configured |
| 1458 | * to allow user_event files to be less locked down. The extreme case |
| 1459 | * being "other" has read/write access to user_events_data/status. |
| 1460 | * |
| 1461 | * When not locked down, processes may not have permissions to |
| 1462 | * add/remove calls themselves to tracefs. We need to temporarily |
| 1463 | * switch to root file permission to allow for this scenario. |
| 1464 | */ |
| 1465 | cred->fsuid = GLOBAL_ROOT_UID; |
| 1466 | |
| 1467 | scoped_with_creds(cred) { |
| 1468 | if (visible) |
| 1469 | return trace_add_event_call(&user->call); |
| 1470 | |
| 1471 | return trace_remove_event_call(&user->call); |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | static int destroy_user_event(struct user_event *user) |
| 1476 | { |
| 1477 | int ret = 0; |
| 1478 | |
| 1479 | lockdep_assert_held(&event_mutex); |
| 1480 | |
| 1481 | /* Must destroy fields before call removal */ |
| 1482 | user_event_destroy_fields(user); |
| 1483 | |
| 1484 | ret = user_event_set_call_visible(user, visible: false); |
| 1485 | |
| 1486 | if (ret) |
| 1487 | return ret; |
| 1488 | |
| 1489 | dyn_event_remove(ev: &user->devent); |
| 1490 | hash_del(node: &user->node); |
| 1491 | |
| 1492 | user_event_destroy_validators(user); |
| 1493 | |
| 1494 | /* If we have different names, both must be freed */ |
| 1495 | if (EVENT_NAME(user) != EVENT_TP_NAME(user)) |
| 1496 | kfree(EVENT_TP_NAME(user)); |
| 1497 | |
| 1498 | kfree(objp: user->call.print_fmt); |
| 1499 | kfree(EVENT_NAME(user)); |
| 1500 | kfree(objp: user); |
| 1501 | |
| 1502 | if (current_user_events > 0) |
| 1503 | current_user_events--; |
| 1504 | else |
| 1505 | pr_alert("BUG: Bad current_user_events\n" ); |
| 1506 | |
| 1507 | return ret; |
| 1508 | } |
| 1509 | |
| 1510 | static struct user_event *find_user_event(struct user_event_group *group, |
| 1511 | char *name, int argc, const char **argv, |
| 1512 | u32 flags, u32 *outkey) |
| 1513 | { |
| 1514 | struct user_event *user; |
| 1515 | u32 key = user_event_key(name); |
| 1516 | |
| 1517 | *outkey = key; |
| 1518 | |
| 1519 | hash_for_each_possible(group->register_table, user, node, key) { |
| 1520 | /* |
| 1521 | * Single-format events shouldn't return multi-format |
| 1522 | * events. Callers expect the underlying tracepoint to match |
| 1523 | * the name exactly in these cases. Only check like-formats. |
| 1524 | */ |
| 1525 | if (EVENT_MULTI_FORMAT(flags) != EVENT_MULTI_FORMAT(user->reg_flags)) |
| 1526 | continue; |
| 1527 | |
| 1528 | if (strcmp(EVENT_NAME(user), name)) |
| 1529 | continue; |
| 1530 | |
| 1531 | if (user_fields_match(user, argc, argv)) |
| 1532 | return user_event_get(user); |
| 1533 | |
| 1534 | /* Scan others if this is a multi-format event */ |
| 1535 | if (EVENT_MULTI_FORMAT(flags)) |
| 1536 | continue; |
| 1537 | |
| 1538 | return ERR_PTR(error: -EADDRINUSE); |
| 1539 | } |
| 1540 | |
| 1541 | return NULL; |
| 1542 | } |
| 1543 | |
| 1544 | static int user_event_validate(struct user_event *user, void *data, int len) |
| 1545 | { |
| 1546 | struct list_head *head = &user->validators; |
| 1547 | struct user_event_validator *validator; |
| 1548 | void *pos, *end = data + len; |
| 1549 | u32 loc, offset, size; |
| 1550 | |
| 1551 | list_for_each_entry(validator, head, user_event_link) { |
| 1552 | pos = data + validator->offset; |
| 1553 | |
| 1554 | /* Already done min_size check, no bounds check here */ |
| 1555 | loc = *(u32 *)pos; |
| 1556 | offset = loc & 0xffff; |
| 1557 | size = loc >> 16; |
| 1558 | |
| 1559 | if (likely(validator->flags & VALIDATOR_REL)) |
| 1560 | pos += offset + sizeof(loc); |
| 1561 | else |
| 1562 | pos = data + offset; |
| 1563 | |
| 1564 | pos += size; |
| 1565 | |
| 1566 | if (unlikely(pos > end)) |
| 1567 | return -EFAULT; |
| 1568 | |
| 1569 | if (likely(validator->flags & VALIDATOR_ENSURE_NULL)) |
| 1570 | if (unlikely(*(char *)(pos - 1) != '\0')) |
| 1571 | return -EFAULT; |
| 1572 | } |
| 1573 | |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
| 1577 | /* |
| 1578 | * Writes the user supplied payload out to a trace file. |
| 1579 | */ |
| 1580 | static void user_event_ftrace(struct user_event *user, struct iov_iter *i, |
| 1581 | void *tpdata, bool *faulted) |
| 1582 | { |
| 1583 | struct trace_event_file *file; |
| 1584 | struct trace_entry *entry; |
| 1585 | struct trace_event_buffer event_buffer; |
| 1586 | size_t size = sizeof(*entry) + i->count; |
| 1587 | |
| 1588 | file = (struct trace_event_file *)tpdata; |
| 1589 | |
| 1590 | if (!file || |
| 1591 | !(file->flags & EVENT_FILE_FL_ENABLED) || |
| 1592 | trace_trigger_soft_disabled(file)) |
| 1593 | return; |
| 1594 | |
| 1595 | /* Allocates and fills trace_entry, + 1 of this is data payload */ |
| 1596 | entry = trace_event_buffer_reserve(fbuffer: &event_buffer, trace_file: file, len: size); |
| 1597 | |
| 1598 | if (unlikely(!entry)) |
| 1599 | return; |
| 1600 | |
| 1601 | if (unlikely(i->count != 0 && !copy_nofault(entry + 1, i->count, i))) |
| 1602 | goto discard; |
| 1603 | |
| 1604 | if (!list_empty(head: &user->validators) && |
| 1605 | unlikely(user_event_validate(user, entry, size))) |
| 1606 | goto discard; |
| 1607 | |
| 1608 | trace_event_buffer_commit(fbuffer: &event_buffer); |
| 1609 | |
| 1610 | return; |
| 1611 | discard: |
| 1612 | *faulted = true; |
| 1613 | __trace_event_discard_commit(buffer: event_buffer.buffer, |
| 1614 | event: event_buffer.event); |
| 1615 | } |
| 1616 | |
| 1617 | #ifdef CONFIG_PERF_EVENTS |
| 1618 | /* |
| 1619 | * Writes the user supplied payload out to perf ring buffer. |
| 1620 | */ |
| 1621 | static void user_event_perf(struct user_event *user, struct iov_iter *i, |
| 1622 | void *tpdata, bool *faulted) |
| 1623 | { |
| 1624 | struct hlist_head *perf_head; |
| 1625 | |
| 1626 | perf_head = this_cpu_ptr(user->call.perf_events); |
| 1627 | |
| 1628 | if (perf_head && !hlist_empty(h: perf_head)) { |
| 1629 | struct trace_entry *perf_entry; |
| 1630 | struct pt_regs *regs; |
| 1631 | size_t size = sizeof(*perf_entry) + i->count; |
| 1632 | int context; |
| 1633 | |
| 1634 | perf_entry = perf_trace_buf_alloc(ALIGN(size, 8), |
| 1635 | regs: ®s, rctxp: &context); |
| 1636 | |
| 1637 | if (unlikely(!perf_entry)) |
| 1638 | return; |
| 1639 | |
| 1640 | perf_fetch_caller_regs(regs); |
| 1641 | |
| 1642 | if (unlikely(i->count != 0 && !copy_nofault(perf_entry + 1, i->count, i))) |
| 1643 | goto discard; |
| 1644 | |
| 1645 | if (!list_empty(head: &user->validators) && |
| 1646 | unlikely(user_event_validate(user, perf_entry, size))) |
| 1647 | goto discard; |
| 1648 | |
| 1649 | perf_trace_buf_submit(raw_data: perf_entry, size, rctx: context, |
| 1650 | type: user->call.event.type, count: 1, regs, |
| 1651 | head: perf_head, NULL); |
| 1652 | |
| 1653 | return; |
| 1654 | discard: |
| 1655 | *faulted = true; |
| 1656 | perf_swevent_put_recursion_context(rctx: context); |
| 1657 | } |
| 1658 | } |
| 1659 | #endif |
| 1660 | |
| 1661 | /* |
| 1662 | * Update the enabled bit among all user processes. |
| 1663 | */ |
| 1664 | static void update_enable_bit_for(struct user_event *user) |
| 1665 | { |
| 1666 | struct tracepoint *tp = &user->tracepoint; |
| 1667 | char status = 0; |
| 1668 | |
| 1669 | if (static_key_enabled(&tp->key)) { |
| 1670 | struct tracepoint_func *probe_func_ptr; |
| 1671 | user_event_func_t probe_func; |
| 1672 | |
| 1673 | rcu_read_lock_sched(); |
| 1674 | |
| 1675 | probe_func_ptr = rcu_dereference_sched(tp->funcs); |
| 1676 | |
| 1677 | if (probe_func_ptr) { |
| 1678 | do { |
| 1679 | probe_func = probe_func_ptr->func; |
| 1680 | |
| 1681 | if (probe_func == user_event_ftrace) |
| 1682 | status |= EVENT_STATUS_FTRACE; |
| 1683 | #ifdef CONFIG_PERF_EVENTS |
| 1684 | else if (probe_func == user_event_perf) |
| 1685 | status |= EVENT_STATUS_PERF; |
| 1686 | #endif |
| 1687 | else |
| 1688 | status |= EVENT_STATUS_OTHER; |
| 1689 | } while ((++probe_func_ptr)->func); |
| 1690 | } |
| 1691 | |
| 1692 | rcu_read_unlock_sched(); |
| 1693 | } |
| 1694 | |
| 1695 | user->status = status; |
| 1696 | |
| 1697 | user_event_enabler_update(user); |
| 1698 | } |
| 1699 | |
| 1700 | /* |
| 1701 | * Register callback for our events from tracing sub-systems. |
| 1702 | */ |
| 1703 | static int user_event_reg(struct trace_event_call *call, |
| 1704 | enum trace_reg type, |
| 1705 | void *data) |
| 1706 | { |
| 1707 | struct user_event *user = (struct user_event *)call->data; |
| 1708 | int ret = 0; |
| 1709 | |
| 1710 | if (!user) |
| 1711 | return -ENOENT; |
| 1712 | |
| 1713 | switch (type) { |
| 1714 | case TRACE_REG_REGISTER: |
| 1715 | ret = tracepoint_probe_register(tp: call->tp, |
| 1716 | probe: call->class->probe, |
| 1717 | data); |
| 1718 | if (!ret) |
| 1719 | goto inc; |
| 1720 | break; |
| 1721 | |
| 1722 | case TRACE_REG_UNREGISTER: |
| 1723 | tracepoint_probe_unregister(tp: call->tp, |
| 1724 | probe: call->class->probe, |
| 1725 | data); |
| 1726 | goto dec; |
| 1727 | |
| 1728 | #ifdef CONFIG_PERF_EVENTS |
| 1729 | case TRACE_REG_PERF_REGISTER: |
| 1730 | ret = tracepoint_probe_register(tp: call->tp, |
| 1731 | probe: call->class->perf_probe, |
| 1732 | data); |
| 1733 | if (!ret) |
| 1734 | goto inc; |
| 1735 | break; |
| 1736 | |
| 1737 | case TRACE_REG_PERF_UNREGISTER: |
| 1738 | tracepoint_probe_unregister(tp: call->tp, |
| 1739 | probe: call->class->perf_probe, |
| 1740 | data); |
| 1741 | goto dec; |
| 1742 | |
| 1743 | case TRACE_REG_PERF_OPEN: |
| 1744 | case TRACE_REG_PERF_CLOSE: |
| 1745 | case TRACE_REG_PERF_ADD: |
| 1746 | case TRACE_REG_PERF_DEL: |
| 1747 | break; |
| 1748 | #endif |
| 1749 | } |
| 1750 | |
| 1751 | return ret; |
| 1752 | inc: |
| 1753 | user_event_get(user); |
| 1754 | update_enable_bit_for(user); |
| 1755 | return 0; |
| 1756 | dec: |
| 1757 | update_enable_bit_for(user); |
| 1758 | user_event_put(user, locked: true); |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
| 1762 | static int user_event_create(const char *raw_command) |
| 1763 | { |
| 1764 | struct user_event_group *group; |
| 1765 | struct user_event *user; |
| 1766 | char *name; |
| 1767 | int ret; |
| 1768 | |
| 1769 | if (!str_has_prefix(str: raw_command, USER_EVENTS_PREFIX)) |
| 1770 | return -ECANCELED; |
| 1771 | |
| 1772 | raw_command += USER_EVENTS_PREFIX_LEN; |
| 1773 | raw_command = skip_spaces(raw_command); |
| 1774 | |
| 1775 | name = kstrdup(s: raw_command, GFP_KERNEL_ACCOUNT); |
| 1776 | |
| 1777 | if (!name) |
| 1778 | return -ENOMEM; |
| 1779 | |
| 1780 | group = current_user_event_group(); |
| 1781 | |
| 1782 | if (!group) { |
| 1783 | kfree(objp: name); |
| 1784 | return -ENOENT; |
| 1785 | } |
| 1786 | |
| 1787 | mutex_lock(&group->reg_mutex); |
| 1788 | |
| 1789 | /* Dyn events persist, otherwise they would cleanup immediately */ |
| 1790 | ret = user_event_parse_cmd(group, raw_command: name, newuser: &user, reg_flags: USER_EVENT_REG_PERSIST); |
| 1791 | |
| 1792 | if (!ret) |
| 1793 | user_event_put(user, locked: false); |
| 1794 | |
| 1795 | mutex_unlock(lock: &group->reg_mutex); |
| 1796 | |
| 1797 | if (ret) |
| 1798 | kfree(objp: name); |
| 1799 | |
| 1800 | return ret; |
| 1801 | } |
| 1802 | |
| 1803 | static int user_event_show(struct seq_file *m, struct dyn_event *ev) |
| 1804 | { |
| 1805 | struct user_event *user = container_of(ev, struct user_event, devent); |
| 1806 | struct ftrace_event_field *field; |
| 1807 | struct list_head *head; |
| 1808 | int depth = 0; |
| 1809 | |
| 1810 | seq_printf(m, fmt: "%s%s" , USER_EVENTS_PREFIX, EVENT_NAME(user)); |
| 1811 | |
| 1812 | head = trace_get_fields(event_call: &user->call); |
| 1813 | |
| 1814 | list_for_each_entry_reverse(field, head, link) { |
| 1815 | if (depth == 0) |
| 1816 | seq_puts(m, s: " " ); |
| 1817 | else |
| 1818 | seq_puts(m, s: "; " ); |
| 1819 | |
| 1820 | seq_printf(m, fmt: "%s %s" , field->type, field->name); |
| 1821 | |
| 1822 | if (str_has_prefix(str: field->type, prefix: "struct " )) |
| 1823 | seq_printf(m, fmt: " %d" , field->size); |
| 1824 | |
| 1825 | depth++; |
| 1826 | } |
| 1827 | |
| 1828 | seq_puts(m, s: "\n" ); |
| 1829 | |
| 1830 | return 0; |
| 1831 | } |
| 1832 | |
| 1833 | static bool user_event_is_busy(struct dyn_event *ev) |
| 1834 | { |
| 1835 | struct user_event *user = container_of(ev, struct user_event, devent); |
| 1836 | |
| 1837 | return !user_event_last_ref(user); |
| 1838 | } |
| 1839 | |
| 1840 | static int user_event_free(struct dyn_event *ev) |
| 1841 | { |
| 1842 | struct user_event *user = container_of(ev, struct user_event, devent); |
| 1843 | |
| 1844 | if (!user_event_last_ref(user)) |
| 1845 | return -EBUSY; |
| 1846 | |
| 1847 | if (!user_event_capable(reg_flags: user->reg_flags)) |
| 1848 | return -EPERM; |
| 1849 | |
| 1850 | return destroy_user_event(user); |
| 1851 | } |
| 1852 | |
| 1853 | static bool user_field_match(struct ftrace_event_field *field, int argc, |
| 1854 | const char **argv, int *iout) |
| 1855 | { |
| 1856 | char *field_name = NULL, *dyn_field_name = NULL; |
| 1857 | bool colon = false, match = false; |
| 1858 | int dyn_len, len; |
| 1859 | |
| 1860 | if (*iout >= argc) |
| 1861 | return false; |
| 1862 | |
| 1863 | dyn_len = user_dyn_field_set_string(argc, argv, iout, buf: dyn_field_name, |
| 1864 | len: 0, colon: &colon); |
| 1865 | |
| 1866 | len = user_field_set_string(field, buf: field_name, len: 0, colon); |
| 1867 | |
| 1868 | if (dyn_len != len) |
| 1869 | return false; |
| 1870 | |
| 1871 | dyn_field_name = kmalloc(dyn_len, GFP_KERNEL); |
| 1872 | field_name = kmalloc(len, GFP_KERNEL); |
| 1873 | |
| 1874 | if (!dyn_field_name || !field_name) |
| 1875 | goto out; |
| 1876 | |
| 1877 | user_dyn_field_set_string(argc, argv, iout, buf: dyn_field_name, |
| 1878 | len: dyn_len, colon: &colon); |
| 1879 | |
| 1880 | user_field_set_string(field, buf: field_name, len, colon); |
| 1881 | |
| 1882 | match = strcmp(dyn_field_name, field_name) == 0; |
| 1883 | out: |
| 1884 | kfree(objp: dyn_field_name); |
| 1885 | kfree(objp: field_name); |
| 1886 | |
| 1887 | return match; |
| 1888 | } |
| 1889 | |
| 1890 | static bool user_fields_match(struct user_event *user, int argc, |
| 1891 | const char **argv) |
| 1892 | { |
| 1893 | struct ftrace_event_field *field; |
| 1894 | struct list_head *head = &user->fields; |
| 1895 | int i = 0; |
| 1896 | |
| 1897 | if (argc == 0) |
| 1898 | return list_empty(head); |
| 1899 | |
| 1900 | list_for_each_entry_reverse(field, head, link) { |
| 1901 | if (!user_field_match(field, argc, argv, iout: &i)) |
| 1902 | return false; |
| 1903 | } |
| 1904 | |
| 1905 | if (i != argc) |
| 1906 | return false; |
| 1907 | |
| 1908 | return true; |
| 1909 | } |
| 1910 | |
| 1911 | static bool user_event_match(const char *system, const char *event, |
| 1912 | int argc, const char **argv, struct dyn_event *ev) |
| 1913 | { |
| 1914 | struct user_event *user = container_of(ev, struct user_event, devent); |
| 1915 | bool match; |
| 1916 | |
| 1917 | match = strcmp(EVENT_NAME(user), event) == 0; |
| 1918 | |
| 1919 | if (match && system) { |
| 1920 | match = strcmp(system, user->group->system_name) == 0 || |
| 1921 | strcmp(system, user->group->system_multi_name) == 0; |
| 1922 | } |
| 1923 | |
| 1924 | if (match) |
| 1925 | match = user_fields_match(user, argc, argv); |
| 1926 | |
| 1927 | return match; |
| 1928 | } |
| 1929 | |
| 1930 | static struct dyn_event_operations user_event_dops = { |
| 1931 | .create = user_event_create, |
| 1932 | .show = user_event_show, |
| 1933 | .is_busy = user_event_is_busy, |
| 1934 | .free = user_event_free, |
| 1935 | .match = user_event_match, |
| 1936 | }; |
| 1937 | |
| 1938 | static int user_event_trace_register(struct user_event *user) |
| 1939 | { |
| 1940 | int ret; |
| 1941 | |
| 1942 | ret = register_trace_event(event: &user->call.event); |
| 1943 | |
| 1944 | if (!ret) |
| 1945 | return -ENODEV; |
| 1946 | |
| 1947 | ret = user_event_set_call_visible(user, visible: true); |
| 1948 | |
| 1949 | if (ret) |
| 1950 | unregister_trace_event(event: &user->call.event); |
| 1951 | |
| 1952 | return ret; |
| 1953 | } |
| 1954 | |
| 1955 | static int user_event_set_tp_name(struct user_event *user) |
| 1956 | { |
| 1957 | lockdep_assert_held(&user->group->reg_mutex); |
| 1958 | |
| 1959 | if (EVENT_MULTI_FORMAT(user->reg_flags)) { |
| 1960 | char *multi_name; |
| 1961 | |
| 1962 | multi_name = kasprintf(GFP_KERNEL_ACCOUNT, fmt: "%s.%llx" , |
| 1963 | user->reg_name, user->group->multi_id); |
| 1964 | |
| 1965 | if (!multi_name) |
| 1966 | return -ENOMEM; |
| 1967 | |
| 1968 | user->call.name = multi_name; |
| 1969 | user->tracepoint.name = multi_name; |
| 1970 | |
| 1971 | /* Inc to ensure unique multi-event name next time */ |
| 1972 | user->group->multi_id++; |
| 1973 | } else { |
| 1974 | /* Non Multi-format uses register name */ |
| 1975 | user->call.name = user->reg_name; |
| 1976 | user->tracepoint.name = user->reg_name; |
| 1977 | } |
| 1978 | |
| 1979 | return 0; |
| 1980 | } |
| 1981 | |
| 1982 | /* |
| 1983 | * Counts how many ';' without a trailing space are in the args. |
| 1984 | */ |
| 1985 | static int count_semis_no_space(char *args) |
| 1986 | { |
| 1987 | int count = 0; |
| 1988 | |
| 1989 | while ((args = strchr(args, ';'))) { |
| 1990 | args++; |
| 1991 | |
| 1992 | if (!isspace(*args)) |
| 1993 | count++; |
| 1994 | } |
| 1995 | |
| 1996 | return count; |
| 1997 | } |
| 1998 | |
| 1999 | /* |
| 2000 | * Copies the arguments while ensuring all ';' have a trailing space. |
| 2001 | */ |
| 2002 | static char *insert_space_after_semis(char *args, int count) |
| 2003 | { |
| 2004 | char *fixed, *pos; |
| 2005 | int len; |
| 2006 | |
| 2007 | len = strlen(args) + count; |
| 2008 | fixed = kmalloc(len + 1, GFP_KERNEL); |
| 2009 | |
| 2010 | if (!fixed) |
| 2011 | return NULL; |
| 2012 | |
| 2013 | pos = fixed; |
| 2014 | |
| 2015 | /* Insert a space after ';' if there is no trailing space. */ |
| 2016 | while (*args) { |
| 2017 | *pos = *args++; |
| 2018 | |
| 2019 | if (*pos++ == ';' && !isspace(*args)) |
| 2020 | *pos++ = ' '; |
| 2021 | } |
| 2022 | |
| 2023 | *pos = '\0'; |
| 2024 | |
| 2025 | return fixed; |
| 2026 | } |
| 2027 | |
| 2028 | static char **user_event_argv_split(char *args, int *argc) |
| 2029 | { |
| 2030 | char **split; |
| 2031 | char *fixed; |
| 2032 | int count; |
| 2033 | |
| 2034 | /* Count how many ';' without a trailing space */ |
| 2035 | count = count_semis_no_space(args); |
| 2036 | |
| 2037 | /* No fixup is required */ |
| 2038 | if (!count) |
| 2039 | return argv_split(GFP_KERNEL, str: args, argcp: argc); |
| 2040 | |
| 2041 | /* We must fixup 'field;field' to 'field; field' */ |
| 2042 | fixed = insert_space_after_semis(args, count); |
| 2043 | |
| 2044 | if (!fixed) |
| 2045 | return NULL; |
| 2046 | |
| 2047 | /* We do a normal split afterwards */ |
| 2048 | split = argv_split(GFP_KERNEL, str: fixed, argcp: argc); |
| 2049 | |
| 2050 | /* We can free since argv_split makes a copy */ |
| 2051 | kfree(objp: fixed); |
| 2052 | |
| 2053 | return split; |
| 2054 | } |
| 2055 | |
| 2056 | /* |
| 2057 | * Parses the event name, arguments and flags then registers if successful. |
| 2058 | * The name buffer lifetime is owned by this method for success cases only. |
| 2059 | * Upon success the returned user_event has its ref count increased by 1. |
| 2060 | */ |
| 2061 | static int user_event_parse(struct user_event_group *group, char *name, |
| 2062 | char *args, char *flags, |
| 2063 | struct user_event **newuser, int reg_flags) |
| 2064 | { |
| 2065 | struct user_event *user; |
| 2066 | char **argv = NULL; |
| 2067 | int argc = 0; |
| 2068 | int ret; |
| 2069 | u32 key; |
| 2070 | |
| 2071 | /* Currently don't support any text based flags */ |
| 2072 | if (flags != NULL) |
| 2073 | return -EINVAL; |
| 2074 | |
| 2075 | if (!user_event_capable(reg_flags)) |
| 2076 | return -EPERM; |
| 2077 | |
| 2078 | if (args) { |
| 2079 | argv = user_event_argv_split(args, argc: &argc); |
| 2080 | |
| 2081 | if (!argv) |
| 2082 | return -ENOMEM; |
| 2083 | } |
| 2084 | |
| 2085 | /* Prevent dyn_event from racing */ |
| 2086 | mutex_lock(&event_mutex); |
| 2087 | user = find_user_event(group, name, argc, argv: (const char **)argv, |
| 2088 | flags: reg_flags, outkey: &key); |
| 2089 | mutex_unlock(lock: &event_mutex); |
| 2090 | |
| 2091 | if (argv) |
| 2092 | argv_free(argv); |
| 2093 | |
| 2094 | if (IS_ERR(ptr: user)) |
| 2095 | return PTR_ERR(ptr: user); |
| 2096 | |
| 2097 | if (user) { |
| 2098 | *newuser = user; |
| 2099 | /* |
| 2100 | * Name is allocated by caller, free it since it already exists. |
| 2101 | * Caller only worries about failure cases for freeing. |
| 2102 | */ |
| 2103 | kfree(objp: name); |
| 2104 | |
| 2105 | return 0; |
| 2106 | } |
| 2107 | |
| 2108 | user = kzalloc(sizeof(*user), GFP_KERNEL_ACCOUNT); |
| 2109 | |
| 2110 | if (!user) |
| 2111 | return -ENOMEM; |
| 2112 | |
| 2113 | INIT_LIST_HEAD(list: &user->class.fields); |
| 2114 | INIT_LIST_HEAD(list: &user->fields); |
| 2115 | INIT_LIST_HEAD(list: &user->validators); |
| 2116 | |
| 2117 | user->group = group; |
| 2118 | user->reg_name = name; |
| 2119 | user->reg_flags = reg_flags; |
| 2120 | |
| 2121 | ret = user_event_set_tp_name(user); |
| 2122 | |
| 2123 | if (ret) |
| 2124 | goto put_user; |
| 2125 | |
| 2126 | ret = user_event_parse_fields(user, args); |
| 2127 | |
| 2128 | if (ret) |
| 2129 | goto put_user; |
| 2130 | |
| 2131 | ret = user_event_create_print_fmt(user); |
| 2132 | |
| 2133 | if (ret) |
| 2134 | goto put_user; |
| 2135 | |
| 2136 | user->call.data = user; |
| 2137 | user->call.class = &user->class; |
| 2138 | user->call.flags = TRACE_EVENT_FL_TRACEPOINT; |
| 2139 | user->call.tp = &user->tracepoint; |
| 2140 | user->call.event.funcs = &user_event_funcs; |
| 2141 | |
| 2142 | if (EVENT_MULTI_FORMAT(user->reg_flags)) |
| 2143 | user->class.system = group->system_multi_name; |
| 2144 | else |
| 2145 | user->class.system = group->system_name; |
| 2146 | |
| 2147 | user->class.fields_array = user_event_fields_array; |
| 2148 | user->class.get_fields = user_event_get_fields; |
| 2149 | user->class.reg = user_event_reg; |
| 2150 | user->class.probe = user_event_ftrace; |
| 2151 | #ifdef CONFIG_PERF_EVENTS |
| 2152 | user->class.perf_probe = user_event_perf; |
| 2153 | #endif |
| 2154 | |
| 2155 | mutex_lock(&event_mutex); |
| 2156 | |
| 2157 | if (current_user_events >= max_user_events) { |
| 2158 | ret = -EMFILE; |
| 2159 | goto put_user_lock; |
| 2160 | } |
| 2161 | |
| 2162 | ret = user_event_trace_register(user); |
| 2163 | |
| 2164 | if (ret) |
| 2165 | goto put_user_lock; |
| 2166 | |
| 2167 | if (user->reg_flags & USER_EVENT_REG_PERSIST) { |
| 2168 | /* Ensure we track self ref and caller ref (2) */ |
| 2169 | refcount_set(r: &user->refcnt, n: 2); |
| 2170 | } else { |
| 2171 | /* Ensure we track only caller ref (1) */ |
| 2172 | refcount_set(r: &user->refcnt, n: 1); |
| 2173 | } |
| 2174 | |
| 2175 | dyn_event_init(ev: &user->devent, ops: &user_event_dops); |
| 2176 | dyn_event_add(ev: &user->devent, call: &user->call); |
| 2177 | hash_add(group->register_table, &user->node, key); |
| 2178 | current_user_events++; |
| 2179 | |
| 2180 | mutex_unlock(lock: &event_mutex); |
| 2181 | |
| 2182 | *newuser = user; |
| 2183 | return 0; |
| 2184 | put_user_lock: |
| 2185 | mutex_unlock(lock: &event_mutex); |
| 2186 | put_user: |
| 2187 | user_event_destroy_fields(user); |
| 2188 | user_event_destroy_validators(user); |
| 2189 | kfree(objp: user->call.print_fmt); |
| 2190 | |
| 2191 | /* Caller frees reg_name on error, but not multi-name */ |
| 2192 | if (EVENT_NAME(user) != EVENT_TP_NAME(user)) |
| 2193 | kfree(EVENT_TP_NAME(user)); |
| 2194 | |
| 2195 | kfree(objp: user); |
| 2196 | return ret; |
| 2197 | } |
| 2198 | |
| 2199 | /* |
| 2200 | * Deletes previously created events if they are no longer being used. |
| 2201 | */ |
| 2202 | static int delete_user_event(struct user_event_group *group, char *name) |
| 2203 | { |
| 2204 | struct user_event *user; |
| 2205 | struct hlist_node *tmp; |
| 2206 | u32 key = user_event_key(name); |
| 2207 | int ret = -ENOENT; |
| 2208 | |
| 2209 | /* Attempt to delete all event(s) with the name passed in */ |
| 2210 | hash_for_each_possible_safe(group->register_table, user, tmp, node, key) { |
| 2211 | if (strcmp(EVENT_NAME(user), name)) |
| 2212 | continue; |
| 2213 | |
| 2214 | if (!user_event_last_ref(user)) |
| 2215 | return -EBUSY; |
| 2216 | |
| 2217 | if (!user_event_capable(reg_flags: user->reg_flags)) |
| 2218 | return -EPERM; |
| 2219 | |
| 2220 | ret = destroy_user_event(user); |
| 2221 | |
| 2222 | if (ret) |
| 2223 | goto out; |
| 2224 | } |
| 2225 | out: |
| 2226 | return ret; |
| 2227 | } |
| 2228 | |
| 2229 | /* |
| 2230 | * Validates the user payload and writes via iterator. |
| 2231 | */ |
| 2232 | static ssize_t user_events_write_core(struct file *file, struct iov_iter *i) |
| 2233 | { |
| 2234 | struct user_event_file_info *info = file->private_data; |
| 2235 | struct user_event_refs *refs; |
| 2236 | struct user_event *user = NULL; |
| 2237 | struct tracepoint *tp; |
| 2238 | ssize_t ret = i->count; |
| 2239 | int idx; |
| 2240 | |
| 2241 | if (unlikely(copy_from_iter(&idx, sizeof(idx), i) != sizeof(idx))) |
| 2242 | return -EFAULT; |
| 2243 | |
| 2244 | if (idx < 0) |
| 2245 | return -EINVAL; |
| 2246 | |
| 2247 | rcu_read_lock_sched(); |
| 2248 | |
| 2249 | refs = rcu_dereference_sched(info->refs); |
| 2250 | |
| 2251 | /* |
| 2252 | * The refs->events array is protected by RCU, and new items may be |
| 2253 | * added. But the user retrieved from indexing into the events array |
| 2254 | * shall be immutable while the file is opened. |
| 2255 | */ |
| 2256 | if (likely(refs && idx < refs->count)) |
| 2257 | user = refs->events[idx]; |
| 2258 | |
| 2259 | rcu_read_unlock_sched(); |
| 2260 | |
| 2261 | if (unlikely(user == NULL)) |
| 2262 | return -ENOENT; |
| 2263 | |
| 2264 | if (unlikely(i->count < user->min_size)) |
| 2265 | return -EINVAL; |
| 2266 | |
| 2267 | tp = &user->tracepoint; |
| 2268 | |
| 2269 | /* |
| 2270 | * It's possible key.enabled disables after this check, however |
| 2271 | * we don't mind if a few events are included in this condition. |
| 2272 | */ |
| 2273 | if (likely(static_key_enabled(&tp->key))) { |
| 2274 | struct tracepoint_func *probe_func_ptr; |
| 2275 | user_event_func_t probe_func; |
| 2276 | struct iov_iter copy; |
| 2277 | void *tpdata; |
| 2278 | bool faulted; |
| 2279 | |
| 2280 | if (unlikely(fault_in_iov_iter_readable(i, i->count))) |
| 2281 | return -EFAULT; |
| 2282 | |
| 2283 | faulted = false; |
| 2284 | |
| 2285 | rcu_read_lock_sched(); |
| 2286 | |
| 2287 | probe_func_ptr = rcu_dereference_sched(tp->funcs); |
| 2288 | |
| 2289 | if (probe_func_ptr) { |
| 2290 | do { |
| 2291 | copy = *i; |
| 2292 | probe_func = probe_func_ptr->func; |
| 2293 | tpdata = probe_func_ptr->data; |
| 2294 | probe_func(user, ©, tpdata, &faulted); |
| 2295 | } while ((++probe_func_ptr)->func); |
| 2296 | } |
| 2297 | |
| 2298 | rcu_read_unlock_sched(); |
| 2299 | |
| 2300 | if (unlikely(faulted)) |
| 2301 | return -EFAULT; |
| 2302 | } else |
| 2303 | return -EBADF; |
| 2304 | |
| 2305 | return ret; |
| 2306 | } |
| 2307 | |
| 2308 | static int user_events_open(struct inode *node, struct file *file) |
| 2309 | { |
| 2310 | struct user_event_group *group; |
| 2311 | struct user_event_file_info *info; |
| 2312 | |
| 2313 | group = current_user_event_group(); |
| 2314 | |
| 2315 | if (!group) |
| 2316 | return -ENOENT; |
| 2317 | |
| 2318 | info = kzalloc(sizeof(*info), GFP_KERNEL_ACCOUNT); |
| 2319 | |
| 2320 | if (!info) |
| 2321 | return -ENOMEM; |
| 2322 | |
| 2323 | info->group = group; |
| 2324 | |
| 2325 | file->private_data = info; |
| 2326 | |
| 2327 | return 0; |
| 2328 | } |
| 2329 | |
| 2330 | static ssize_t user_events_write(struct file *file, const char __user *ubuf, |
| 2331 | size_t count, loff_t *ppos) |
| 2332 | { |
| 2333 | struct iov_iter i; |
| 2334 | |
| 2335 | if (unlikely(*ppos != 0)) |
| 2336 | return -EFAULT; |
| 2337 | |
| 2338 | if (unlikely(import_ubuf(ITER_SOURCE, (char __user *)ubuf, count, &i))) |
| 2339 | return -EFAULT; |
| 2340 | |
| 2341 | return user_events_write_core(file, i: &i); |
| 2342 | } |
| 2343 | |
| 2344 | static ssize_t user_events_write_iter(struct kiocb *kp, struct iov_iter *i) |
| 2345 | { |
| 2346 | return user_events_write_core(file: kp->ki_filp, i); |
| 2347 | } |
| 2348 | |
| 2349 | static int user_events_ref_add(struct user_event_file_info *info, |
| 2350 | struct user_event *user) |
| 2351 | { |
| 2352 | struct user_event_group *group = info->group; |
| 2353 | struct user_event_refs *refs, *new_refs; |
| 2354 | int i, size, count = 0; |
| 2355 | |
| 2356 | refs = rcu_dereference_protected(info->refs, |
| 2357 | lockdep_is_held(&group->reg_mutex)); |
| 2358 | |
| 2359 | if (refs) { |
| 2360 | count = refs->count; |
| 2361 | |
| 2362 | for (i = 0; i < count; ++i) |
| 2363 | if (refs->events[i] == user) |
| 2364 | return i; |
| 2365 | } |
| 2366 | |
| 2367 | size = struct_size(refs, events, count + 1); |
| 2368 | |
| 2369 | new_refs = kzalloc(size, GFP_KERNEL_ACCOUNT); |
| 2370 | |
| 2371 | if (!new_refs) |
| 2372 | return -ENOMEM; |
| 2373 | |
| 2374 | new_refs->count = count + 1; |
| 2375 | |
| 2376 | for (i = 0; i < count; ++i) |
| 2377 | new_refs->events[i] = refs->events[i]; |
| 2378 | |
| 2379 | new_refs->events[i] = user_event_get(user); |
| 2380 | |
| 2381 | rcu_assign_pointer(info->refs, new_refs); |
| 2382 | |
| 2383 | if (refs) |
| 2384 | kfree_rcu(refs, rcu); |
| 2385 | |
| 2386 | return i; |
| 2387 | } |
| 2388 | |
| 2389 | static long user_reg_get(struct user_reg __user *ureg, struct user_reg *kreg) |
| 2390 | { |
| 2391 | u32 size; |
| 2392 | long ret; |
| 2393 | |
| 2394 | ret = get_user(size, &ureg->size); |
| 2395 | |
| 2396 | if (ret) |
| 2397 | return ret; |
| 2398 | |
| 2399 | if (size > PAGE_SIZE) |
| 2400 | return -E2BIG; |
| 2401 | |
| 2402 | if (size < offsetofend(struct user_reg, write_index)) |
| 2403 | return -EINVAL; |
| 2404 | |
| 2405 | ret = copy_struct_from_user(dst: kreg, ksize: sizeof(*kreg), src: ureg, usize: size); |
| 2406 | |
| 2407 | if (ret) |
| 2408 | return ret; |
| 2409 | |
| 2410 | /* Ensure only valid flags */ |
| 2411 | if (kreg->flags & ~(USER_EVENT_REG_MAX-1)) |
| 2412 | return -EINVAL; |
| 2413 | |
| 2414 | /* Ensure supported size */ |
| 2415 | switch (kreg->enable_size) { |
| 2416 | case 4: |
| 2417 | /* 32-bit */ |
| 2418 | break; |
| 2419 | #if BITS_PER_LONG >= 64 |
| 2420 | case 8: |
| 2421 | /* 64-bit */ |
| 2422 | break; |
| 2423 | #endif |
| 2424 | default: |
| 2425 | return -EINVAL; |
| 2426 | } |
| 2427 | |
| 2428 | /* Ensure natural alignment */ |
| 2429 | if (kreg->enable_addr % kreg->enable_size) |
| 2430 | return -EINVAL; |
| 2431 | |
| 2432 | /* Ensure bit range for size */ |
| 2433 | if (kreg->enable_bit > (kreg->enable_size * BITS_PER_BYTE) - 1) |
| 2434 | return -EINVAL; |
| 2435 | |
| 2436 | /* Ensure accessible */ |
| 2437 | if (!access_ok((const void __user *)(uintptr_t)kreg->enable_addr, |
| 2438 | kreg->enable_size)) |
| 2439 | return -EFAULT; |
| 2440 | |
| 2441 | kreg->size = size; |
| 2442 | |
| 2443 | return 0; |
| 2444 | } |
| 2445 | |
| 2446 | /* |
| 2447 | * Registers a user_event on behalf of a user process. |
| 2448 | */ |
| 2449 | static long user_events_ioctl_reg(struct user_event_file_info *info, |
| 2450 | unsigned long uarg) |
| 2451 | { |
| 2452 | struct user_reg __user *ureg = (struct user_reg __user *)uarg; |
| 2453 | struct user_reg reg; |
| 2454 | struct user_event *user; |
| 2455 | struct user_event_enabler *enabler; |
| 2456 | char *name; |
| 2457 | long ret; |
| 2458 | int write_result; |
| 2459 | |
| 2460 | ret = user_reg_get(ureg, kreg: ®); |
| 2461 | |
| 2462 | if (ret) |
| 2463 | return ret; |
| 2464 | |
| 2465 | /* |
| 2466 | * Prevent users from using the same address and bit multiple times |
| 2467 | * within the same mm address space. This can cause unexpected behavior |
| 2468 | * for user processes that is far easier to debug if this is explicitly |
| 2469 | * an error upon registering. |
| 2470 | */ |
| 2471 | if (current_user_event_enabler_exists(uaddr: (unsigned long)reg.enable_addr, |
| 2472 | bit: reg.enable_bit)) |
| 2473 | return -EADDRINUSE; |
| 2474 | |
| 2475 | name = strndup_user((const char __user *)(uintptr_t)reg.name_args, |
| 2476 | MAX_EVENT_DESC); |
| 2477 | |
| 2478 | if (IS_ERR(ptr: name)) { |
| 2479 | ret = PTR_ERR(ptr: name); |
| 2480 | return ret; |
| 2481 | } |
| 2482 | |
| 2483 | ret = user_event_parse_cmd(group: info->group, raw_command: name, newuser: &user, reg_flags: reg.flags); |
| 2484 | |
| 2485 | if (ret) { |
| 2486 | kfree(objp: name); |
| 2487 | return ret; |
| 2488 | } |
| 2489 | |
| 2490 | ret = user_events_ref_add(info, user); |
| 2491 | |
| 2492 | /* No longer need parse ref, ref_add either worked or not */ |
| 2493 | user_event_put(user, locked: false); |
| 2494 | |
| 2495 | /* Positive number is index and valid */ |
| 2496 | if (ret < 0) |
| 2497 | return ret; |
| 2498 | |
| 2499 | /* |
| 2500 | * user_events_ref_add succeeded: |
| 2501 | * At this point we have a user_event, it's lifetime is bound by the |
| 2502 | * reference count, not this file. If anything fails, the user_event |
| 2503 | * still has a reference until the file is released. During release |
| 2504 | * any remaining references (from user_events_ref_add) are decremented. |
| 2505 | * |
| 2506 | * Attempt to create an enabler, which too has a lifetime tied in the |
| 2507 | * same way for the event. Once the task that caused the enabler to be |
| 2508 | * created exits or issues exec() then the enablers it has created |
| 2509 | * will be destroyed and the ref to the event will be decremented. |
| 2510 | */ |
| 2511 | enabler = user_event_enabler_create(reg: ®, user, write_result: &write_result); |
| 2512 | |
| 2513 | if (!enabler) |
| 2514 | return -ENOMEM; |
| 2515 | |
| 2516 | /* Write failed/faulted, give error back to caller */ |
| 2517 | if (write_result) |
| 2518 | return write_result; |
| 2519 | |
| 2520 | put_user((u32)ret, &ureg->write_index); |
| 2521 | |
| 2522 | return 0; |
| 2523 | } |
| 2524 | |
| 2525 | /* |
| 2526 | * Deletes a user_event on behalf of a user process. |
| 2527 | */ |
| 2528 | static long user_events_ioctl_del(struct user_event_file_info *info, |
| 2529 | unsigned long uarg) |
| 2530 | { |
| 2531 | void __user *ubuf = (void __user *)uarg; |
| 2532 | char *name; |
| 2533 | long ret; |
| 2534 | |
| 2535 | name = strndup_user(ubuf, MAX_EVENT_DESC); |
| 2536 | |
| 2537 | if (IS_ERR(ptr: name)) |
| 2538 | return PTR_ERR(ptr: name); |
| 2539 | |
| 2540 | /* event_mutex prevents dyn_event from racing */ |
| 2541 | mutex_lock(&event_mutex); |
| 2542 | ret = delete_user_event(group: info->group, name); |
| 2543 | mutex_unlock(lock: &event_mutex); |
| 2544 | |
| 2545 | kfree(objp: name); |
| 2546 | |
| 2547 | return ret; |
| 2548 | } |
| 2549 | |
| 2550 | static long user_unreg_get(struct user_unreg __user *ureg, |
| 2551 | struct user_unreg *kreg) |
| 2552 | { |
| 2553 | u32 size; |
| 2554 | long ret; |
| 2555 | |
| 2556 | ret = get_user(size, &ureg->size); |
| 2557 | |
| 2558 | if (ret) |
| 2559 | return ret; |
| 2560 | |
| 2561 | if (size > PAGE_SIZE) |
| 2562 | return -E2BIG; |
| 2563 | |
| 2564 | if (size < offsetofend(struct user_unreg, disable_addr)) |
| 2565 | return -EINVAL; |
| 2566 | |
| 2567 | ret = copy_struct_from_user(dst: kreg, ksize: sizeof(*kreg), src: ureg, usize: size); |
| 2568 | |
| 2569 | /* Ensure no reserved values, since we don't support any yet */ |
| 2570 | if (kreg->__reserved || kreg->__reserved2) |
| 2571 | return -EINVAL; |
| 2572 | |
| 2573 | return ret; |
| 2574 | } |
| 2575 | |
| 2576 | static int user_event_mm_clear_bit(struct user_event_mm *user_mm, |
| 2577 | unsigned long uaddr, unsigned char bit, |
| 2578 | unsigned long flags) |
| 2579 | { |
| 2580 | struct user_event_enabler enabler; |
| 2581 | int result; |
| 2582 | int attempt = 0; |
| 2583 | |
| 2584 | memset(&enabler, 0, sizeof(enabler)); |
| 2585 | enabler.addr = uaddr; |
| 2586 | enabler.values = bit | flags; |
| 2587 | retry: |
| 2588 | /* Prevents state changes from racing with new enablers */ |
| 2589 | mutex_lock(&event_mutex); |
| 2590 | |
| 2591 | /* Force the bit to be cleared, since no event is attached */ |
| 2592 | mmap_read_lock(mm: user_mm->mm); |
| 2593 | result = user_event_enabler_write(mm: user_mm, enabler: &enabler, fixup_fault: false, attempt: &attempt); |
| 2594 | mmap_read_unlock(mm: user_mm->mm); |
| 2595 | |
| 2596 | mutex_unlock(lock: &event_mutex); |
| 2597 | |
| 2598 | if (result) { |
| 2599 | /* Attempt to fault-in and retry if it worked */ |
| 2600 | if (!user_event_mm_fault_in(mm: user_mm, uaddr, attempt)) |
| 2601 | goto retry; |
| 2602 | } |
| 2603 | |
| 2604 | return result; |
| 2605 | } |
| 2606 | |
| 2607 | /* |
| 2608 | * Unregisters an enablement address/bit within a task/user mm. |
| 2609 | */ |
| 2610 | static long user_events_ioctl_unreg(unsigned long uarg) |
| 2611 | { |
| 2612 | struct user_unreg __user *ureg = (struct user_unreg __user *)uarg; |
| 2613 | struct user_event_mm *mm = current->user_event_mm; |
| 2614 | struct user_event_enabler *enabler, *next; |
| 2615 | struct user_unreg reg; |
| 2616 | unsigned long flags; |
| 2617 | long ret; |
| 2618 | |
| 2619 | ret = user_unreg_get(ureg, kreg: ®); |
| 2620 | |
| 2621 | if (ret) |
| 2622 | return ret; |
| 2623 | |
| 2624 | if (!mm) |
| 2625 | return -ENOENT; |
| 2626 | |
| 2627 | flags = 0; |
| 2628 | ret = -ENOENT; |
| 2629 | |
| 2630 | /* |
| 2631 | * Flags freeing and faulting are used to indicate if the enabler is in |
| 2632 | * use at all. When faulting is set a page-fault is occurring asyncly. |
| 2633 | * During async fault if freeing is set, the enabler will be destroyed. |
| 2634 | * If no async fault is happening, we can destroy it now since we hold |
| 2635 | * the event_mutex during these checks. |
| 2636 | */ |
| 2637 | mutex_lock(&event_mutex); |
| 2638 | |
| 2639 | list_for_each_entry_safe(enabler, next, &mm->enablers, mm_enablers_link) { |
| 2640 | if (enabler->addr == reg.disable_addr && |
| 2641 | ENABLE_BIT(enabler) == reg.disable_bit) { |
| 2642 | set_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(enabler)); |
| 2643 | |
| 2644 | /* We must keep compat flags for the clear */ |
| 2645 | flags |= enabler->values & ENABLE_VAL_COMPAT_MASK; |
| 2646 | |
| 2647 | if (!test_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler))) |
| 2648 | user_event_enabler_destroy(enabler, locked: true); |
| 2649 | |
| 2650 | /* Removed at least one */ |
| 2651 | ret = 0; |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | mutex_unlock(lock: &event_mutex); |
| 2656 | |
| 2657 | /* Ensure bit is now cleared for user, regardless of event status */ |
| 2658 | if (!ret) |
| 2659 | ret = user_event_mm_clear_bit(user_mm: mm, uaddr: reg.disable_addr, |
| 2660 | bit: reg.disable_bit, flags); |
| 2661 | |
| 2662 | return ret; |
| 2663 | } |
| 2664 | |
| 2665 | /* |
| 2666 | * Handles the ioctl from user mode to register or alter operations. |
| 2667 | */ |
| 2668 | static long user_events_ioctl(struct file *file, unsigned int cmd, |
| 2669 | unsigned long uarg) |
| 2670 | { |
| 2671 | struct user_event_file_info *info = file->private_data; |
| 2672 | struct user_event_group *group = info->group; |
| 2673 | long ret = -ENOTTY; |
| 2674 | |
| 2675 | switch (cmd) { |
| 2676 | case DIAG_IOCSREG: |
| 2677 | mutex_lock(&group->reg_mutex); |
| 2678 | ret = user_events_ioctl_reg(info, uarg); |
| 2679 | mutex_unlock(lock: &group->reg_mutex); |
| 2680 | break; |
| 2681 | |
| 2682 | case DIAG_IOCSDEL: |
| 2683 | mutex_lock(&group->reg_mutex); |
| 2684 | ret = user_events_ioctl_del(info, uarg); |
| 2685 | mutex_unlock(lock: &group->reg_mutex); |
| 2686 | break; |
| 2687 | |
| 2688 | case DIAG_IOCSUNREG: |
| 2689 | mutex_lock(&group->reg_mutex); |
| 2690 | ret = user_events_ioctl_unreg(uarg); |
| 2691 | mutex_unlock(lock: &group->reg_mutex); |
| 2692 | break; |
| 2693 | } |
| 2694 | |
| 2695 | return ret; |
| 2696 | } |
| 2697 | |
| 2698 | /* |
| 2699 | * Handles the final close of the file from user mode. |
| 2700 | */ |
| 2701 | static int user_events_release(struct inode *node, struct file *file) |
| 2702 | { |
| 2703 | struct user_event_file_info *info = file->private_data; |
| 2704 | struct user_event_group *group; |
| 2705 | struct user_event_refs *refs; |
| 2706 | int i; |
| 2707 | |
| 2708 | if (!info) |
| 2709 | return -EINVAL; |
| 2710 | |
| 2711 | group = info->group; |
| 2712 | |
| 2713 | /* |
| 2714 | * Ensure refs cannot change under any situation by taking the |
| 2715 | * register mutex during the final freeing of the references. |
| 2716 | */ |
| 2717 | mutex_lock(&group->reg_mutex); |
| 2718 | |
| 2719 | refs = info->refs; |
| 2720 | |
| 2721 | if (!refs) |
| 2722 | goto out; |
| 2723 | |
| 2724 | /* |
| 2725 | * The lifetime of refs has reached an end, it's tied to this file. |
| 2726 | * The underlying user_events are ref counted, and cannot be freed. |
| 2727 | * After this decrement, the user_events may be freed elsewhere. |
| 2728 | */ |
| 2729 | for (i = 0; i < refs->count; ++i) |
| 2730 | user_event_put(user: refs->events[i], locked: false); |
| 2731 | |
| 2732 | out: |
| 2733 | file->private_data = NULL; |
| 2734 | |
| 2735 | mutex_unlock(lock: &group->reg_mutex); |
| 2736 | |
| 2737 | kfree(objp: refs); |
| 2738 | kfree(objp: info); |
| 2739 | |
| 2740 | return 0; |
| 2741 | } |
| 2742 | |
| 2743 | static const struct file_operations user_data_fops = { |
| 2744 | .open = user_events_open, |
| 2745 | .write = user_events_write, |
| 2746 | .write_iter = user_events_write_iter, |
| 2747 | .unlocked_ioctl = user_events_ioctl, |
| 2748 | .release = user_events_release, |
| 2749 | }; |
| 2750 | |
| 2751 | static void *user_seq_start(struct seq_file *m, loff_t *pos) |
| 2752 | { |
| 2753 | if (*pos) |
| 2754 | return NULL; |
| 2755 | |
| 2756 | return (void *)1; |
| 2757 | } |
| 2758 | |
| 2759 | static void *user_seq_next(struct seq_file *m, void *p, loff_t *pos) |
| 2760 | { |
| 2761 | ++*pos; |
| 2762 | return NULL; |
| 2763 | } |
| 2764 | |
| 2765 | static void user_seq_stop(struct seq_file *m, void *p) |
| 2766 | { |
| 2767 | } |
| 2768 | |
| 2769 | static int user_seq_show(struct seq_file *m, void *p) |
| 2770 | { |
| 2771 | struct user_event_group *group = m->private; |
| 2772 | struct user_event *user; |
| 2773 | char status; |
| 2774 | int i, active = 0, busy = 0; |
| 2775 | |
| 2776 | if (!group) |
| 2777 | return -EINVAL; |
| 2778 | |
| 2779 | mutex_lock(&group->reg_mutex); |
| 2780 | |
| 2781 | hash_for_each(group->register_table, i, user, node) { |
| 2782 | status = user->status; |
| 2783 | |
| 2784 | seq_printf(m, fmt: "%s" , EVENT_TP_NAME(user)); |
| 2785 | |
| 2786 | if (status != 0) { |
| 2787 | seq_puts(m, s: " # Used by" ); |
| 2788 | if (status & EVENT_STATUS_FTRACE) |
| 2789 | seq_puts(m, s: " ftrace" ); |
| 2790 | if (status & EVENT_STATUS_PERF) |
| 2791 | seq_puts(m, s: " perf" ); |
| 2792 | if (status & EVENT_STATUS_OTHER) |
| 2793 | seq_puts(m, s: " other" ); |
| 2794 | busy++; |
| 2795 | } |
| 2796 | |
| 2797 | seq_puts(m, s: "\n" ); |
| 2798 | active++; |
| 2799 | } |
| 2800 | |
| 2801 | mutex_unlock(lock: &group->reg_mutex); |
| 2802 | |
| 2803 | seq_puts(m, s: "\n" ); |
| 2804 | seq_printf(m, fmt: "Active: %d\n" , active); |
| 2805 | seq_printf(m, fmt: "Busy: %d\n" , busy); |
| 2806 | |
| 2807 | return 0; |
| 2808 | } |
| 2809 | |
| 2810 | static const struct seq_operations user_seq_ops = { |
| 2811 | .start = user_seq_start, |
| 2812 | .next = user_seq_next, |
| 2813 | .stop = user_seq_stop, |
| 2814 | .show = user_seq_show, |
| 2815 | }; |
| 2816 | |
| 2817 | static int user_status_open(struct inode *node, struct file *file) |
| 2818 | { |
| 2819 | struct user_event_group *group; |
| 2820 | int ret; |
| 2821 | |
| 2822 | group = current_user_event_group(); |
| 2823 | |
| 2824 | if (!group) |
| 2825 | return -ENOENT; |
| 2826 | |
| 2827 | ret = seq_open(file, &user_seq_ops); |
| 2828 | |
| 2829 | if (!ret) { |
| 2830 | /* Chain group to seq_file */ |
| 2831 | struct seq_file *m = file->private_data; |
| 2832 | |
| 2833 | m->private = group; |
| 2834 | } |
| 2835 | |
| 2836 | return ret; |
| 2837 | } |
| 2838 | |
| 2839 | static const struct file_operations user_status_fops = { |
| 2840 | .open = user_status_open, |
| 2841 | .read = seq_read, |
| 2842 | .llseek = seq_lseek, |
| 2843 | .release = seq_release, |
| 2844 | }; |
| 2845 | |
| 2846 | /* |
| 2847 | * Creates a set of tracefs files to allow user mode interactions. |
| 2848 | */ |
| 2849 | static int create_user_tracefs(void) |
| 2850 | { |
| 2851 | struct dentry *edata, *emmap; |
| 2852 | |
| 2853 | edata = tracefs_create_file(name: "user_events_data" , TRACE_MODE_WRITE, |
| 2854 | NULL, NULL, fops: &user_data_fops); |
| 2855 | |
| 2856 | if (!edata) { |
| 2857 | pr_warn("Could not create tracefs 'user_events_data' entry\n" ); |
| 2858 | goto err; |
| 2859 | } |
| 2860 | |
| 2861 | emmap = tracefs_create_file(name: "user_events_status" , TRACE_MODE_READ, |
| 2862 | NULL, NULL, fops: &user_status_fops); |
| 2863 | |
| 2864 | if (!emmap) { |
| 2865 | tracefs_remove(dentry: edata); |
| 2866 | pr_warn("Could not create tracefs 'user_events_mmap' entry\n" ); |
| 2867 | goto err; |
| 2868 | } |
| 2869 | |
| 2870 | return 0; |
| 2871 | err: |
| 2872 | return -ENODEV; |
| 2873 | } |
| 2874 | |
| 2875 | static int set_max_user_events_sysctl(const struct ctl_table *table, int write, |
| 2876 | void *buffer, size_t *lenp, loff_t *ppos) |
| 2877 | { |
| 2878 | int ret; |
| 2879 | |
| 2880 | mutex_lock(&event_mutex); |
| 2881 | |
| 2882 | ret = proc_douintvec(table, write, buffer, lenp, ppos); |
| 2883 | |
| 2884 | mutex_unlock(lock: &event_mutex); |
| 2885 | |
| 2886 | return ret; |
| 2887 | } |
| 2888 | |
| 2889 | static const struct ctl_table user_event_sysctls[] = { |
| 2890 | { |
| 2891 | .procname = "user_events_max" , |
| 2892 | .data = &max_user_events, |
| 2893 | .maxlen = sizeof(unsigned int), |
| 2894 | .mode = 0644, |
| 2895 | .proc_handler = set_max_user_events_sysctl, |
| 2896 | }, |
| 2897 | }; |
| 2898 | |
| 2899 | static int __init trace_events_user_init(void) |
| 2900 | { |
| 2901 | int ret; |
| 2902 | |
| 2903 | fault_cache = KMEM_CACHE(user_event_enabler_fault, 0); |
| 2904 | |
| 2905 | if (!fault_cache) |
| 2906 | return -ENOMEM; |
| 2907 | |
| 2908 | init_group = user_event_group_create(); |
| 2909 | |
| 2910 | if (!init_group) { |
| 2911 | kmem_cache_destroy(s: fault_cache); |
| 2912 | return -ENOMEM; |
| 2913 | } |
| 2914 | |
| 2915 | ret = create_user_tracefs(); |
| 2916 | |
| 2917 | if (ret) { |
| 2918 | pr_warn("user_events could not register with tracefs\n" ); |
| 2919 | user_event_group_destroy(group: init_group); |
| 2920 | kmem_cache_destroy(s: fault_cache); |
| 2921 | init_group = NULL; |
| 2922 | return ret; |
| 2923 | } |
| 2924 | |
| 2925 | if (dyn_event_register(ops: &user_event_dops)) |
| 2926 | pr_warn("user_events could not register with dyn_events\n" ); |
| 2927 | |
| 2928 | register_sysctl_init("kernel" , user_event_sysctls); |
| 2929 | |
| 2930 | return 0; |
| 2931 | } |
| 2932 | |
| 2933 | fs_initcall(trace_events_user_init); |
| 2934 | |