xref: /linux-6.15/include/linux/user_events.h (revision 0da4cebe)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2022, Microsoft Corporation.
4  *
5  * Authors:
6  *   Beau Belgrave <[email protected]>
7  */
8 
9 #ifndef _LINUX_USER_EVENTS_H
10 #define _LINUX_USER_EVENTS_H
11 
12 #include <linux/list.h>
13 #include <linux/refcount.h>
14 #include <linux/mm_types.h>
15 #include <linux/workqueue.h>
16 #include <uapi/linux/user_events.h>
17 
18 #ifdef CONFIG_USER_EVENTS
19 struct user_event_mm {
20 	struct list_head	link;
21 	struct list_head	enablers;
22 	struct mm_struct	*mm;
23 	struct user_event_mm	*next;
24 	refcount_t		refcnt;
25 	refcount_t		tasks;
26 	struct rcu_work		put_rwork;
27 };
28 
29 extern void user_event_mm_dup(struct task_struct *t,
30 			      struct user_event_mm *old_mm);
31 
32 extern void user_event_mm_remove(struct task_struct *t);
33 
34 static inline void user_events_fork(struct task_struct *t,
35 				    unsigned long clone_flags)
36 {
37 	struct user_event_mm *old_mm;
38 
39 	if (!t || !current->user_event_mm)
40 		return;
41 
42 	old_mm = current->user_event_mm;
43 
44 	if (clone_flags & CLONE_VM) {
45 		t->user_event_mm = old_mm;
46 		refcount_inc(&old_mm->tasks);
47 		return;
48 	}
49 
50 	user_event_mm_dup(t, old_mm);
51 }
52 
53 static inline void user_events_execve(struct task_struct *t)
54 {
55 	if (!t || !t->user_event_mm)
56 		return;
57 
58 	user_event_mm_remove(t);
59 }
60 
61 static inline void user_events_exit(struct task_struct *t)
62 {
63 	if (!t || !t->user_event_mm)
64 		return;
65 
66 	user_event_mm_remove(t);
67 }
68 #else
69 static inline void user_events_fork(struct task_struct *t,
70 				    unsigned long clone_flags)
71 {
72 }
73 
74 static inline void user_events_execve(struct task_struct *t)
75 {
76 }
77 
78 static inline void user_events_exit(struct task_struct *t)
79 {
80 }
81 #endif /* CONFIG_USER_EVENTS */
82 
83 #endif /* _LINUX_USER_EVENTS_H */
84