linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kernfs: fix locking around kernfs_ops->release() callback
       [not found]   ` <20170211044855.GC3897@atomide.com>
@ 2017-02-11 20:33     ` Tejun Heo
  2017-02-13 17:57       ` Tony Lindgren
  2017-02-21 20:50       ` Tejun Heo
  0 siblings, 2 replies; 4+ messages in thread
From: Tejun Heo @ 2017-02-11 20:33 UTC (permalink / raw)
  To: linux-kernel, Greg Kroah-Hartman; +Cc: Tony Lindgren

The release callback may be called from two places - file release
operation and kernfs open file draining.  kernfs_open_file->mutex is
used to synchronize the two callsites.  This unfortunately leads to
possible circular locking because of->mutex is used to protect the
usual kernfs operations which may use locking constructs which are
held while removing and thus draining kernfs files.

@of->mutex is for synchronizing concurrent kernfs access operations
and all we need here is synchronization between the releaes and drain
paths.  As the drain path has to grab kernfs_open_file_mutex anyway,
let's use the mutex to synchronize the release operation instead.

Signed-off-by: Tejun Heo <tj@kernel.org>
Reported-by: Tony Lindgren <tony@atomide.com>
Fixes: 0e67db2f9fe9 ("kernfs: add kernfs_ops->open/release() callbacks")
---
Hello,

Tony, can you please verify that this resolves the lockdep warnings
that you've been seeing on linux-next?

Greg, this is a fix for the kernfs patches which are being routed
through the cgroup tree.  Once Tony confirms, I'll apply this patch on
top.

Thanks!

 fs/kernfs/file.c |   19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

