Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tracing/user_events: Update documentation for ABI

The ABI for user_events has changed from mmap() based to remote writes.
Update the documentation to reflect these changes, add new section for
unregistering events since lifetime is now tied to tasks instead of
files.

Link: https://lkml.kernel.org/r/20230328235219.203-10-beaub@linux.microsoft.com

Signed-off-by: Beau Belgrave <beaub@linux.microsoft.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

authored by

Beau Belgrave and committed by
Steven Rostedt (Google)
27dc2ae7 9211ddaa

+104 -77
+104 -77
Documentation/trace/user_events.rst
··· 20 20 21 21 Typically programs will register a set of events that they wish to expose to 22 22 tools that can read trace_events (such as ftrace and perf). The registration 23 - process gives back two ints to the program for each event. The first int is 24 - the status bit. This describes which bit in little-endian format in the 25 - /sys/kernel/tracing/user_events_status file represents this event. The 26 - second int is the write index which describes the data when a write() or 27 - writev() is called on the /sys/kernel/tracing/user_events_data file. 23 + process tells the kernel which address and bit to reflect if any tool has 24 + enabled the event and data should be written. The registration will give back 25 + a write index which describes the data when a write() or writev() is called 26 + on the /sys/kernel/tracing/user_events_data file. 28 27 29 28 The structures referenced in this document are contained within the 30 29 /include/uapi/linux/user_events.h file in the source tree. ··· 40 41 This command takes a packed struct user_reg as an argument:: 41 42 42 43 struct user_reg { 43 - u32 size; 44 - u64 name_args; 45 - u32 status_bit; 46 - u32 write_index; 47 - }; 44 + /* Input: Size of the user_reg structure being used */ 45 + __u32 size; 48 46 49 - The struct user_reg requires two inputs, the first is the size of the structure 50 - to ensure forward and backward compatibility. The second is the command string 51 - to issue for registering. Upon success two outputs are set, the status bit 52 - and the write index. 47 + /* Input: Bit in enable address to use */ 48 + __u8 enable_bit; 49 + 50 + /* Input: Enable size in bytes at address */ 51 + __u8 enable_size; 52 + 53 + /* Input: Flags for future use, set to 0 */ 54 + __u16 flags; 55 + 56 + /* Input: Address to update when enabled */ 57 + __u64 enable_addr; 58 + 59 + /* Input: Pointer to string with event name, description and flags */ 60 + __u64 name_args; 61 + 62 + /* Output: Index of the event to use when writing data */ 63 + __u32 write_index; 64 + } __attribute__((__packed__)); 65 + 66 + The struct user_reg requires all the above inputs to be set appropriately. 67 + 68 + + size: This must be set to sizeof(struct user_reg). 69 + 70 + + enable_bit: The bit to reflect the event status at the address specified by 71 + enable_addr. 72 + 73 + + enable_size: The size of the value specified by enable_addr. 74 + This must be 4 (32-bit) or 8 (64-bit). 64-bit values are only allowed to be 75 + used on 64-bit kernels, however, 32-bit can be used on all kernels. 76 + 77 + + flags: The flags to use, if any. For the initial version this must be 0. 78 + Callers should first attempt to use flags and retry without flags to ensure 79 + support for lower versions of the kernel. If a flag is not supported -EINVAL 80 + is returned. 81 + 82 + + enable_addr: The address of the value to use to reflect event status. This 83 + must be naturally aligned and write accessible within the user program. 84 + 85 + + name_args: The name and arguments to describe the event, see command format 86 + for details. 87 + 88 + Upon successful registration the following is set. 89 + 90 + + write_index: The index to use for this file descriptor that represents this 91 + event when writing out data. The index is unique to this instance of the file 92 + descriptor that was used for the registration. See writing data for details. 53 93 54 94 User based events show up under tracefs like any other event under the 55 95 subsystem named "user_events". This means tools that wish to attach to the 56 96 events need to use /sys/kernel/tracing/events/user_events/[name]/enable 57 97 or perf record -e user_events:[name] when attaching/recording. 58 98 59 - **NOTE:** *The write_index returned is only valid for the FD that was used* 99 + **NOTE:** The event subsystem name by default is "user_events". Callers should 100 + not assume it will always be "user_events". Operators reserve the right in the 101 + future to change the subsystem name per-process to accomodate event isolation. 60 102 61 103 Command Format 62 104 ^^^^^^^^^^^^^^ ··· 134 94 struct mytype myname 20 135 95 136 96 Deleting 137 - ----------- 97 + -------- 138 98 Deleting an event from within a user process is done via ioctl() out to the 139 99 /sys/kernel/tracing/user_events_data file. The command to issue is 140 100 DIAG_IOCSDEL. ··· 144 104 event (in both user and kernel space). User programs should use a separate file 145 105 to request deletes than the one used for registration due to this. 146 106 107 + Unregistering 108 + ------------- 109 + If after registering an event it is no longer wanted to be updated then it can 110 + be disabled via ioctl() out to the /sys/kernel/tracing/user_events_data file. 111 + The command to issue is DIAG_IOCSUNREG. This is different than deleting, where 112 + deleting actually removes the event from the system. Unregistering simply tells 113 + the kernel your process is no longer interested in updates to the event. 114 + 115 + This command takes a packed struct user_unreg as an argument:: 116 + 117 + struct user_unreg { 118 + /* Input: Size of the user_unreg structure being used */ 119 + __u32 size; 120 + 121 + /* Input: Bit to unregister */ 122 + __u8 disable_bit; 123 + 124 + /* Input: Reserved, set to 0 */ 125 + __u8 __reserved; 126 + 127 + /* Input: Reserved, set to 0 */ 128 + __u16 __reserved2; 129 + 130 + /* Input: Address to unregister */ 131 + __u64 disable_addr; 132 + } __attribute__((__packed__)); 133 + 134 + The struct user_unreg requires all the above inputs to be set appropriately. 135 + 136 + + size: This must be set to sizeof(struct user_unreg). 137 + 138 + + disable_bit: This must be set to the bit to disable (same bit that was 139 + previously registered via enable_bit). 140 + 141 + + disable_addr: This must be set to the address to disable (same address that was 142 + previously registered via enable_addr). 143 + 144 + **NOTE:** Events are automatically unregistered when execve() is invoked. During 145 + fork() the registered events will be retained and must be unregistered manually 146 + in each process if wanted. 147 + 147 148 Status 148 149 ------ 149 150 When tools attach/record user based events the status of the event is updated 150 151 in realtime. This allows user programs to only incur the cost of the write() or 151 152 writev() calls when something is actively attached to the event. 152 153 153 - User programs call mmap() on /sys/kernel/tracing/user_events_status to 154 - check the status for each event that is registered. The bit to check in the 155 - file is given back after the register ioctl() via user_reg.status_bit. The bit 156 - is always in little-endian format. Programs can check if the bit is set either 157 - using a byte-wise index with a mask or a long-wise index with a little-endian 158 - mask. 159 - 160 - Currently the size of user_events_status is a single page, however, custom 161 - kernel configurations can change this size to allow more user based events. In 162 - all cases the size of the file is a multiple of a page size. 163 - 164 - For example, if the register ioctl() gives back a status_bit of 3 you would 165 - check byte 0 (3 / 8) of the returned mmap data and then AND the result with 8 166 - (1 << (3 % 8)) to see if anything is attached to that event. 167 - 168 - A byte-wise index check is performed as follows:: 169 - 170 - int index, mask; 171 - char *status_page; 172 - 173 - index = status_bit / 8; 174 - mask = 1 << (status_bit % 8); 175 - 176 - ... 177 - 178 - if (status_page[index] & mask) { 179 - /* Enabled */ 180 - } 181 - 182 - A long-wise index check is performed as follows:: 183 - 184 - #include <asm/bitsperlong.h> 185 - #include <endian.h> 186 - 187 - #if __BITS_PER_LONG == 64 188 - #define endian_swap(x) htole64(x) 189 - #else 190 - #define endian_swap(x) htole32(x) 191 - #endif 192 - 193 - long index, mask, *status_page; 194 - 195 - index = status_bit / __BITS_PER_LONG; 196 - mask = 1L << (status_bit % __BITS_PER_LONG); 197 - mask = endian_swap(mask); 198 - 199 - ... 200 - 201 - if (status_page[index] & mask) { 202 - /* Enabled */ 203 - } 154 + The kernel will update the specified bit that was registered for the event as 155 + tools attach/detach from the event. User programs simply check if the bit is set 156 + to see if something is attached or not. 204 157 205 158 Administrators can easily check the status of all registered events by reading 206 159 the user_events_status file directly via a terminal. The output is as follows:: 207 160 208 - Byte:Name [# Comments] 161 + Name [# Comments] 209 162 ... 210 163 211 164 Active: ActiveCount 212 165 Busy: BusyCount 213 - Max: MaxCount 214 166 215 167 For example, on a system that has a single event the output looks like this:: 216 168 217 - 1:test 169 + test 218 170 219 171 Active: 1 220 172 Busy: 0 221 - Max: 32768 222 173 223 174 If a user enables the user event via ftrace, the output would change to this:: 224 175 225 - 1:test # Used by ftrace 176 + test # Used by ftrace 226 177 227 178 Active: 1 228 179 Busy: 1 229 - Max: 32768 230 - 231 - **NOTE:** *A status bit of 0 will never be returned. This allows user programs 232 - to have a bit that can be used on error cases.* 233 180 234 181 Writing Data 235 182 ------------ ··· 244 217 int src; 245 218 int dst; 246 219 int flags; 247 - }; 220 + } __attribute__((__packed__)); 248 221 249 222 It's advised for user programs to do the following:: 250 223