Re: [PATCH] kernfs: recheck of->released after acquiring the active reference

Tejun Heo <[email protected]>
Newsgroups dev.linux.lists.driver-core,org.kernel.vger.linux-kernel,org.kernel.vger.stable
Message-ID <[email protected]>
Hello,

On Thu, Aug 20, 2026 at 02:18:10AM +0000, Fan Wu wrote:
> @@ -73,12 +73,22 @@ static struct kernfs_open_node *of_on(struct kernfs_open_file *of)
>  /* Get active reference to kernfs node for an open file */
>  static struct kernfs_open_file *kernfs_get_active_of(struct kernfs_open_file *of)
>  {
> -	/* Skip if file was already released */
> -	if (unlikely(of->released))
> +	if (!kernfs_get_active(of->kn))
>  		return NULL;
>  
> -	if (!kernfs_get_active(of->kn))
> +	/*
> +	 * @of->released is set under kernfs_open_file_mutex.  While the
> +	 * active reference is held, @kn can't be drained anymore and
> +	 * kernfs_fop_release() can't run, so re-reading @of->released
> +	 * here settles whether @of was released for good.
> +	 */
> +	mutex_lock(kernfs_open_file_mutex_ptr(of->kn));
> +	if (unlikely(of->released)) {
> +		mutex_unlock(kernfs_open_file_mutex_ptr(of->kn));
> +		kernfs_put_active(of->kn);
>  		return NULL;
> +	}
> +	mutex_unlock(kernfs_open_file_mutex_ptr(of->kn));

"lock -> test something -> unlock" pattern is usually unnecessary. What's
the mutex achieving doing? Doesn't kernfs_get_active() already have strong
enough memory barrier? If not, it'd be better to solve it by iterlocking
kernfs_get_active() and released clearing.

Thanks.

-- 
tejun
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.