Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp5734002rwr; Mon, 1 May 2023 09:59:16 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7F7BQPnugd76qCSA6tRIaT2FM2oQzIYb4MvDhvERl9NBLNXgSot7lJwCzdHAVOt+NARgjW X-Received: by 2002:a17:903:244f:b0:1a9:b977:81c7 with SMTP id l15-20020a170903244f00b001a9b97781c7mr18203587pls.62.1682960355834; Mon, 01 May 2023 09:59:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682960355; cv=none; d=google.com; s=arc-20160816; b=UL6UnVsZEwPxEo/sM0WslyN8kXknrTBV4dglW/ZGeapEQRWb/9YdYt4vjThdUmZpWC 6z1O17EW7WekKk8d/Z93wg1EyiRAr2dx5g/56pXYBZbbDWtO8XQt7IhxvBH9txDGvodF t+uTVGDMCgxRF/NIW8Om2oqoTQzpsV4ioA9xbnRpZCc9wEA8/vkgSn4BEYfn6r0LPdWx F3yZJmBdKuqv1DNCcHPuE62/Hi7v3X7YLsz9Dv+k1AEfkGlV1Y3Bx4DNzyO042yxZqUX z+yWvoeJjc7NGoMTw2BL7tb4CCPYuza0ofGpBY+t6P6z6UPCnqx3fEGj2/H+A1pwYQg3 ZZgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=cfU8v4uljuDyceCulpsszZZ0MEofJFrAynDM4I2qv80=; b=HzZhf5kt6xogqrupTD2bNGvZAgJE1qGrRGRiccQAvaViDh29r8qevEMoZL66BLrbmt sJmGn0DbPS1RPxSNKNstT6+Da9O3IPnNDZWwblLyCaRHOYwQ7/hKxJVXDOZgne4PN78b H67EGtrblNq/q2Kh9HvEgZOYewmlaUnAc2jfiLAVITjONpH9oRFPiodiXM8DbMmbHazW ZOfH4TIwWjmXlAiefBdmCb0RkYP53ODmeehdnAiZxggNP4bsI3eQQuPEHtHFgDhplQWa ZGqIvtkjmC2BId6fJHIvaS20fMATbVuiQ+YchuLB3Na52TWTUT0C/+c7AfW0+cZGrJl/ VbMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=7DF+QMYA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q7-20020a17090311c700b001a526bc2b84si29329943plh.620.2023.05.01.09.59.02; Mon, 01 May 2023 09:59:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=7DF+QMYA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232680AbjEAQ6l (ORCPT + 99 others); Mon, 1 May 2023 12:58:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232573AbjEAQ51 (ORCPT ); Mon, 1 May 2023 12:57:27 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98FBD2719 for ; Mon, 1 May 2023 09:55:57 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-b9a6f15287eso27706473276.1 for ; Mon, 01 May 2023 09:55:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1682960145; x=1685552145; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=cfU8v4uljuDyceCulpsszZZ0MEofJFrAynDM4I2qv80=; b=7DF+QMYAAMLutPK+67W4THAVtEPwoqDsbe/74l1tBpyC07tcRQ6NcM5IV8D6bbGSDO mAKOZqw8S8PkrBoJSwJTCQQPpA7SwA7cyZxlWbMDiokIgQwGSl9egB8QrEXUBH/T64s1 zNCYsAsh3eGpCoX4dQnQUqKiZzSauwZNQoR9XmoXX99k0D1LyT9NYpubJVC6Qn7rUrDx j9q+sgeZA/RMhwqSDAOuZPyoK6vZlZ73Jq2IAunuj8KRGdDA/IrEELOEoHnv6XAiPWlM lMDNSxrkbvL1rl0oDCkvCwbUBvti8udh5vGgrmSEH3MVGbNpRYhf9xH2dPTrsqzR9Dja mOjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682960145; x=1685552145; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cfU8v4uljuDyceCulpsszZZ0MEofJFrAynDM4I2qv80=; b=iO69YAhsXgA8JdAanRT8Jc13V2Te1V/5RnX/dMImSPugg7x5v8jYt1GHO8vd/T76Px ZEvlZRmwsNhHutn7PvbA3B+nkuDlMNj4O+4ZGEvt7l3Fabpr61oATCN4iZsk05qLOhlY icIi3ddgnF1H9pMVjtdpRxs6IdbnGB9pI3NVauZx8WszpTdvYj4KhCEbLTxSWEpz84VM MjxnsN+7Ez73daHSJAbvuO67QqIQE0rk9L62iduOYRZBB8uK1/QU3oiHUFJdFvKbCgC1 D3ZQyn/A6xSB05qGdmci8uLN9a2XO3WHnwDjCnlbvTPiFG2DS+ZMcvlb0m2W2S7N/S3c OnlQ== X-Gm-Message-State: AC+VfDwRwN5JtipXPlGR6fWPg9VxxNQcaVRqd6/iDd+1GeOmNdlymkA0 VyjV0XItrQVsQjJlgPE2kVa8pPaX10U= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:6d24:3efd:facc:7ac4]) (user=surenb job=sendgmr) by 2002:a25:3482:0:b0:b94:6989:7fa6 with SMTP id b124-20020a253482000000b00b9469897fa6mr8599498yba.4.1682960144921; Mon, 01 May 2023 09:55:44 -0700 (PDT) Date: Mon, 1 May 2023 09:54:26 -0700 In-Reply-To: <20230501165450.15352-1-surenb@google.com> Mime-Version: 1.0 References: <20230501165450.15352-1-surenb@google.com> X-Mailer: git-send-email 2.40.1.495.gc816e09b53d-goog Message-ID: <20230501165450.15352-17-surenb@google.com> Subject: [PATCH 16/40] lib: code tagging query helper functions From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, roman.gushchin@linux.dev, mgorman@suse.de, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, corbet@lwn.net, void@manifault.com, peterz@infradead.org, juri.lelli@redhat.com, ldufour@linux.ibm.com, catalin.marinas@arm.com, will@kernel.org, arnd@arndb.de, tglx@linutronix.de, mingo@redhat.com, dave.hansen@linux.intel.com, x86@kernel.org, peterx@redhat.com, david@redhat.com, axboe@kernel.dk, mcgrof@kernel.org, masahiroy@kernel.org, nathan@kernel.org, dennis@kernel.org, tj@kernel.org, muchun.song@linux.dev, rppt@kernel.org, paulmck@kernel.org, pasha.tatashin@soleen.com, yosryahmed@google.com, yuzhao@google.com, dhowells@redhat.com, hughd@google.com, andreyknvl@gmail.com, keescook@chromium.org, ndesaulniers@google.com, gregkh@linuxfoundation.org, ebiggers@google.com, ytcoode@gmail.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, bristot@redhat.com, vschneid@redhat.com, cl@linux.com, penberg@kernel.org, iamjoonsoo.kim@lge.com, 42.hyeyoo@gmail.com, glider@google.com, elver@google.com, dvyukov@google.com, shakeelb@google.com, songmuchun@bytedance.com, jbaron@akamai.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, surenb@google.com, kernel-team@android.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux.dev, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kasan-dev@googlegroups.com, cgroups@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kent Overstreet Provide codetag_query_parse() to parse codetag queries and codetag_matches_query() to check if the query affects a given codetag. Signed-off-by: Kent Overstreet Signed-off-by: Suren Baghdasaryan --- include/linux/codetag.h | 27 ++++++++ lib/codetag.c | 135 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) diff --git a/include/linux/codetag.h b/include/linux/codetag.h index d98e4c8e86f0..87207f199ac9 100644 --- a/include/linux/codetag.h +++ b/include/linux/codetag.h @@ -80,4 +80,31 @@ static inline void codetag_load_module(struct module *mod) {} static inline bool codetag_unload_module(struct module *mod) { return true; } #endif +/* Codetag query parsing */ + +struct codetag_query { + const char *filename; + const char *module; + const char *function; + const char *class; + unsigned int first_line, last_line; + unsigned int first_index, last_index; + unsigned int cur_index; + + bool match_line:1; + bool match_index:1; + + unsigned int set_enabled:1; + unsigned int enabled:2; + + unsigned int set_frequency:1; + unsigned int frequency; +}; + +char *codetag_query_parse(struct codetag_query *q, char *buf); +bool codetag_matches_query(struct codetag_query *q, + const struct codetag *ct, + const struct codetag_module *mod, + const char *class); + #endif /* _LINUX_CODETAG_H */ diff --git a/lib/codetag.c b/lib/codetag.c index 0ad4ea66c769..84f90f3b922c 100644 --- a/lib/codetag.c +++ b/lib/codetag.c @@ -256,3 +256,138 @@ bool codetag_unload_module(struct module *mod) return unload_ok; } + +/* Codetag query parsing */ + +#define CODETAG_QUERY_TOKENS() \ + x(func) \ + x(file) \ + x(line) \ + x(module) \ + x(class) \ + x(index) + +enum tokens { +#define x(name) TOK_##name, + CODETAG_QUERY_TOKENS() +#undef x +}; + +static const char * const token_strs[] = { +#define x(name) #name, + CODETAG_QUERY_TOKENS() +#undef x + NULL +}; + +static int parse_range(char *str, unsigned int *first, unsigned int *last) +{ + char *first_str = str; + char *last_str = strchr(first_str, '-'); + + if (last_str) + *last_str++ = '\0'; + + if (kstrtouint(first_str, 10, first)) + return -EINVAL; + + if (!last_str) + *last = *first; + else if (kstrtouint(last_str, 10, last)) + return -EINVAL; + + return 0; +} + +char *codetag_query_parse(struct codetag_query *q, char *buf) +{ + while (1) { + char *p = buf; + char *str1 = strsep_no_empty(&p, " \t\r\n"); + char *str2 = strsep_no_empty(&p, " \t\r\n"); + int ret, token; + + if (!str1 || !str2) + break; + + token = match_string(token_strs, ARRAY_SIZE(token_strs), str1); + if (token < 0) + break; + + switch (token) { + case TOK_func: + q->function = str2; + break; + case TOK_file: + q->filename = str2; + break; + case TOK_line: + ret = parse_range(str2, &q->first_line, &q->last_line); + if (ret) + return ERR_PTR(ret); + q->match_line = true; + break; + case TOK_module: + q->module = str2; + break; + case TOK_class: + q->class = str2; + break; + case TOK_index: + ret = parse_range(str2, &q->first_index, &q->last_index); + if (ret) + return ERR_PTR(ret); + q->match_index = true; + break; + } + + buf = p; + } + + return buf; +} + +bool codetag_matches_query(struct codetag_query *q, + const struct codetag *ct, + const struct codetag_module *mod, + const char *class) +{ + size_t classlen = q->class ? strlen(q->class) : 0; + + if (q->module && + (!mod->mod || + strcmp(q->module, ct->modname))) + return false; + + if (q->filename && + strcmp(q->filename, ct->filename) && + strcmp(q->filename, kbasename(ct->filename))) + return false; + + if (q->function && + strcmp(q->function, ct->function)) + return false; + + /* match against the line number range */ + if (q->match_line && + (ct->lineno < q->first_line || + ct->lineno > q->last_line)) + return false; + + /* match against the class */ + if (classlen && + (strncmp(q->class, class, classlen) || + (class[classlen] && class[classlen] != ':'))) + return false; + + /* match against the fault index */ + if (q->match_index && + (q->cur_index < q->first_index || + q->cur_index > q->last_index)) { + q->cur_index++; + return false; + } + + q->cur_index++; + return true; +} -- 2.40.1.495.gc816e09b53d-goog