From patchwork Thu Jul 20 16:30:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 13320786 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 033D5EB64DC for ; Thu, 20 Jul 2023 16:32:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7A88F280130; Thu, 20 Jul 2023 12:32:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7310928004C; Thu, 20 Jul 2023 12:32:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5AA44280130; Thu, 20 Jul 2023 12:32:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 480AC28004C for ; Thu, 20 Jul 2023 12:32:17 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 20485160216 for ; Thu, 20 Jul 2023 16:32:17 +0000 (UTC) X-FDA: 81032532714.29.D9D9AE1 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf01.hostedemail.com (Postfix) with ESMTP id 520934000D for ; Thu, 20 Jul 2023 16:32:15 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=UPvUlz5P; spf=pass (imf01.hostedemail.com: domain of vschneid@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=vschneid@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689870735; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=C96oWrbX6n5TlLE7O7XWEwueAW9U6q6ni5+a3A4c/ko=; b=s0yYHqjcuFBZoEyBJ8BvELu9BjjoB+aA+mBOU6g4Vfd+jfZPeHX46KGPOovS27C9xGArio 4cnSzPHdXZwhhDabpRtZz+WxnSPgoPOQ0ZinHJMIkUFIDj4YXuzbRARt9ZfKV8H2wX40Tw jeESrrIm69U24xPoBMy20zPxBVFNkpA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689870735; a=rsa-sha256; cv=none; b=Dv/6qYs+jZPveXMArBm1+otuZwpK7N2syi6VYmtzBefWi1wsMSIPV6ifV80BXMDVm2svf8 DJ84RPQzsSGErVkt+PCEg2cFSsCUmnnOk5orw4t5dsqA4c8ZhnrZMrMIX6kOJRl+TkCwNR 5MYJmTwvtNNbSVMdW7AZt3kXFbRG460= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=UPvUlz5P; spf=pass (imf01.hostedemail.com: domain of vschneid@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=vschneid@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689870734; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=C96oWrbX6n5TlLE7O7XWEwueAW9U6q6ni5+a3A4c/ko=; b=UPvUlz5PioKud+LCsIM+pWBk3efICAb7bIQZZemCybVmnjLcwuNdaoNMZwgIKb+bgrznjq ThGHDmbgMI0T1Hy2nkVaQ2sZ0dJVTamAERqqkk4dY139XUQEGxpnNpJcCEpOwZ4mw2rA0e 3fkJemkaeyKbwaGEYi10oODcXx0+6Go= Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-649-TP5rCbtOO8Oz4kDb2HxfDg-1; Thu, 20 Jul 2023 12:32:11 -0400 X-MC-Unique: TP5rCbtOO8Oz4kDb2HxfDg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3FC5B1C05EA8; Thu, 20 Jul 2023 16:32:08 +0000 (UTC) Received: from vschneid.remote.csb (unknown [10.42.28.48]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C8AC440C206F; Thu, 20 Jul 2023 16:31:49 +0000 (UTC) From: Valentin Schneider To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, x86@kernel.org, rcu@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: Steven Rostedt , Masami Hiramatsu , Jonathan Corbet , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Paolo Bonzini , Wanpeng Li , Vitaly Kuznetsov , Andy Lutomirski , Peter Zijlstra , Frederic Weisbecker , "Paul E. McKenney" , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Andrew Morton , Uladzislau Rezki , Christoph Hellwig , Lorenzo Stoakes , Josh Poimboeuf , Jason Baron , Kees Cook , Sami Tolvanen , Ard Biesheuvel , Nicholas Piggin , Juerg Haefliger , Nicolas Saenz Julienne , "Kirill A. Shutemov" , Nadav Amit , Dan Carpenter , Chuang Wang , Yang Jihong , Petr Mladek , "Jason A. Donenfeld" , Song Liu , Julian Pidancet , Tom Lendacky , Dionna Glaze , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , Juri Lelli , Daniel Bristot de Oliveira , Marcelo Tosatti , Yair Podemsky Subject: [RFC PATCH v2 02/20] tracing/filters: Enable filtering a cpumask field by another cpumask Date: Thu, 20 Jul 2023 17:30:38 +0100 Message-Id: <20230720163056.2564824-3-vschneid@redhat.com> In-Reply-To: <20230720163056.2564824-1-vschneid@redhat.com> References: <20230720163056.2564824-1-vschneid@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Stat-Signature: rr4gpicqd55jeurjppi7gzr9z6yf157y X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 520934000D X-Rspam-User: X-HE-Tag: 1689870735-546168 X-HE-Meta: U2FsdGVkX18x8ugb6u388LcjCz1wncLPGAikfP17wUGaBa9UMmOcDATIfOqdPGT0EODWnyefLbnuiu5grKzeckL0qRaZ2CwUTDbwINmE9ALR/pa8CqcC5TFx7ijN8R+BY9qUJ4gov8iIPtakeONGDPNq2+Oib0DNEd31gNz5w3doMOyx5POG56j4UTv+cKkC/1QXdFiuKdFyxt0nj+1D6pX4lgyd/kxzlyPKVe+LlOTnJElo+OHT9FxPEY+pXJIwc5K+Ot2VjHNkITfd8eivrhTqRTUTay/IHiJS5zTzhNuSttTAASz26Jwj9OyGv6s+hF2tsR2ns4suA79m2Gg/u64xvRyBmUM+EMkliNMjCyIsPQ34j8vqChXjfRgvk16McViNa3+ZlAui+zRDn9dZ5AGXFpdcM3g77COYMtpiKQm9gZbooXnJAgaZsGh8rI2ucGbwA2lsJCfxE0ONC6JurjT1V8vMzLHxwwAzopGdwwG8/z8ByHP+CWMmd/Wr8gff1clcG9b7WfJZdX5+Sh4/LgTlhEFJrRIT2Tr4IkObgb35BXgALsf4UF7SXEm/PmpHH0hQ+CY2qM5RA81L5uAlTiD11/nn/tWh4tCg9XoGeA0s7Tq0jkaUtsg6ZZCwQXLU8oYu5rgoE6mOJ3GjKQ+X9gad3uvdI13t7p0wI6GdLWfd0dM3mqXbRqSPkgfk7FyyWjadnTKzkSRv9DbZ0fh1D7XoLdPvrjFV/ruAbTMkYfDjkFH5rEOj5eYjbOenW6pTZeeUy6BCRy3N+if2plTtlOQFTgQ3STd4ewvbP0ZMl0fk7hpi98wssbuXZf3uedlu31QmbIaN91fTHl2abfg9K5T31ClypkybgfEpB+sBJwphUtK0e9ZSnhcpIxaQeWriB6oEHlTbjDqGjFmkiLeNnSRBKs+umECONfStRIAqba5W2xKAuKQJeEGO4HpXbRFg8D9Z5DCiw/l3m0tKOQZ TdX/bXXB nlFOJSTn1FoTOs7JGi0HArLLwkPw7FvooQwnQ2er3UtExfoCDTjBYwI545KBnT75tkUR7dryFFwzKsJPOPWExDFgDwdbnyAdN2tVhXSOQ3Losl5/6SLa8tWAOXMgvSYEngUKlcawLCgqCovVmoyWduziYWWTxa8oFUx2lamZxYq+MXN8cNwTsDVMS0GBTLIz9lkanbjeIdyUHDhXOrAqpjJY2h/E3gpGMuYuWE7pQ/NwmCYZjfggOYrxRnA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The recently introduced ipi_send_cpumask trace event contains a cpumask field, but it currently cannot be used in filter expressions. Make event filtering aware of cpumask fields, and allow these to be filtered by a user-provided cpumask. The user-provided cpumask is to be given in cpulist format and wrapped as: "CPUS{$cpulist}". The use of curly braces instead of parentheses is to prevent predicate_parse() from parsing the contents of CPUS{...} as a full-fledged predicate subexpression. This enables e.g.: $ trace-cmd record -e 'ipi_send_cpumask' -f 'cpumask & CPUS{2,4,6,8-32}' Signed-off-by: Valentin Schneider --- include/linux/trace_events.h | 1 + kernel/trace/trace_events_filter.c | 97 +++++++++++++++++++++++++++++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h index 3930e676436c9..302be73918336 100644 --- a/include/linux/trace_events.h +++ b/include/linux/trace_events.h @@ -807,6 +807,7 @@ enum { FILTER_RDYN_STRING, FILTER_PTR_STRING, FILTER_TRACE_FN, + FILTER_CPUMASK, FILTER_COMM, FILTER_CPU, FILTER_STACKTRACE, diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c index 91fc9990107f1..cb1863dfa280b 100644 --- a/kernel/trace/trace_events_filter.c +++ b/kernel/trace/trace_events_filter.c @@ -64,6 +64,7 @@ enum filter_pred_fn { FILTER_PRED_FN_PCHAR_USER, FILTER_PRED_FN_PCHAR, FILTER_PRED_FN_CPU, + FILTER_PRED_FN_CPUMASK, FILTER_PRED_FN_FUNCTION, FILTER_PRED_FN_, FILTER_PRED_TEST_VISITED, @@ -71,6 +72,7 @@ enum filter_pred_fn { struct filter_pred { struct regex *regex; + struct cpumask *mask; unsigned short *ops; struct ftrace_event_field *field; u64 val; @@ -94,6 +96,8 @@ struct filter_pred { C(TOO_MANY_OPEN, "Too many '('"), \ C(TOO_MANY_CLOSE, "Too few '('"), \ C(MISSING_QUOTE, "Missing matching quote"), \ + C(MISSING_BRACE_OPEN, "Missing '{'"), \ + C(MISSING_BRACE_CLOSE, "Missing '}'"), \ C(OPERAND_TOO_LONG, "Operand too long"), \ C(EXPECT_STRING, "Expecting string field"), \ C(EXPECT_DIGIT, "Expecting numeric field"), \ @@ -103,6 +107,7 @@ struct filter_pred { C(BAD_SUBSYS_FILTER, "Couldn't find or set field in one of a subsystem's events"), \ C(TOO_MANY_PREDS, "Too many terms in predicate expression"), \ C(INVALID_FILTER, "Meaningless filter expression"), \ + C(INVALID_CPULIST, "Invalid cpulist"), \ C(IP_FIELD_ONLY, "Only 'ip' field is supported for function trace"), \ C(INVALID_VALUE, "Invalid value (did you forget quotes)?"), \ C(NO_FUNCTION, "Function not found"), \ @@ -190,6 +195,7 @@ static void free_predicate(struct filter_pred *pred) { if (pred) { kfree(pred->regex); + kfree(pred->mask); kfree(pred); } } @@ -877,6 +883,26 @@ static int filter_pred_cpu(struct filter_pred *pred, void *event) } } +/* Filter predicate for cpumask field vs user-provided cpumask */ +static int filter_pred_cpumask(struct filter_pred *pred, void *event) +{ + u32 item = *(u32 *)(event + pred->offset); + int loc = item & 0xffff; + const struct cpumask *mask = (event + loc); + const struct cpumask *cmp = pred->mask; + + switch (pred->op) { + case OP_EQ: + return cpumask_equal(mask, cmp); + case OP_NE: + return !cpumask_equal(mask, cmp); + case OP_BAND: + return cpumask_intersects(mask, cmp); + default: + return 0; + } +} + /* Filter predicate for COMM. */ static int filter_pred_comm(struct filter_pred *pred, void *event) { @@ -1244,8 +1270,12 @@ static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir, int filter_assign_type(const char *type) { - if (strstr(type, "__data_loc") && strstr(type, "char")) - return FILTER_DYN_STRING; + if (strstr(type, "__data_loc")) { + if (strstr(type, "char")) + return FILTER_DYN_STRING; + if (strstr(type, "cpumask_t")) + return FILTER_CPUMASK; + } if (strstr(type, "__rel_loc") && strstr(type, "char")) return FILTER_RDYN_STRING; @@ -1357,6 +1387,8 @@ static int filter_pred_fn_call(struct filter_pred *pred, void *event) return filter_pred_pchar(pred, event); case FILTER_PRED_FN_CPU: return filter_pred_cpu(pred, event); + case FILTER_PRED_FN_CPUMASK: + return filter_pred_cpumask(pred, event); case FILTER_PRED_FN_FUNCTION: return filter_pred_function(pred, event); case FILTER_PRED_TEST_VISITED: @@ -1568,6 +1600,67 @@ static int parse_pred(const char *str, void *data, strncpy(pred->regex->pattern, str + s, len); pred->regex->pattern[len] = 0; + } else if (!strncmp(str + i, "CPUS", 4)) { + unsigned int maskstart; + char *tmp; + + switch (field->filter_type) { + case FILTER_CPUMASK: + break; + default: + parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i); + goto err_free; + } + + switch (op) { + case OP_EQ: + case OP_NE: + case OP_BAND: + break; + default: + parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i); + goto err_free; + } + + /* Skip CPUS */ + i += 4; + if (str[i++] != '{') { + parse_error(pe, FILT_ERR_MISSING_BRACE_OPEN, pos + i); + goto err_free; + } + maskstart = i; + + /* Walk the cpulist until closing } */ + for (; str[i] && str[i] != '}'; i++); + if (str[i] != '}') { + parse_error(pe, FILT_ERR_MISSING_BRACE_CLOSE, pos + i); + goto err_free; + } + + if (maskstart == i) { + parse_error(pe, FILT_ERR_INVALID_CPULIST, pos + i); + goto err_free; + } + + /* Copy the cpulist between { and } */ + tmp = kmalloc((i - maskstart) + 1, GFP_KERNEL); + strscpy(tmp, str + maskstart, (i - maskstart) + 1); + + pred->mask = kzalloc(cpumask_size(), GFP_KERNEL); + if (!pred->mask) + goto err_mem; + + /* Now parse it */ + if (cpulist_parse(tmp, pred->mask)) { + parse_error(pe, FILT_ERR_INVALID_CPULIST, pos + i); + goto err_free; + } + + /* Move along */ + i++; + if (field->filter_type == FILTER_CPUMASK) + pred->fn_num = FILTER_PRED_FN_CPUMASK; + /* This is either a string, or an integer */ } else if (str[i] == '\'' || str[i] == '"') { char q = str[i];