Skip to contentSkip to conversation

xfs_repair: improve caching in the face of ENOMEM

PATCH Superseded user.mail 3 participants 9 messages Aug 24 – Aug 25

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

user.mail

author
#0 lore raw

cover xfs_repair: improve caching in the face of ENOMEM

There are problems with how we react to cache allocation failures that are more apparent before:
6b6239689e36 ("libxfs: unmap xmbuf pages to avoid disaster") landed, but with enough pressure they show up again.

Two problems: cache_expand has no bounds checking, so call it enough and c_maxcount will overflow, which typically results in an infinite loop.

That can be triggered by the fact that today, an underlying ENOMEM can look like a "cache too small" return, so we double when that won't help at all. Do that enough times and kaboom.

I can trigger this with something like:

mkfs.xfs -d file,name=testfile.img,agcount=10000,size=1t echo 1000 > /proc/sys/vm/max_map_count xfs_repair -n -P -m 1280 testfile.img

to force an ENOMEM from the max map count, though the actual max_map_count value needed varies depending on the machine I'm on, for some reason.

These were quickly tested with ./check -g quick

author
#1 lore raw

PATCH 1/2 xfs_repair: do not allow cache growth to overflow

Nothing stops cache_expand() from growing c_maxcount (via *2) repeatedly until it overflows.

Add a bounds check here and return failure if for any reason we try to grow too much.

---
 libxfs/cache.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
libxfs/cache.c +16 -4
index d5d9ba56..f8be9b89 100644
@@ -79,16 +79,23 @@ cache_init(
 	return cache;
 }
 
-static void
+static int
 cache_expand(
 	struct cache *		cache)
 {
+	int			success = 1;
 	pthread_mutex_lock(&cache->c_mutex);
+	if (cache->c_maxcount <= UINT_MAX/2) {
 #ifdef CACHE_DEBUG
-	fprintf(stderr, "doubling cache size to %u\n", 2 * cache->c_maxcount);
+		fprintf(stderr, "doubling cache size to %u\n",
+			2 * cache->c_maxcount);
 #endif
-	cache->c_maxcount *= 2;
+		cache->c_maxcount *= 2;
+	} else
+		success = 0;
 	pthread_mutex_unlock(&cache->c_mutex);
+
+	return success;
 }
 
 void
@@ -468,7 +475,8 @@ next_object:
 		 */
 		if (priority > CACHE_MAX_PRIORITY) {
 			priority = 0;
-			cache_expand(cache);
+			if (!cache_expand(cache))
+				goto fail;
 		}
 	}
 
@@ -488,6 +496,10 @@ next_object:
 
 	*nodep = node;
 	return 1;
+
+fail:
+	*nodep = NULL;
+	return -1;
 }
 
 void
Signature · 1 line
2.55.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] xfs_repair: do not allow cache growth to overflow

Quoted 25 lines · user.mail

Nothing stops cache_expand() from growing c_maxcount (via *2) repeatedly until it overflows.

Add a bounds check here and return failure if for any reason we try to grow too much.

---
 libxfs/cache.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
libxfs/cache.c +1 -1
index d5d9ba56..f8be9b89 100644
@@ -79,16 +79,23 @@ cache_init(
 	return cache;
 }
 
-static void
+static int
 cache_expand(
 	struct cache *		cache)
 {

libxfs/cache.c cache_init( · line 93

Quoted 10 lines
libxfs/cache.c +5 -2
+	int			success = 1;
 	pthread_mutex_lock(&cache->c_mutex);
+	if (cache->c_maxcount <= UINT_MAX/2) {
 #ifdef CACHE_DEBUG
-	fprintf(stderr, "doubling cache size to %u\n", 2 * cache->c_maxcount);
+		fprintf(stderr, "doubling cache size to %u\n",
+			2 * cache->c_maxcount);
 #endif
-	cache->c_maxcount *= 2;
+		cache->c_maxcount *= 2;

I agree that it's useful not to overflow c_maxcount, but how did you end up with that many cached buffers? Or is this LLM-inspired bugfixes?

Either way I'm ok with this part, so...

Quoted 19 lines
libxfs/cache.c +6 -1
+	} else
+		success = 0;
 	pthread_mutex_unlock(&cache->c_mutex);
+
+	return success;
 }
 
 void
@@ -468,7 +475,8 @@ next_object:
 		 */
 		if (priority > CACHE_MAX_PRIORITY) {
 			priority = 0;
-			cache_expand(cache);
+			if (!cache_expand(cache))
+				goto fail;
 		}
 	}
 

