Skip to contentSkip to conversation

nfsd: when reusing an existing repcache entry, unhash it first

PATCH In review Jeff Layton 3 participants 16 messages Dec 2, 2013 – Dec 4, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nfsd: when reusing an existing repcache entry, unhash it first

The DRC code will attempt to reuse an existing, expired cache entry in preference to allocating a new one. It'll then search the cache, and if it gets a hit it'll then free the cache entry that it was going to reuse.

The cache code doesn't unhash the entry that it's going to reuse however, so it's possible for it end up designating an entry for reuse and then subsequently freeing the same entry after it finds it. This leads it to a later use-after-free situation and usually some list corruption warnings or an oops.

Fix this by simply unhashing the entry that we intend to reuse. That will mean that it's not findable via a search and should prevent this situation from occurring.

---
 fs/nfsd/nfscache.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/nfsd/nfscache.c +8 -1
index 9186c7c..b6af150 100644
@@ -132,6 +132,13 @@ nfsd_reply_cache_alloc(void)
 }
 
 static void
+nfsd_reply_cache_unhash(struct svc_cacherep *rp)
+{
+	hlist_del_init(&rp->c_hash);
+	list_del_init(&rp->c_lru);
+}
+
+static void
 nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
 {
 	if (rp->c_type == RC_REPLBUFF && rp->c_replvec.iov_base) {
@@ -417,7 +424,7 @@ nfsd_cache_lookup(struct svc_rqst *rqstp)
 		rp = list_first_entry(&lru_head, struct svc_cacherep, c_lru);
 		if (nfsd_cache_entry_expired(rp) ||
 		    num_drc_entries >= max_drc_entries) {
-			lru_put_end(rp);
+			nfsd_reply_cache_unhash(rp);
 			prune_cache_entries();
 			goto search_cache;
 		}
Signature · 1 line
1.8.4.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Jeff Layton

The DRC code will attempt to reuse an existing, expired cache entry in preference to allocating a new one. It'll then search the cache, and if it gets a hit it'll then free the cache entry that it was going to reuse.

The cache code doesn't unhash the entry that it's going to reuse however, so it's possible for it end up designating an entry for reuse and then subsequently freeing the same entry after it finds it. This leads it to a later use-after-free situation and usually some list corruption warnings or an oops.

Quoted 3 lines

Fix this by simply unhashing the entry that we intend to reuse. That will mean that it's not findable via a search and should prevent this situation from occurring.

The fix looks reasonable to me,

Btw, it seems like this code would benefit from being converted to the list_lru structure.

#2 lore raw
Quoted 12 lines · On Tue, 3 Dec 2013 02:25:17 -0800 Christoph Hellwig <[email protected]> wrote:
Quoted 11 lines · Jeff Layton

The DRC code will attempt to reuse an existing, expired cache entry in preference to allocating a new one. It'll then search the cache, and if it gets a hit it'll then free the cache entry that it was going to reuse.

The cache code doesn't unhash the entry that it's going to reuse however, so it's possible for it end up designating an entry for reuse and then subsequently freeing the same entry after it finds it. This leads it to a later use-after-free situation and usually some list corruption warnings or an oops.

Quoted 8 lines
Quoted 3 lines

Fix this by simply unhashing the entry that we intend to reuse. That will mean that it's not findable via a search and should prevent this situation from occurring.

The fix looks reasonable to me,

Thanks.

Quoted 2 lines

Btw, it seems like this code would benefit from being converted to the list_lru structure.

Maybe.

Most of this code is protected by a single spinlock (cache_lock). The main benefit to switching to list_lru would be that we could move to a per-node lock. But, to make that worthwhile would mean we'd need to redesign the locking and break the cache_lock into multiple locks.

Also, the existing code does take pains to reuse an expired entry off the LRU list in preference to allocating a new one. The list_lru code doesn't have a mechanism to scrape the first entry off the LRU list, though I suppose we could add one or abuse the cb_arg in the walk callback as a return pointer.

I'm not sure the resulting code would be any clearer however...

Maybe when we get to the point that the cache_lock shows up as a bottleneck, it'll make more sense?

Signature · 1 line
Jeff Layton <[email protected]>
#3 lore raw
Quoted 4 lines · Jeff Layton

Most of this code is protected by a single spinlock (cache_lock). The main benefit to switching to list_lru would be that we could move to a per-node lock. But, to make that worthwhile would mean we'd need to redesign the locking and break the cache_lock into multiple locks.

No need to redo locks just yet, the biggest benefit is to use a well debug library for lru list handling, with the second biggest one being that you get out of the box shrinker support.

Quoted 5 lines

Also, the existing code does take pains to reuse an expired entry off the LRU list in preference to allocating a new one. The list_lru code doesn't have a mechanism to scrape the first entry off the LRU list, though I suppose we could add one or abuse the cb_arg in the walk callback as a return pointer.

That's just because it is an old-school cache with a fixed upper bound of entries and no shrinker support. With proper shrinker integration this nasty code isn't needed.

#4 lore raw
Quoted 16 lines · On Wed, 4 Dec 2013 00:33:36 -0800 Christoph Hellwig <[email protected]> wrote:
Quoted 4 lines · Jeff Layton

Most of this code is protected by a single spinlock (cache_lock). The main benefit to switching to list_lru would be that we could move to a per-node lock. But, to make that worthwhile would mean we'd need to redesign the locking and break the cache_lock into multiple locks.

No need to redo locks just yet, the biggest benefit is to use a well debug library for lru list handling, with the second biggest one being that you get out of the box shrinker support.

Quoted 5 lines

Also, the existing code does take pains to reuse an expired entry off the LRU list in preference to allocating a new one. The list_lru code doesn't have a mechanism to scrape the first entry off the LRU list, though I suppose we could add one or abuse the cb_arg in the walk callback as a return pointer.

Quoted 3 lines

That's just because it is an old-school cache with a fixed upper bound of entries and no shrinker support. With proper shrinker integration this nasty code isn't needed.

Well, the old code just had a fixed number of entries (1024 or so?), which was ridiculously small on modern server. A busy server could blow out the cache in less than the retransmit interval, which made it effectively worthless.

The new code has a much larger upper bound, but it is still bounded by the amount of low memory on the host. We have shrinker integration (though its propriety might be in question).

Are you suggesting that we shouldn't try to bound the cache at all and should instead just let it grow and rely on the shrinker to keep it in check?

Signature · 1 line
Jeff Layton <[email protected]>
#5 lore raw
Quoted 3 lines · Jeff Layton

Are you suggesting that we shouldn't try to bound the cache at all and should instead just let it grow and rely on the shrinker to keep it in check?

Exactly. Btw, what about the comment that the DRC should be per-client on top of the file? That also seems like some very low hanging fruit.

#6 lore raw
Quoted 8 lines · On Wed, 4 Dec 2013 05:09:44 -0800 Christoph Hellwig <[email protected]> wrote:
Quoted 3 lines · Jeff Layton

Are you suggesting that we shouldn't try to bound the cache at all and should instead just let it grow and rely on the shrinker to keep it in check?

Exactly. Btw, what about the comment that the DRC should be per-client on top of the file? That also seems like some very low hanging fruit.

Ok, I'll see about spinning up a patchset that does that. We'll also probably want to just allocate entries out of the slabcache in that case, so that'll make things quite a bit simpler.

As far as making this a per-client cache...sounds great! Go for it!

Signature · 1 line
Jeff Layton <[email protected]>
#7 lore raw
Quoted 7 lines · Jeff Layton
Quoted 3 lines

Exactly. Btw, what about the comment that the DRC should be per-client on top of the file? That also seems like some very low hanging fruit.

Ok, I'll see about spinning up a patchset that does that. We'll also probably want to just allocate entries out of the slabcache in that case, so that'll make things quite a bit simpler.

FYI I'm testing a patch at the moment that just rips out the "direct reclaim" as an alternative to your patch, without actually redoing the whole LRU infrastructure. Unfortunately with your patch 075 still hangs for me, although it at lest doesn't crash.

#8 lore raw
Quoted 5 lines · On Wed, 4 Dec 2013 05:40:36 -0800 Christoph Hellwig <[email protected]> wrote:
Quoted 4 lines · Jeff Layton
Quoted 3 lines

Exactly. Btw, what about the comment that the DRC should be per-client on top of the file? That also seems like some very low hanging fruit.

Quoted 9 lines
Quoted 3 lines

Ok, I'll see about spinning up a patchset that does that. We'll also probably want to just allocate entries out of the slabcache in that case, so that'll make things quite a bit simpler.

FYI I'm testing a patch at the moment that just rips out the "direct reclaim" as an alternative to your patch, without actually redoing the whole LRU infrastructure. Unfortunately with your patch 075 still hangs for me, although it at lest doesn't crash.

Yeah, I've noticed the same hang, but hadn't able to determine why it was hanging. I suspect that that hang is what's tickles the bug that my patch fixes. With the hang, we see the client doing retransmits and not getting replies and that means that we exercise the DRC more...

Signature · 1 line
Jeff Layton <[email protected]>
#9 lore raw
Quoted 4 lines

Yeah, I've noticed the same hang, but hadn't able to determine why it was hanging. I suspect that that hang is what's tickles the bug that my patch fixes. With the hang, we see the client doing retransmits and not getting replies and that means that we exercise the DRC more...

FYI here is the one that just kills the silly direct reclaim. It also fixes the oops, but I still see the hang:

fs/nfsd/nfscache.c +3 -30
index 9186c7c..dd260a1 100644
@@ -380,11 +380,8 @@ nfsd_cache_search(struct svc_rqst *rqstp, __wsum csum)
 }
 
 /*
- * Try to find an entry matching the current call in the cache. When none
- * is found, we try to grab the oldest expired entry off the LRU list. If
- * a suitable one isn't there, then drop the cache_lock and allocate a
- * new one, then search again in case one got inserted while this thread
- * didn't hold the lock.
+ * Try to find an entry matching the current call in the cache and if none is
+ * found allocate and insert a new one.
  */
 int
 nfsd_cache_lookup(struct svc_rqst *rqstp)
@@ -409,22 +406,8 @@ nfsd_cache_lookup(struct svc_rqst *rqstp)
 
 	/*
 	 * Since the common case is a cache miss followed by an insert,
-	 * preallocate an entry. First, try to reuse the first entry on the LRU
-	 * if it works, then go ahead and prune the LRU list.
+	 * preallocate an entry.
 	 */
-	spin_lock(&cache_lock);
-	if (!list_empty(&lru_head)) {
-		rp = list_first_entry(&lru_head, struct svc_cacherep, c_lru);
-		if (nfsd_cache_entry_expired(rp) ||
-		    num_drc_entries >= max_drc_entries) {
-			lru_put_end(rp);
-			prune_cache_entries();
-			goto search_cache;
-		}
-	}
-
-	/* No expired ones available, allocate a new one. */
-	spin_unlock(&cache_lock);
 	rp = nfsd_reply_cache_alloc();
 	spin_lock(&cache_lock);
 	if (likely(rp)) {
@@ -432,7 +415,6 @@ nfsd_cache_lookup(struct svc_rqst *rqstp)
 		drc_mem_usage += sizeof(*rp);
 	}
 
-search_cache:
 	found = nfsd_cache_search(rqstp, csum);
 	if (found) {
 		if (likely(rp))
@@ -446,15 +428,6 @@ search_cache:
 		goto out;
 	}
 
-	/*
-	 * We're keeping the one we just allocated. Are we now over the
-	 * limit? Prune one off the tip of the LRU in trade for the one we
-	 * just allocated if so.
-	 */
-	if (num_drc_entries >= max_drc_entries)
-		nfsd_reply_cache_free_locked(list_first_entry(&lru_head,
-						struct svc_cacherep, c_lru));
-
 	nfsdstats.rcmisses++;
 	rqstp->rq_cacherep = rp;
 	rp->c_state = RC_INPROG;
#10 lore raw
Quoted 52 lines · On Wed, 4 Dec 2013 05:54:57 -0800 Christoph Hellwig <[email protected]> wrote:
Quoted 4 lines

Yeah, I've noticed the same hang, but hadn't able to determine why it was hanging. I suspect that that hang is what's tickles the bug that my patch fixes. With the hang, we see the client doing retransmits and not getting replies and that means that we exercise the DRC more...

FYI here is the one that just kills the silly direct reclaim. It also fixes the oops, but I still see the hang:

fs/nfsd/nfscache.c +3 -20
index 9186c7c..dd260a1 100644
@@ -380,11 +380,8 @@ nfsd_cache_search(struct svc_rqst *rqstp, __wsum csum)
 }
 
 /*
- * Try to find an entry matching the current call in the cache. When none
- * is found, we try to grab the oldest expired entry off the LRU list. If
- * a suitable one isn't there, then drop the cache_lock and allocate a
- * new one, then search again in case one got inserted while this thread
- * didn't hold the lock.
+ * Try to find an entry matching the current call in the cache and if none is
+ * found allocate and insert a new one.
  */
 int
 nfsd_cache_lookup(struct svc_rqst *rqstp)
