From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (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 2A94831326B for ; Thu, 16 Apr 2026 16:41:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776357703; cv=none; b=qOOHTeK31UMF1aClrKDfq6+H5KIDO1X3dxbYjGxXvhejQrt07hoMOqfuJpazO3Srpl7g8s7R/KrMT9hoAHyBkUSaIJ9BQ3yLnJSX6uOHZ9mHYOxY8bQRzUF727x31AomI+dEVFjO5fI4pPe6IQI1l5FfZZT5iBLgnJV/b0yZ7OQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776357703; c=relaxed/simple; bh=l6Rmx5K6Ntq6CFC0gtN3rxwhj+CgmxqLmk+1545M1Dg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=azbUKkzuzgtpQe35vtcW0YcCZQHHvTah69jYtXu1DYaEsGmKD9SYjcJgZuAAjcE+/2l1D3L38YaQB+ANCTv+//12PJO3jthcP9/rLmNN6qJhY/DvylvDOUVBw2y/0ZI6Aw6A7jJXMKl+zb08pI+pmvgCOSq0T6xe0yCsp5qK68I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DHpLXKX9; arc=none smtp.client-ip=209.85.221.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DHpLXKX9" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-43d77f6092eso3338140f8f.2 for ; Thu, 16 Apr 2026 09:41:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776357701; x=1776962501; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ld46oPO3p3ufmRzPskilBxxvzozvgh7NzEXkq2AckD0=; b=DHpLXKX9vXG+9w/83OzPVtJ1ueFO/opx6D7v08YWyqxwNQkA7eTP4tdJG+AmlOHRcn LF4N/94A6T92GvvLN0rDAjEXWMQojR4XOSipj0vAUCi0NLfwrN4AxdfPmUN0LAoLUB5h 0U6ZP/WOF6+ysJc09UZsfDVLkSZFnUlyJXlGGMaV9lkjdYc4PLEjjey5otOfLM1ZvXI5 6LglUUv97uBqyde5Ynvc/EbqXoCV+Gj+cwtUZAVhlODjVCdVDalOVBacQKVHLTIbQJTl wH6T66WEQkE3xrhv+Is/+G8HLP6HfMsyqlAmUqowU6/u8TzUQcawkUp9a0fQbizajb+J ESOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776357701; x=1776962501; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=ld46oPO3p3ufmRzPskilBxxvzozvgh7NzEXkq2AckD0=; b=eYZhBxhmSBqzil9dlpJAxEAUd/D4pSh7Xz+G4yOhp10rXkuJ2/3iYJNga8vDuSWlgo 3cCrllXp+FuRFLvRIG1LqM+73SUqz95ZZJ26sFR8onZKd1ygd+Zhg/zo0D8+kG6CplO2 ms7q02s2k3MIz9Vyx5tn510aaGqoAWZbG7yLwICqZm83rqNNeX3rGQwehBL+kzAkU9QU Qz+YVC5+KpGYPlCIbVHjEVYn076FJTu1wVAJvInBtqfdN09eCELis8rORFRQA11L/kIz x0N/Xc93Jw3v5c4fw+1Hs9Al7wFwwx6Fs6PuTOMuzFwieTkPSxlpW9U6qfe8pg7KS9SS 9JZQ== X-Forwarded-Encrypted: i=1; AFNElJ/wjOt16/O9vG9FABjQt1dU+W9I9Ari7mUP77dRbMai+Ohq1zWtskxITUm7oc7axQpoeI0ChsRBOIRaYYc=@vger.kernel.org X-Gm-Message-State: AOJu0YwWoHJxaSpUgpUchpIlLqOrHEwXFVvTAavEzk6017w9Z1e60OwE 1UaZzLa4RoD2C5nf7RNn+P+5+9kLxhfA9T2FbH6AnGPlywyLouX0QV8= X-Gm-Gg: AeBDievpHyt7iMq6evj+EXWmzc4V30wv8EtXZA1pHdrkGzA80DGtHkYEPYbvWIPRWvj cMurk7HGJ6xQMpkemptPEgPglhKDMthsVu9R1WGNR57kctpzHMZyzhHaEcgKroHwXGqJw7nUDD+ VKkWbybZq6SbwnRdLpfVMLd8u1fuLQiCkFs9mlmhQ3cPum74Nwadnjn3XMCK7kvXv51xFCedWo4 SDNSuCdJxpjDuzi2L44HGSYb/cMs7M7R3ScER+ESvlqqwUtcLg8P3hxunpooB0tSDMqEmwig+Th PP4B8daLKcjKGJIktJ7/cV+8bQHXNpx2OR2dTd8QmWSHFUR0blIvjGoT+SERCYvPmO5gSNo0UO7 lB3Zi+Cgs/3nQtt56AH9EhG77U/HSntPGPMJt+nyYAo1gZp6epNqbSX0X+V64io+9yMwpqUewRR upDDY+kj+fVA4a9JRf79UwyuY= X-Received: by 2002:a05:6000:1446:b0:43d:77a8:3bb5 with SMTP id ffacd0b85a97d-43fe15a2942mr102540f8f.37.1776357699373; Thu, 16 Apr 2026 09:41:39 -0700 (PDT) Received: from p183 ([178.172.147.205]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43ead402ee8sm14843662f8f.37.2026.04.16.09.41.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Apr 2026 09:41:38 -0700 (PDT) From: Alexey Dobriyan To: akpm@linux-foundation.org Cc: adobriyan@gmail.com, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 2/2] proc: rewrite next_tgid() Date: Thu, 16 Apr 2026 19:43:30 +0300 Message-ID: <20260416164330.77043-2-adobriyan@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260416164330.77043-1-adobriyan@gmail.com> References: <20260416164330.77043-1-adobriyan@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit * deduplicate "iter.tgid += 1" line, Right now it is done once inside next_tgid() itself and second time inside "for" loop. * deduplicate next_tgid() call itself with different loop style: auto it = make_tgid_iter(); while (next_tgid(&it)) { ... } gcc seems to inline it twice: $ ./scripts/bloat-o-meter ../vmlinux-000 ../obj/vmlinux add/remove: 0/1 grow/shrink: 1/0 up/down: 100/-245 (-145) Function old new delta proc_pid_readdir 531 631 +100 next_tgid 245 - -245 * make tgid_iter.pid_ns const it never changes during readdir anyway --- fs/proc/base.c | 57 +++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/fs/proc/base.c b/fs/proc/base.c index f2db455dbbfd..f38ac943ec32 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -3543,30 +3543,43 @@ struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags) struct tgid_iter { unsigned int tgid; struct task_struct *task; - struct pid_namespace *pid_ns; + struct pid_namespace *const pid_ns; }; -static struct tgid_iter next_tgid(struct tgid_iter iter) +static +struct tgid_iter +make_tgid_iter(unsigned int init_tgid, struct pid_namespace *pid_ns) { - struct pid *pid; + return (struct tgid_iter){ + .tgid = init_tgid - 1, + .pid_ns = pid_ns, + }; +} + +static bool next_tgid(struct tgid_iter *it) +{ + if (it->task) { + put_task_struct(it->task); + it->task = NULL; + } - if (iter.task) - put_task_struct(iter.task); rcu_read_lock(); -retry: - iter.task = NULL; - pid = find_ge_pid(iter.tgid, iter.pid_ns); - if (pid) { - iter.tgid = pid_nr_ns(pid, iter.pid_ns); - iter.task = pid_task(pid, PIDTYPE_TGID); - if (!iter.task) { - iter.tgid += 1; - goto retry; + while (1) { + it->tgid += 1; + const auto pid = find_ge_pid(it->tgid, it->pid_ns); + if (pid) { + it->tgid = pid_nr_ns(pid, it->pid_ns); + it->task = pid_task(pid, PIDTYPE_TGID); + if (it->task) { + get_task_struct(it->task); + rcu_read_unlock(); + return true; + } + } else { + rcu_read_unlock(); + return false; } - get_task_struct(iter.task); } - rcu_read_unlock(); - return iter; } #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2) @@ -3574,7 +3587,6 @@ static struct tgid_iter next_tgid(struct tgid_iter iter) /* for the /proc/ directory itself, after non-process stuff has been done */ int proc_pid_readdir(struct file *file, struct dir_context *ctx) { - struct tgid_iter iter; struct proc_fs_info *fs_info = proc_sb_info(file_inode(file)->i_sb); struct pid_namespace *pid_ns = proc_pid_ns(file_inode(file)->i_sb); loff_t pos = ctx->pos; @@ -3592,12 +3604,9 @@ int proc_pid_readdir(struct file *file, struct dir_context *ctx) return 0; ctx->pos = pos = pos + 1; } - iter.tgid = pos - TGID_OFFSET; - iter.task = NULL; - iter.pid_ns = pid_ns; - for (iter = next_tgid(iter); - iter.task; - iter.tgid += 1, iter = next_tgid(iter)) { + + auto iter = make_tgid_iter(pos - TGID_OFFSET, pid_ns); + while (next_tgid(&iter)) { char name[10 + 1]; unsigned int len; -- 2.52.0