libxfs/cache.c next_object: · line 502

Quoted 7 lines
libxfs/cache.c +4 -0
@@ -488,6 +496,10 @@ next_object:
 
 	*nodep = node;
 	return 1;
+
+fail:
+	*nodep = NULL;
+	return -1;

...but a general comment about this patch and the next one: it seems reasonable to make cache_node_get return a null nodep and -1 to indicate error, but AFAICT the only caller is __cache_lookup (aka the buffer cache), which doesn't check the return value at all, only the value of *nodep.

I don't see a change in this series to change __cache_lookup. Did that get lost somewhere?

--D

Quoted 7 lines
libxfs/cache.c +0 -0
 }
 
 void
Signature · 1 line
2.55.0
#3 lore raw

Re: [PATCH 1/2] xfs_repair: do not allow cache growth to overflow

Quoted 31 lines · Darrick J. Wong
Quoted 30 lines · user.mail

Nothing stops cache_expand() from growing c_maxcount (via *2) repeatedly until it overflows.

Add a bounds check here and return failure if for any reason we try to grow too much.

---
 libxfs/cache.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
libxfs/cache.c +3 -2
index d5d9ba56..f8be9b89 100644
@@ -79,16 +79,23 @@ cache_init(
 	return cache;
 }
 
-static void
+static int
 cache_expand(
 	struct cache *		cache)
 {
+	int			success = 1;
 	pthread_mutex_lock(&cache->c_mutex);
+	if (cache->c_maxcount <= UINT_MAX/2) {
 #ifdef CACHE_DEBUG
-	fprintf(stderr, "doubling cache size to %u\n", 2 * cache->c_maxcount);
Quoted 8 lines

libxfs/cache.c cache_init( · line 93

Quoted 5 lines
libxfs/cache.c +3 -1
+		fprintf(stderr, "doubling cache size to %u\n",
+			2 * cache->c_maxcount);
 #endif
-	cache->c_maxcount *= 2;
+		cache->c_maxcount *= 2;

I agree that it's useful not to overflow c_maxcount, but how did you end up with that many cached buffers? Or is this LLM-inspired bugfixes?

See the cover letter (sorta) ;) An ENOMEM from lower layers is indistinguishable from "cache too small" so when we ran out of memory it rapidly doubled its way to overflow.

In a quaint throwback to simpler times, this was actually observed by a user.

I think it was actually a low max_map_count in a small vm and a large filesystem, though I don't recall the exact details now.

Quoted 23 lines

Either way I'm ok with this part, so...

Quoted 20 lines
libxfs/cache.c +6 -1
+	} else
+		success = 0;
 	pthread_mutex_unlock(&cache->c_mutex);
+
+	return success;
 }
 
 void
@@ -468,7 +475,8 @@ next_object:
 		 */
 		if (priority > CACHE_MAX_PRIORITY) {
 			priority = 0;
-			cache_expand(cache);
+			if (!cache_expand(cache))
+				goto fail;
 		}
 	}
 
@@ -488,6 +496,10 @@ next_object:
 
Quoted 12 lines

libxfs/cache.c next_object: · line 502

Quoted 6 lines
libxfs/cache.c +4 -0
 	*nodep = node;
 	return 1;
+
+fail:
+	*nodep = NULL;
+	return -1;

...but a general comment about this patch and the next one: it seems reasonable to make cache_node_get return a null nodep and -1 to indicate error, but AFAICT the only caller is __cache_lookup (aka the buffer cache), which doesn't check the return value at all, only the value of *nodep.

Uh, I guess that was a leftover, I thought "I should return something different on error!" before I realized nobody was checking return val and then didn't go back.

Quoted 2 lines

I don't see a change in this series to change __cache_lookup. Did that get lost somewhere?

Not really, I just changed cache_expand before I realized __cache_lookup didn't care. We could make it a void function, I suppose, or start checking return val instead ...

-Eric

author
#4 lore raw

PATCH 2/2 xfs_repair: distinguish true ENOMEM from "not found" in cache_node_get

In reply to user.mail

In the cache_node_get() callchain, we may fail in cache_node_allocate for two distinct reasons:
- The cache may be at its current size limit, and must be expanded
- An underlying allocation may have actually failed with ENOMEM

Today, we do not distinguish these 2 failure modes and will try to expand the cache size even in the face of an ENOMEM, which won't help. We can distinguish these two failures, because:

