fs/kernfs: Fix lockdep warning in kernfs_active()

Message ID 20221109120415.55759-1-zhouchengming@bytedance.com
State New
Headers
Series fs/kernfs: Fix lockdep warning in kernfs_active() |

Commit Message

Chengming Zhou Nov. 9, 2022, 12:04 p.m. UTC
  syzbot found a lockdep warning in kernfs_find_and_get_node_by_id(),
bisected to the commit c25491747b21 ("kernfs: Add KERNFS_REMOVING flags"),
which didn't hold kernfs_rwsem before call kernfs_active(kn).

Since kernfs_find_and_get_node_by_id() doesn't have to get active count
of kn, only need to get a stable refcount of kn, so it should be enough
to just check kn has been KERNFS_ACTIVATED.

Reported-by: syzbot+2fdf66e68f5f882c1074@syzkaller.appspotmail.com
Fixes: c25491747b21 ("kernfs: Add KERNFS_REMOVING flags")
Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com>
---
 fs/kernfs/dir.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
  

Comments

Tejun Heo Nov. 9, 2022, 3:46 p.m. UTC | #1
On Wed, Nov 09, 2022 at 08:04:15PM +0800, Chengming Zhou wrote:
> syzbot found a lockdep warning in kernfs_find_and_get_node_by_id(),
> bisected to the commit c25491747b21 ("kernfs: Add KERNFS_REMOVING flags"),
> which didn't hold kernfs_rwsem before call kernfs_active(kn).
> 
> Since kernfs_find_and_get_node_by_id() doesn't have to get active count
> of kn, only need to get a stable refcount of kn, so it should be enough
> to just check kn has been KERNFS_ACTIVATED.
> 
> Reported-by: syzbot+2fdf66e68f5f882c1074@syzkaller.appspotmail.com
> Fixes: c25491747b21 ("kernfs: Add KERNFS_REMOVING flags")
> Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com>

Acked-by: Tejun Heo <tj@kernel.org>

Thanks.
  
Greg KH Nov. 10, 2022, 6 p.m. UTC | #2
On Wed, Nov 09, 2022 at 08:04:15PM +0800, Chengming Zhou wrote:
> syzbot found a lockdep warning in kernfs_find_and_get_node_by_id(),
> bisected to the commit c25491747b21 ("kernfs: Add KERNFS_REMOVING flags"),
> which didn't hold kernfs_rwsem before call kernfs_active(kn).
> 
> Since kernfs_find_and_get_node_by_id() doesn't have to get active count
> of kn, only need to get a stable refcount of kn, so it should be enough
> to just check kn has been KERNFS_ACTIVATED.
> 
> Reported-by: syzbot+2fdf66e68f5f882c1074@syzkaller.appspotmail.com
> Fixes: c25491747b21 ("kernfs: Add KERNFS_REMOVING flags")
> Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com>
> ---
>  fs/kernfs/dir.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
> index 6acd9c3d4cff..08f0f1570cd7 100644
> --- a/fs/kernfs/dir.c
> +++ b/fs/kernfs/dir.c
> @@ -705,7 +705,13 @@ struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
>  			goto err_unlock;
>  	}
>  
> -	if (unlikely(!kernfs_active(kn) || !atomic_inc_not_zero(&kn->count)))
> +	/*
> +	 * ACTIVATED is protected with kernfs_mutex but it was clear when
> +	 * @kn was added to idr and we just wanna see it set.  No need to
> +	 * grab kernfs_mutex.
> +	 */
> +	if (unlikely(!(kn->flags & KERNFS_ACTIVATED) ||
> +		     !atomic_inc_not_zero(&kn->count)))
>  		goto err_unlock;
>  
>  	spin_unlock(&kernfs_idr_lock);
> -- 
> 2.37.2
> 

Shouldn't:
	https://lore.kernel.org/r/Y0SwqBsZ9BMmZv6x@slm.duckdns.org fix this
instead?
  
Tejun Heo Nov. 10, 2022, 6:11 p.m. UTC | #3
On Thu, Nov 10, 2022 at 07:00:52PM +0100, Greg KH wrote:
> Shouldn't:
> 	https://lore.kernel.org/r/Y0SwqBsZ9BMmZv6x@slm.duckdns.org fix this
> instead?

The difference is around whether the id based lookup can see hidden files or
not. But yeah, it makes more sense to hide it.

Thanks.
  
