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