@@ -409,22 +406,8 @@ nfsd_cache_lookup(struct svc_rqst *rqstp)
 
 	/*
 	 * Since the common case is a cache miss followed by an insert,
-	 * preallocate an entry. First, try to reuse the first entry on the LRU
-	 * if it works, then go ahead and prune the LRU list.
+	 * preallocate an entry.
 	 */
-	spin_lock(&cache_lock);
-	if (!list_empty(&lru_head)) {
-		rp = list_first_entry(&lru_head, struct svc_cacherep, c_lru);
-		if (nfsd_cache_entry_expired(rp) ||
-		    num_drc_entries >= max_drc_entries) {
-			lru_put_end(rp);
-			prune_cache_entries();
-			goto search_cache;
-		}
-	}
-
-	/* No expired ones available, allocate a new one. */
-	spin_unlock(&cache_lock);
 	rp = nfsd_reply_cache_alloc();
 	spin_lock(&cache_lock);
 	if (likely(rp)) {

fs/nfsd/nfscache.c nfsd_cache_lookup(struct svc_rqst *rqstp) · line 417

Quoted 3 lines
fs/nfsd/nfscache.c +0 -0
@@ -432,7 +415,6 @@ nfsd_cache_lookup(struct svc_rqst *rqstp)
 		drc_mem_usage += sizeof(*rp);
 	}
 

