The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Alexey Dobriyan <adobriyan@gmail.com>
To: "Krzysztof Wilczyński" <kwilczynski@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
	Wei Yang <albinwyang@tencent.com>,
	Zijie Wang <wangzijie1@honor.com>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] proc: only bump parent nlink when registering directories
Date: Sat, 13 Jun 2026 18:23:09 +0300	[thread overview]
Message-ID: <ac6b0e86-468d-4806-a9e4-60df0efdfcef@p183> (raw)
In-Reply-To: <20260612153031.536525-1-kwilczynski@kernel.org>

On Fri, Jun 12, 2026 at 03:30:31PM +0000, Krzysztof Wilczyński wrote:
> proc_register() increments the parent directory's link count for every
> entry it registers, while remove_proc_entry() and remove_proc_subtree()
> decrement it only when the removed entry is a directory.  Regular files
> thus inflate the parent's count while they exist, and leak one link
> permanently on every create and remove cycle.
> 
> For example, /proc/bus/pci/00 with twenty-two device files and no
> subdirectories reports nlink 24 instead of 2, and SR-IOV VF enable
> and disable cycles, each creating and removing the VF config space
> entries under /proc/bus/pci/<bus>, inflate the link count of that
> directory without bound.
> 
> Before commit e06689bf5701 ("proc: change ->nlink under
> proc_subdir_lock"), the increment lived in proc_mkdir_data() and
> proc_create_mount_point(), and was therefore applied only to
> directories.  Moving it into proc_register() to bring it under
> proc_subdir_lock dropped the S_ISDIR check.
> 
> Thus, increment the parent link count only for directories, matching
> the decrements in remove_proc_entry() and remove_proc_subtree().
> 
> Fixes: e06689bf5701 ("proc: change ->nlink under proc_subdir_lock")
> Cc: stable@vger.kernel.org # v5.5+
> Signed-off-by: Krzysztof Wilczyński <kwilczynski@kernel.org>

> --- a/fs/proc/generic.c
> +++ b/fs/proc/generic.c
> @@ -404,7 +404,8 @@ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
>  		write_unlock(&proc_subdir_lock);
>  		goto out_free_inum;
>  	}
> -	dir->nlink++;
> +	if (S_ISDIR(dp->mode))
> +		dir->nlink++;
>  	write_unlock(&proc_subdir_lock);

nlink accounting should be folded into pde_subdir_insert/pde_erase
probably but as minimal patch it's OK.

Reviewed-by: Alexey Dobriyan <adobriyan@gmail.com>

  reply	other threads:[~2026-06-13 15:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-12 15:30 [PATCH] proc: only bump parent nlink when registering directories Krzysztof Wilczyński
2026-06-13 15:23 ` Alexey Dobriyan [this message]
2026-06-13 20:07   ` Krzysztof Wilczyński

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ac6b0e86-468d-4806-a9e4-60df0efdfcef@p183 \
    --to=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=albinwyang@tencent.com \
    --cc=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=kwilczynski@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=wangzijie1@honor.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox