From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dl1-f73.google.com (mail-dl1-f73.google.com [74.125.82.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EAF003BB48 for ; Wed, 10 Jun 2026 00:13:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781050393; cv=none; b=Y102sgt17S5NVXjfRrL17NZ+F4loqZkxQ7wFUyYAOoYrQuToLmtIVdx4Hk2c3aOAt8zb6/x4W1AKUZRNzJjlbvfZVmhLWtPI6RCTNLYkec41zAhxQQ8fgGpRuDe2WwXCE/P1OGhUQR4p6A3wtlkcPUnBIA8zHXyS8NcGCxgVg9U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781050393; c=relaxed/simple; bh=Z7ffwxw0napNaUds37qYbwJH+a7GQhHdlnpEXkywxXk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FPdmC/JjqMBkOm2lfXazviOmjnRoFL9h/uChJpKxH0fDrvHf3PG3RPgWTA2tYAkP36Vtzt9HvLweIYwG9g7hz17i2ZNwkCNtbO3/wXHD+df8fzSgk3yKksTAbM1zVNIl6nA029KsOmkAqvHNhOWFGDPQce7kTjnTjd0VXH5/gRs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--abhishekbapat.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=N3hmImlq; arc=none smtp.client-ip=74.125.82.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--abhishekbapat.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="N3hmImlq" Received: by mail-dl1-f73.google.com with SMTP id a92af1059eb24-137fe4a3640so9045767c88.0 for ; Tue, 09 Jun 2026 17:13:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781050391; x=1781655191; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GuiL1uopUb0V1ZSKvFrHplWv7LS00WDxcZQ+1Oh0CoI=; b=N3hmImlq2zt+a/42M4Agbq5PMsEsYxw7boLSfgpTQK0qowQuLcxmkf2wZiPGZbb20z itgl4HUMvltZ89wlYJ2nvicxwUgNkH4YZOTShqUHc5s2k+t9NoyGyE3vmlU8fFD6hzah CH0wCk/3cNqRSNsucFiWX5ziIkLr8P13SIr8FdEPcRtwc5IOgMZ5wG3d5jFxdW/zjdEV NenqPwLgibfBYcynvni//NmQ3t6U36WIvRkLSdBhTJjIpsdptkYkLEKNT44MIgmczacM uYjVDyGo9IO07QjZtSTWKMgtVHE2XAT7bI2luj3/dqYhQRT0DhrVJLm8A9ur7Jm7XOId VPvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781050391; x=1781655191; 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=GuiL1uopUb0V1ZSKvFrHplWv7LS00WDxcZQ+1Oh0CoI=; b=C/3sRrtrEMDL9lgoeBsGJtH+IDHDxDF3vxfN67AxAFiEExjdZTWuSybo/wmEBvDNoh nE/sRvlaDswHJyHTXrW4TVRq1neyEGEGjsDYN7YkitX9nBJWH1Q4xuIk6aadRoTAcQpU r+VLbKtpFRxc7ntp4p3kwUKMfqHSPhIA5sseO+gwOmhMfEEZkTYoEaeCiz+l5y7d4bCd Kz+0Ex9LjaRpHTeGSC9BfvA4337t7pSjJcsWhm62ksVZBIZ8jgRiTaZw4LGChgLWvZ4l 79pn1DI7NWrpeJVEeoV7dFWXWg0SbBbkRnYvWNqye5Vp+CcD5NS9C4CXpXkMupijhEK2 9fFg== X-Forwarded-Encrypted: i=1; AFNElJ9i0tqQ/zjO09zh0w26CrjdqQn3WR1q8sKRWMgXhisctfMcyFWDn1Pg5oB7KSRPy9+WbdaZi1BUUHJGoSM=@vger.kernel.org X-Gm-Message-State: AOJu0YyBME9gE1zFZSo2INjZrCFxnbIu3PqJ0lwjkJzduE97QwbOAwXo icsZuhIQQSvxE7lbqalY8D2oqhS3k+cicy+BiubL0ST9reXtMKmHh4t4+ueAbtIXw1I2ShnNk7E KIJvykYYJq2hlBHwkPbVtB1hfPYnQAZ4JJQ== X-Received: from dld33.prod.google.com ([2002:a05:7022:321:b0:138:281:799f]) (user=abhishekbapat job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:e01:b0:12d:de3f:f3e6 with SMTP id a92af1059eb24-1380675c41cmr10307493c88.38.1781050390727; Tue, 09 Jun 2026 17:13:10 -0700 (PDT) Date: Wed, 10 Jun 2026 00:12:55 +0000 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.54.0.1099.g489fc7bff1-goog Message-ID: <8cd864b3bdbf89973e9a1fbd6e8ed1e9c08989b9.1781042698.git.abhishekbapat@google.com> Subject: [PATCH v4 2/6] alloc_tag: add ioctl filters to /proc/allocinfo From: Abhishek Bapat To: Suren Baghdasaryan , Andrew Morton , Kent Overstreet , Hao Ge Cc: Shuah Khan , Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Sourav Panda , Abhishek Bapat Content-Type: text/plain; charset="UTF-8" Extend the capability of the IOCTL mechanism to filter allocations based on tag's module name, function name, file name and line number. Signed-off-by: Abhishek Bapat --- include/uapi/linux/alloc_tag.h | 26 ++++++++++++- lib/alloc_tag.c | 68 ++++++++++++++++++++++++++++++++-- 2 files changed, 89 insertions(+), 5 deletions(-) diff --git a/include/uapi/linux/alloc_tag.h b/include/uapi/linux/alloc_tag.h index 0928e1a48d49..3b11877955b9 100644 --- a/include/uapi/linux/alloc_tag.h +++ b/include/uapi/linux/alloc_tag.h @@ -40,8 +40,32 @@ struct allocinfo_tag_data { struct allocinfo_counter counter; }; +enum { + ALLOCINFO_FILTER_MODNAME, + ALLOCINFO_FILTER_FUNCTION, + ALLOCINFO_FILTER_FILENAME, + ALLOCINFO_FILTER_LINENO, + __ALLOCINFO_FILTER_LAST = ALLOCINFO_FILTER_LINENO +}; + +#define ALLOCINFO_FILTER_MASK_MODNAME (1 << ALLOCINFO_FILTER_MODNAME) +#define ALLOCINFO_FILTER_MASK_FUNCTION (1 << ALLOCINFO_FILTER_FUNCTION) +#define ALLOCINFO_FILTER_MASK_FILENAME (1 << ALLOCINFO_FILTER_FILENAME) +#define ALLOCINFO_FILTER_MASK_LINENO (1 << ALLOCINFO_FILTER_LINENO) + +#define ALLOCINFO_FILTER_MASKS \ + ((1 << (__ALLOCINFO_FILTER_LAST + 1)) - 1) + +struct allocinfo_filter { + __u64 mask; /* bitmask of the filter fields used */ + struct allocinfo_tag fields; +}; + struct allocinfo_get_at { - __u64 pos; /* input */ + /* inputs */ + __u64 pos; + struct allocinfo_filter filter; + /* output */ struct allocinfo_tag_data data; }; diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index a0577215eb3d..378fcd63b6c9 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -49,6 +49,7 @@ struct allocinfo_private { struct codetag_iterator iter; struct codetag_iterator reported_iter; bool print_header; + struct allocinfo_filter filter; /* ioctl uses a separate iterator not to interfere with reads */ struct codetag_iterator ioctl_iter; bool positioned; /* seq_open_private() sets to 0 */ @@ -184,6 +185,12 @@ static void allocinfo_copy_str(char *dest, const char *src) strscpy_pad(dest, allocinfo_str(src), ALLOCINFO_STR_SIZE); } +/* Compare two strings and only consider the trimmed suffix if s1 is too long */ +static int allocinfo_cmp_str(const char *str, const char *template) +{ + return strncmp(allocinfo_str(str), template, ALLOCINFO_STR_SIZE); +} + /* * Populates the UAPI allocinfo_tag_data structure with active runtime * profiling counters extracted from the given kernel codetag. @@ -223,6 +230,40 @@ static int allocinfo_ioctl_get_content_id(struct seq_file *m, void __user *arg) return 0; } +/* + * Verifies whether a given codetag satisfies the active filtering criteria by + * matching its characteristics against the specified filter. + */ +static bool matches_filter(struct codetag *ct, struct allocinfo_filter *filter) +{ + if (!filter || !filter->mask) + return true; + + if (filter->mask & ALLOCINFO_FILTER_MASK_MODNAME) { + /* user wants to filter by modname but ct->modname is NULL */ + if (!ct->modname) { + /* validate if user was attempting to filter for built-in allocations */ + if (filter->fields.modname[0] != '\0') + return false; + } else if (allocinfo_cmp_str(ct->modname, filter->fields.modname)) + return false; + } + + if ((filter->mask & ALLOCINFO_FILTER_MASK_FUNCTION) && + ct->function && allocinfo_cmp_str(ct->function, filter->fields.function)) + return false; + + if ((filter->mask & ALLOCINFO_FILTER_MASK_FILENAME) && + ct->filename && allocinfo_cmp_str(ct->filename, filter->fields.filename)) + return false; + + if ((filter->mask & ALLOCINFO_FILTER_MASK_LINENO) && + ct->lineno != filter->fields.lineno) + return false; + + return true; +} + /* * Seeks the ioctl iterator to the specified 0-indexed tag position, reads its * profiling data and returns it to userspace. @@ -231,29 +272,46 @@ static int allocinfo_ioctl_get_at(struct seq_file *m, void __user *arg) { struct allocinfo_private *priv; struct codetag *ct; - __u64 pos; struct allocinfo_get_at params = {0}; + __u64 skip_count; if (copy_from_user(¶ms, arg, sizeof(params))) return -EFAULT; + if (params.filter.mask & ~ALLOCINFO_FILTER_MASKS) + return -EINVAL; + priv = m->private; - pos = params.pos; mutex_lock(&priv->ioctl_lock); codetag_lock_module_list(alloc_tag_cttype); - if (pos >= codetag_get_count(alloc_tag_cttype)) { + if (params.pos >= codetag_get_count(alloc_tag_cttype)) { codetag_unlock_module_list(alloc_tag_cttype); mutex_unlock(&priv->ioctl_lock); return -ENOENT; } + skip_count = params.pos; + + if (params.filter.mask) + priv->filter = params.filter; + else + priv->filter.mask = 0; + /* Find the codetag */ priv->ioctl_iter = codetag_get_ct_iter(alloc_tag_cttype); ct = codetag_next_ct(&priv->ioctl_iter); - while (ct && pos--) + + while (ct) { + if (matches_filter(ct, &priv->filter)) { + if (skip_count == 0) + break; + skip_count--; + } ct = codetag_next_ct(&priv->ioctl_iter); + } + if (ct) { allocinfo_to_params(ct, ¶ms.data); priv->positioned = true; @@ -294,6 +352,8 @@ static int allocinfo_ioctl_get_next(struct seq_file *m, void __user *arg) } ct = codetag_next_ct(&priv->ioctl_iter); + while (ct && !matches_filter(ct, &priv->filter)) + ct = codetag_next_ct(&priv->ioctl_iter); if (ct) allocinfo_to_params(ct, ¶ms); -- 2.54.0.1099.g489fc7bff1-goog