All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@primarydata.com>
To: bfields@fieldses.org
Cc: linux-nfs@vger.kernel.org, hch@infradead.org
Subject: [PATCH 39/40] nfsd: reduce cl_lock thrashing in release_openowner
Date: Mon, 21 Jul 2014 11:02:51 -0400	[thread overview]
Message-ID: <1405954972-28904-40-git-send-email-jlayton@primarydata.com> (raw)
In-Reply-To: <1405954972-28904-1-git-send-email-jlayton@primarydata.com>

Releasing an openowner is a bit inefficient as it can potentially thrash
the cl_lock if you have a lot of stateids attached to it. Once we remove
the client_mutex, it'll also potentially be dangerous to do this.

Add some functions to make it easier to defer the part of putting a
generic stateid reference that needs to be done outside the cl_lock while
doing the parts that must be done while holding it under a single lock.

First we unhash each open stateid. Then we call
put_generic_stateid_locked which will put the reference to an
nfs4_ol_stateid. If it turns out to be the last reference, it'll go
ahead and remove the stid from the IDR tree and put it onto the reaplist
using the st_locks list_head.

Then, after dropping the lock we'll call free_ol_stateid_reaplist to
walk the list of stateids that are fully unhashed and ready to be freed,
and free each of them. This function can sleep, so it must be done
outside any spinlocks.

Signed-off-by: Jeff Layton <jlayton@primarydata.com>
---
 fs/nfsd/nfs4state.c | 95 +++++++++++++++++++++++++++++++++++------------------
 1 file changed, 63 insertions(+), 32 deletions(-)

diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index 181c4cc36711..918c09b45843 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -944,6 +944,30 @@ static void put_generic_stateid(struct nfs4_ol_stateid *stp)
 	nfs4_put_stid(&stp->st_stid);
 }
 
+/*
+ * Put the persistent reference to an already unhashed generic stateid, while
+ * holding the cl_lock. If it's the last reference, then put it onto the
+ * reaplist for later destruction.
+ */
+static void put_generic_stateid_locked(struct nfs4_ol_stateid *stp,
+				       struct list_head *reaplist)
+{
+	struct nfs4_stid *s = &stp->st_stid;
+	struct nfs4_client *clp = s->sc_client;
+
+	lockdep_assert_held(&clp->cl_lock);
+
+	WARN_ON_ONCE(!list_empty(&stp->st_locks));
+
+	if (!atomic_dec_and_test(&s->sc_count)) {
+		wake_up_all(&close_wq);
+		return;
+	}
+
+	remove_stid_locked(clp, s);
+	list_add(&stp->st_locks, reaplist);
+}
+
 static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
 {
 	struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
@@ -974,6 +998,25 @@ static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
 	list_del_init(&lo->lo_owner.so_strhash);
 }
 
+/*
+ * Free a list of generic stateids that were collected earlier after being
+ * fully unhashed.
+ */
+static void
+free_ol_stateid_reaplist(struct list_head *reaplist)
+{
+	struct nfs4_ol_stateid *stp;
+
+	might_sleep();
+
+	while (!list_empty(reaplist)) {
+		stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
+				       st_locks);
+		list_del(&stp->st_locks);
+		stp->st_stid.sc_free(&stp->st_stid);
+	}
+}
+
 static void release_lockowner(struct nfs4_lockowner *lo)
 {
 	struct nfs4_client *clp = lo->lo_owner.so_client;
@@ -988,23 +1031,10 @@ static void release_lockowner(struct nfs4_lockowner *lo)
 		stp = list_first_entry(&lo->lo_owner.so_stateids,
 				struct nfs4_ol_stateid, st_perstateowner);
 		unhash_lock_stateid(stp);
-		/*
-		 * We now know that no new references can be added to the
-		 * stateid. If ours is the last one, finish the unhashing
-		 * and put it on the list to be reaped.
-		 */
-		if (atomic_dec_and_test(&stp->st_stid.sc_count)) {
-			remove_stid_locked(clp, &stp->st_stid);
-			list_add(&stp->st_locks, &reaplist);
-		}
+		put_generic_stateid_locked(stp, &reaplist);
 	}
 	spin_unlock(&clp->cl_lock);
-	while (!list_empty(&reaplist)) {
-		stp = list_first_entry(&reaplist, struct nfs4_ol_stateid,
-					st_locks);
-		list_del(&stp->st_locks);
-		stp->st_stid.sc_free(&stp->st_stid);
-	}
+	free_ol_stateid_reaplist(&reaplist);
 	nfs4_put_stateowner(&lo->lo_owner);
 }
 
