From patchwork Mon May 1 16:54:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13227651 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 8B9E4C77B7C for ; Mon, 1 May 2023 16:55:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F4051900012; Mon, 1 May 2023 12:55:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EF0C9900002; Mon, 1 May 2023 12:55:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB7CD900012; Mon, 1 May 2023 12:55:47 -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 CC048900002 for ; Mon, 1 May 2023 12:55:47 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A7A991607F3 for ; Mon, 1 May 2023 16:55:47 +0000 (UTC) X-FDA: 80742287934.12.C544C49 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf17.hostedemail.com (Postfix) with ESMTP id D6AA74000A for ; Mon, 1 May 2023 16:55:45 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=7DF+QMYA; spf=pass (imf17.hostedemail.com: domain of 3EO9PZAYKCFcHJG3C05DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3EO9PZAYKCFcHJG3C05DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682960145; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=cfU8v4uljuDyceCulpsszZZ0MEofJFrAynDM4I2qv80=; b=PHwV5Px8UAXmFbVG9m2th55zm8e73EyRovArLhZJ5YOxFVB2VxdvYkhScfOaeePuj9i8Ik 2w2BQLdbuRRAW4Kei1Q8cYjVlrFCpsNW7xWg2XsF25cMge5UrdnlprAKlNReEVNB4/lNvE 1I/SrfDHOo0Xz8pp59Mxf070sWa1cLQ= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=7DF+QMYA; spf=pass (imf17.hostedemail.com: domain of 3EO9PZAYKCFcHJG3C05DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3EO9PZAYKCFcHJG3C05DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682960145; a=rsa-sha256; cv=none; b=B2A4MEOFl7Iv4jdv8Qw7TTvMKIWAgPHKFUEJAr/kPT3VOOB2mu6wL7qShvTJaC1V9NsGPY VJ/cjqXNm1vQAqFUeL8KfBVVi8y5Sccr2U8vDuv+ipwQDA036/qmfvKkYpSg+ZMwtBZXjY h86HG7ua5vDiwVOpXKryj3EHlIITlP8= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-b9a6eeea78cso27814717276.0 for ; Mon, 01 May 2023 09:55:45 -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=GkYgwnM5gVufJHFwxsCbW1U6OhEzp4WULfgHMLiXyK5+qjOVb4oh5aK9Lu8Qv1oaSy 0BNd2Wolfrpl/8rcoZtDOkRF0XbP3DCf7gYX7Ld2kudGH/wHAjiNjx6zeqskTxuYB0mH /DmRgWlnVAEe3MLrczslQCYs4n5E4cOAPqKw/K7v/MaJKBrP8GCD/KJCwx/BwXbwhd0L /1G2JL7zMumc2lWqGAkWuhyqtaq7nv7H5e+p78RewfaFiG6X5zMWUr+rOWqdpGkKBMy9 pQ6G5LkJInzlX0eREGKYO3KCjHRQnGvvGL11fgqQSCFZ4tPTkAj6wk9gUe6Y2lZPsJ8T Y+ig== X-Gm-Message-State: AC+VfDwj9AE1OAny12OSnVoFR78NR1QBtDGZuK49kTeyLu9zTp9emtWh /oETxuPOlu15I840AHU/yROJ8AuNujg= X-Google-Smtp-Source: ACHHUZ7KwLnOhs3Bj4j+dfecjkq9PpPEjcCUN0XtdJ++CCEZIkvWVHHnPtSXEKWIoTIOpnfnSo0AXCZYwNA= 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 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: D6AA74000A X-Stat-Signature: 58efmjwzfoohbqarsrdzq6teokzbkppp X-Rspam-User: X-HE-Tag: 1682960145-58892 X-HE-Meta: U2FsdGVkX18gC8T4qoD29CSNwIkrDd4NiW4hmPqj+QEJq6nBBWcoMod85YwkvfBOWK/09KKjF8HOcQRGyCqWp6XmvYrzuhQpOKsfqT3/DX27pLrh+yrrScwts+25WS7qHHmyE6PgDwzgN/evlQflL7fT9opACpSHQZKQWfenJtD05Z3KD+kF/Ei0omg/xZb12Fv2hOQWk4badFdOvDLs0U3sgHUREtUNpqSmhFVmsw5Wv6O33twM/V0IXtGDPVFPesjoXRs4yAKkOV21S+1PlxRE4QphI16McT32KUHp60KLNQOrC5Wkz1AXMd91816L01kFds/i5Kxx6fRM8j49RMbQfDSN91VqeKdcoJ0lsbTpasAxnAdp9WSqWq9OxLltdQDyMJc0Q3NP/ZXS3Bcs0nnWXhKjfEdCrys0KwlKsXiGwYUjlZCmwA4caYadW4oRpNWopazOoDodlJsvAkAYWdWRSCRgvucKUA7r2jtPVBeMAWdjKq2L6KOPlHkpsjCzluPDAL+AnsjNZw0sj8tUsLzOpkHQNQbDa3ItJLpBjrGjl3jY0qlxDUQXAadWqEUKbPyCbW9+fk/bzNbddjD7f+BRmbxIhzgEL8hbb/EnSnd1R+ahP/t34iPAFX7DVRA2qpPeUI69z25lcuvTnzeRORI/Aa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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: 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; +}