It might be good to run prune_cache_entries(); at this point. Otherwise, this looks like it'll be fine...

Quoted 20 lines
fs/nfsd/nfscache.c +0 -10
-search_cache:
 	found = nfsd_cache_search(rqstp, csum);
 	if (found) {
 		if (likely(rp))
@@ -446,15 +428,6 @@ search_cache:
 		goto out;
 	}
 
-	/*
-	 * We're keeping the one we just allocated. Are we now over the
-	 * limit? Prune one off the tip of the LRU in trade for the one we
-	 * just allocated if so.
-	 */
-	if (num_drc_entries >= max_drc_entries)
-		nfsd_reply_cache_free_locked(list_first_entry(&lru_head,
-						struct svc_cacherep, c_lru));
-
 	nfsdstats.rcmisses++;
 	rqstp->rq_cacherep = rp;
 	rp->c_state = RC_INPROG;
Signature · 1 line
Jeff Layton <[email protected]>
#11 lore raw
In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig
Quoted 3 lines · Jeff Layton

Are you suggesting that we shouldn't try to bound the cache at all and should instead just let it grow and rely on the shrinker to keep it in check?

Exactly.

As long as the lookup times stay reasonable....

--b.

#12 lore raw
Quoted 10 lines · On Wed, 4 Dec 2013 09:25:09 -0500 "J. Bruce Fields" <[email protected]>…
Quoted 6 lines · Christoph Hellwig
Quoted 3 lines · Jeff Layton

