Skip to content

Commit 009a8e6

Browse files
walaclenticularis39
authored andcommitted
rtla: Exit on memory allocation failures during initialization
Most memory allocations in rtla happen during early initialization before any resources are acquired that would require cleanup. In these cases, propagating allocation errors just adds complexity without any benefit. There's nothing to clean up, and the program must exit anyway. This patch introduces fatal allocation wrappers (calloc_fatal, reallocarray_fatal, strdup_fatal) that call fatal() on allocation failure. These wrappers simplify the code by eliminating unnecessary error propagation paths. The patch converts early allocations to use these wrappers in actions_init() and related action functions, osnoise_context_alloc() and osnoise_init_tool(), trace_instance_init() and trace event functions, and parameter structure allocations in main functions. This simplifies the code while maintaining the same behavior: immediate exit on allocation failure during initialization. Allocations that require cleanup, such as those in histogram allocation functions with goto cleanup paths, are left unchanged and continue to return errors. Signed-off-by: Wander Lairson Costa <wander@redhat.com> Link: https://lore.kernel.org/r/20260309195040.1019085-2-wander@redhat.com Signed-off-by: Tomas Glozar <tglozar@redhat.com>
1 parent 458c951 commit 009a8e6

11 files changed

Lines changed: 108 additions & 132 deletions

File tree

tools/tracing/rtla/src/actions.c

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void
1515
actions_init(struct actions *self)
1616
{
1717
self->size = action_default_size;
18-
self->list = calloc(self->size, sizeof(struct action));
18+
self->list = calloc_fatal(self->size, sizeof(struct action));
1919
self->len = 0;
2020
self->continue_flag = false;
2121

@@ -50,8 +50,10 @@ static struct action *
5050
actions_new(struct actions *self)
5151
{
5252
if (self->len >= self->size) {
53-
self->size *= 2;
54-
self->list = realloc(self->list, self->size * sizeof(struct action));
53+
const size_t new_size = self->size * 2;
54+
55+
self->list = reallocarray_fatal(self->list, new_size, sizeof(struct action));
56+
self->size = new_size;
5557
}
5658

5759
return &self->list[self->len++];
@@ -60,25 +62,21 @@ actions_new(struct actions *self)
6062
/*
6163
* actions_add_trace_output - add an action to output trace
6264
*/
63-
int
65+
void
6466
actions_add_trace_output(struct actions *self, const char *trace_output)
6567
{
6668
struct action *action = actions_new(self);
6769

6870
self->present[ACTION_TRACE_OUTPUT] = true;
6971
action->type = ACTION_TRACE_OUTPUT;
70-
action->trace_output = calloc(strlen(trace_output) + 1, sizeof(char));
71-
if (!action->trace_output)
72-
return -1;
72+
action->trace_output = calloc_fatal(strlen(trace_output) + 1, sizeof(char));
7373
strcpy(action->trace_output, trace_output);
74-
75-
return 0;
7674
}
7775

7876
/*
7977
* actions_add_trace_output - add an action to send signal to a process
8078
*/
81-
int
79+
void
8280
actions_add_signal(struct actions *self, int signal, int pid)
8381
{
8482
struct action *action = actions_new(self);
@@ -87,40 +85,32 @@ actions_add_signal(struct actions *self, int signal, int pid)
8785
action->type = ACTION_SIGNAL;
8886
action->signal = signal;
8987
action->pid = pid;
90-
91-
return 0;
9288
}
9389

9490
/*
9591
* actions_add_shell - add an action to execute a shell command
9692
*/
97-
int
93+
void
9894
actions_add_shell(struct actions *self, const char *command)
9995
{
10096
struct action *action = actions_new(self);
10197

10298
self->present[ACTION_SHELL] = true;
10399
action->type = ACTION_SHELL;
104-
action->command = calloc(strlen(command) + 1, sizeof(char));
105-
if (!action->command)
106-
return -1;
100+
action->command = calloc_fatal(strlen(command) + 1, sizeof(char));
107101
strcpy(action->command, command);
108-
109-
return 0;
110102
}
111103

112104
/*
113105
* actions_add_continue - add an action to resume measurement
114106
*/
115-
int
107+
void
116108
actions_add_continue(struct actions *self)
117109
{
118110
struct action *action = actions_new(self);
119111

120112
self->present[ACTION_CONTINUE] = true;
121113
action->type = ACTION_CONTINUE;
122-
123-
return 0;
124114
}
125115

126116
/*
@@ -176,7 +166,8 @@ actions_parse(struct actions *self, const char *trigger, const char *tracefn)
176166
/* Only one argument allowed */
177167
return -1;
178168
}
179-
return actions_add_trace_output(self, trace_output);
169+
actions_add_trace_output(self, trace_output);
170+
break;
180171
case ACTION_SIGNAL:
181172
/* Takes two arguments, num (signal) and pid */
182173
while (token != NULL) {
@@ -200,21 +191,26 @@ actions_parse(struct actions *self, const char *trigger, const char *tracefn)
200191
/* Missing argument */
201192
return -1;
202193

203-
return actions_add_signal(self, signal, pid);
194+
actions_add_signal(self, signal, pid);
195+
break;
204196
case ACTION_SHELL:
205197
if (token == NULL)
206198
return -1;
207-
if (strlen(token) > 8 && strncmp(token, "command=", 8) == 0)
208-
return actions_add_shell(self, token + 8);
209-
return -1;
199+
if (strlen(token) > 8 && strncmp(token, "command=", 8))
200+
return -1;
201+
actions_add_shell(self, token + 8);
202+
break;
210203
case ACTION_CONTINUE:
211204
/* Takes no argument */
212205
if (token != NULL)
213206
return -1;
214-
return actions_add_continue(self);
207+
actions_add_continue(self);
208+
break;
215209
default:
216210
return -1;
217211
}
212+
213+
return 0;
218214
}
219215

