1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2019, 2020 Amazon.com, Inc. or its affiliates. All rights reserved.
6 * User extended attribute client side cache functions.
8 * Author: Frank van der Linden <fllinden@amazon.com>
10 #include <linux/errno.h>
11 #include <linux/nfs_fs.h>
12 #include <linux/hashtable.h>
13 #include <linux/refcount.h>
14 #include <uapi/linux/xattr.h>
20 * User extended attributes client side caching is implemented by having
21 * a cache structure attached to NFS inodes. This structure is allocated
22 * when needed, and freed when the cache is zapped.
24 * The cache structure contains as hash table of entries, and a pointer
25 * to a special-cased entry for the listxattr cache.
27 * Accessing and allocating / freeing the caches is done via reference
28 * counting. The cache entries use a similar refcounting scheme.
30 * This makes freeing a cache, both from the shrinker and from the
31 * zap cache path, easy. It also means that, in current use cases,
32 * the large majority of inodes will not waste any memory, as they
33 * will never have any user extended attributes assigned to them.
35 * Attribute entries are hashed in to a simple hash table. They are
36 * also part of an LRU.
38 * There are three shrinkers.
40 * Two shrinkers deal with the cache entries themselves: one for
41 * large entries (> PAGE_SIZE), and one for smaller entries. The
42 * shrinker for the larger entries works more aggressively than
43 * those for the smaller entries.
45 * The other shrinker frees the cache structures themselves.
49 * 64 buckets is a good default. There is likely no reasonable
50 * workload that uses more than even 64 user extended attributes.
51 * You can certainly add a lot more - but you get what you ask for
52 * in those circumstances.
54 #define NFS4_XATTR_HASH_SIZE 64
56 #define NFSDBG_FACILITY NFSDBG_XATTRCACHE
58 struct nfs4_xattr_cache
;
59 struct nfs4_xattr_entry
;
61 struct nfs4_xattr_bucket
{
63 struct hlist_head hlist
;
64 struct nfs4_xattr_cache
*cache
;
68 struct nfs4_xattr_cache
{
70 struct nfs4_xattr_bucket buckets
[NFS4_XATTR_HASH_SIZE
];
72 struct list_head dispose
;
74 spinlock_t listxattr_lock
;
76 struct nfs4_xattr_entry
*listxattr
;
79 struct nfs4_xattr_entry
{
81 struct hlist_node hnode
;
83 struct list_head dispose
;
87 struct nfs4_xattr_bucket
*bucket
;
91 #define NFS4_XATTR_ENTRY_EXTVAL 0x0001
94 * LRU list of NFS inodes that have xattr caches.
96 static struct list_lru nfs4_xattr_cache_lru
;
97 static struct list_lru nfs4_xattr_entry_lru
;
98 static struct list_lru nfs4_xattr_large_entry_lru
;
100 static struct kmem_cache
*nfs4_xattr_cache_cachep
;
103 * Hashing helper functions.
106 nfs4_xattr_hash_init(struct nfs4_xattr_cache
*cache
)
110 for (i
= 0; i
< NFS4_XATTR_HASH_SIZE
; i
++) {
111 INIT_HLIST_HEAD(&cache
->buckets
[i
].hlist
);
112 spin_lock_init(&cache
->buckets
[i
].lock
);
113 cache
->buckets
[i
].cache
= cache
;
114 cache
->buckets
[i
].draining
= false;
120 * 1. inode i_lock or bucket lock
121 * 2. list_lru lock (taken by list_lru_* functions)
125 * Wrapper functions to add a cache entry to the right LRU.
128 nfs4_xattr_entry_lru_add(struct nfs4_xattr_entry
*entry
)
130 struct list_lru
*lru
;
132 lru
= (entry
->flags
& NFS4_XATTR_ENTRY_EXTVAL
) ?
133 &nfs4_xattr_large_entry_lru
: &nfs4_xattr_entry_lru
;
135 return list_lru_add(lru
, &entry
->lru
);
139 nfs4_xattr_entry_lru_del(struct nfs4_xattr_entry
*entry
)
141 struct list_lru
*lru
;
143 lru
= (entry
->flags
& NFS4_XATTR_ENTRY_EXTVAL
) ?
144 &nfs4_xattr_large_entry_lru
: &nfs4_xattr_entry_lru
;
146 return list_lru_del(lru
, &entry
->lru
);
150 * This function allocates cache entries. They are the normal
151 * extended attribute name/value pairs, but may also be a listxattr
152 * cache. Those allocations use the same entry so that they can be
153 * treated as one by the memory shrinker.
155 * xattr cache entries are allocated together with names. If the
156 * value fits in to one page with the entry structure and the name,
157 * it will also be part of the same allocation (kmalloc). This is
158 * expected to be the vast majority of cases. Larger allocations
159 * have a value pointer that is allocated separately by kvmalloc.
163 * @name: Name of the extended attribute. NULL for listxattr cache
165 * @value: Value of attribute, or listxattr cache. NULL if the
166 * value is to be copied from pages instead.
167 * @pages: Pages to copy the value from, if not NULL. Passed in to
168 * make it easier to copy the value after an RPC, even if
169 * the value will not be passed up to application (e.g.
170 * for a 'query' getxattr with NULL buffer).
171 * @len: Length of the value. Can be 0 for zero-length attribues.
172 * @value and @pages will be NULL if @len is 0.
174 static struct nfs4_xattr_entry
*
175 nfs4_xattr_alloc_entry(const char *name
, const void *value
,
176 struct page
**pages
, size_t len
)
178 struct nfs4_xattr_entry
*entry
;
181 size_t alloclen
, slen
;
185 BUILD_BUG_ON(sizeof(struct nfs4_xattr_entry
) +
186 XATTR_NAME_MAX
+ 1 > PAGE_SIZE
);
188 alloclen
= sizeof(struct nfs4_xattr_entry
);
190 slen
= strlen(name
) + 1;
195 if (alloclen
+ len
<= PAGE_SIZE
) {
199 flags
= NFS4_XATTR_ENTRY_EXTVAL
;
202 buf
= kmalloc(alloclen
, GFP_KERNEL_ACCOUNT
| GFP_NOFS
);
205 entry
= (struct nfs4_xattr_entry
*)buf
;
208 namep
= buf
+ sizeof(struct nfs4_xattr_entry
);
209 memcpy(namep
, name
, slen
);
215 if (flags
& NFS4_XATTR_ENTRY_EXTVAL
) {
216 valp
= kvmalloc(len
, GFP_KERNEL_ACCOUNT
| GFP_NOFS
);
221 } else if (len
!= 0) {
222 valp
= buf
+ sizeof(struct nfs4_xattr_entry
) + slen
;
228 memcpy(valp
, value
, len
);
230 _copy_from_pages(valp
, pages
, 0, len
);
233 entry
->flags
= flags
;
234 entry
->xattr_value
= valp
;
235 kref_init(&entry
->ref
);
236 entry
->xattr_name
= namep
;
237 entry
->xattr_size
= len
;
238 entry
->bucket
= NULL
;
239 INIT_LIST_HEAD(&entry
->lru
);
240 INIT_LIST_HEAD(&entry
->dispose
);
241 INIT_HLIST_NODE(&entry
->hnode
);
247 nfs4_xattr_free_entry(struct nfs4_xattr_entry
*entry
)
249 if (entry
->flags
& NFS4_XATTR_ENTRY_EXTVAL
)
250 kvfree(entry
->xattr_value
);
255 nfs4_xattr_free_entry_cb(struct kref
*kref
)
257 struct nfs4_xattr_entry
*entry
;
259 entry
= container_of(kref
, struct nfs4_xattr_entry
, ref
);
261 if (WARN_ON(!list_empty(&entry
->lru
)))
264 nfs4_xattr_free_entry(entry
);
268 nfs4_xattr_free_cache_cb(struct kref
*kref
)
270 struct nfs4_xattr_cache
*cache
;
273 cache
= container_of(kref
, struct nfs4_xattr_cache
, ref
);
275 for (i
= 0; i
< NFS4_XATTR_HASH_SIZE
; i
++) {
276 if (WARN_ON(!hlist_empty(&cache
->buckets
[i
].hlist
)))
278 cache
->buckets
[i
].draining
= false;
281 cache
->listxattr
= NULL
;
283 kmem_cache_free(nfs4_xattr_cache_cachep
, cache
);
287 static struct nfs4_xattr_cache
*
288 nfs4_xattr_alloc_cache(void)
290 struct nfs4_xattr_cache
*cache
;
292 cache
= kmem_cache_alloc(nfs4_xattr_cache_cachep
,
293 GFP_KERNEL_ACCOUNT
| GFP_NOFS
);
297 kref_init(&cache
->ref
);
298 atomic_long_set(&cache
->nent
, 0);
304 * Set the listxattr cache, which is a special-cased cache entry.
305 * The special value ERR_PTR(-ESTALE) is used to indicate that
306 * the cache is being drained - this prevents a new listxattr
307 * cache from being added to what is now a stale cache.
310 nfs4_xattr_set_listcache(struct nfs4_xattr_cache
*cache
,
311 struct nfs4_xattr_entry
*new)
313 struct nfs4_xattr_entry
*old
;
316 spin_lock(&cache
->listxattr_lock
);
318 old
= cache
->listxattr
;
320 if (old
== ERR_PTR(-ESTALE
)) {
325 cache
->listxattr
= new;
326 if (new != NULL
&& new != ERR_PTR(-ESTALE
))
327 nfs4_xattr_entry_lru_add(new);
330 nfs4_xattr_entry_lru_del(old
);
331 kref_put(&old
->ref
, nfs4_xattr_free_entry_cb
);
334 spin_unlock(&cache
->listxattr_lock
);
340 * Unlink a cache from its parent inode, clearing out an invalid
341 * cache. Must be called with i_lock held.
343 static struct nfs4_xattr_cache
*
344 nfs4_xattr_cache_unlink(struct inode
*inode
)
346 struct nfs_inode
*nfsi
;
347 struct nfs4_xattr_cache
*oldcache
;
351 oldcache
= nfsi
->xattr_cache
;
352 if (oldcache
!= NULL
) {
353 list_lru_del(&nfs4_xattr_cache_lru
, &oldcache
->lru
);
354 oldcache
->inode
= NULL
;
356 nfsi
->xattr_cache
= NULL
;
357 nfsi
->cache_validity
&= ~NFS_INO_INVALID_XATTR
;
364 * Discard a cache. Called by get_cache() if there was an old,
365 * invalid cache. Can also be called from a shrinker callback.
367 * The cache is dead, it has already been unlinked from its inode,
368 * and no longer appears on the cache LRU list.
370 * Mark all buckets as draining, so that no new entries are added. This
371 * could still happen in the unlikely, but possible case that another
372 * thread had grabbed a reference before it was unlinked from the inode,
373 * and is still holding it for an add operation.
375 * Remove all entries from the LRU lists, so that there is no longer
376 * any way to 'find' this cache. Then, remove the entries from the hash
379 * At that point, the cache will remain empty and can be freed when the final
380 * reference drops, which is very likely the kref_put at the end of
381 * this function, or the one called immediately afterwards in the
385 nfs4_xattr_discard_cache(struct nfs4_xattr_cache
*cache
)
388 struct nfs4_xattr_entry
*entry
;
389 struct nfs4_xattr_bucket
*bucket
;
390 struct hlist_node
*n
;
392 nfs4_xattr_set_listcache(cache
, ERR_PTR(-ESTALE
));
394 for (i
= 0; i
< NFS4_XATTR_HASH_SIZE
; i
++) {
395 bucket
= &cache
->buckets
[i
];
397 spin_lock(&bucket
->lock
);
398 bucket
->draining
= true;
399 hlist_for_each_entry_safe(entry
, n
, &bucket
->hlist
, hnode
) {
400 nfs4_xattr_entry_lru_del(entry
);
401 hlist_del_init(&entry
->hnode
);
402 kref_put(&entry
->ref
, nfs4_xattr_free_entry_cb
);
404 spin_unlock(&bucket
->lock
);
407 atomic_long_set(&cache
->nent
, 0);
409 kref_put(&cache
->ref
, nfs4_xattr_free_cache_cb
);
413 * Get a referenced copy of the cache structure. Avoid doing allocs
414 * while holding i_lock. Which means that we do some optimistic allocation,
415 * and might have to free the result in rare cases.
417 * This function only checks the NFS_INO_INVALID_XATTR cache validity bit
418 * and acts accordingly, replacing the cache when needed. For the read case
419 * (!add), this means that the caller must make sure that the cache
420 * is valid before caling this function. getxattr and listxattr call
421 * revalidate_inode to do this. The attribute cache timeout (for the
422 * non-delegated case) is expected to be dealt with in the revalidate
426 static struct nfs4_xattr_cache
*
427 nfs4_xattr_get_cache(struct inode
*inode
, int add
)
429 struct nfs_inode
*nfsi
;
430 struct nfs4_xattr_cache
*cache
, *oldcache
, *newcache
;
434 cache
= oldcache
= NULL
;
436 spin_lock(&inode
->i_lock
);
438 if (nfsi
->cache_validity
& NFS_INO_INVALID_XATTR
)
439 oldcache
= nfs4_xattr_cache_unlink(inode
);
441 cache
= nfsi
->xattr_cache
;
444 kref_get(&cache
->ref
);
446 spin_unlock(&inode
->i_lock
);
448 if (add
&& cache
== NULL
) {
451 cache
= nfs4_xattr_alloc_cache();
455 spin_lock(&inode
->i_lock
);
456 if (nfsi
->cache_validity
& NFS_INO_INVALID_XATTR
) {
458 * The cache was invalidated again. Give up,
459 * since what we want to enter is now likely
462 spin_unlock(&inode
->i_lock
);
463 kref_put(&cache
->ref
, nfs4_xattr_free_cache_cb
);
469 * Check if someone beat us to it.
471 if (nfsi
->xattr_cache
!= NULL
) {
472 newcache
= nfsi
->xattr_cache
;
473 kref_get(&newcache
->ref
);
475 kref_get(&cache
->ref
);
476 nfsi
->xattr_cache
= cache
;
477 cache
->inode
= inode
;
478 list_lru_add(&nfs4_xattr_cache_lru
, &cache
->lru
);
481 spin_unlock(&inode
->i_lock
);
484 * If there was a race, throw away the cache we just
485 * allocated, and use the new one allocated by someone
488 if (newcache
!= NULL
) {
489 kref_put(&cache
->ref
, nfs4_xattr_free_cache_cb
);
496 * Discard the now orphaned old cache.
498 if (oldcache
!= NULL
)
499 nfs4_xattr_discard_cache(oldcache
);
504 static inline struct nfs4_xattr_bucket
*
505 nfs4_xattr_hash_bucket(struct nfs4_xattr_cache
*cache
, const char *name
)
507 return &cache
->buckets
[jhash(name
, strlen(name
), 0) &
508 (ARRAY_SIZE(cache
->buckets
) - 1)];
511 static struct nfs4_xattr_entry
*
512 nfs4_xattr_get_entry(struct nfs4_xattr_bucket
*bucket
, const char *name
)
514 struct nfs4_xattr_entry
*entry
;
518 hlist_for_each_entry(entry
, &bucket
->hlist
, hnode
) {
519 if (!strcmp(entry
->xattr_name
, name
))
527 nfs4_xattr_hash_add(struct nfs4_xattr_cache
*cache
,
528 struct nfs4_xattr_entry
*entry
)
530 struct nfs4_xattr_bucket
*bucket
;
531 struct nfs4_xattr_entry
*oldentry
= NULL
;
534 bucket
= nfs4_xattr_hash_bucket(cache
, entry
->xattr_name
);
535 entry
->bucket
= bucket
;
537 spin_lock(&bucket
->lock
);
539 if (bucket
->draining
) {
544 oldentry
= nfs4_xattr_get_entry(bucket
, entry
->xattr_name
);
545 if (oldentry
!= NULL
) {
546 hlist_del_init(&oldentry
->hnode
);
547 nfs4_xattr_entry_lru_del(oldentry
);
549 atomic_long_inc(&cache
->nent
);
552 hlist_add_head(&entry
->hnode
, &bucket
->hlist
);
553 nfs4_xattr_entry_lru_add(entry
);
556 spin_unlock(&bucket
->lock
);
558 if (oldentry
!= NULL
)
559 kref_put(&oldentry
->ref
, nfs4_xattr_free_entry_cb
);
565 nfs4_xattr_hash_remove(struct nfs4_xattr_cache
*cache
, const char *name
)
567 struct nfs4_xattr_bucket
*bucket
;
568 struct nfs4_xattr_entry
*entry
;
570 bucket
= nfs4_xattr_hash_bucket(cache
, name
);
572 spin_lock(&bucket
->lock
);
574 entry
= nfs4_xattr_get_entry(bucket
, name
);
576 hlist_del_init(&entry
->hnode
);
577 nfs4_xattr_entry_lru_del(entry
);
578 atomic_long_dec(&cache
->nent
);
581 spin_unlock(&bucket
->lock
);
584 kref_put(&entry
->ref
, nfs4_xattr_free_entry_cb
);
587 static struct nfs4_xattr_entry
*
588 nfs4_xattr_hash_find(struct nfs4_xattr_cache
*cache
, const char *name
)
590 struct nfs4_xattr_bucket
*bucket
;
591 struct nfs4_xattr_entry
*entry
;
593 bucket
= nfs4_xattr_hash_bucket(cache
, name
);
595 spin_lock(&bucket
->lock
);
597 entry
= nfs4_xattr_get_entry(bucket
, name
);
599 kref_get(&entry
->ref
);
601 spin_unlock(&bucket
->lock
);
607 * Entry point to retrieve an entry from the cache.
609 ssize_t
nfs4_xattr_cache_get(struct inode
*inode
, const char *name
, char *buf
,
612 struct nfs4_xattr_cache
*cache
;
613 struct nfs4_xattr_entry
*entry
;
616 cache
= nfs4_xattr_get_cache(inode
, 0);
621 entry
= nfs4_xattr_hash_find(cache
, name
);
624 dprintk("%s: cache hit '%s', len %lu\n", __func__
,
625 entry
->xattr_name
, (unsigned long)entry
->xattr_size
);
627 /* Length probe only */
628 ret
= entry
->xattr_size
;
629 } else if (buflen
< entry
->xattr_size
)
632 memcpy(buf
, entry
->xattr_value
, entry
->xattr_size
);
633 ret
= entry
->xattr_size
;
635 kref_put(&entry
->ref
, nfs4_xattr_free_entry_cb
);
637 dprintk("%s: cache miss '%s'\n", __func__
, name
);
641 kref_put(&cache
->ref
, nfs4_xattr_free_cache_cb
);
647 * Retrieve a cached list of xattrs from the cache.
649 ssize_t
nfs4_xattr_cache_list(struct inode
*inode
, char *buf
, ssize_t buflen
)
651 struct nfs4_xattr_cache
*cache
;
652 struct nfs4_xattr_entry
*entry
;
655 cache
= nfs4_xattr_get_cache(inode
, 0);
659 spin_lock(&cache
->listxattr_lock
);
661 entry
= cache
->listxattr
;
663 if (entry
!= NULL
&& entry
!= ERR_PTR(-ESTALE
)) {
665 /* Length probe only */
666 ret
= entry
->xattr_size
;
667 } else if (entry
->xattr_size
> buflen
)
670 memcpy(buf
, entry
->xattr_value
, entry
->xattr_size
);
671 ret
= entry
->xattr_size
;
677 spin_unlock(&cache
->listxattr_lock
);
679 kref_put(&cache
->ref
, nfs4_xattr_free_cache_cb
);
685 * Add an xattr to the cache.
687 * This also invalidates the xattr list cache.
689 void nfs4_xattr_cache_add(struct inode
*inode
, const char *name
,
690 const char *buf
, struct page
**pages
, ssize_t buflen
)
692 struct nfs4_xattr_cache
*cache
;
693 struct nfs4_xattr_entry
*entry
;
695 dprintk("%s: add '%s' len %lu\n", __func__
,
696 name
, (unsigned long)buflen
);
698 cache
= nfs4_xattr_get_cache(inode
, 1);
702 entry
= nfs4_xattr_alloc_entry(name
, buf
, pages
, buflen
);
706 (void)nfs4_xattr_set_listcache(cache
, NULL
);
708 if (!nfs4_xattr_hash_add(cache
, entry
))
709 kref_put(&entry
->ref
, nfs4_xattr_free_entry_cb
);
712 kref_put(&cache
->ref
, nfs4_xattr_free_cache_cb
);
717 * Remove an xattr from the cache.
719 * This also invalidates the xattr list cache.
721 void nfs4_xattr_cache_remove(struct inode
*inode
, const char *name
)
723 struct nfs4_xattr_cache
*cache
;
725 dprintk("%s: remove '%s'\n", __func__
, name
);
727 cache
= nfs4_xattr_get_cache(inode
, 0);
731 (void)nfs4_xattr_set_listcache(cache
, NULL
);
732 nfs4_xattr_hash_remove(cache
, name
);
734 kref_put(&cache
->ref
, nfs4_xattr_free_cache_cb
);
738 * Cache listxattr output, replacing any possible old one.
740 void nfs4_xattr_cache_set_list(struct inode
*inode
, const char *buf
,
743 struct nfs4_xattr_cache
*cache
;
744 struct nfs4_xattr_entry
*entry
;
746 cache
= nfs4_xattr_get_cache(inode
, 1);
750 entry
= nfs4_xattr_alloc_entry(NULL
, buf
, NULL
, buflen
);
755 * This is just there to be able to get to bucket->cache,
756 * which is obviously the same for all buckets, so just
759 entry
->bucket
= &cache
->buckets
[0];
761 if (!nfs4_xattr_set_listcache(cache
, entry
))
762 kref_put(&entry
->ref
, nfs4_xattr_free_entry_cb
);
765 kref_put(&cache
->ref
, nfs4_xattr_free_cache_cb
);
769 * Zap the entire cache. Called when an inode is evicted.
771 void nfs4_xattr_cache_zap(struct inode
*inode
)
773 struct nfs4_xattr_cache
*oldcache
;
775 spin_lock(&inode
->i_lock
);
776 oldcache
= nfs4_xattr_cache_unlink(inode
);
777 spin_unlock(&inode
->i_lock
);
780 nfs4_xattr_discard_cache(oldcache
);
784 * The entry LRU is shrunk more aggressively than the cache LRU,
785 * by settings @seeks to 1.
787 * Cache structures are freed only when they've become empty, after
788 * pruning all but one entry.
791 static unsigned long nfs4_xattr_cache_count(struct shrinker
*shrink
,
792 struct shrink_control
*sc
);
793 static unsigned long nfs4_xattr_entry_count(struct shrinker
*shrink
,
794 struct shrink_control
*sc
);
795 static unsigned long nfs4_xattr_cache_scan(struct shrinker
*shrink
,
796 struct shrink_control
*sc
);
797 static unsigned long nfs4_xattr_entry_scan(struct shrinker
*shrink
,
798 struct shrink_control
*sc
);
800 static struct shrinker nfs4_xattr_cache_shrinker
= {
801 .count_objects
= nfs4_xattr_cache_count
,
802 .scan_objects
= nfs4_xattr_cache_scan
,
803 .seeks
= DEFAULT_SEEKS
,
804 .flags
= SHRINKER_MEMCG_AWARE
,
807 static struct shrinker nfs4_xattr_entry_shrinker
= {
808 .count_objects
= nfs4_xattr_entry_count
,
809 .scan_objects
= nfs4_xattr_entry_scan
,
810 .seeks
= DEFAULT_SEEKS
,
812 .flags
= SHRINKER_MEMCG_AWARE
,
815 static struct shrinker nfs4_xattr_large_entry_shrinker
= {
816 .count_objects
= nfs4_xattr_entry_count
,
817 .scan_objects
= nfs4_xattr_entry_scan
,
820 .flags
= SHRINKER_MEMCG_AWARE
,
823 static enum lru_status
824 cache_lru_isolate(struct list_head
*item
,
825 struct list_lru_one
*lru
, spinlock_t
*lru_lock
, void *arg
)
827 struct list_head
*dispose
= arg
;
829 struct nfs4_xattr_cache
*cache
= container_of(item
,
830 struct nfs4_xattr_cache
, lru
);
832 if (atomic_long_read(&cache
->nent
) > 1)
836 * If a cache structure is on the LRU list, we know that
837 * its inode is valid. Try to lock it to break the link.
838 * Since we're inverting the lock order here, only try.
840 inode
= cache
->inode
;
842 if (!spin_trylock(&inode
->i_lock
))
845 kref_get(&cache
->ref
);
848 NFS_I(inode
)->xattr_cache
= NULL
;
849 NFS_I(inode
)->cache_validity
&= ~NFS_INO_INVALID_XATTR
;
850 list_lru_isolate(lru
, &cache
->lru
);
852 spin_unlock(&inode
->i_lock
);
854 list_add_tail(&cache
->dispose
, dispose
);
859 nfs4_xattr_cache_scan(struct shrinker
*shrink
, struct shrink_control
*sc
)
863 struct nfs4_xattr_cache
*cache
;
865 freed
= list_lru_shrink_walk(&nfs4_xattr_cache_lru
, sc
,
866 cache_lru_isolate
, &dispose
);
867 while (!list_empty(&dispose
)) {
868 cache
= list_first_entry(&dispose
, struct nfs4_xattr_cache
,
870 list_del_init(&cache
->dispose
);
871 nfs4_xattr_discard_cache(cache
);
872 kref_put(&cache
->ref
, nfs4_xattr_free_cache_cb
);
880 nfs4_xattr_cache_count(struct shrinker
*shrink
, struct shrink_control
*sc
)
884 count
= list_lru_shrink_count(&nfs4_xattr_cache_lru
, sc
);
885 return vfs_pressure_ratio(count
);
888 static enum lru_status
889 entry_lru_isolate(struct list_head
*item
,
890 struct list_lru_one
*lru
, spinlock_t
*lru_lock
, void *arg
)
892 struct list_head
*dispose
= arg
;
893 struct nfs4_xattr_bucket
*bucket
;
894 struct nfs4_xattr_cache
*cache
;
895 struct nfs4_xattr_entry
*entry
= container_of(item
,
896 struct nfs4_xattr_entry
, lru
);
898 bucket
= entry
->bucket
;
899 cache
= bucket
->cache
;
902 * Unhook the entry from its parent (either a cache bucket
903 * or a cache structure if it's a listxattr buf), so that
904 * it's no longer found. Then add it to the isolate list,
907 * In both cases, we're reverting lock order, so use
908 * trylock and skip the entry if we can't get the lock.
910 if (entry
->xattr_name
!= NULL
) {
911 /* Regular cache entry */
912 if (!spin_trylock(&bucket
->lock
))
915 kref_get(&entry
->ref
);
917 hlist_del_init(&entry
->hnode
);
918 atomic_long_dec(&cache
->nent
);
919 list_lru_isolate(lru
, &entry
->lru
);
921 spin_unlock(&bucket
->lock
);
923 /* Listxattr cache entry */
924 if (!spin_trylock(&cache
->listxattr_lock
))
927 kref_get(&entry
->ref
);
929 cache
->listxattr
= NULL
;
930 list_lru_isolate(lru
, &entry
->lru
);
932 spin_unlock(&cache
->listxattr_lock
);
935 list_add_tail(&entry
->dispose
, dispose
);
940 nfs4_xattr_entry_scan(struct shrinker
*shrink
, struct shrink_control
*sc
)
944 struct nfs4_xattr_entry
*entry
;
945 struct list_lru
*lru
;
947 lru
= (shrink
== &nfs4_xattr_large_entry_shrinker
) ?
948 &nfs4_xattr_large_entry_lru
: &nfs4_xattr_entry_lru
;
950 freed
= list_lru_shrink_walk(lru
, sc
, entry_lru_isolate
, &dispose
);
952 while (!list_empty(&dispose
)) {
953 entry
= list_first_entry(&dispose
, struct nfs4_xattr_entry
,
955 list_del_init(&entry
->dispose
);
958 * Drop two references: the one that we just grabbed
959 * in entry_lru_isolate, and the one that was set
960 * when the entry was first allocated.
962 kref_put(&entry
->ref
, nfs4_xattr_free_entry_cb
);
963 kref_put(&entry
->ref
, nfs4_xattr_free_entry_cb
);
970 nfs4_xattr_entry_count(struct shrinker
*shrink
, struct shrink_control
*sc
)
973 struct list_lru
*lru
;
975 lru
= (shrink
== &nfs4_xattr_large_entry_shrinker
) ?
976 &nfs4_xattr_large_entry_lru
: &nfs4_xattr_entry_lru
;
978 count
= list_lru_shrink_count(lru
, sc
);
979 return vfs_pressure_ratio(count
);
983 static void nfs4_xattr_cache_init_once(void *p
)
985 struct nfs4_xattr_cache
*cache
= (struct nfs4_xattr_cache
*)p
;
987 spin_lock_init(&cache
->listxattr_lock
);
988 atomic_long_set(&cache
->nent
, 0);
989 nfs4_xattr_hash_init(cache
);
990 cache
->listxattr
= NULL
;
991 INIT_LIST_HEAD(&cache
->lru
);
992 INIT_LIST_HEAD(&cache
->dispose
);
995 int __init
nfs4_xattr_cache_init(void)
999 nfs4_xattr_cache_cachep
= kmem_cache_create("nfs4_xattr_cache_cache",
1000 sizeof(struct nfs4_xattr_cache
), 0,
1001 (SLAB_RECLAIM_ACCOUNT
|SLAB_MEM_SPREAD
|SLAB_ACCOUNT
),
1002 nfs4_xattr_cache_init_once
);
1003 if (nfs4_xattr_cache_cachep
== NULL
)
1006 ret
= list_lru_init_memcg(&nfs4_xattr_large_entry_lru
,
1007 &nfs4_xattr_large_entry_shrinker
);
1011 ret
= list_lru_init_memcg(&nfs4_xattr_entry_lru
,
1012 &nfs4_xattr_entry_shrinker
);
1016 ret
= list_lru_init_memcg(&nfs4_xattr_cache_lru
,
1017 &nfs4_xattr_cache_shrinker
);
1021 ret
= register_shrinker(&nfs4_xattr_cache_shrinker
);
1025 ret
= register_shrinker(&nfs4_xattr_entry_shrinker
);
1029 ret
= register_shrinker(&nfs4_xattr_large_entry_shrinker
);
1033 unregister_shrinker(&nfs4_xattr_entry_shrinker
);
1035 unregister_shrinker(&nfs4_xattr_cache_shrinker
);
1037 list_lru_destroy(&nfs4_xattr_cache_lru
);
1039 list_lru_destroy(&nfs4_xattr_entry_lru
);
1041 list_lru_destroy(&nfs4_xattr_large_entry_lru
);
1043 kmem_cache_destroy(nfs4_xattr_cache_cachep
);
1048 void nfs4_xattr_cache_exit(void)
1050 unregister_shrinker(&nfs4_xattr_large_entry_shrinker
);
1051 unregister_shrinker(&nfs4_xattr_entry_shrinker
);
1052 unregister_shrinker(&nfs4_xattr_cache_shrinker
);
1053 list_lru_destroy(&nfs4_xattr_large_entry_lru
);
1054 list_lru_destroy(&nfs4_xattr_entry_lru
);
1055 list_lru_destroy(&nfs4_xattr_cache_lru
);
1056 kmem_cache_destroy(nfs4_xattr_cache_cachep
);