If cache_node_allocate fails because all slots are full, c_count is untouched, and it is >= c_maxcount (all slots full.)

If cache_node_allocate fails due to an underlying ENOMEM, c_count is decremented before return and c_count will remain < c_maxcount.

So, if the cache has been fully shaken, cache_node_allocate stil fails, and c_count < c_maxcount, this is a true ENOMEM situation, and we should give up and tell the caller.

This adds yet another return value to cache_node_get (-1) for ENOMEM failures, and corrects the comment about the existing 0-vs-1 returns
- though nobody looks at that return value today.

---
 libxfs/cache.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
libxfs/cache.c +14 -2
index f8be9b89..63224e58 100644
@@ -373,8 +373,8 @@ __cache_node_purge(
  * hit, in which case this will all be over quickly and painlessly.
  * Otherwise, we allocate a new node, taking care not to expand the
  * cache beyond the requested maximum size (shrink it if it would).
- * Returns one if hit in cache, otherwise zero.  A node is _always_
- * returned, however.
+ * Returns zero if hit in cache, otherwise return 1 for new node;
+ * allocation failures return -1 and a null nodep.
  */
 int
 cache_node_get(
@@ -474,6 +474,18 @@ next_object:
 		 * If we exceed CACHE_MAX_PRIORITY all slots are full; grow it.
 		 */
 		if (priority > CACHE_MAX_PRIORITY) {
+			/*
+			 * Shaking failed to make room. If the cache is not
+			 * full, expanding the limit won't help - this is a
+			 * genuine ENOMEM. Tell the caller.
+			 */
+			pthread_mutex_lock(&cache->c_mutex);
+			if (cache->c_count < cache->c_maxcount) {
+				pthread_mutex_unlock(&cache->c_mutex);
+				*nodep = NULL;
+				return -1;
+			}
+			pthread_mutex_unlock(&cache->c_mutex);
 			priority = 0;
 			if (!cache_expand(cache))
 				goto fail;
Signature · 1 line
2.55.0
#5 lore raw

Re: [PATCH 2/2] xfs_repair: distinguish true ENOMEM from "not found" in cache_node_get

Quoted 19 lines · user.mail

In the cache_node_get() callchain, we may fail in cache_node_allocate for two distinct reasons:
- The cache may be at its current size limit, and must be expanded
- An underlying allocation may have actually failed with ENOMEM

Today, we do not distinguish these 2 failure modes and will try to expand the cache size even in the face of an ENOMEM, which won't help. We can distinguish these two failures, because:

If cache_node_allocate fails because all slots are full, c_count is untouched, and it is >= c_maxcount (all slots full.)

If cache_node_allocate fails due to an underlying ENOMEM, c_count is decremented before return and c_count will remain < c_maxcount.

So, if the cache has been fully shaken, cache_node_allocate stil fails, and c_count < c_maxcount, this is a true ENOMEM situation, and we should give up and tell the caller.

Quoted 6 lines

This adds yet another return value to cache_node_get (-1) for ENOMEM failures, and corrects the comment about the existing 0-vs-1 returns
- though nobody looks at that return value today.

Dunno who "user.mail" is but I feel like I know them from somewhere ;)

Quoted 25 lines
---
 libxfs/cache.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
libxfs/cache.c +4 -2
index f8be9b89..63224e58 100644
@@ -373,8 +373,8 @@ __cache_node_purge(
  * hit, in which case this will all be over quickly and painlessly.
  * Otherwise, we allocate a new node, taking care not to expand the
  * cache beyond the requested maximum size (shrink it if it would).
- * Returns one if hit in cache, otherwise zero.  A node is _always_
- * returned, however.
+ * Returns zero if hit in cache, otherwise return 1 for new node;
+ * allocation failures return -1 and a null nodep.
  */
 int
 cache_node_get(
@@ -474,6 +474,18 @@ next_object:
 		 * If we exceed CACHE_MAX_PRIORITY all slots are full; grow it.
 		 */
 		if (priority > CACHE_MAX_PRIORITY) {
+			/*
+			 * Shaking failed to make room. If the cache is not

libxfs/cache.c next_object: · line 488

Quoted 10 lines
libxfs/cache.c +10 -0
+			 * full, expanding the limit won't help - this is a
+			 * genuine ENOMEM. Tell the caller.
+			 */
+			pthread_mutex_lock(&cache->c_mutex);
+			if (cache->c_count < cache->c_maxcount) {
+				pthread_mutex_unlock(&cache->c_mutex);
+				*nodep = NULL;
+				return -1;
+			}
+			pthread_mutex_unlock(&cache->c_mutex);

Also seems fine to me, but nobody checks for these -1 returns. Also, maybe we should be "return -ENOMEM" here or something?

--D

Quoted 7 lines
libxfs/cache.c +0 -0
 			priority = 0;
 			if (!cache_expand(cache))
 				goto fail;
Signature · 1 line
2.55.0
#6 lore raw

Re: [PATCH 2/2] xfs_repair: distinguish true ENOMEM from "not found" in cache_node_get

Quoted 20 lines · Darrick J. Wong
Quoted 19 lines · user.mail

In the cache_node_get() callchain, we may fail in cache_node_allocate for two distinct reasons:
- The cache may be at its current size limit, and must be expanded
- An underlying allocation may have actually failed with ENOMEM

Today, we do not distinguish these 2 failure modes and will try to expand the cache size even in the face of an ENOMEM, which won't help. We can distinguish these two failures, because:

If cache_node_allocate fails because all slots are full, c_count is untouched, and it is >= c_maxcount (all slots full.)

If cache_node_allocate fails due to an underlying ENOMEM, c_count is decremented before return and c_count will remain < c_maxcount.

So, if the cache has been fully shaken, cache_node_allocate stil fails, and c_count < c_maxcount, this is a true ENOMEM situation, and we should give up and tell the caller.

Quoted 8 lines
Quoted 6 lines

This adds yet another return value to cache_node_get (-1) for ENOMEM failures, and corrects the comment about the existing 0-vs-1 returns
- though nobody looks at that return value today.

Dunno who "user.mail" is but I feel like I know them from somewhere ;)

I'll check my git config in that tree, oops :( Been a minute since I sent an xfsprogs patch I guess!

Quoted 29 lines
Quoted 29 lines
---
 libxfs/cache.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
libxfs/cache.c +8 -2
index f8be9b89..63224e58 100644
@@ -373,8 +373,8 @@ __cache_node_purge(
  * hit, in which case this will all be over quickly and painlessly.
  * Otherwise, we allocate a new node, taking care not to expand the
  * cache beyond the requested maximum size (shrink it if it would).
- * Returns one if hit in cache, otherwise zero.  A node is _always_
- * returned, however.
+ * Returns zero if hit in cache, otherwise return 1 for new node;
+ * allocation failures return -1 and a null nodep.
  */
 int
 cache_node_get(
@@ -474,6 +474,18 @@ next_object:
 		 * If we exceed CACHE_MAX_PRIORITY all slots are full; grow it.
 		 */
 		if (priority > CACHE_MAX_PRIORITY) {
+			/*
+			 * Shaking failed to make room. If the cache is not
+			 * full, expanding the limit won't help - this is a
+			 * genuine ENOMEM. Tell the caller.
+			 */
+			pthread_mutex_lock(&cache->c_mutex);
Quoted 8 lines

libxfs/cache.c next_object: · line 488

Quoted 6 lines
libxfs/cache.c +6 -0
+			if (cache->c_count < cache->c_maxcount) {
+				pthread_mutex_unlock(&cache->c_mutex);
+				*nodep = NULL;
+				return -1;
+			}
+			pthread_mutex_unlock(&cache->c_mutex);

Also seems fine to me, but nobody checks for these -1 returns.

Yeah, lazily mentioned in the commit log.

Quoted 2 lines

Also,
maybe we should be "return -ENOMEM" here or something?

Then have callers check, etc. Do you want me to rework things to rationalize the return values (and caller checks?)

-Eric

Quoted 10 lines

--D

Quoted 7 lines
libxfs/cache.c +0 -0
 			priority = 0;
 			if (!cache_expand(cache))
 				goto fail;
Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH 2/2] xfs_repair: distinguish true ENOMEM from "not found" in cache_node_get

Quoted 71 lines · Eric Sandeen
Quoted 20 lines · Darrick J. Wong
Quoted 19 lines · user.mail

In the cache_node_get() callchain, we may fail in cache_node_allocate for two distinct reasons:
- The cache may be at its current size limit, and must be expanded
- An underlying allocation may have actually failed with ENOMEM

Today, we do not distinguish these 2 failure modes and will try to expand the cache size even in the face of an ENOMEM, which won't help. We can distinguish these two failures, because:

If cache_node_allocate fails because all slots are full, c_count is untouched, and it is >= c_maxcount (all slots full.)

If cache_node_allocate fails due to an underlying ENOMEM, c_count is decremented before return and c_count will remain < c_maxcount.

So, if the cache has been fully shaken, cache_node_allocate stil fails, and c_count < c_maxcount, this is a true ENOMEM situation, and we should give up and tell the caller.

Quoted 8 lines
Quoted 6 lines

This adds yet another return value to cache_node_get (-1) for ENOMEM failures, and corrects the comment about the existing 0-vs-1 returns
- though nobody looks at that return value today.

Dunno who "user.mail" is but I feel like I know them from somewhere ;)

I'll check my git config in that tree, oops :( Been a minute since I sent an xfsprogs patch I guess!

Quoted 29 lines
Quoted 29 lines
---
 libxfs/cache.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
libxfs/cache.c +8 -2
index f8be9b89..63224e58 100644
@@ -373,8 +373,8 @@ __cache_node_purge(
  * hit, in which case this will all be over quickly and painlessly.
  * Otherwise, we allocate a new node, taking care not to expand the
  * cache beyond the requested maximum size (shrink it if it would).
- * Returns one if hit in cache, otherwise zero.  A node is _always_
- * returned, however.
+ * Returns zero if hit in cache, otherwise return 1 for new node;
+ * allocation failures return -1 and a null nodep.
  */
 int
 cache_node_get(
@@ -474,6 +474,18 @@ next_object:
 		 * If we exceed CACHE_MAX_PRIORITY all slots are full; grow it.
 		 */
 		if (priority > CACHE_MAX_PRIORITY) {
+			/*
+			 * Shaking failed to make room. If the cache is not
+			 * full, expanding the limit won't help - this is a
+			 * genuine ENOMEM. Tell the caller.
+			 */
+			pthread_mutex_lock(&cache->c_mutex);
Quoted 8 lines

libxfs/cache.c next_object: · line 488

Quoted 6 lines
libxfs/cache.c +6 -0
+			if (cache->c_count < cache->c_maxcount) {
+				pthread_mutex_unlock(&cache->c_mutex);
+				*nodep = NULL;
+				return -1;
+			}
+			pthread_mutex_unlock(&cache->c_mutex);

Also seems fine to me, but nobody checks for these -1 returns.

Quoted 7 lines

Yeah, lazily mentioned in the commit log.

Quoted 2 lines

Also,
maybe we should be "return -ENOMEM" here or something?

Then have callers check, etc. Do you want me to rework things to rationalize the return values (and caller checks?)

Yeah, I think __cache_lookup should do something with the new return codes, e.g.

	int ret = cache_node_get(bcache, key, &cn);
	if (ret < 0) {
		fprintf(stderr,
			"%s occurred, go buy a real computer!\n",
			strerror(-ret));
		abort();
	}
	if (!cn)
		return -ENOMEM;
	bp = container_of(cn, struct xfs_buf, b_node);

This can be a third patch, the first two patches look good to me aside from having weird SoB lines.

--D

Quoted 14 lines

-Eric

Quoted 10 lines

--D

Quoted 7 lines
libxfs/cache.c +0 -0
 			priority = 0;
 			if (!cache_expand(cache))
 				goto fail;
Signature · 1 line
2.55.0
#8 lore raw

Re: [PATCH 2/2] xfs_repair: distinguish true ENOMEM from "not found" in cache_node_get

Quoted 1 line · Darrick J. Wong

On Tue, Aug 25, 2026 at 10:39:08AM -0500, Eric Sandeen wrote:

...

Quoted 20 lines
Quoted 5 lines
Quoted 2 lines

Also,
maybe we should be "return -ENOMEM" here or something?

Then have callers check, etc. Do you want me to rework things to rationalize the return values (and caller checks?)

Yeah, I think __cache_lookup should do something with the new return codes, e.g.

	int ret = cache_node_get(bcache, key, &cn);
	if (ret < 0) {
		fprintf(stderr,
			"%s occurred, go buy a real computer!\n",
			strerror(-ret));
		abort();
	}
	if (!cn)
		return -ENOMEM;
	bp = container_of(cn, struct xfs_buf, b_node);
Quoted 2 lines

This can be a third patch, the first two patches look good to me aside from having weird SoB lines.

Fair enough, I was staying in the weird "we have distinct error codes that are never checked" world and should have fixed that up at the same time, for tidiness.

-Eric