[PATCH 15/23] cache: add a "get only if incore" flag to cache_node_get

Darrick J. Wong
2025-11-06T22:47:04Z

Add a new flag to cache_node_get so that callers can specify that they
only want the cache to return an existing cache node, and not create a
new one.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
---
lib/support/cache.h | 5 ++++-
lib/support/cache.c | 7 +++++++
2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/lib/support/cache.h b/lib/support/cache.h
index 8d39ca5c02a285..98b2182d49a6e0 100644
--- a/lib/support/cache.h
+++ b/lib/support/cache.h
@@ -134,7 +134,10 @@ void cache_walk(struct cache *cache, cache_walk_t fn, void *data);
void cache_purge(struct cache *);
bool cache_flush(struct cache *cache);

-int cache_node_get(struct cache *, cache_key_t, struct cache_node **);
+/* don't allocate a new node */
+#define CACHE_GET_INCORE (1U << 0)
+int cache_node_get(struct cache *c, cache_key_t key, unsigned int cgflags,
+ struct cache_node **nodep);
void cache_node_put(struct cache *, struct cache_node *);
void cache_node_set_priority(struct cache *, struct cache_node *, int);
int cache_node_get_priority(struct cache_node *);
diff --git a/lib/support/cache.c b/lib/support/cache.c
index fa07b4ad8222d2..9da6c59b3b6391 100644
--- a/lib/support/cache.c
+++ b/lib/support/cache.c
@@ -403,6 +403,7 @@ int
cache_node_get(
struct cache *cache,
cache_key_t key,
+ unsigned int cgflags,
struct cache_node **nodep)
{
struct cache_hash *hash;
@@ -456,6 +457,12 @@ cache_node_get(
continue; /* what the hell, gcc? */
}
pthread_mutex_unlock(&hash->ch_mutex);
+
+ if (cgflags & CACHE_GET_INCORE) {
+ *nodep = NULL;
+ return 0;
+ }
+
/*
* not found, allocate a new entry
*/