@@ -1025,16 +1055,21 @@ static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
 
 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
 {
-	spin_lock(&stp->st_stateowner->so_client->cl_lock);
+	lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
+
 	unhash_generic_stateid(stp);
 	release_open_stateid_locks(stp);
-	spin_unlock(&stp->st_stateowner->so_client->cl_lock);
 }
 
 static void release_open_stateid(struct nfs4_ol_stateid *stp)
 {
+	LIST_HEAD(reaplist);
+
+	spin_lock(&stp->st_stid.sc_client->cl_lock);
 	unhash_open_stateid(stp);
-	put_generic_stateid(stp);
+	put_generic_stateid_locked(stp, &reaplist);
+	spin_unlock(&stp->st_stid.sc_client->cl_lock);
+	free_ol_stateid_reaplist(&reaplist);
 }
 
 static void unhash_openowner_locked(struct nfs4_openowner *oo)
@@ -1058,30 +1093,24 @@ static void release_last_closed_stateid(struct nfs4_openowner *oo)
 	}
 }
 
-static void release_openowner_stateids(struct nfs4_openowner *oo)
+static void release_openowner(struct nfs4_openowner *oo)
 {
 	struct nfs4_ol_stateid *stp;
 	struct nfs4_client *clp = oo->oo_owner.so_client;
+	struct list_head reaplist;
 
-	lockdep_assert_held(&clp->cl_lock);
+	INIT_LIST_HEAD(&reaplist);
 
+	spin_lock(&clp->cl_lock);
+	unhash_openowner_locked(oo);
 	while (!list_empty(&oo->oo_owner.so_stateids)) {
 		stp = list_first_entry(&oo->oo_owner.so_stateids,
 				struct nfs4_ol_stateid, st_perstateowner);
-		spin_unlock(&clp->cl_lock);
-		release_open_stateid(stp);
-		spin_lock(&clp->cl_lock);
+		unhash_open_stateid(stp);
+		put_generic_stateid_locked(stp, &reaplist);
 	}
-}
-
-static void release_openowner(struct nfs4_openowner *oo)
-{
-	struct nfs4_client *clp = oo->oo_owner.so_client;
-
-	spin_lock(&clp->cl_lock);
-	unhash_openowner_locked(oo);
-	release_openowner_stateids(oo);
 	spin_unlock(&clp->cl_lock);
+	free_ol_stateid_reaplist(&reaplist);
 	release_last_closed_stateid(oo);
 	nfs4_put_stateowner(&oo->oo_owner);
 }
@@ -4690,7 +4719,9 @@ static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
 	struct nfs4_client *clp = s->st_stid.sc_client;
 
 	s->st_stid.sc_type = NFS4_CLOSED_STID;
+	spin_lock(&clp->cl_lock);
 	unhash_open_stateid(s);
+	spin_unlock(&clp->cl_lock);
 
 	if (clp->cl_minorversion)
 		put_generic_stateid(s);