Greg KH Nov. 10, 2022, 6:20 p.m. UTC | #4
On Thu, Nov 10, 2022 at 08:11:34AM -1000, Tejun Heo wrote:
> On Thu, Nov 10, 2022 at 07:00:52PM +0100, Greg KH wrote:
> > Shouldn't:
> > 	https://lore.kernel.org/r/Y0SwqBsZ9BMmZv6x@slm.duckdns.org fix this
> > instead?
> 
> The difference is around whether the id based lookup can see hidden files or
> not. But yeah, it makes more sense to hide it.

So your patch should not have been applied to my tree?  And I should
take this one instead?

confused,

greg k-h
  
Tejun Heo Nov. 10, 2022, 6:28 p.m. UTC | #5
On Thu, Nov 10, 2022 at 07:20:55PM +0100, Greg KH wrote:
> On Thu, Nov 10, 2022 at 08:11:34AM -1000, Tejun Heo wrote:
> > On Thu, Nov 10, 2022 at 07:00:52PM +0100, Greg KH wrote:
> > > Shouldn't:
> > > 	https://lore.kernel.org/r/Y0SwqBsZ9BMmZv6x@slm.duckdns.org fix this
> > > instead?
> > 
> > The difference is around whether the id based lookup can see hidden files or
> > not. But yeah, it makes more sense to hide it.
> 
> So your patch should not have been applied to my tree?  And I should
> take this one instead?

Oh, sorry. The patch you took is the better one and I was confused when I
was looking at this patch. No action necessary. It was just me being
confused.

Thanks.
  
Greg KH Nov. 23, 2022, 6:30 p.m. UTC | #6
On Wed, Nov 09, 2022 at 08:04:15PM +0800, Chengming Zhou wrote:
> syzbot found a lockdep warning in kernfs_find_and_get_node_by_id(),
> bisected to the commit c25491747b21 ("kernfs: Add KERNFS_REMOVING flags"),
> which didn't hold kernfs_rwsem before call kernfs_active(kn).
> 
> Since kernfs_find_and_get_node_by_id() doesn't have to get active count
> of kn, only need to get a stable refcount of kn, so it should be enough
> to just check kn has been KERNFS_ACTIVATED.
> 
> Reported-by: syzbot+2fdf66e68f5f882c1074@syzkaller.appspotmail.com
> Fixes: c25491747b21 ("kernfs: Add KERNFS_REMOVING flags")
> Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com>
> ---
>  fs/kernfs/dir.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
> index 6acd9c3d4cff..08f0f1570cd7 100644
> --- a/fs/kernfs/dir.c
> +++ b/fs/kernfs/dir.c
> @@ -705,7 +705,13 @@ struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
>  			goto err_unlock;
>  	}
>  
> -	if (unlikely(!kernfs_active(kn) || !atomic_inc_not_zero(&kn->count)))
> +	/*
> +	 * ACTIVATED is protected with kernfs_mutex but it was clear when
> +	 * @kn was added to idr and we just wanna see it set.  No need to
> +	 * grab kernfs_mutex.
> +	 */
> +	if (unlikely(!(kn->flags & KERNFS_ACTIVATED) ||
> +		     !atomic_inc_not_zero(&kn->count)))
>  		goto err_unlock;
>  
>  	spin_unlock(&kernfs_idr_lock);
> -- 
> 2.37.2
> 

Is this still needed in 6.1-rc6?

thanks,

greg k-h
  
Tejun Heo Nov. 23, 2022, 6:35 p.m. UTC | #7
On Wed, Nov 23, 2022 at 07:30:01PM +0100, Greg KH wrote:
> Is this still needed in 6.1-rc6?

1edfe4ea16ca ("kernfs: Fix spurious lockdep warning in
kernfs_find_and_get_node_by_id()") already fixed the issue. So, not needed.

Thanks.
  

Patch

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 6acd9c3d4cff..08f0f1570cd7 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -705,7 +705,13 @@  struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
 			goto err_unlock;
 	}
 
-	if (unlikely(!kernfs_active(kn) || !atomic_inc_not_zero(&kn->count)))
+	/*
+	 * ACTIVATED is protected with kernfs_mutex but it was clear when
+	 * @kn was added to idr and we just wanna see it set.  No need to
+	 * grab kernfs_mutex.
+	 */
+	if (unlikely(!(kn->flags & KERNFS_ACTIVATED) ||
+		     !atomic_inc_not_zero(&kn->count)))
 		goto err_unlock;
 
 	spin_unlock(&kernfs_idr_lock);