49
49
#define EVENT_STATUS_PERF BIT(1)
50
50
#define EVENT_STATUS_OTHER BIT(7)
51
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
+
52
64
/*
53
65
* Stores the system name, tables, and locks for a group of events. This
54
66
* allows isolation for events by various means.
@@ -85,6 +97,7 @@ struct user_event {
85
97
struct hlist_node node ;
86
98
struct list_head fields ;
87
99
struct list_head validators ;
100
+ struct work_struct put_work ;
88
101
refcount_t refcnt ;
89
102
int min_size ;
90
103
int reg_flags ;
@@ -171,6 +184,7 @@ static int user_event_parse(struct user_event_group *group, char *name,
171
184
static struct user_event_mm * user_event_mm_get (struct user_event_mm * mm );
172
185
static struct user_event_mm * user_event_mm_get_all (struct user_event * user );
173
186
static void user_event_mm_put (struct user_event_mm * mm );
187
+ static int destroy_user_event (struct user_event * user );
174
188
175
189
static u32 user_event_key (char * name )
176
190
{
@@ -184,19 +198,103 @@ static struct user_event *user_event_get(struct user_event *user)
184
198
return user ;
185
199
}
186
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
+
187
225
static void user_event_put (struct user_event * user , bool locked )
188
226
{
189
- #ifdef CONFIG_LOCKDEP
190
- if (locked )
191
- lockdep_assert_held (& event_mutex );
192
- else
193
- lockdep_assert_not_held (& event_mutex );
194
- #endif
227
+ bool delete ;
195
228
196
229
if (unlikely (!user ))
197
230
return ;
198
231
199
- refcount_dec (& user -> refcnt );
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 );
200
298
}
201
299
202
300
static void user_event_group_destroy (struct user_event_group * group )
@@ -793,7 +891,12 @@ static struct user_event_enabler
793
891
static __always_inline __must_check
794
892
bool user_event_last_ref (struct user_event * user )
795
893
{
796
- return refcount_read (& user -> refcnt ) == 1 ;
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 ;
797
900
}
798
901
799
902
static __always_inline __must_check
@@ -1609,7 +1712,8 @@ static int user_event_create(const char *raw_command)
1609
1712
1610
1713
mutex_lock (& group -> reg_mutex );
1611
1714
1612
- ret = user_event_parse_cmd (group , name , & user , 0 );
1715
+ /* Dyn events persist, otherwise they would cleanup immediately */
1716
+ ret = user_event_parse_cmd (group , name , & user , USER_EVENT_REG_PERSIST );
1613
1717
1614
1718
if (!ret )
1615
1719
user_event_put (user , false);
@@ -1780,6 +1884,10 @@ static int user_event_parse(struct user_event_group *group, char *name,
1780
1884
int argc = 0 ;
1781
1885
char * * argv ;
1782
1886
1887
+ /* User register flags are not ready yet */
1888
+ if (reg_flags != 0 || flags != NULL )
1889
+ return - EINVAL ;
1890
+
1783
1891
/* Prevent dyn_event from racing */
1784
1892
mutex_lock (& event_mutex );
1785
1893
user = find_user_event (group , name , & key );
@@ -1869,8 +1977,13 @@ static int user_event_parse(struct user_event_group *group, char *name,
1869
1977
1870
1978
user -> reg_flags = reg_flags ;
1871
1979
1872
- /* Ensure we track self ref and caller ref (2) */
1873
- refcount_set (& user -> refcnt , 2 );
1980
+ if (user -> reg_flags & USER_EVENT_REG_PERSIST ) {
1981
+ /* Ensure we track self ref and caller ref (2) */
1982
+ refcount_set (& user -> refcnt , 2 );
1983
+ } else {
1984
+ /* Ensure we track only caller ref (1) */
1985
+ refcount_set (& user -> refcnt , 1 );
1986
+ }
1874
1987
1875
1988
dyn_event_init (& user -> devent , & user_event_dops );
1876
1989
dyn_event_add (& user -> devent , & user -> call );
@@ -2092,8 +2205,8 @@ static long user_reg_get(struct user_reg __user *ureg, struct user_reg *kreg)
2092
2205
if (ret )
2093
2206
return ret ;
2094
2207
2095
- /* Ensure no flags, since we don't support any yet */
2096
- if (kreg -> flags != 0 )
2208
+ /* Ensure only valid flags */
2209
+ if (kreg -> flags & ~( USER_EVENT_REG_MAX - 1 ) )
2097
2210
return - EINVAL ;
2098
2211
2099
2212
/* Ensure supported size */
0 commit comments