Skip to content

Commit 63c7264

Browse files
committed
tracing: Switch trace_events_trigger.c code over to use guard()
There are a few functions in trace_events_trigger.c that have "goto out" or equivalent on error in order to release locks that were taken. This can be error prone or just simply make the code more complex. Switch every location that ends with unlocking a mutex on error over to using the guard(mutex)() infrastructure to let the compiler worry about releasing locks. This makes the code easier to read and understand. Also use __free() for free a temporary buffer in event_trigger_regex_write(). Cc: Masami Hiramatsu <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/[email protected] Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 2b36a97 commit 63c7264

File tree

1 file changed

+23
-44
lines changed

1 file changed

+23
-44
lines changed

kernel/trace/trace_events_trigger.c

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,10 @@ static int event_trigger_regex_open(struct inode *inode, struct file *file)
211211
if (ret)
212212
return ret;
213213

214-
mutex_lock(&event_mutex);
214+
guard(mutex)(&event_mutex);
215215

216-
if (unlikely(!event_file_file(file))) {
217-
mutex_unlock(&event_mutex);
216+
if (unlikely(!event_file_file(file)))
218217
return -ENODEV;
219-
}
220218

221219
if ((file->f_mode & FMODE_WRITE) &&
222220
(file->f_flags & O_TRUNC)) {
@@ -239,16 +237,13 @@ static int event_trigger_regex_open(struct inode *inode, struct file *file)
239237
}
240238
}
241239

242-
mutex_unlock(&event_mutex);
243-
244240
return ret;
245241
}
246242

247243
int trigger_process_regex(struct trace_event_file *file, char *buff)
248244
{
249245
char *command, *next;
250246
struct event_command *p;
251-
int ret = -EINVAL;
252247

253248
next = buff = skip_spaces(buff);
254249
command = strsep(&next, ": \t");
@@ -259,17 +254,14 @@ int trigger_process_regex(struct trace_event_file *file, char *buff)
259254
}
260255
command = (command[0] != '!') ? command : command + 1;
261256

262-
mutex_lock(&trigger_cmd_mutex);
257+
guard(mutex)(&trigger_cmd_mutex);
258+
263259
list_for_each_entry(p, &trigger_commands, list) {
264-
if (strcmp(p->name, command) == 0) {
265-
ret = p->parse(p, file, buff, command, next);
266-
goto out_unlock;
267-
}
260+
if (strcmp(p->name, command) == 0)
261+
return p->parse(p, file, buff, command, next);
268262
}
269-
out_unlock:
270-
mutex_unlock(&trigger_cmd_mutex);
271263

272-
return ret;
264+
return -EINVAL;
273265
}
274266

275267
static ssize_t event_trigger_regex_write(struct file *file,
@@ -278,7 +270,7 @@ static ssize_t event_trigger_regex_write(struct file *file,
278270
{
279271
struct trace_event_file *event_file;
280272
ssize_t ret;
281-
char *buf;
273+
char *buf __free(kfree) = NULL;
282274

283275
if (!cnt)
284276
return 0;
@@ -292,24 +284,18 @@ static ssize_t event_trigger_regex_write(struct file *file,
292284

293285
strim(buf);
294286

295-
mutex_lock(&event_mutex);
287+
guard(mutex)(&event_mutex);
288+
296289
event_file = event_file_file(file);
297-
if (unlikely(!event_file)) {
298-
mutex_unlock(&event_mutex);
299-
kfree(buf);
290+
if (unlikely(!event_file))
300291
return -ENODEV;
301-
}
302-
ret = trigger_process_regex(event_file, buf);
303-
mutex_unlock(&event_mutex);
304292

305-
kfree(buf);
293+
ret = trigger_process_regex(event_file, buf);
306294
if (ret < 0)
307-
goto out;
295+
return ret;
308296

309297
*ppos += cnt;
310-
ret = cnt;
311-
out:
312-
return ret;
298+
return cnt;
313299
}
314300

315301
static int event_trigger_regex_release(struct inode *inode, struct file *file)
@@ -359,20 +345,16 @@ const struct file_operations event_trigger_fops = {
359345
__init int register_event_command(struct event_command *cmd)
360346
{
361347
struct event_command *p;
362-
int ret = 0;
363348

364-
mutex_lock(&trigger_cmd_mutex);
349+
guard(mutex)(&trigger_cmd_mutex);
350+
365351
list_for_each_entry(p, &trigger_commands, list) {
366-
if (strcmp(cmd->name, p->name) == 0) {
367-
ret = -EBUSY;
368-
goto out_unlock;
369-
}
352+
if (strcmp(cmd->name, p->name) == 0)
353+
return -EBUSY;
370354
}
371355
list_add(&cmd->list, &trigger_commands);
372-
out_unlock:
373-
mutex_unlock(&trigger_cmd_mutex);
374356

375-
return ret;
357+
return 0;
376358
}
377359

378360
/*
@@ -382,20 +364,17 @@ __init int register_event_command(struct event_command *cmd)
382364
__init int unregister_event_command(struct event_command *cmd)
383365
{
384366
struct event_command *p, *n;
385-
int ret = -ENODEV;
386367

387-
mutex_lock(&trigger_cmd_mutex);
368+
guard(mutex)(&trigger_cmd_mutex);
369+
388370
list_for_each_entry_safe(p, n, &trigger_commands, list) {
389371
if (strcmp(cmd->name, p->name) == 0) {
390-
ret = 0;
391372
list_del_init(&p->list);
392-
goto out_unlock;
373+
return 0;
393374
}
394375
}
395-
out_unlock:
396-
mutex_unlock(&trigger_cmd_mutex);
397376

398-
return ret;
377+
return -ENODEV;
399378
}
400379

401380
/**

0 commit comments

Comments
 (0)