Are you suggesting that we shouldn't try to bound the cache at all and should instead just let it grow and rely on the shrinker to keep it in check?

Exactly.

As long as the lookup times stay reasonable....

That is another tricky question. Currently, we size the number of hash buckets based on the max size of the cache. If we allow it to grow without bound, then how do we determine how many hash buckets we need?

Signature · 1 line
Jeff Layton <[email protected]>
#13 lore raw
Quoted 3 lines · Jeff Layton

That is another tricky question. Currently, we size the number of hash buckets based on the max size of the cache. If we allow it to grow without bound, then how do we determine how many hash buckets we need?

It won't grow without bound as long as it's trimmed below the size by both the periodic workqueue and the shrinker.

Of course replacing the hash with an autoscaling data structure would be useful, but given that 4.1 obsoletes the DRC I'm not sure how much effort we want to put into it.

#14 lore raw
Quoted 8 lines · On Wed, 4 Dec 2013 09:06:02 -0800 Christoph Hellwig <[email protected]> wrote:
Quoted 3 lines · Jeff Layton

That is another tricky question. Currently, we size the number of hash buckets based on the max size of the cache. If we allow it to grow without bound, then how do we determine how many hash buckets we need?

It won't grow without bound as long as it's trimmed below the size by both the periodic workqueue and the shrinker.