--- a/fs/kernfs/file.c
+++ b/fs/kernfs/file.c
@@ -747,10 +747,15 @@ err_out:
 static void kernfs_release_file(struct kernfs_node *kn,
 				struct kernfs_open_file *of)
 {
-	if (!(kn->flags & KERNFS_HAS_RELEASE))
-		return;
+	/*
+	 * @of is guaranteed to have no other file operations in flight and
+	 * we just want to synchronize release and drain paths.
+	 * @kernfs_open_file_mutex is enough.  @of->mutex can't be used
+	 * here because drain path may be called from places which can
+	 * cause circular dependency.
+	 */
+	lockdep_assert_held(&kernfs_open_file_mutex);
 
-	mutex_lock(&of->mutex);
 	if (!of->released) {
 		/*
 		 * A file is never detached without being released and we
@@ -760,7 +765,6 @@ static void kernfs_release_file(struct k
 		kn->attr.ops->release(of);
 		of->released = true;
 	}
-	mutex_unlock(&of->mutex);
 }
 
 static int kernfs_fop_release(struct inode *inode, struct file *filp)
@@ -768,7 +772,12 @@ static int kernfs_fop_release(struct ino
 	struct kernfs_node *kn = filp->f_path.dentry->d_fsdata;
 	struct kernfs_open_file *of = kernfs_of(filp);
 
-	kernfs_release_file(kn, of);
+	if (kn->flags & KERNFS_HAS_RELEASE) {
+		mutex_lock(&kernfs_open_file_mutex);
+		kernfs_release_file(kn, of);
+		mutex_unlock(&kernfs_open_file_mutex);
+	}
+
 	kernfs_put_open_node(kn, of);
 	seq_release(inode, filp);
 	kfree(of->prealloc_buf);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kernfs: fix locking around kernfs_ops->release() callback
  2017-02-11 20:33     ` [PATCH] kernfs: fix locking around kernfs_ops->release() callback Tejun Heo
@ 2017-02-13 17:57       ` Tony Lindgren
  2017-02-13 18:22         ` Greg Kroah-Hartman
  2017-02-21 20:50       ` Tejun Heo
  1 sibling, 1 reply; 4+ messages in thread
From: Tony Lindgren @ 2017-02-13 17:57 UTC (permalink / raw)
  To: Tejun Heo; +Cc: linux-kernel, Greg Kroah-Hartman

* Tejun Heo <tj@kernel.org> [170211 12:34]:
> The release callback may be called from two places - file release
> operation and kernfs open file draining.  kernfs_open_file->mutex is
> used to synchronize the two callsites.  This unfortunately leads to
> possible circular locking because of->mutex is used to protect the
> usual kernfs operations which may use locking constructs which are
> held while removing and thus draining kernfs files.
> 
> @of->mutex is for synchronizing concurrent kernfs access operations
> and all we need here is synchronization between the releaes and drain
> paths.  As the drain path has to grab kernfs_open_file_mutex anyway,
> let's use the mutex to synchronize the release operation instead.
> 
> Signed-off-by: Tejun Heo <tj@kernel.org>
> Reported-by: Tony Lindgren <tony@atomide.com>
> Fixes: 0e67db2f9fe9 ("kernfs: add kernfs_ops->open/release() callbacks")
> ---
> Hello,
> 
> Tony, can you please verify that this resolves the lockdep warnings
> that you've been seeing on linux-next?

Yes thanks this fixes the issue I was seeing:

Tested-by: Tony Lindgren <tony@atomide.com>

> Greg, this is a fix for the kernfs patches which are being routed
> through the cgroup tree.  Once Tony confirms, I'll apply this patch on
> top.
> 
> Thanks!
> 
>  fs/kernfs/file.c |   19 ++++++++++++++-----
>  1 file changed, 14 insertions(+), 5 deletions(-)
> 
> --- a/fs/kernfs/file.c
> +++ b/fs/kernfs/file.c
> @@ -747,10 +747,15 @@ err_out:
>  static void kernfs_release_file(struct kernfs_node *kn,
>  				struct kernfs_open_file *of)
>  {
> -	if (!(kn->flags & KERNFS_HAS_RELEASE))
> -		return;
> +	/*
> +	 * @of is guaranteed to have no other file operations in flight and
> +	 * we just want to synchronize release and drain paths.
> +	 * @kernfs_open_file_mutex is enough.  @of->mutex can't be used
> +	 * here because drain path may be called from places which can
> +	 * cause circular dependency.
> +	 */
> +	lockdep_assert_held(&kernfs_open_file_mutex);
>  
> -	mutex_lock(&of->mutex);
>  	if (!of->released) {
>  		/*
>  		 * A file is never detached without being released and we
> @@ -760,7 +765,6 @@ static void kernfs_release_file(struct k
>  		kn->attr.ops->release(of);
>  		of->released = true;
>  	}
> -	mutex_unlock(&of->mutex);
>  }
>  
>  static int kernfs_fop_release(struct inode *inode, struct file *filp)
> @@ -768,7 +772,12 @@ static int kernfs_fop_release(struct ino
>  	struct kernfs_node *kn = filp->f_path.dentry->d_fsdata;
>  	struct kernfs_open_file *of = kernfs_of(filp);
>  
> -	kernfs_release_file(kn, of);
> +	if (kn->flags & KERNFS_HAS_RELEASE) {
> +		mutex_lock(&kernfs_open_file_mutex);
> +		kernfs_release_file(kn, of);
> +		mutex_unlock(&kernfs_open_file_mutex);
> +	}
> +
>  	kernfs_put_open_node(kn, of);
>  	seq_release(inode, filp);
>  	kfree(of->prealloc_buf);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kernfs: fix locking around kernfs_ops->release() callback
  2017-02-13 17:57       ` Tony Lindgren
@ 2017-02-13 18:22         ` Greg Kroah-Hartman
  0 siblings, 0 replies; 4+ messages in thread
From: Greg Kroah-Hartman @ 2017-02-13 18:22 UTC (permalink / raw)
  To: Tony Lindgren; +Cc: Tejun Heo, linux-kernel

On Mon, Feb 13, 2017 at 09:57:25AM -0800, Tony Lindgren wrote:
> * Tejun Heo <tj@kernel.org> [170211 12:34]:
> > The release callback may be called from two places - file release
> > operation and kernfs open file draining.  kernfs_open_file->mutex is
> > used to synchronize the two callsites.  This unfortunately leads to
> > possible circular locking because of->mutex is used to protect the
> > usual kernfs operations which may use locking constructs which are
> > held while removing and thus draining kernfs files.
> > 
> > @of->mutex is for synchronizing concurrent kernfs access operations
> > and all we need here is synchronization between the releaes and drain
> > paths.  As the drain path has to grab kernfs_open_file_mutex anyway,
> > let's use the mutex to synchronize the release operation instead.
> > 
> > Signed-off-by: Tejun Heo <tj@kernel.org>
> > Reported-by: Tony Lindgren <tony@atomide.com>
> > Fixes: 0e67db2f9fe9 ("kernfs: add kernfs_ops->open/release() callbacks")
> > ---
> > Hello,
> > 
> > Tony, can you please verify that this resolves the lockdep warnings
> > that you've been seeing on linux-next?
> 
> Yes thanks this fixes the issue I was seeing:
> 
> Tested-by: Tony Lindgren <tony@atomide.com>
> 
> > Greg, this is a fix for the kernfs patches which are being routed
> > through the cgroup tree.  Once Tony confirms, I'll apply this patch on
> > top.

Great, no objection from me:

Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] kernfs: fix locking around kernfs_ops->release() callback
  2017-02-11 20:33     ` [PATCH] kernfs: fix locking around kernfs_ops->release() callback Tejun Heo
  2017-02-13 17:57       ` Tony Lindgren
@ 2017-02-21 20:50       ` Tejun Heo
  1 sibling, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2017-02-21 20:50 UTC (permalink / raw)
  To: linux-kernel, Greg Kroah-Hartman; +Cc: Tony Lindgren

On Sun, Feb 12, 2017 at 05:33:02AM +0900, Tejun Heo wrote:
> The release callback may be called from two places - file release
> operation and kernfs open file draining.  kernfs_open_file->mutex is
> used to synchronize the two callsites.  This unfortunately leads to
> possible circular locking because of->mutex is used to protect the
> usual kernfs operations which may use locking constructs which are
> held while removing and thus draining kernfs files.
> 
> @of->mutex is for synchronizing concurrent kernfs access operations
> and all we need here is synchronization between the releaes and drain
> paths.  As the drain path has to grab kernfs_open_file_mutex anyway,
> let's use the mutex to synchronize the release operation instead.
> 
> Signed-off-by: Tejun Heo <tj@kernel.org>
> Reported-by: Tony Lindgren <tony@atomide.com>
> Fixes: 0e67db2f9fe9 ("kernfs: add kernfs_ops->open/release() callbacks")

Applied to cgroup/for-4.11.

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-02-21 20:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20170209003642.GY3897@atomide.com>
     [not found] ` <20170211031819.GC19050@mtj.duckdns.org>
     [not found]   ` <20170211044855.GC3897@atomide.com>
2017-02-11 20:33     ` [PATCH] kernfs: fix locking around kernfs_ops->release() callback Tejun Heo
2017-02-13 17:57       ` Tony Lindgren
2017-02-13 18:22         ` Greg Kroah-Hartman
2017-02-21 20:50       ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).