220216
/*

tools/tracing/rtla/src/actions.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ struct actions {
4949

5050
void actions_init(struct actions *self);
5151
void actions_destroy(struct actions *self);
52-
int actions_add_trace_output(struct actions *self, const char *trace_output);
53-
int actions_add_signal(struct actions *self, int signal, int pid);
54-
int actions_add_shell(struct actions *self, const char *command);
55-
int actions_add_continue(struct actions *self);
52+
void actions_add_trace_output(struct actions *self, const char *trace_output);
53+
void actions_add_signal(struct actions *self, int signal, int pid);
54+
void actions_add_shell(struct actions *self, const char *command);
55+
void actions_add_continue(struct actions *self);
5656
int actions_parse(struct actions *self, const char *trigger, const char *tracefn);
5757
int actions_perform(struct actions *self);

tools/tracing/rtla/src/osnoise.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -938,9 +938,7 @@ struct osnoise_context *osnoise_context_alloc(void)
938938
{
939939
struct osnoise_context *context;
940940

941-
context = calloc(1, sizeof(*context));
942-
if (!context)
943-
return NULL;
941+
context = calloc_fatal(1, sizeof(*context));
944942

945943
context->orig_stop_us = OSNOISE_OPTION_INIT_VAL;
946944
context->stop_us = OSNOISE_OPTION_INIT_VAL;
@@ -1017,24 +1015,16 @@ void osnoise_destroy_tool(struct osnoise_tool *top)
10171015
struct osnoise_tool *osnoise_init_tool(char *tool_name)
10181016
{
10191017
struct osnoise_tool *top;
1020-
int retval;
1021-
1022-
top = calloc(1, sizeof(*top));
1023-
if (!top)
1024-
return NULL;
10251018

1019+
top = calloc_fatal(1, sizeof(*top));
10261020
top->context = osnoise_context_alloc();
1027-
if (!top->context)
1028-
goto out_err;
10291021

1030-
retval = trace_instance_init(&top->trace, tool_name);
1031-
if (retval)
1032-
goto out_err;
1022+
if (trace_instance_init(&top->trace, tool_name)) {
1023+
osnoise_destroy_tool(top);
1024+
return NULL;
1025+
}
10331026

10341027
return top;
1035-
out_err:
1036-
osnoise_destroy_tool(top);
1037-
return NULL;
10381028
}
10391029

10401030
/*

tools/tracing/rtla/src/osnoise_hist.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,7 @@ static struct common_params
463463
int c;
464464
char *trace_output = NULL;
465465

466-
params = calloc(1, sizeof(*params));
467-
if (!params)
468-
exit(1);
466+
params = calloc_fatal(1, sizeof(*params));
469467

470468
actions_init(&params->common.threshold_actions);
471469
actions_init(&params->common.end_actions);
@@ -575,22 +573,16 @@ static struct common_params
575573
params->common.hist.with_zeros = 1;
576574
break;
577575
case '4': /* trigger */
578-
if (params->common.events) {
579-
retval = trace_event_add_trigger(params->common.events, optarg);
580-
if (retval)
581-
fatal("Error adding trigger %s", optarg);
582-
} else {
576+
if (params->common.events)
577+
trace_event_add_trigger(params->common.events, optarg);
578+
else
583579
fatal("--trigger requires a previous -e");
584-
}
585580
break;
586581
case '5': /* filter */
587-
if (params->common.events) {
588-
retval = trace_event_add_filter(params->common.events, optarg);
589-
if (retval)
590-
fatal("Error adding filter %s", optarg);
591-
} else {
582+
if (params->common.events)
583+
trace_event_add_filter(params->common.events, optarg);
584+
else
592585
fatal("--filter requires a previous -e");
593-
}
594586
break;
595587
case '6':
596588
params->common.warmup = get_llong_from_str(optarg);

