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

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Wander Lairson Costa and committed by
Tomas Glozar
009a8e68 458c9519

+108 -132
+23 -27
tools/tracing/rtla/src/actions.c
··· 15 15 actions_init(struct actions *self) 16 16 { 17 17 self->size = action_default_size; 18 - self->list = calloc(self->size, sizeof(struct action)); 18 + self->list = calloc_fatal(self->size, sizeof(struct action)); 19 19 self->len = 0; 20 20 self->continue_flag = false; 21 21 ··· 50 50 actions_new(struct actions *self) 51 51 { 52 52 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; 55 57 } 56 58 57 59 return &self->list[self->len++]; ··· 62 60 /* 63 61 * actions_add_trace_output - add an action to output trace 64 62 */ 65 - int 63 + void 66 64 actions_add_trace_output(struct actions *self, const char *trace_output) 67 65 { 68 66 struct action *action = actions_new(self); 69 67 70 68 self->present[ACTION_TRACE_OUTPUT] = true; 71 69 action->type = ACTION_TRACE_OUTPUT; 72 - action->trace_output = calloc(strlen(trace_output) + 1, sizeof(char)); 73 - if (!action->trace_output) 74 - return -1; 70 + action->trace_output = calloc_fatal(strlen(trace_output) + 1, sizeof(char)); 75 71 strcpy(action->trace_output, trace_output); 76 - 77 - return 0; 78 72 } 79 73 80 74 /* 81 75 * actions_add_trace_output - add an action to send signal to a process 82 76 */ 83 - int 77 + void 84 78 actions_add_signal(struct actions *self, int signal, int pid) 85 79 { 86 80 struct action *action = actions_new(self); ··· 85 87 action->type = ACTION_SIGNAL; 86 88 action->signal = signal; 87 89 action->pid = pid; 88 - 89 - return 0; 90 90 } 91 91 92 92 /* 93 93 * actions_add_shell - add an action to execute a shell command 94 94 */ 95 - int 95 + void 96 96 actions_add_shell(struct actions *self, const char *command) 97 97 { 98 98 struct action *action = actions_new(self); 99 99 100 100 self->present[ACTION_SHELL] = true; 101 101 action->type = ACTION_SHELL; 102 - action->command = calloc(strlen(command) + 1, sizeof(char)); 103 - if (!action->command) 104 - return -1; 102 + action->command = calloc_fatal(strlen(command) + 1, sizeof(char)); 105 103 strcpy(action->command, command); 106 - 107 - return 0; 108 104 } 109 105 110 106 /* 111 107 * actions_add_continue - add an action to resume measurement 112 108 */ 113 - int 109 + void 114 110 actions_add_continue(struct actions *self) 115 111 { 116 112 struct action *action = actions_new(self); 117 113 118 114 self->present[ACTION_CONTINUE] = true; 119 115 action->type = ACTION_CONTINUE; 120 - 121 - return 0; 122 116 } 123 117 124 118 /* ··· 166 176 /* Only one argument allowed */ 167 177 return -1; 168 178 } 169 - return actions_add_trace_output(self, trace_output); 179 + actions_add_trace_output(self, trace_output); 180 + break; 170 181 case ACTION_SIGNAL: 171 182 /* Takes two arguments, num (signal) and pid */ 172 183 while (token != NULL) { ··· 191 200 /* Missing argument */ 192 201 return -1; 193 202 194 - return actions_add_signal(self, signal, pid); 203 + actions_add_signal(self, signal, pid); 204 + break; 195 205 case ACTION_SHELL: 196 206 if (token == NULL) 197 207 return -1; 198 - if (strlen(token) > 8 && strncmp(token, "command=", 8) == 0) 199 - return actions_add_shell(self, token + 8); 200 - return -1; 208 + if (strlen(token) > 8 && strncmp(token, "command=", 8)) 209 + return -1; 210 + actions_add_shell(self, token + 8); 211 + break; 201 212 case ACTION_CONTINUE: 202 213 /* Takes no argument */ 203 214 if (token != NULL) 204 215 return -1; 205 - return actions_add_continue(self); 216 + actions_add_continue(self); 217 + break; 206 218 default: 207 219 return -1; 208 220 } 221 + 222 + return 0; 209 223 } 210 224 211 225 /*
+4 -4
tools/tracing/rtla/src/actions.h
··· 49 49 50 50 void actions_init(struct actions *self); 51 51 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); 56 56 int actions_parse(struct actions *self, const char *trigger, const char *tracefn); 57 57 int actions_perform(struct actions *self);
+6 -16
tools/tracing/rtla/src/osnoise.c
··· 938 938 { 939 939 struct osnoise_context *context; 940 940 941 - context = calloc(1, sizeof(*context)); 942 - if (!context) 943 - return NULL; 941 + context = calloc_fatal(1, sizeof(*context)); 944 942 945 943 context->orig_stop_us = OSNOISE_OPTION_INIT_VAL; 946 944 context->stop_us = OSNOISE_OPTION_INIT_VAL; ··· 1015 1017 struct osnoise_tool *osnoise_init_tool(char *tool_name) 1016 1018 { 1017 1019 struct osnoise_tool *top; 1018 - int retval; 1019 1020 1020 - top = calloc(1, sizeof(*top)); 1021 - if (!top) 1022 - return NULL; 1023 - 1021 + top = calloc_fatal(1, sizeof(*top)); 1024 1022 top->context = osnoise_context_alloc(); 1025 - if (!top->context) 1026 - goto out_err; 1027 1023 1028 - retval = trace_instance_init(&top->trace, tool_name); 1029 - if (retval) 1030 - goto out_err; 1024 + if (trace_instance_init(&top->trace, tool_name)) { 1025 + osnoise_destroy_tool(top); 1026 + return NULL; 1027 + } 1031 1028 1032 1029 return top; 1033 - out_err: 1034 - osnoise_destroy_tool(top); 1035 - return NULL; 1036 1030 } 1037 1031 1038 1032 /*
+7 -15
tools/tracing/rtla/src/osnoise_hist.c
··· 463 463 int c; 464 464 char *trace_output = NULL; 465 465 466 - params = calloc(1, sizeof(*params)); 467 - if (!params) 468 - exit(1); 466 + params = calloc_fatal(1, sizeof(*params)); 469 467 470 468 actions_init(&params->common.threshold_actions); 471 469 actions_init(&params->common.end_actions); ··· 573 575 params->common.hist.with_zeros = 1; 574 576 break; 575 577 case '4': /* trigger */ 576 - if (params->common.events) { 577 - retval = trace_event_add_trigger(params->common.events, optarg); 578 - if (retval) 579 - fatal("Error adding trigger %s", optarg); 580 - } else { 578 + if (params->common.events) 579 + trace_event_add_trigger(params->common.events, optarg); 580 + else 581 581 fatal("--trigger requires a previous -e"); 582 - } 583 582 break; 584 583 case '5': /* filter */ 585 - if (params->common.events) { 586 - retval = trace_event_add_filter(params->common.events, optarg); 587 - if (retval) 588 - fatal("Error adding filter %s", optarg); 589 - } else { 584 + if (params->common.events) 585 + trace_event_add_filter(params->common.events, optarg); 586 + else 590 587 fatal("--filter requires a previous -e"); 591 - } 592 588 break; 593 589 case '6': 594 590 params->common.warmup = get_llong_from_str(optarg);
+7 -15
tools/tracing/rtla/src/osnoise_top.c
··· 312 312 int c; 313 313 char *trace_output = NULL; 314 314 315 - params = calloc(1, sizeof(*params)); 316 - if (!params) 317 - exit(1); 315 + params = calloc_fatal(1, sizeof(*params)); 318 316 319 317 actions_init(&params->common.threshold_actions); 320 318 actions_init(&params->common.end_actions); ··· 400 402 params->threshold = get_llong_from_str(optarg); 401 403 break; 402 404 case '0': /* trigger */ 403 - if (params->common.events) { 404 - retval = trace_event_add_trigger(params->common.events, optarg); 405 - if (retval) 406 - fatal("Error adding trigger %s", optarg); 407 - } else { 405 + if (params->common.events) 406 + trace_event_add_trigger(params->common.events, optarg); 407 + else 408 408 fatal("--trigger requires a previous -e"); 409 - } 410 409 break; 411 410 case '1': /* filter */ 412 - if (params->common.events) { 413 - retval = trace_event_add_filter(params->common.events, optarg); 414 - if (retval) 415 - fatal("Error adding filter %s", optarg); 416 - } else { 411 + if (params->common.events) 412 + trace_event_add_filter(params->common.events, optarg); 413 + else 417 414 fatal("--filter requires a previous -e"); 418 - } 419 415 break; 420 416 case '2': 421 417 params->common.warmup = get_llong_from_str(optarg);
+7 -15
tools/tracing/rtla/src/timerlat_hist.c
··· 760 760 int c; 761 761 char *trace_output = NULL; 762 762 763 - params = calloc(1, sizeof(*params)); 764 - if (!params) 765 - exit(1); 763 + params = calloc_fatal(1, sizeof(*params)); 766 764 767 765 actions_init(&params->common.threshold_actions); 768 766 actions_init(&params->common.end_actions); ··· 909 911 params->common.hist.with_zeros = 1; 910 912 break; 911 913 case '6': /* trigger */ 912 - if (params->common.events) { 913 - retval = trace_event_add_trigger(params->common.events, optarg); 914 - if (retval) 915 - fatal("Error adding trigger %s", optarg); 916 - } else { 914 + if (params->common.events) 915 + trace_event_add_trigger(params->common.events, optarg); 916 + else 917 917 fatal("--trigger requires a previous -e"); 918 - } 919 918 break; 920 919 case '7': /* filter */ 921 - if (params->common.events) { 922 - retval = trace_event_add_filter(params->common.events, optarg); 923 - if (retval) 924 - fatal("Error adding filter %s", optarg); 925 - } else { 920 + if (params->common.events) 921 + trace_event_add_filter(params->common.events, optarg); 922 + else 926 923 fatal("--filter requires a previous -e"); 927 - } 928 924 break; 929 925 case '8': 930 926 params->dma_latency = get_llong_from_str(optarg);
+7 -15
tools/tracing/rtla/src/timerlat_top.c
··· 526 526 int c; 527 527 char *trace_output = NULL; 528 528 529 - params = calloc(1, sizeof(*params)); 530 - if (!params) 531 - exit(1); 529 + params = calloc_fatal(1, sizeof(*params)); 532 530 533 531 actions_init(&params->common.threshold_actions); 534 532 actions_init(&params->common.end_actions); ··· 654 656 params->common.user_data = true; 655 657 break; 656 658 case '0': /* trigger */ 657 - if (params->common.events) { 658 - retval = trace_event_add_trigger(params->common.events, optarg); 659 - if (retval) 660 - fatal("Error adding trigger %s", optarg); 661 - } else { 659 + if (params->common.events) 660 + trace_event_add_trigger(params->common.events, optarg); 661 + else 662 662 fatal("--trigger requires a previous -e"); 663 - } 664 663 break; 665 664 case '1': /* filter */ 666 - if (params->common.events) { 667 - retval = trace_event_add_filter(params->common.events, optarg); 668 - if (retval) 669 - fatal("Error adding filter %s", optarg); 670 - } else { 665 + if (params->common.events) 666 + trace_event_add_filter(params->common.events, optarg); 667 + else 671 668 fatal("--filter requires a previous -e"); 672 - } 673 669 break; 674 670 case '2': /* dma-latency */ 675 671 params->dma_latency = get_llong_from_str(optarg);
+7 -23
tools/tracing/rtla/src/trace.c
··· 191 191 */ 192 192 int trace_instance_init(struct trace_instance *trace, char *tool_name) 193 193 { 194 - trace->seq = calloc(1, sizeof(*trace->seq)); 195 - if (!trace->seq) 196 - goto out_err; 194 + trace->seq = calloc_fatal(1, sizeof(*trace->seq)); 197 195 198 196 trace_seq_init(trace->seq); 199 197 ··· 272 274 { 273 275 struct trace_events *tevent; 274 276 275 - tevent = calloc(1, sizeof(*tevent)); 276 - if (!tevent) 277 - return NULL; 277 + tevent = calloc_fatal(1, sizeof(*tevent)); 278 278 279 - tevent->system = strdup(event_string); 280 - if (!tevent->system) { 281 - free(tevent); 282 - return NULL; 283 - } 279 + tevent->system = strdup_fatal(event_string); 284 280 285 281 tevent->event = strstr(tevent->system, ":"); 286 282 if (tevent->event) { ··· 288 296 /* 289 297 * trace_event_add_filter - record an event filter 290 298 */ 291 - int trace_event_add_filter(struct trace_events *event, char *filter) 299 + void trace_event_add_filter(struct trace_events *event, char *filter) 292 300 { 293 301 if (event->filter) 294 302 free(event->filter); 295 303 296 - event->filter = strdup(filter); 297 - if (!event->filter) 298 - return 1; 299 - 300 - return 0; 304 + event->filter = strdup_fatal(filter); 301 305 } 302 306 303 307 /* 304 308 * trace_event_add_trigger - record an event trigger action 305 309 */ 306 - int trace_event_add_trigger(struct trace_events *event, char *trigger) 310 + void trace_event_add_trigger(struct trace_events *event, char *trigger) 307 311 { 308 312 if (event->trigger) 309 313 free(event->trigger); 310 314 311 - event->trigger = strdup(trigger); 312 - if (!event->trigger) 313 - return 1; 314 - 315 - return 0; 315 + event->trigger = strdup_fatal(trigger); 316 316 } 317 317 318 318 /*
+2 -2
tools/tracing/rtla/src/trace.h
··· 45 45 int trace_events_enable(struct trace_instance *instance, 46 46 struct trace_events *events); 47 47 48 - int trace_event_add_filter(struct trace_events *event, char *filter); 49 - int trace_event_add_trigger(struct trace_events *event, char *trigger); 48 + void trace_event_add_filter(struct trace_events *event, char *filter); 49 + void trace_event_add_trigger(struct trace_events *event, char *trigger); 50 50 int trace_set_buffer_size(struct trace_instance *trace, int size);
+35
tools/tracing/rtla/src/utils.c
··· 1040 1040 *res = (int) lres; 1041 1041 return 0; 1042 1042 } 1043 + 1044 + static inline void fatal_alloc(void) 1045 + { 1046 + fatal("Error allocating memory\n"); 1047 + } 1048 + 1049 + void *calloc_fatal(size_t n, size_t size) 1050 + { 1051 + void *p = calloc(n, size); 1052 + 1053 + if (!p) 1054 + fatal_alloc(); 1055 + 1056 + return p; 1057 + } 1058 + 1059 + void *reallocarray_fatal(void *p, size_t n, size_t size) 1060 + { 1061 + p = reallocarray(p, n, size); 1062 + 1063 + if (!p) 1064 + fatal_alloc(); 1065 + 1066 + return p; 1067 + } 1068 + 1069 + char *strdup_fatal(const char *s) 1070 + { 1071 + char *p = strdup(s); 1072 + 1073 + if (!p) 1074 + fatal_alloc(); 1075 + 1076 + return p; 1077 + }
+3
tools/tracing/rtla/src/utils.h
··· 76 76 int set_comm_cgroup(const char *comm_prefix, const char *cgroup); 77 77 int set_pid_cgroup(pid_t pid, const char *cgroup); 78 78 int set_cpu_dma_latency(int32_t latency); 79 + void *calloc_fatal(size_t n, size_t size); 80 + void *reallocarray_fatal(void *p, size_t n, size_t size); 81 + char *strdup_fatal(const char *s); 79 82 #ifdef HAVE_LIBCPUPOWER_SUPPORT 80 83 int save_cpu_idle_disable_state(unsigned int cpu); 81 84 int restore_cpu_idle_disable_state(unsigned int cpu);