From patchwork Wed Jul 5 18:12:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Schneider X-Patchwork-Id: 13302553 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 BAD84EB64DD for ; Wed, 5 Jul 2023 18:16:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 547ED8D0002; Wed, 5 Jul 2023 14:16:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D07A8D0001; Wed, 5 Jul 2023 14:16:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 323E18D0002; Wed, 5 Jul 2023 14:16:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 21D628D0001 for ; Wed, 5 Jul 2023 14:16:06 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E5D40C0B90 for ; Wed, 5 Jul 2023 18:16:05 +0000 (UTC) X-FDA: 80978362290.04.1EA5655 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf23.hostedemail.com (Postfix) with ESMTP id E498F140024 for ; Wed, 5 Jul 2023 18:16:03 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=DwfrZSat; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf23.hostedemail.com: domain of vschneid@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=vschneid@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688580964; 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=BTFUs8gxr9moRkZBUGlN4k/3c1CssODo/4E7AM+Boo0=; b=Hy7d+5EEmVGvgXMFC1Vow6GkQKle3+iNaN23iZ8Ff2mDDCJDNegRUqz7RiI1LutjKVF/Kx hyxVfeoJz7AG2LJ9AZ6eDLdAMVeSrW2ZTcJwcPJHxVxUWkPYTn//eZNO2HFpyEwh1GrG16 RSQjtWCMGQ/+0zaE4MzumMHueDTTZ/w= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=DwfrZSat; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf23.hostedemail.com: domain of vschneid@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=vschneid@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688580964; a=rsa-sha256; cv=none; b=RTRP2/X+SHT+hO/G2mX9wKsKBGh+AVOl2kCU+36UB/Tv8RcMIRRiXLMen1YuM2gwPeU8r7 xtYYt1VwjRfYel9wHPPJ4rfMdxStAVgMqbgS2pmCN4EX6AKw+eYWelfkloFTK0btoRZM5E wKEuuz0URI6ZhcWYuoK0Dzn9LK5qH1A= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688580963; 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=BTFUs8gxr9moRkZBUGlN4k/3c1CssODo/4E7AM+Boo0=; b=DwfrZSattLI106DxHpiKLZ5+2xdaV9eqM1HOVNuR2VoHJJx+XBR80mwquyVKFl6BbPFcon qOxEYbbJd6trH2cKrRoCnanJprUPBic0f9eNRJYAjCa76KuT/vYWWCwr3oBOtk7tSFzAW1 KRH3HTnbOGQcha3JW2YUFGd1n8rZmhs= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-187-6Xa3y2PROnCrrdM8iKBVQA-1; Wed, 05 Jul 2023 14:15:56 -0400 X-MC-Unique: 6Xa3y2PROnCrrdM8iKBVQA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9D2EE185A78F; Wed, 5 Jul 2023 18:15:54 +0000 (UTC) Received: from vschneid.remote.csb (unknown [10.42.28.164]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D149AF5CFA; Wed, 5 Jul 2023 18:15: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 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" , Andrew Morton , Uladzislau Rezki , Christoph Hellwig , Lorenzo Stoakes , Josh Poimboeuf , 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 03/14] tracing/filters: Enable filtering a scalar field by a cpumask Date: Wed, 5 Jul 2023 19:12:45 +0100 Message-Id: <20230705181256.3539027-4-vschneid@redhat.com> In-Reply-To: <20230705181256.3539027-1-vschneid@redhat.com> References: <20230705181256.3539027-1-vschneid@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspamd-Queue-Id: E498F140024 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: gyzffbyg776ngrurwmaz3q59zeyth4y8 X-HE-Tag: 1688580963-133359 X-HE-Meta: U2FsdGVkX1+NjGN9UdKuEZ56fMfO2dE+WNoHUHG1m6kl/6Lb6L9Twikabi6OoErUwruFoPhU6tgZVaHxdn+S4Am+3qjvmBqwCxNOagEDGLHptbT33aSxHiFlX0TXp7D4LW5XxRjy9BtDa0jq4SmHH7qGM8KGzoxC4t9U6m7klcG1a7oo7r9fEEEvgfPiqnDdNWN8V/3hT9qOOnFIxuBmq3idnvEuA2lDane+DUHfN8OiEX9/QyPpRC08ubR7J5WYC9qQpjQwdwRxyJabl1ZoPaka5caP+Q4BfG1NsMofAsY63CVF7WrUO7fQ5G2ahL9brKynYryC0Ed+eotvV4w8fbgFUl917+4Gla2kuEFq6UnR5UMi2rtdqOKMNAAsSLatZdyoNsdMNIWxdyzGMoDIlDMJ12GbpCg0LS7rmAD7iJRKRQAG85pNcMDevjtolf+fwNjMrHX3BIg9ocybJkZTr0WURAOdzi+ntyGOVI7j7UW8FmhVzJ2OQpIF+a020oVH3AxIkjf/18ft04Xtad9afoL54tDgzFQE8w1+B2jyTgfQNyz9pWm4awuibZgyk5b0YdxUcBu1MQ70eDWcU7GN5EGhOH1XtN0kcrWdy2A7bY4q6LtnuwkDnUj7RJqeid4ixdB5vdaFtPLS7dv+3bPQfn6ysbdP0O0OQCx2//ZvoH3I4ausN4vXq3nba3D7Dq6h9svSsKpHCpc9QxOfE+o4fkbfY4d9wLWoFxoCw5XLxnIrwehtRQAikSfhVPWGyQsVweMyItUwAdPLcaaUzwGK10hExgRi/bU+ccd3pjwEWJFep/MxVIq59yuTTZ9iG2q4xqSRAdw8XTScKQmhZszzI2bjG8tWEv5JSZ+woE32XjcBSnR9pkc88/fgKcKoak9ukoiUQHhf4HnlhVgJpS4KOyTlSOY2NGDvvtM/uHENcVDdTz+br+y87ZTuM2sPr/4l3pMJr+a5ZZDN3S6moME T27y2qIA jSzCaO+WM9GvG1fktUuvgi2dbbg0sNFwKfox1sYWdu82W+PciSSVfFM83Q+BBsWHUIDVZd0mSrCOSfXaL7ayTI6jqC2Ru6Onn38OlME+/k1vi8PbrnBeDDvr9ydqrenaMpIEm+4EqsCK5uvKa5MqTL20lu/ZKxeGDUT6xu5sYwl7ZLKI= 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: Several events use a scalar field to denote a CPU: o sched_wakeup.target_cpu o sched_migrate_task.orig_cpu,dest_cpu o sched_move_numa.src_cpu,dst_cpu o ipi_send_cpu.cpu o ... Filtering these currently requires using arithmetic comparison functions, which can be tedious when dealing with interleaved SMT or NUMA CPU ids. Allow these to be filtered by a user-provided cpumask, which enables e.g.: $ trace-cmd record -e 'sched_wakeup' -f 'target_cpu & MASK{2,4,6,8-32}' Signed-off-by: Valentin Schneider --- NOTE: I went with an implicit cpumask conversion of the event field, as AFAICT predicate_parse() does not support parsing the application of a function to a field (e.g. 'MASK(target_cpu) & MASK{2,4,6,8-32}') --- kernel/trace/trace_events_filter.c | 92 ++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 12 deletions(-) diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c index 8af00caa363f7..99e111c237a93 100644 --- a/kernel/trace/trace_events_filter.c +++ b/kernel/trace/trace_events_filter.c @@ -46,15 +46,19 @@ static const char * ops[] = { OPS }; enum filter_pred_fn { FILTER_PRED_FN_NOP, FILTER_PRED_FN_64, + FILTER_PRED_FN_64_CPUMASK, FILTER_PRED_FN_S64, FILTER_PRED_FN_U64, FILTER_PRED_FN_32, + FILTER_PRED_FN_32_CPUMASK, FILTER_PRED_FN_S32, FILTER_PRED_FN_U32, FILTER_PRED_FN_16, + FILTER_PRED_FN_16_CPUMASK, FILTER_PRED_FN_S16, FILTER_PRED_FN_U16, FILTER_PRED_FN_8, + FILTER_PRED_FN_8_CPUMASK, FILTER_PRED_FN_S8, FILTER_PRED_FN_U8, FILTER_PRED_FN_COMM, @@ -641,6 +645,37 @@ predicate_parse(const char *str, int nr_parens, int nr_preds, return ERR_PTR(ret); } +static inline int +do_filter_cpumask(int op, const struct cpumask *mask, const struct cpumask *cmp) +{ + switch (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; + } +} + +/* Optimisation of do_filter_cpumask() for scalar values */ +static inline int +do_filter_cpumask_scalar(int op, unsigned int cpu, const struct cpumask *mask) +{ + switch (op) { + case OP_EQ: + return cpumask_equal(mask, cpumask_of(cpu)); + case OP_NE: + return !cpumask_equal(mask, cpumask_of(cpu)); + case OP_BAND: + return cpumask_test_cpu(cpu, mask); + default: + return 0; + } +} + enum pred_cmp_types { PRED_CMP_TYPE_NOP, PRED_CMP_TYPE_LT, @@ -684,6 +719,18 @@ static int filter_pred_##type(struct filter_pred *pred, void *event) \ } \ } +#define DEFINE_CPUMASK_COMPARISON_PRED(size) \ +static int filter_pred_##size##_cpumask(struct filter_pred *pred, void *event) \ +{ \ + u##size *addr = (u##size *)(event + pred->offset); \ + unsigned int cpu = *addr; \ + \ + if (cpu >= nr_cpu_ids) \ + return 0; \ + \ + return do_filter_cpumask_scalar(pred->op, cpu, pred->mask); \ +} + #define DEFINE_EQUALITY_PRED(size) \ static int filter_pred_##size(struct filter_pred *pred, void *event) \ { \ @@ -705,6 +752,11 @@ DEFINE_COMPARISON_PRED(u16); DEFINE_COMPARISON_PRED(s8); DEFINE_COMPARISON_PRED(u8); +DEFINE_CPUMASK_COMPARISON_PRED(64); +DEFINE_CPUMASK_COMPARISON_PRED(32); +DEFINE_CPUMASK_COMPARISON_PRED(16); +DEFINE_CPUMASK_COMPARISON_PRED(8); + DEFINE_EQUALITY_PRED(64); DEFINE_EQUALITY_PRED(32); DEFINE_EQUALITY_PRED(16); @@ -889,16 +941,7 @@ static int filter_pred_cpumask(struct filter_pred *pred, void *event) 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; - } + return do_filter_cpumask(pred->op, mask, cmp); } /* Filter predicate for COMM. */ @@ -1349,24 +1392,32 @@ static int filter_pred_fn_call(struct filter_pred *pred, void *event) switch (pred->fn_num) { case FILTER_PRED_FN_64: return filter_pred_64(pred, event); + case FILTER_PRED_FN_64_CPUMASK: + return filter_pred_64_cpumask(pred, event); case FILTER_PRED_FN_S64: return filter_pred_s64(pred, event); case FILTER_PRED_FN_U64: return filter_pred_u64(pred, event); case FILTER_PRED_FN_32: return filter_pred_32(pred, event); + case FILTER_PRED_FN_32_CPUMASK: + return filter_pred_32_cpumask(pred, event); case FILTER_PRED_FN_S32: return filter_pred_s32(pred, event); case FILTER_PRED_FN_U32: return filter_pred_u32(pred, event); case FILTER_PRED_FN_16: return filter_pred_16(pred, event); + case FILTER_PRED_FN_16_CPUMASK: + return filter_pred_16_cpumask(pred, event); case FILTER_PRED_FN_S16: return filter_pred_s16(pred, event); case FILTER_PRED_FN_U16: return filter_pred_u16(pred, event); case FILTER_PRED_FN_8: return filter_pred_8(pred, event); + case FILTER_PRED_FN_8_CPUMASK: + return filter_pred_8_cpumask(pred, event); case FILTER_PRED_FN_S8: return filter_pred_s8(pred, event); case FILTER_PRED_FN_U8: @@ -1602,7 +1653,8 @@ static int parse_pred(const char *str, void *data, unsigned int maskstart; char *tmp; - if (field->filter_type != FILTER_CPUMASK) { + if (field->filter_type != FILTER_CPUMASK && + field->filter_type != FILTER_OTHER) { parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i); goto err_free; } @@ -1644,8 +1696,24 @@ static int parse_pred(const char *str, void *data, /* Move along */ i++; - if (field->filter_type == FILTER_CPUMASK) + if (field->filter_type == FILTER_CPUMASK) { pred->fn_num = FILTER_PRED_FN_CPUMASK; + } else { + switch (field->size) { + case 8: + pred->fn_num = FILTER_PRED_FN_64_CPUMASK; + break; + case 4: + pred->fn_num = FILTER_PRED_FN_32_CPUMASK; + break; + case 2: + pred->fn_num = FILTER_PRED_FN_16_CPUMASK; + break; + case 1: + pred->fn_num = FILTER_PRED_FN_8_CPUMASK; + break; + } + } /* This is either a string, or an integer */ } else if (str[i] == '\'' || str[i] == '"') {