tools/tracing/rtla/src/osnoise_top.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,7 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
312312
int c;
313313
char *trace_output = NULL;
314314

315-
params = calloc(1, sizeof(*params));
316-
if (!params)
317-
exit(1);
315+
params = calloc_fatal(1, sizeof(*params));
318316

319317
actions_init(&params->common.threshold_actions);
320318
actions_init(&params->common.end_actions);
@@ -402,22 +400,16 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
402400
params->threshold = get_llong_from_str(optarg);
403401
break;
404402
case '0': /* trigger */
405-
if (params->common.events) {
406-
retval = trace_event_add_trigger(params->common.events, optarg);
407-
if (retval)
408-
fatal("Error adding trigger %s", optarg);
409-
} else {
403+
if (params->common.events)
404+
trace_event_add_trigger(params->common.events, optarg);
405+
else
410406
fatal("--trigger requires a previous -e");
411-
}
412407
break;
413408
case '1': /* filter */
414-
if (params->common.events) {
415-
retval = trace_event_add_filter(params->common.events, optarg);
416-
if (retval)
417-
fatal("Error adding filter %s", optarg);
418-
} else {
409+
if (params->common.events)
410+
trace_event_add_filter(params->common.events, optarg);
411+
else
419412
fatal("--filter requires a previous -e");
420-
}
421413
break;
422414
case '2':
423415
params->common.warmup = get_llong_from_str(optarg);

tools/tracing/rtla/src/timerlat_hist.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -760,9 +760,7 @@ static struct common_params
760760
int c;
761761
char *trace_output = NULL;
762762

763-
params = calloc(1, sizeof(*params));
764-
if (!params)
765-
exit(1);
763+
params = calloc_fatal(1, sizeof(*params));
766764

767765
actions_init(&params->common.threshold_actions);
768766
actions_init(&params->common.end_actions);
@@ -911,22 +909,16 @@ static struct common_params
911909
params->common.hist.with_zeros = 1;
912910
break;
913911
case '6': /* trigger */
914-
if (params->common.events) {
915-
retval = trace_event_add_trigger(params->common.events, optarg);
916-
if (retval)
917-
fatal("Error adding trigger %s", optarg);
918-
} else {
912+
if (params->common.events)
913+
trace_event_add_trigger(params->common.events, optarg);
914+
else
919915
fatal("--trigger requires a previous -e");
920-
}
921916
break;
922917
case '7': /* filter */
923-
if (params->common.events) {
924-
retval = trace_event_add_filter(params->common.events, optarg);
925-
if (retval)
926-
fatal("Error adding filter %s", optarg);
927-
} else {
918+
if (params->common.events)
919+
trace_event_add_filter(params->common.events, optarg);
920+
else
928921
fatal("--filter requires a previous -e");
929-
}
930922
break;
931923
case '8':
932924
params->dma_latency = get_llong_from_str(optarg);

tools/tracing/rtla/src/timerlat_top.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,7 @@ static struct common_params
526526
int c;
527527
char *trace_output = NULL;
528528

529-
params = calloc(1, sizeof(*params));
530-
if (!params)
531-
exit(1);
529+
params = calloc_fatal(1, sizeof(*params));
532530

533531
actions_init(&params->common.threshold_actions);
534532
actions_init(&params->common.end_actions);
@@ -656,22 +654,16 @@ static struct common_params
656654
params->common.user_data = true;
657655
break;
658656
case '0': /* trigger */
659-
if (params->common.events) {
660-
retval = trace_event_add_trigger(params->common.events, optarg);
661-
if (retval)
662-
fatal("Error adding trigger %s", optarg);
663-
} else {
657+
if (params->common.events)
658+
trace_event_add_trigger(params->common.events, optarg);
659+
else
664660
fatal("--trigger requires a previous -e");
665-
}
666661
break;
667662
case '1': /* filter */
668-
if (params->common.events) {
669-
retval = trace_event_add_filter(params->common.events, optarg);
670-
if (retval)
671-
fatal("Error adding filter %s", optarg);
672-
} else {
663+
if (params->common.events)
664+
trace_event_add_filter(params->common.events, optarg);
665+
else
673666
fatal("--filter requires a previous -e");
674-
}
675667
break;
676668
case '2': /* dma-latency */
677669
params->dma_latency = get_llong_from_str(optarg);

0 commit comments

Comments
 (0)