From nobody Thu Dec 18 08:36:25 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2FA94C27C40 for ; Thu, 24 Aug 2023 02:19:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239352AbjHXCTK (ORCPT ); Wed, 23 Aug 2023 22:19:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239266AbjHXCSd (ORCPT ); Wed, 23 Aug 2023 22:18:33 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C2D310C4 for ; Wed, 23 Aug 2023 19:18:31 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9C15B633C6 for ; Thu, 24 Aug 2023 02:18:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F34E6C433C9; Thu, 24 Aug 2023 02:18:29 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.96) (envelope-from ) id 1qYzvu-001hQ7-2H; Wed, 23 Aug 2023 22:18:50 -0400 Message-ID: <20230824021850.515849153@goodmis.org> User-Agent: quilt/0.66 Date: Wed, 23 Aug 2023 22:18:13 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Andrew Morton , Jonathan Corbet , Juri Lelli , Daniel Bristot de Oliveira , Marcelo Tosatti , Leonardo Bras , Frederic Weisbecker , Valentin Schneider Subject: [for-next][PATCH 01/14] tracing/filters: Dynamically allocate filter_pred.regex References: <20230824021812.938245293@goodmis.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Valentin Schneider Every predicate allocation includes a MAX_FILTER_STR_VAL (256) char array in the regex field, even if the predicate function does not use the field. A later commit will introduce a dynamically allocated cpumask to struct filter_pred, which will require a dedicated freeing function. Bite the bullet and make filter_pred.regex dynamically allocated. While at it, reorder the fields of filter_pred to fill in the byte holes. The struct now fits on a single cacheline. No change in behaviour intended. The kfree()'s were patched via Coccinelle: @@ struct filter_pred *pred; @@ -kfree(pred); +free_predicate(pred); Link: https://lkml.kernel.org/r/20230707172155.70873-2-vschneid@redhat.com Cc: Masami Hiramatsu Cc: Jonathan Corbet Cc: Juri Lelli Cc: Daniel Bristot de Oliveira Cc: Marcelo Tosatti Cc: Leonardo Bras Cc: Frederic Weisbecker Signed-off-by: Valentin Schneider Signed-off-by: Steven Rostedt (Google) --- kernel/trace/trace_events_filter.c | 64 ++++++++++++++++++------------ 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events= _filter.c index 1dad64267878..91fc9990107f 100644 --- a/kernel/trace/trace_events_filter.c +++ b/kernel/trace/trace_events_filter.c @@ -70,15 +70,15 @@ enum filter_pred_fn { }; =20 struct filter_pred { - enum filter_pred_fn fn_num; - u64 val; - u64 val2; - struct regex regex; + struct regex *regex; unsigned short *ops; struct ftrace_event_field *field; - int offset; + u64 val; + u64 val2; + enum filter_pred_fn fn_num; + int offset; int not; - int op; + int op; }; =20 /* @@ -186,6 +186,14 @@ enum { PROCESS_OR =3D 4, }; =20 +static void free_predicate(struct filter_pred *pred) +{ + if (pred) { + kfree(pred->regex); + kfree(pred); + } +} + /* * Without going into a formal proof, this explains the method that is use= d in * parsing the logical expressions. @@ -623,7 +631,7 @@ predicate_parse(const char *str, int nr_parens, int nr_= preds, kfree(inverts); if (prog_stack) { for (i =3D 0; prog_stack[i].pred; i++) - kfree(prog_stack[i].pred); + free_predicate(prog_stack[i].pred); kfree(prog_stack); } return ERR_PTR(ret); @@ -750,7 +758,7 @@ static int filter_pred_string(struct filter_pred *pred,= void *event) char *addr =3D (char *)(event + pred->offset); int cmp, match; =20 - cmp =3D pred->regex.match(addr, &pred->regex, pred->regex.field_len); + cmp =3D pred->regex->match(addr, pred->regex, pred->regex->field_len); =20 match =3D cmp ^ pred->not; =20 @@ -763,7 +771,7 @@ static __always_inline int filter_pchar(struct filter_p= red *pred, char *str) int len; =20 len =3D strlen(str) + 1; /* including tailing '\0' */ - cmp =3D pred->regex.match(str, &pred->regex, len); + cmp =3D pred->regex->match(str, pred->regex, len); =20 match =3D cmp ^ pred->not; =20 @@ -813,7 +821,7 @@ static int filter_pred_strloc(struct filter_pred *pred,= void *event) char *addr =3D (char *)(event + str_loc); int cmp, match; =20 - cmp =3D pred->regex.match(addr, &pred->regex, str_len); + cmp =3D pred->regex->match(addr, pred->regex, str_len); =20 match =3D cmp ^ pred->not; =20 @@ -836,7 +844,7 @@ static int filter_pred_strrelloc(struct filter_pred *pr= ed, void *event) char *addr =3D (char *)(&item[1]) + str_loc; int cmp, match; =20 - cmp =3D pred->regex.match(addr, &pred->regex, str_len); + cmp =3D pred->regex->match(addr, pred->regex, str_len); =20 match =3D cmp ^ pred->not; =20 @@ -874,7 +882,7 @@ static int filter_pred_comm(struct filter_pred *pred, v= oid *event) { int cmp; =20 - cmp =3D pred->regex.match(current->comm, &pred->regex, + cmp =3D pred->regex->match(current->comm, pred->regex, TASK_COMM_LEN); return cmp ^ pred->not; } @@ -1004,7 +1012,7 @@ enum regex_type filter_parse_regex(char *buff, int le= n, char **search, int *not) =20 static void filter_build_regex(struct filter_pred *pred) { - struct regex *r =3D &pred->regex; + struct regex *r =3D pred->regex; char *search; enum regex_type type =3D MATCH_FULL; =20 @@ -1169,7 +1177,7 @@ static void free_prog(struct event_filter *filter) return; =20 for (i =3D 0; prog[i].pred; i++) - kfree(prog[i].pred); + free_predicate(prog[i].pred); kfree(prog); } =20 @@ -1553,9 +1561,12 @@ static int parse_pred(const char *str, void *data, goto err_free; } =20 - pred->regex.len =3D len; - strncpy(pred->regex.pattern, str + s, len); - pred->regex.pattern[len] =3D 0; + pred->regex =3D kzalloc(sizeof(*pred->regex), GFP_KERNEL); + if (!pred->regex) + goto err_mem; + pred->regex->len =3D len; + strncpy(pred->regex->pattern, str + s, len); + pred->regex->pattern[len] =3D 0; =20 /* This is either a string, or an integer */ } else if (str[i] =3D=3D '\'' || str[i] =3D=3D '"') { @@ -1597,9 +1608,12 @@ static int parse_pred(const char *str, void *data, goto err_free; } =20 - pred->regex.len =3D len; - strncpy(pred->regex.pattern, str + s, len); - pred->regex.pattern[len] =3D 0; + pred->regex =3D kzalloc(sizeof(*pred->regex), GFP_KERNEL); + if (!pred->regex) + goto err_mem; + pred->regex->len =3D len; + strncpy(pred->regex->pattern, str + s, len); + pred->regex->pattern[len] =3D 0; =20 filter_build_regex(pred); =20 @@ -1608,7 +1622,7 @@ static int parse_pred(const char *str, void *data, =20 } else if (field->filter_type =3D=3D FILTER_STATIC_STRING) { pred->fn_num =3D FILTER_PRED_FN_STRING; - pred->regex.field_len =3D field->size; + pred->regex->field_len =3D field->size; =20 } else if (field->filter_type =3D=3D FILTER_DYN_STRING) { pred->fn_num =3D FILTER_PRED_FN_STRLOC; @@ -1691,10 +1705,10 @@ static int parse_pred(const char *str, void *data, return i; =20 err_free: - kfree(pred); + free_predicate(pred); return -EINVAL; err_mem: - kfree(pred); + free_predicate(pred); return -ENOMEM; } =20 @@ -2287,8 +2301,8 @@ static int ftrace_function_set_filter_pred(struct fil= ter_pred *pred, return ret; =20 return __ftrace_function_set_filter(pred->op =3D=3D OP_EQ, - pred->regex.pattern, - pred->regex.len, + pred->regex->pattern, + pred->regex->len, data); } =20 --=20 2.40.1