Right, but we don't know a priori how big the cache can grow, so that makes determining the number of buckets to allocate a little tricky.

Quoted 3 lines

Of course replacing the hash with an autoscaling data structure would be useful, but given that 4.1 obsoletes the DRC I'm not sure how much effort we want to put into it.

Agreed.

Signature · 1 line
Jeff Layton <[email protected]>
#15 lore raw
In reply to Jeff Layton
Quoted 11 lines · Jeff Layton

The DRC code will attempt to reuse an existing, expired cache entry in preference to allocating a new one. It'll then search the cache, and if it gets a hit it'll then free the cache entry that it was going to reuse.

The cache code doesn't unhash the entry that it's going to reuse however, so it's possible for it end up designating an entry for reuse and then subsequently freeing the same entry after it finds it. This leads it to a later use-after-free situation and usually some list corruption warnings or an oops.

Quoted 3 lines

Fix this by simply unhashing the entry that we intend to reuse. That will mean that it's not findable via a search and should prevent this situation from occurring.

And that also makes it simpler to verify that prune_cache_entries() isn't going to free rp, good.

Thanks, applying!

(But, note: I may not get things pushed out till next week as I don't have as convenient a testing setup while I'm travelling this week.)

--b.

Quoted 39 lines
---
 fs/nfsd/nfscache.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/nfsd/nfscache.c +8 -1
index 9186c7c..b6af150 100644
@@ -132,6 +132,13 @@ nfsd_reply_cache_alloc(void)
 }
 
 static void
+nfsd_reply_cache_unhash(struct svc_cacherep *rp)
+{
+	hlist_del_init(&rp->c_hash);
+	list_del_init(&rp->c_lru);
+}
+
+static void
 nfsd_reply_cache_free_locked(struct svc_cacherep *rp)
 {
 	if (rp->c_type == RC_REPLBUFF && rp->c_replvec.iov_base) {
@@ -417,7 +424,7 @@ nfsd_cache_lookup(struct svc_rqst *rqstp)
 		rp = list_first_entry(&lru_head, struct svc_cacherep, c_lru);
 		if (nfsd_cache_entry_expired(rp) ||
 		    num_drc_entries >= max_drc_entries) {
-			lru_put_end(rp);
+			nfsd_reply_cache_unhash(rp);
 			prune_cache_entries();
 			goto search_cache;
 		}
Signature · 1 line
1.8.4.2