From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-178.mta0.migadu.com (out-178.mta0.migadu.com [91.218.175.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5EF2919D8A8 for ; Mon, 25 May 2026 03:00:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779678049; cv=none; b=SnIjCHwNSELiZp+1K0bqlplyBPwQ0rI9LwBL6amH15hPlOUe3151rfLK1/NcQPEujV7PJTQaa2utonMlv6NP/wb5yv7ccx/GTPBz+2brMu7Uh0JA3oIVR2bfztOwgMxmdL5WAg5iOPPmXGJKZqbIz/k9i/POttw7/BTu+ciiooo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779678049; c=relaxed/simple; bh=3MY7N95JCk7amtourd9Vmk/l4tLv6TWs739xKXOavZs=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=b3PAFlknOJ5SHAsB9kLtzteYg+nK8YK4nC0P/FzC1Qb5KS1w+96mu0MwFOvVHLB9R+aUEWYzIkNYJy7Z1Y4ZLMTTMZPD1k2P1Khn5J6YN1mORRnkQ4P7hTTn/NyD4GW/duczP4+R/x7IypBomjzJ++jJ/tgi0xBmsquQH0qwcBQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=PADIikT6; arc=none smtp.client-ip=91.218.175.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="PADIikT6" Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1779678043; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=L/vfFUAUvvzoN2lU2+u7hA3dbwyNyJR1IImbRDNj9+w=; b=PADIikT6imgTJUGlmhoiLnsI4UwjhCyANuEDOb9Or4Ke1HY1P0qsbbsC6qUNRsSFdEPm4t VA8ZAt94OfjHC/RqK+Intb/zt++crwxRFOyp045rVGCMehtteMs6/I3J8Swmwu9BNgpUP7 q/nejSFaamNfADN24GqeVw2wD+y4k3Q= Date: Mon, 25 May 2026 10:59:51 +0800 Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH v2 2/6] alloc_tag: add ioctl filters to /proc/allocinfo To: Abhishek Bapat , Suren Baghdasaryan , Andrew Morton , Kent Overstreet Cc: Shuah Khan , Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Sourav Panda References: Content-Language: en-US X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Hao Ge In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT Hi Abhishek On 2026/5/23 01:45, Abhishek Bapat wrote: > 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 | 58 ++++++++++++++++++++++++++++++++-- > 2 files changed, 80 insertions(+), 4 deletions(-) > > diff --git a/include/uapi/linux/alloc_tag.h b/include/uapi/linux/alloc_tag.h > index e9a5b55fcc7a..0cc9db5298c6 100644 > --- a/include/uapi/linux/alloc_tag.h > +++ b/include/uapi/linux/alloc_tag.h > @@ -34,8 +34,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 3598735b6c93..56c394ef721f 100644 > --- a/lib/alloc_tag.c > +++ b/lib/alloc_tag.c > @@ -48,6 +48,7 @@ int alloc_tag_ref_offs; > struct allocinfo_private { > struct codetag_iterator 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 */ > @@ -167,6 +168,11 @@ static void allocinfo_copy_str(char *dest, const char *src) > strscpy(dest, allocinfo_str(src), ALLOCINFO_STR_SIZE); > } > > +static int allocinfo_cmp_str(const char *str, const char *template) > +{ > + return strncmp(allocinfo_str(str), template, ALLOCINFO_STR_SIZE); > +} > + > static void allocinfo_to_params(struct codetag *ct, > struct allocinfo_tag_data *data) > { > @@ -198,27 +204,71 @@ static int allocinfo_ioctl_get_content_id(struct seq_file *m, void __user *arg) > return 0; > } > > +static bool matches_filter(struct codetag *ct, struct allocinfo_filter *filter) > +{ > + if (!filter || !filter->mask) > + return true; > + > + if (filter->mask & ALLOCINFO_FILTER_MASK_MODNAME) { > + if (!ct->modname) > + return false; > + if (allocinfo_cmp_str(ct->modname, filter->fields.modname)) > + return false; > + } > + Apologies -- I previously suggested the "!ct->modname -> return false" approach, but I realized we broke the ability to filter for built-in allocations.  allocinfo_to_params() returns modname="" for those, so a user would naturally try to filter by modname="" -- except it never matches because ct->modname is NULL, not an empty string. Maybe something like this instead? if (filter->mask & ALLOCINFO_FILTER_MASK_MODNAME) {     if (ct->modname) {         if (allocinfo_cmp_str(ct->modname, filter->fields.modname))             return false;         } else if (filter->fields.modname[0] != '\0') {             return false;         } } That way modname="" matches built-in tags, which lines up with what the API actually returns. Thanks Hao > + 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; > +} > + > 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 = (struct allocinfo_private *)m->private; > - pos = params.pos; > + > + skip_count = params.pos; > > mutex_lock(&priv->ioctl_lock); > codetag_lock_module_list(alloc_tag_cttype, true); > > + 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; > @@ -254,6 +304,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); >