-- 
1.9.3


  parent reply	other threads:[~2014-07-21 15:03 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-21 15:02 [PATCH 00/40] nfsd: stateid and stateowner refcounting overhaul Jeff Layton
2014-07-21 15:02 ` [PATCH 01/40] nfsd4: use cl_lock to synchronize all stateid idr calls Jeff Layton
2014-07-27 13:21   ` Christoph Hellwig
2014-07-21 15:02 ` [PATCH 02/40] nfsd: Add reference counting to the lock and open stateids Jeff Layton
2014-07-27 13:23   ` Christoph Hellwig
2014-07-21 15:02 ` [PATCH 03/40] nfsd: Add a struct nfs4_file field to struct nfs4_stid Jeff Layton
2014-07-27 13:24   ` Christoph Hellwig
2014-07-21 15:02 ` [PATCH 04/40] nfsd: Replace nfs4_ol_stateid->st_file with the st_stid.sc_file Jeff Layton
2014-07-27 13:25   ` Christoph Hellwig
2014-07-21 15:02 ` [PATCH 05/40] nfsd: Ensure atomicity of stateid destruction and idr tree removal Jeff Layton
2014-07-27 13:28   ` Christoph Hellwig
2014-07-21 15:02 ` [PATCH 06/40] nfsd: Cleanup the freeing of stateids Jeff Layton
2014-07-27 13:35   ` Christoph Hellwig
2014-07-21 15:02 ` [PATCH 07/40] nfsd: do filp_close in sc_free callback for lock stateids Jeff Layton
2014-07-27 13:37   ` Christoph Hellwig
2014-07-21 15:02 ` [PATCH 08/40] nfsd: Add locking to protect the state owner lists Jeff Layton
2014-07-27 13:42   ` Christoph Hellwig
2014-07-29 11:42     ` Jeff Layton
2014-07-21 15:02 ` [PATCH 09/40] nfsd: clean up races in lock stateid searching and creation Jeff Layton
2014-07-27 13:44   ` Christoph Hellwig
2014-07-21 15:02 ` [PATCH 10/40] nfsd: Slight cleanup of find_stateid() Jeff Layton
2014-07-27 13:38   ` Christoph Hellwig
2014-07-21 15:02 ` [PATCH 11/40] nfsd: ensure atomicity in nfsd4_free_stateid and nfsd4_validate_stateid Jeff Layton
2014-07-27 13:46   ` Christoph Hellwig
2014-07-21 15:02 ` [PATCH 12/40] nfsd: Add reference counting to lock stateids Jeff Layton
2014-07-21 15:02 ` [PATCH 13/40] nfsd: nfsd4_locku() must reference the lock stateid Jeff Layton
2014-07-21 15:02 ` [PATCH 14/40] nfsd: Ensure that nfs4_open_delegation() references the delegation stateid Jeff Layton
2014-07-21 15:02 ` [PATCH 15/40] nfsd: nfsd4_process_open2() must reference " Jeff Layton
2014-07-21 15:02 ` [PATCH 16/40] nfsd: nfsd4_process_open2() must reference the open stateid Jeff Layton
2014-07-21 15:02 ` [PATCH 17/40] nfsd: Prepare nfsd4_close() for open stateid referencing Jeff Layton
2014-07-21 15:02 ` [PATCH 18/40] nfsd: nfsd4_open_confirm() must reference the open stateid Jeff Layton
2014-07-21 15:02 ` [PATCH 19/40] nfsd: Add reference counting to nfs4_preprocess_confirmed_seqid_op Jeff Layton
2014-07-21 15:02 ` [PATCH 20/40] nfsd: Migrate the stateid reference into nfs4_preprocess_seqid_op Jeff Layton
2014-07-21 15:02 ` [PATCH 21/40] nfsd: Migrate the stateid reference into nfs4_lookup_stateid() Jeff Layton
2014-07-21 15:02 ` [PATCH 22/40] nfsd: Migrate the stateid reference into nfs4_find_stateid_by_type() Jeff Layton
2014-07-21 15:02 ` [PATCH 23/40] nfsd: Add reference counting to state owners Jeff Layton
2014-07-27 13:58   ` Christoph Hellwig
2014-07-21 15:02 ` [PATCH 24/40] nfsd: Add a mutex to protect the NFSv4.0 open owner replay cache Jeff Layton
2014-07-21 15:02 ` [PATCH 25/40] nfsd: Keep a reference to the open stateid for the NFSv4.0 " Jeff Layton
2014-07-27 13:59   ` Christoph Hellwig
2014-07-21 15:02 ` [PATCH 26/40] nfsd: clean up lockowner refcounting when finding them Jeff Layton
2014-07-21 15:02 ` [PATCH 27/40] nfsd: add an operation for unhashing a stateowner Jeff Layton
2014-07-21 15:02 ` [PATCH 28/40] nfsd: Make lock stateid take a reference to the lockowner Jeff Layton
2014-07-21 15:02 ` [PATCH 29/40] nfsd: clean up refcounting for lockowners Jeff Layton
2014-07-21 15:02 ` [PATCH 30/40] nfsd: make openstateids hold references to their openowners Jeff Layton
2014-07-21 15:02 ` [PATCH 31/40] nfsd: don't allow CLOSE to proceed until refcount on stateid drops Jeff Layton
2014-07-21 15:02 ` [PATCH 32/40] nfsd: Protect adding/removing open state owners using client_lock Jeff Layton
2014-07-21 15:02 ` [PATCH 33/40] nfsd: Protect adding/removing lock " Jeff Layton
2014-07-21 15:02 ` [PATCH 34/40] nfsd: Move the open owner hash table into struct nfs4_client Jeff Layton
2014-07-21 15:02 ` [PATCH 35/40] nfsd: clean up and reorganize release_lockowner Jeff Layton
2014-07-21 15:02 ` [PATCH 36/40] nfsd: add locking to stateowner release Jeff Layton
2014-07-21 15:02 ` [PATCH 37/40] nfsd: optimize destroy_lockowner cl_lock thrashing Jeff Layton
2014-07-21 15:02 ` [PATCH 38/40] nfsd: close potential race in nfsd4_free_stateid Jeff Layton
2014-07-21 15:02 ` Jeff Layton [this message]
2014-07-21 15:02 ` [PATCH 40/40] nfsd: don't thrash the cl_lock while freeing an open stateid Jeff Layton

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=1405954972-28904-40-git-send-email-jlayton@primarydata.com \
    --to=jlayton@primarydata.com \
    --cc=bfields@fieldses.org \
    --cc=hch@infradead.org \
    --cc=linux-nfs@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.