• Home
  • Raw
  • Download

Lines Matching +full:key +full:- +full:value

1 // SPDX-License-Identifier: GPL-2.0-only
12 * Mbcache is a simple key-value store. Keys need not be unique, however
13 * key-value pairs are expected to be unique (we use this fact in
18 * They use hash of data as a key and provide a value that may represent a
20 * data may be the same). However user provided value always uniquely
23 * We provide functions for creation and removal of entries, search by key,
24 * and a special "delete entry with given key-value pair" operation. Fixed
25 * size hash table is used for fast key lookups.
51 u32 key) in mb_cache_entry_head() argument
53 return &cache->c_hash[hash_32(key, cache->c_bucket_bits)]; in mb_cache_entry_head()
63 * mb_cache_entry_create - create entry in cache
64 * @cache - cache where the entry should be created
65 * @mask - gfp mask with which the entry should be allocated
66 * @key - key of the entry
67 * @value - value of the entry
68 * @reusable - is the entry reusable by others?
70 * Creates entry in @cache with key @key and value @value. The function returns
71 * -EBUSY if entry with the same key and value already exists in cache.
74 int mb_cache_entry_create(struct mb_cache *cache, gfp_t mask, u32 key, in mb_cache_entry_create() argument
75 u64 value, bool reusable) in mb_cache_entry_create() argument
82 if (cache->c_entry_count >= cache->c_max_entries) in mb_cache_entry_create()
83 schedule_work(&cache->c_shrink_work); in mb_cache_entry_create()
85 if (cache->c_entry_count >= 2*cache->c_max_entries) in mb_cache_entry_create()
90 return -ENOMEM; in mb_cache_entry_create()
92 INIT_LIST_HEAD(&entry->e_list); in mb_cache_entry_create()
97 * avoids nesting of cache->c_list_lock into hash table bit locks which in mb_cache_entry_create()
100 atomic_set(&entry->e_refcnt, 2); in mb_cache_entry_create()
101 entry->e_key = key; in mb_cache_entry_create()
102 entry->e_value = value; in mb_cache_entry_create()
103 entry->e_flags = 0; in mb_cache_entry_create()
105 set_bit(MBE_REUSABLE_B, &entry->e_flags); in mb_cache_entry_create()
106 head = mb_cache_entry_head(cache, key); in mb_cache_entry_create()
109 if (dup->e_key == key && dup->e_value == value) { in mb_cache_entry_create()
112 return -EBUSY; in mb_cache_entry_create()
115 hlist_bl_add_head(&entry->e_hash_list, head); in mb_cache_entry_create()
117 spin_lock(&cache->c_list_lock); in mb_cache_entry_create()
118 list_add_tail(&entry->e_list, &cache->c_list); in mb_cache_entry_create()
119 cache->c_entry_count++; in mb_cache_entry_create()
120 spin_unlock(&cache->c_list_lock); in mb_cache_entry_create()
131 head = mb_cache_entry_head(cache, entry->e_key); in __mb_cache_entry_free()
133 hlist_bl_del(&entry->e_hash_list); in __mb_cache_entry_free()
140 * mb_cache_entry_wait_unused - wait to be the last user of the entry
142 * @entry - entry to work on
148 wait_var_event(&entry->e_refcnt, atomic_read(&entry->e_refcnt) <= 2); in mb_cache_entry_wait_unused()
154 u32 key) in __entry_find() argument
160 head = mb_cache_entry_head(cache, key); in __entry_find()
162 if (entry && !hlist_bl_unhashed(&entry->e_hash_list)) in __entry_find()
163 node = entry->e_hash_list.next; in __entry_find()
169 if (entry->e_key == key && in __entry_find()
170 test_bit(MBE_REUSABLE_B, &entry->e_flags) && in __entry_find()
171 atomic_inc_not_zero(&entry->e_refcnt)) in __entry_find()
173 node = node->next; in __entry_find()
185 * mb_cache_entry_find_first - find the first reusable entry with the given key
187 * @key: key to look for
189 * Search in @cache for a reusable entry with key @key. Grabs reference to the
193 u32 key) in mb_cache_entry_find_first() argument
195 return __entry_find(cache, NULL, key); in mb_cache_entry_find_first()
200 * mb_cache_entry_find_next - find next reusable entry with the same key
204 * Finds next reusable entry in the hash chain which has the same key as @entry.
212 return __entry_find(cache, entry, entry->e_key); in mb_cache_entry_find_next()
217 * mb_cache_entry_get - get a cache entry by value (and key)
218 * @cache - cache we work with
219 * @key - key
220 * @value - value
222 struct mb_cache_entry *mb_cache_entry_get(struct mb_cache *cache, u32 key, in mb_cache_entry_get() argument
223 u64 value) in mb_cache_entry_get() argument
229 head = mb_cache_entry_head(cache, key); in mb_cache_entry_get()
232 if (entry->e_key == key && entry->e_value == value && in mb_cache_entry_get()
233 atomic_inc_not_zero(&entry->e_refcnt)) in mb_cache_entry_get()
243 /* mb_cache_entry_delete - try to remove a cache entry
244 * @cache - cache we work with
245 * @key - key
246 * @value - value
248 * Remove entry from cache @cache with key @key and value @value.
250 void mb_cache_entry_delete(struct mb_cache *cache, u32 key, u64 value) in mb_cache_entry_delete() argument
256 head = mb_cache_entry_head(cache, key); in mb_cache_entry_delete()
259 if (entry->e_key == key && entry->e_value == value) { in mb_cache_entry_delete()
261 hlist_bl_del_init(&entry->e_hash_list); in mb_cache_entry_delete()
263 spin_lock(&cache->c_list_lock); in mb_cache_entry_delete()
264 if (!list_empty(&entry->e_list)) { in mb_cache_entry_delete()
265 list_del_init(&entry->e_list); in mb_cache_entry_delete()
266 if (!WARN_ONCE(cache->c_entry_count == 0, in mb_cache_entry_delete()
268 cache->c_entry_count--; in mb_cache_entry_delete()
269 atomic_dec(&entry->e_refcnt); in mb_cache_entry_delete()
271 spin_unlock(&cache->c_list_lock); in mb_cache_entry_delete()
280 /* mb_cache_entry_delete_or_get - remove a cache entry if it has no users
281 * @cache - cache we work with
282 * @key - key
283 * @value - value
285 * Remove entry from cache @cache with key @key and value @value. The removal
292 u32 key, u64 value) in mb_cache_entry_delete_or_get() argument
296 entry = mb_cache_entry_get(cache, key, value); in mb_cache_entry_delete_or_get()
304 if (atomic_cmpxchg(&entry->e_refcnt, 2, 0) != 2) in mb_cache_entry_delete_or_get()
307 spin_lock(&cache->c_list_lock); in mb_cache_entry_delete_or_get()
308 if (!list_empty(&entry->e_list)) in mb_cache_entry_delete_or_get()
309 list_del_init(&entry->e_list); in mb_cache_entry_delete_or_get()
310 cache->c_entry_count--; in mb_cache_entry_delete_or_get()
311 spin_unlock(&cache->c_list_lock); in mb_cache_entry_delete_or_get()
317 /* mb_cache_entry_touch - cache entry got used
318 * @cache - cache the entry belongs to
319 * @entry - entry that got used
326 set_bit(MBE_REFERENCED_B, &entry->e_flags); in mb_cache_entry_touch()
336 return cache->c_entry_count; in mb_cache_count()
346 spin_lock(&cache->c_list_lock); in mb_cache_shrink()
347 while (nr_to_scan-- && !list_empty(&cache->c_list)) { in mb_cache_shrink()
348 entry = list_first_entry(&cache->c_list, in mb_cache_shrink()
351 if (test_bit(MBE_REFERENCED_B, &entry->e_flags) || in mb_cache_shrink()
352 atomic_cmpxchg(&entry->e_refcnt, 1, 0) != 1) { in mb_cache_shrink()
353 clear_bit(MBE_REFERENCED_B, &entry->e_flags); in mb_cache_shrink()
354 list_move_tail(&entry->e_list, &cache->c_list); in mb_cache_shrink()
357 list_del_init(&entry->e_list); in mb_cache_shrink()
358 cache->c_entry_count--; in mb_cache_shrink()
359 spin_unlock(&cache->c_list_lock); in mb_cache_shrink()
363 spin_lock(&cache->c_list_lock); in mb_cache_shrink()
365 spin_unlock(&cache->c_list_lock); in mb_cache_shrink()
375 return mb_cache_shrink(cache, sc->nr_to_scan); in mb_cache_scan()
385 mb_cache_shrink(cache, cache->c_max_entries / SHRINK_DIVISOR); in mb_cache_shrink_worker()
389 * mb_cache_create - create cache
403 cache->c_bucket_bits = bucket_bits; in mb_cache_create()
404 cache->c_max_entries = bucket_count << 4; in mb_cache_create()
405 INIT_LIST_HEAD(&cache->c_list); in mb_cache_create()
406 spin_lock_init(&cache->c_list_lock); in mb_cache_create()
407 cache->c_hash = kmalloc_array(bucket_count, in mb_cache_create()
410 if (!cache->c_hash) { in mb_cache_create()
415 INIT_HLIST_BL_HEAD(&cache->c_hash[i]); in mb_cache_create()
417 cache->c_shrink.count_objects = mb_cache_count; in mb_cache_create()
418 cache->c_shrink.scan_objects = mb_cache_scan; in mb_cache_create()
419 cache->c_shrink.seeks = DEFAULT_SEEKS; in mb_cache_create()
420 if (register_shrinker(&cache->c_shrink)) { in mb_cache_create()
421 kfree(cache->c_hash); in mb_cache_create()
426 INIT_WORK(&cache->c_shrink_work, mb_cache_shrink_worker); in mb_cache_create()
436 * mb_cache_destroy - destroy cache
446 unregister_shrinker(&cache->c_shrink); in mb_cache_destroy()
452 list_for_each_entry_safe(entry, next, &cache->c_list, e_list) { in mb_cache_destroy()
453 list_del(&entry->e_list); in mb_cache_destroy()
454 WARN_ON(atomic_read(&entry->e_refcnt) != 1); in mb_cache_destroy()
457 kfree(cache->c_hash); in mb_cache_destroy()
468 return -ENOMEM; in mbcache_init()