Linux 6.9-rc1
[linux-2.6-microblaze.git] / fs / mbcache.c
index 47ccfcb..e60a840 100644 (file)
@@ -37,7 +37,7 @@ struct mb_cache {
        struct list_head        c_list;
        /* Number of entries in cache */
        unsigned long           c_entry_count;
-       struct shrinker         c_shrink;
+       struct shrinker         *c_shrink;
        /* Work for shrinking when the cache has too many entries */
        struct work_struct      c_shrink_work;
 };
@@ -90,12 +90,19 @@ int mb_cache_entry_create(struct mb_cache *cache, gfp_t mask, u32 key,
                return -ENOMEM;
 
        INIT_LIST_HEAD(&entry->e_list);
-       /* Initial hash reference */
-       atomic_set(&entry->e_refcnt, 1);
+       /*
+        * We create entry with two references. One reference is kept by the
+        * hash table, the other reference is used to protect us from
+        * mb_cache_entry_delete_or_get() until the entry is fully setup. This
+        * avoids nesting of cache->c_list_lock into hash table bit locks which
+        * is problematic for RT.
+        */
+       atomic_set(&entry->e_refcnt, 2);
        entry->e_key = key;
        entry->e_value = value;
-       entry->e_reusable = reusable;
-       entry->e_referenced = 0;
+       entry->e_flags = 0;
+       if (reusable)
+               set_bit(MBE_REUSABLE_B, &entry->e_flags);
        head = mb_cache_entry_head(cache, key);
        hlist_bl_lock(head);
        hlist_bl_for_each_entry(dup, dup_node, head, e_hash_list) {
@@ -106,15 +113,12 @@ int mb_cache_entry_create(struct mb_cache *cache, gfp_t mask, u32 key,
                }
        }
        hlist_bl_add_head(&entry->e_hash_list, head);
-       /*
-        * Add entry to LRU list before it can be found by
-        * mb_cache_entry_delete() to avoid races
-        */
+       hlist_bl_unlock(head);
        spin_lock(&cache->c_list_lock);
        list_add_tail(&entry->e_list, &cache->c_list);
        cache->c_entry_count++;
        spin_unlock(&cache->c_list_lock);
-       hlist_bl_unlock(head);
+       mb_cache_entry_put(cache, entry);
 
        return 0;
 }
@@ -162,7 +166,8 @@ static struct mb_cache_entry *__entry_find(struct mb_cache *cache,
        while (node) {
                entry = hlist_bl_entry(node, struct mb_cache_entry,
                                       e_hash_list);
-               if (entry->e_key == key && entry->e_reusable &&
+               if (entry->e_key == key &&
+                   test_bit(MBE_REUSABLE_B, &entry->e_flags) &&
                    atomic_inc_not_zero(&entry->e_refcnt))
                        goto out;
                node = node->next;
@@ -281,15 +286,14 @@ EXPORT_SYMBOL(mb_cache_entry_delete_or_get);
 void mb_cache_entry_touch(struct mb_cache *cache,
                          struct mb_cache_entry *entry)
 {
-       entry->e_referenced = 1;
+       set_bit(MBE_REFERENCED_B, &entry->e_flags);
 }
 EXPORT_SYMBOL(mb_cache_entry_touch);
 
 static unsigned long mb_cache_count(struct shrinker *shrink,
                                    struct shrink_control *sc)
 {
-       struct mb_cache *cache = container_of(shrink, struct mb_cache,
-                                             c_shrink);
+       struct mb_cache *cache = shrink->private_data;
 
        return cache->c_entry_count;
 }
@@ -306,9 +310,9 @@ static unsigned long mb_cache_shrink(struct mb_cache *cache,
                entry = list_first_entry(&cache->c_list,
                                         struct mb_cache_entry, e_list);
                /* Drop initial hash reference if there is no user */
-               if (entry->e_referenced ||
+               if (test_bit(MBE_REFERENCED_B, &entry->e_flags) ||
                    atomic_cmpxchg(&entry->e_refcnt, 1, 0) != 1) {
-                       entry->e_referenced = 0;
+                       clear_bit(MBE_REFERENCED_B, &entry->e_flags);
                        list_move_tail(&entry->e_list, &cache->c_list);
                        continue;
                }
@@ -328,8 +332,7 @@ static unsigned long mb_cache_shrink(struct mb_cache *cache,
 static unsigned long mb_cache_scan(struct shrinker *shrink,
                                   struct shrink_control *sc)
 {
-       struct mb_cache *cache = container_of(shrink, struct mb_cache,
-                                             c_shrink);
+       struct mb_cache *cache = shrink->private_data;
        return mb_cache_shrink(cache, sc->nr_to_scan);
 }
 
@@ -372,15 +375,19 @@ struct mb_cache *mb_cache_create(int bucket_bits)
        for (i = 0; i < bucket_count; i++)
                INIT_HLIST_BL_HEAD(&cache->c_hash[i]);
 
-       cache->c_shrink.count_objects = mb_cache_count;
-       cache->c_shrink.scan_objects = mb_cache_scan;
-       cache->c_shrink.seeks = DEFAULT_SEEKS;
-       if (register_shrinker(&cache->c_shrink, "mbcache-shrinker")) {
+       cache->c_shrink = shrinker_alloc(0, "mbcache-shrinker");
+       if (!cache->c_shrink) {
                kfree(cache->c_hash);
                kfree(cache);
                goto err_out;
        }
 
+       cache->c_shrink->count_objects = mb_cache_count;
+       cache->c_shrink->scan_objects = mb_cache_scan;
+       cache->c_shrink->private_data = cache;
+
+       shrinker_register(cache->c_shrink);
+
        INIT_WORK(&cache->c_shrink_work, mb_cache_shrink_worker);
 
        return cache;
@@ -401,7 +408,7 @@ void mb_cache_destroy(struct mb_cache *cache)
 {
        struct mb_cache_entry *entry, *next;
 
-       unregister_shrinker(&cache->c_shrink);
+       shrinker_free(cache->c_shrink);
 
        /*
         * We don't bother with any locking. Cache must not be used at this
@@ -419,9 +426,7 @@ EXPORT_SYMBOL(mb_cache_destroy);
 
 static int __init mbcache_init(void)
 {
-       mb_entry_cache = kmem_cache_create("mbcache",
-                               sizeof(struct mb_cache_entry), 0,
-                               SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD, NULL);
+       mb_entry_cache = KMEM_CACHE(mb_cache_entry, SLAB_RECLAIM_ACCOUNT);
        if (!mb_entry_cache)
                return -ENOMEM;
        return 0;