xref: /OK3568_Linux_fs/kernel/fs/xfs/xfs_mru_cache.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2006-2007 Silicon Graphics, Inc.
4*4882a593Smuzhiyun  * All Rights Reserved.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #include "xfs.h"
7*4882a593Smuzhiyun #include "xfs_mru_cache.h"
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  * The MRU Cache data structure consists of a data store, an array of lists and
11*4882a593Smuzhiyun  * a lock to protect its internal state.  At initialisation time, the client
12*4882a593Smuzhiyun  * supplies an element lifetime in milliseconds and a group count, as well as a
13*4882a593Smuzhiyun  * function pointer to call when deleting elements.  A data structure for
14*4882a593Smuzhiyun  * queueing up work in the form of timed callbacks is also included.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * The group count controls how many lists are created, and thereby how finely
17*4882a593Smuzhiyun  * the elements are grouped in time.  When reaping occurs, all the elements in
18*4882a593Smuzhiyun  * all the lists whose time has expired are deleted.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * To give an example of how this works in practice, consider a client that
21*4882a593Smuzhiyun  * initialises an MRU Cache with a lifetime of ten seconds and a group count of
22*4882a593Smuzhiyun  * five.  Five internal lists will be created, each representing a two second
23*4882a593Smuzhiyun  * period in time.  When the first element is added, time zero for the data
24*4882a593Smuzhiyun  * structure is initialised to the current time.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * All the elements added in the first two seconds are appended to the first
27*4882a593Smuzhiyun  * list.  Elements added in the third second go into the second list, and so on.
28*4882a593Smuzhiyun  * If an element is accessed at any point, it is removed from its list and
29*4882a593Smuzhiyun  * inserted at the head of the current most-recently-used list.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * The reaper function will have nothing to do until at least twelve seconds
32*4882a593Smuzhiyun  * have elapsed since the first element was added.  The reason for this is that
33*4882a593Smuzhiyun  * if it were called at t=11s, there could be elements in the first list that
34*4882a593Smuzhiyun  * have only been inactive for nine seconds, so it still does nothing.  If it is
35*4882a593Smuzhiyun  * called anywhere between t=12 and t=14 seconds, it will delete all the
36*4882a593Smuzhiyun  * elements that remain in the first list.  It's therefore possible for elements
37*4882a593Smuzhiyun  * to remain in the data store even after they've been inactive for up to
38*4882a593Smuzhiyun  * (t + t/g) seconds, where t is the inactive element lifetime and g is the
39*4882a593Smuzhiyun  * number of groups.
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * The above example assumes that the reaper function gets called at least once
42*4882a593Smuzhiyun  * every (t/g) seconds.  If it is called less frequently, unused elements will
43*4882a593Smuzhiyun  * accumulate in the reap list until the reaper function is eventually called.
44*4882a593Smuzhiyun  * The current implementation uses work queue callbacks to carefully time the
45*4882a593Smuzhiyun  * reaper function calls, so this should happen rarely, if at all.
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * From a design perspective, the primary reason for the choice of a list array
48*4882a593Smuzhiyun  * representing discrete time intervals is that it's only practical to reap
49*4882a593Smuzhiyun  * expired elements in groups of some appreciable size.  This automatically
50*4882a593Smuzhiyun  * introduces a granularity to element lifetimes, so there's no point storing an
51*4882a593Smuzhiyun  * individual timeout with each element that specifies a more precise reap time.
52*4882a593Smuzhiyun  * The bonus is a saving of sizeof(long) bytes of memory per element stored.
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * The elements could have been stored in just one list, but an array of
55*4882a593Smuzhiyun  * counters or pointers would need to be maintained to allow them to be divided
56*4882a593Smuzhiyun  * up into discrete time groups.  More critically, the process of touching or
57*4882a593Smuzhiyun  * removing an element would involve walking large portions of the entire list,
58*4882a593Smuzhiyun  * which would have a detrimental effect on performance.  The additional memory
59*4882a593Smuzhiyun  * requirement for the array of list heads is minimal.
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * When an element is touched or deleted, it needs to be removed from its
62*4882a593Smuzhiyun  * current list.  Doubly linked lists are used to make the list maintenance
63*4882a593Smuzhiyun  * portion of these operations O(1).  Since reaper timing can be imprecise,
64*4882a593Smuzhiyun  * inserts and lookups can occur when there are no free lists available.  When
65*4882a593Smuzhiyun  * this happens, all the elements on the LRU list need to be migrated to the end
66*4882a593Smuzhiyun  * of the reap list.  To keep the list maintenance portion of these operations
67*4882a593Smuzhiyun  * O(1) also, list tails need to be accessible without walking the entire list.
68*4882a593Smuzhiyun  * This is the reason why doubly linked list heads are used.
69*4882a593Smuzhiyun  */
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun  * An MRU Cache is a dynamic data structure that stores its elements in a way
73*4882a593Smuzhiyun  * that allows efficient lookups, but also groups them into discrete time
74*4882a593Smuzhiyun  * intervals based on insertion time.  This allows elements to be efficiently
75*4882a593Smuzhiyun  * and automatically reaped after a fixed period of inactivity.
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * When a client data pointer is stored in the MRU Cache it needs to be added to
78*4882a593Smuzhiyun  * both the data store and to one of the lists.  It must also be possible to
79*4882a593Smuzhiyun  * access each of these entries via the other, i.e. to:
80*4882a593Smuzhiyun  *
81*4882a593Smuzhiyun  *    a) Walk a list, removing the corresponding data store entry for each item.
82*4882a593Smuzhiyun  *    b) Look up a data store entry, then access its list entry directly.
83*4882a593Smuzhiyun  *
84*4882a593Smuzhiyun  * To achieve both of these goals, each entry must contain both a list entry and
85*4882a593Smuzhiyun  * a key, in addition to the user's data pointer.  Note that it's not a good
86*4882a593Smuzhiyun  * idea to have the client embed one of these structures at the top of their own
87*4882a593Smuzhiyun  * data structure, because inserting the same item more than once would most
88*4882a593Smuzhiyun  * likely result in a loop in one of the lists.  That's a sure-fire recipe for
89*4882a593Smuzhiyun  * an infinite loop in the code.
90*4882a593Smuzhiyun  */
91*4882a593Smuzhiyun struct xfs_mru_cache {
92*4882a593Smuzhiyun 	struct radix_tree_root	store;     /* Core storage data structure.  */
93*4882a593Smuzhiyun 	struct list_head	*lists;    /* Array of lists, one per grp.  */
94*4882a593Smuzhiyun 	struct list_head	reap_list; /* Elements overdue for reaping. */
95*4882a593Smuzhiyun 	spinlock_t		lock;      /* Lock to protect this struct.  */
96*4882a593Smuzhiyun 	unsigned int		grp_count; /* Number of discrete groups.    */
97*4882a593Smuzhiyun 	unsigned int		grp_time;  /* Time period spanned by grps.  */
98*4882a593Smuzhiyun 	unsigned int		lru_grp;   /* Group containing time zero.   */
99*4882a593Smuzhiyun 	unsigned long		time_zero; /* Time first element was added. */
100*4882a593Smuzhiyun 	xfs_mru_cache_free_func_t free_func; /* Function pointer for freeing. */
101*4882a593Smuzhiyun 	struct delayed_work	work;      /* Workqueue data for reaping.   */
102*4882a593Smuzhiyun 	unsigned int		queued;	   /* work has been queued */
103*4882a593Smuzhiyun 	void			*data;
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun static struct workqueue_struct	*xfs_mru_reap_wq;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun  * When inserting, destroying or reaping, it's first necessary to update the
110*4882a593Smuzhiyun  * lists relative to a particular time.  In the case of destroying, that time
111*4882a593Smuzhiyun  * will be well in the future to ensure that all items are moved to the reap
112*4882a593Smuzhiyun  * list.  In all other cases though, the time will be the current time.
113*4882a593Smuzhiyun  *
114*4882a593Smuzhiyun  * This function enters a loop, moving the contents of the LRU list to the reap
115*4882a593Smuzhiyun  * list again and again until either a) the lists are all empty, or b) time zero
116*4882a593Smuzhiyun  * has been advanced sufficiently to be within the immediate element lifetime.
117*4882a593Smuzhiyun  *
118*4882a593Smuzhiyun  * Case a) above is detected by counting how many groups are migrated and
119*4882a593Smuzhiyun  * stopping when they've all been moved.  Case b) is detected by monitoring the
120*4882a593Smuzhiyun  * time_zero field, which is updated as each group is migrated.
121*4882a593Smuzhiyun  *
122*4882a593Smuzhiyun  * The return value is the earliest time that more migration could be needed, or
123*4882a593Smuzhiyun  * zero if there's no need to schedule more work because the lists are empty.
124*4882a593Smuzhiyun  */
125*4882a593Smuzhiyun STATIC unsigned long
_xfs_mru_cache_migrate(struct xfs_mru_cache * mru,unsigned long now)126*4882a593Smuzhiyun _xfs_mru_cache_migrate(
127*4882a593Smuzhiyun 	struct xfs_mru_cache	*mru,
128*4882a593Smuzhiyun 	unsigned long		now)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	unsigned int		grp;
131*4882a593Smuzhiyun 	unsigned int		migrated = 0;
132*4882a593Smuzhiyun 	struct list_head	*lru_list;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	/* Nothing to do if the data store is empty. */
135*4882a593Smuzhiyun 	if (!mru->time_zero)
136*4882a593Smuzhiyun 		return 0;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	/* While time zero is older than the time spanned by all the lists. */
139*4882a593Smuzhiyun 	while (mru->time_zero <= now - mru->grp_count * mru->grp_time) {
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 		/*
142*4882a593Smuzhiyun 		 * If the LRU list isn't empty, migrate its elements to the tail
143*4882a593Smuzhiyun 		 * of the reap list.
144*4882a593Smuzhiyun 		 */
145*4882a593Smuzhiyun 		lru_list = mru->lists + mru->lru_grp;
146*4882a593Smuzhiyun 		if (!list_empty(lru_list))
147*4882a593Smuzhiyun 			list_splice_init(lru_list, mru->reap_list.prev);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 		/*
150*4882a593Smuzhiyun 		 * Advance the LRU group number, freeing the old LRU list to
151*4882a593Smuzhiyun 		 * become the new MRU list; advance time zero accordingly.
152*4882a593Smuzhiyun 		 */
153*4882a593Smuzhiyun 		mru->lru_grp = (mru->lru_grp + 1) % mru->grp_count;
154*4882a593Smuzhiyun 		mru->time_zero += mru->grp_time;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 		/*
157*4882a593Smuzhiyun 		 * If reaping is so far behind that all the elements on all the
158*4882a593Smuzhiyun 		 * lists have been migrated to the reap list, it's now empty.
159*4882a593Smuzhiyun 		 */
160*4882a593Smuzhiyun 		if (++migrated == mru->grp_count) {
161*4882a593Smuzhiyun 			mru->lru_grp = 0;
162*4882a593Smuzhiyun 			mru->time_zero = 0;
163*4882a593Smuzhiyun 			return 0;
164*4882a593Smuzhiyun 		}
165*4882a593Smuzhiyun 	}
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	/* Find the first non-empty list from the LRU end. */
168*4882a593Smuzhiyun 	for (grp = 0; grp < mru->grp_count; grp++) {
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 		/* Check the grp'th list from the LRU end. */
171*4882a593Smuzhiyun 		lru_list = mru->lists + ((mru->lru_grp + grp) % mru->grp_count);
172*4882a593Smuzhiyun 		if (!list_empty(lru_list))
173*4882a593Smuzhiyun 			return mru->time_zero +
174*4882a593Smuzhiyun 			       (mru->grp_count + grp) * mru->grp_time;
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	/* All the lists must be empty. */
178*4882a593Smuzhiyun 	mru->lru_grp = 0;
179*4882a593Smuzhiyun 	mru->time_zero = 0;
180*4882a593Smuzhiyun 	return 0;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun  * When inserting or doing a lookup, an element needs to be inserted into the
185*4882a593Smuzhiyun  * MRU list.  The lists must be migrated first to ensure that they're
186*4882a593Smuzhiyun  * up-to-date, otherwise the new element could be given a shorter lifetime in
187*4882a593Smuzhiyun  * the cache than it should.
188*4882a593Smuzhiyun  */
189*4882a593Smuzhiyun STATIC void
_xfs_mru_cache_list_insert(struct xfs_mru_cache * mru,struct xfs_mru_cache_elem * elem)190*4882a593Smuzhiyun _xfs_mru_cache_list_insert(
191*4882a593Smuzhiyun 	struct xfs_mru_cache	*mru,
192*4882a593Smuzhiyun 	struct xfs_mru_cache_elem *elem)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun 	unsigned int		grp = 0;
195*4882a593Smuzhiyun 	unsigned long		now = jiffies;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	/*
198*4882a593Smuzhiyun 	 * If the data store is empty, initialise time zero, leave grp set to
199*4882a593Smuzhiyun 	 * zero and start the work queue timer if necessary.  Otherwise, set grp
200*4882a593Smuzhiyun 	 * to the number of group times that have elapsed since time zero.
201*4882a593Smuzhiyun 	 */
202*4882a593Smuzhiyun 	if (!_xfs_mru_cache_migrate(mru, now)) {
203*4882a593Smuzhiyun 		mru->time_zero = now;
204*4882a593Smuzhiyun 		if (!mru->queued) {
205*4882a593Smuzhiyun 			mru->queued = 1;
206*4882a593Smuzhiyun 			queue_delayed_work(xfs_mru_reap_wq, &mru->work,
207*4882a593Smuzhiyun 			                   mru->grp_count * mru->grp_time);
208*4882a593Smuzhiyun 		}
209*4882a593Smuzhiyun 	} else {
210*4882a593Smuzhiyun 		grp = (now - mru->time_zero) / mru->grp_time;
211*4882a593Smuzhiyun 		grp = (mru->lru_grp + grp) % mru->grp_count;
212*4882a593Smuzhiyun 	}
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	/* Insert the element at the tail of the corresponding list. */
215*4882a593Smuzhiyun 	list_add_tail(&elem->list_node, mru->lists + grp);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun /*
219*4882a593Smuzhiyun  * When destroying or reaping, all the elements that were migrated to the reap
220*4882a593Smuzhiyun  * list need to be deleted.  For each element this involves removing it from the
221*4882a593Smuzhiyun  * data store, removing it from the reap list, calling the client's free
222*4882a593Smuzhiyun  * function and deleting the element from the element zone.
223*4882a593Smuzhiyun  *
224*4882a593Smuzhiyun  * We get called holding the mru->lock, which we drop and then reacquire.
225*4882a593Smuzhiyun  * Sparse need special help with this to tell it we know what we are doing.
226*4882a593Smuzhiyun  */
227*4882a593Smuzhiyun STATIC void
_xfs_mru_cache_clear_reap_list(struct xfs_mru_cache * mru)228*4882a593Smuzhiyun _xfs_mru_cache_clear_reap_list(
229*4882a593Smuzhiyun 	struct xfs_mru_cache	*mru)
230*4882a593Smuzhiyun 		__releases(mru->lock) __acquires(mru->lock)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	struct xfs_mru_cache_elem *elem, *next;
233*4882a593Smuzhiyun 	struct list_head	tmp;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	INIT_LIST_HEAD(&tmp);
236*4882a593Smuzhiyun 	list_for_each_entry_safe(elem, next, &mru->reap_list, list_node) {
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 		/* Remove the element from the data store. */
239*4882a593Smuzhiyun 		radix_tree_delete(&mru->store, elem->key);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 		/*
242*4882a593Smuzhiyun 		 * remove to temp list so it can be freed without
243*4882a593Smuzhiyun 		 * needing to hold the lock
244*4882a593Smuzhiyun 		 */
245*4882a593Smuzhiyun 		list_move(&elem->list_node, &tmp);
246*4882a593Smuzhiyun 	}
247*4882a593Smuzhiyun 	spin_unlock(&mru->lock);
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	list_for_each_entry_safe(elem, next, &tmp, list_node) {
250*4882a593Smuzhiyun 		list_del_init(&elem->list_node);
251*4882a593Smuzhiyun 		mru->free_func(mru->data, elem);
252*4882a593Smuzhiyun 	}
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	spin_lock(&mru->lock);
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun /*
258*4882a593Smuzhiyun  * We fire the reap timer every group expiry interval so
259*4882a593Smuzhiyun  * we always have a reaper ready to run. This makes shutdown
260*4882a593Smuzhiyun  * and flushing of the reaper easy to do. Hence we need to
261*4882a593Smuzhiyun  * keep when the next reap must occur so we can determine
262*4882a593Smuzhiyun  * at each interval whether there is anything we need to do.
263*4882a593Smuzhiyun  */
264*4882a593Smuzhiyun STATIC void
_xfs_mru_cache_reap(struct work_struct * work)265*4882a593Smuzhiyun _xfs_mru_cache_reap(
266*4882a593Smuzhiyun 	struct work_struct	*work)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun 	struct xfs_mru_cache	*mru =
269*4882a593Smuzhiyun 		container_of(work, struct xfs_mru_cache, work.work);
270*4882a593Smuzhiyun 	unsigned long		now, next;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	ASSERT(mru && mru->lists);
273*4882a593Smuzhiyun 	if (!mru || !mru->lists)
274*4882a593Smuzhiyun 		return;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	spin_lock(&mru->lock);
277*4882a593Smuzhiyun 	next = _xfs_mru_cache_migrate(mru, jiffies);
278*4882a593Smuzhiyun 	_xfs_mru_cache_clear_reap_list(mru);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	mru->queued = next;
281*4882a593Smuzhiyun 	if ((mru->queued > 0)) {
282*4882a593Smuzhiyun 		now = jiffies;
283*4882a593Smuzhiyun 		if (next <= now)
284*4882a593Smuzhiyun 			next = 0;
285*4882a593Smuzhiyun 		else
286*4882a593Smuzhiyun 			next -= now;
287*4882a593Smuzhiyun 		queue_delayed_work(xfs_mru_reap_wq, &mru->work, next);
288*4882a593Smuzhiyun 	}
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	spin_unlock(&mru->lock);
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun int
xfs_mru_cache_init(void)294*4882a593Smuzhiyun xfs_mru_cache_init(void)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun 	xfs_mru_reap_wq = alloc_workqueue("xfs_mru_cache",
297*4882a593Smuzhiyun 				WQ_MEM_RECLAIM|WQ_FREEZABLE, 1);
298*4882a593Smuzhiyun 	if (!xfs_mru_reap_wq)
299*4882a593Smuzhiyun 		return -ENOMEM;
300*4882a593Smuzhiyun 	return 0;
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun void
xfs_mru_cache_uninit(void)304*4882a593Smuzhiyun xfs_mru_cache_uninit(void)
305*4882a593Smuzhiyun {
306*4882a593Smuzhiyun 	destroy_workqueue(xfs_mru_reap_wq);
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun /*
310*4882a593Smuzhiyun  * To initialise a struct xfs_mru_cache pointer, call xfs_mru_cache_create()
311*4882a593Smuzhiyun  * with the address of the pointer, a lifetime value in milliseconds, a group
312*4882a593Smuzhiyun  * count and a free function to use when deleting elements.  This function
313*4882a593Smuzhiyun  * returns 0 if the initialisation was successful.
314*4882a593Smuzhiyun  */
315*4882a593Smuzhiyun int
xfs_mru_cache_create(struct xfs_mru_cache ** mrup,void * data,unsigned int lifetime_ms,unsigned int grp_count,xfs_mru_cache_free_func_t free_func)316*4882a593Smuzhiyun xfs_mru_cache_create(
317*4882a593Smuzhiyun 	struct xfs_mru_cache	**mrup,
318*4882a593Smuzhiyun 	void			*data,
319*4882a593Smuzhiyun 	unsigned int		lifetime_ms,
320*4882a593Smuzhiyun 	unsigned int		grp_count,
321*4882a593Smuzhiyun 	xfs_mru_cache_free_func_t free_func)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun 	struct xfs_mru_cache	*mru = NULL;
324*4882a593Smuzhiyun 	int			err = 0, grp;
325*4882a593Smuzhiyun 	unsigned int		grp_time;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	if (mrup)
328*4882a593Smuzhiyun 		*mrup = NULL;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	if (!mrup || !grp_count || !lifetime_ms || !free_func)
331*4882a593Smuzhiyun 		return -EINVAL;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	if (!(grp_time = msecs_to_jiffies(lifetime_ms) / grp_count))
334*4882a593Smuzhiyun 		return -EINVAL;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	if (!(mru = kmem_zalloc(sizeof(*mru), 0)))
337*4882a593Smuzhiyun 		return -ENOMEM;
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	/* An extra list is needed to avoid reaping up to a grp_time early. */
340*4882a593Smuzhiyun 	mru->grp_count = grp_count + 1;
341*4882a593Smuzhiyun 	mru->lists = kmem_zalloc(mru->grp_count * sizeof(*mru->lists), 0);
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	if (!mru->lists) {
344*4882a593Smuzhiyun 		err = -ENOMEM;
345*4882a593Smuzhiyun 		goto exit;
346*4882a593Smuzhiyun 	}
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	for (grp = 0; grp < mru->grp_count; grp++)
349*4882a593Smuzhiyun 		INIT_LIST_HEAD(mru->lists + grp);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	/*
352*4882a593Smuzhiyun 	 * We use GFP_KERNEL radix tree preload and do inserts under a
353*4882a593Smuzhiyun 	 * spinlock so GFP_ATOMIC is appropriate for the radix tree itself.
354*4882a593Smuzhiyun 	 */
355*4882a593Smuzhiyun 	INIT_RADIX_TREE(&mru->store, GFP_ATOMIC);
356*4882a593Smuzhiyun 	INIT_LIST_HEAD(&mru->reap_list);
357*4882a593Smuzhiyun 	spin_lock_init(&mru->lock);
358*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&mru->work, _xfs_mru_cache_reap);
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	mru->grp_time  = grp_time;
361*4882a593Smuzhiyun 	mru->free_func = free_func;
362*4882a593Smuzhiyun 	mru->data = data;
363*4882a593Smuzhiyun 	*mrup = mru;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun exit:
366*4882a593Smuzhiyun 	if (err && mru && mru->lists)
367*4882a593Smuzhiyun 		kmem_free(mru->lists);
368*4882a593Smuzhiyun 	if (err && mru)
369*4882a593Smuzhiyun 		kmem_free(mru);
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	return err;
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun /*
375*4882a593Smuzhiyun  * Call xfs_mru_cache_flush() to flush out all cached entries, calling their
376*4882a593Smuzhiyun  * free functions as they're deleted.  When this function returns, the caller is
377*4882a593Smuzhiyun  * guaranteed that all the free functions for all the elements have finished
378*4882a593Smuzhiyun  * executing and the reaper is not running.
379*4882a593Smuzhiyun  */
380*4882a593Smuzhiyun static void
xfs_mru_cache_flush(struct xfs_mru_cache * mru)381*4882a593Smuzhiyun xfs_mru_cache_flush(
382*4882a593Smuzhiyun 	struct xfs_mru_cache	*mru)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun 	if (!mru || !mru->lists)
385*4882a593Smuzhiyun 		return;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	spin_lock(&mru->lock);
388*4882a593Smuzhiyun 	if (mru->queued) {
389*4882a593Smuzhiyun 		spin_unlock(&mru->lock);
390*4882a593Smuzhiyun 		cancel_delayed_work_sync(&mru->work);
391*4882a593Smuzhiyun 		spin_lock(&mru->lock);
392*4882a593Smuzhiyun 	}
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	_xfs_mru_cache_migrate(mru, jiffies + mru->grp_count * mru->grp_time);
395*4882a593Smuzhiyun 	_xfs_mru_cache_clear_reap_list(mru);
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	spin_unlock(&mru->lock);
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun void
xfs_mru_cache_destroy(struct xfs_mru_cache * mru)401*4882a593Smuzhiyun xfs_mru_cache_destroy(
402*4882a593Smuzhiyun 	struct xfs_mru_cache	*mru)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun 	if (!mru || !mru->lists)
405*4882a593Smuzhiyun 		return;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	xfs_mru_cache_flush(mru);
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	kmem_free(mru->lists);
410*4882a593Smuzhiyun 	kmem_free(mru);
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun /*
414*4882a593Smuzhiyun  * To insert an element, call xfs_mru_cache_insert() with the data store, the
415*4882a593Smuzhiyun  * element's key and the client data pointer.  This function returns 0 on
416*4882a593Smuzhiyun  * success or ENOMEM if memory for the data element couldn't be allocated.
417*4882a593Smuzhiyun  */
418*4882a593Smuzhiyun int
xfs_mru_cache_insert(struct xfs_mru_cache * mru,unsigned long key,struct xfs_mru_cache_elem * elem)419*4882a593Smuzhiyun xfs_mru_cache_insert(
420*4882a593Smuzhiyun 	struct xfs_mru_cache	*mru,
421*4882a593Smuzhiyun 	unsigned long		key,
422*4882a593Smuzhiyun 	struct xfs_mru_cache_elem *elem)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun 	int			error;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	ASSERT(mru && mru->lists);
427*4882a593Smuzhiyun 	if (!mru || !mru->lists)
428*4882a593Smuzhiyun 		return -EINVAL;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	if (radix_tree_preload(GFP_NOFS))
431*4882a593Smuzhiyun 		return -ENOMEM;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	INIT_LIST_HEAD(&elem->list_node);
434*4882a593Smuzhiyun 	elem->key = key;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	spin_lock(&mru->lock);
437*4882a593Smuzhiyun 	error = radix_tree_insert(&mru->store, key, elem);
438*4882a593Smuzhiyun 	radix_tree_preload_end();
439*4882a593Smuzhiyun 	if (!error)
440*4882a593Smuzhiyun 		_xfs_mru_cache_list_insert(mru, elem);
441*4882a593Smuzhiyun 	spin_unlock(&mru->lock);
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	return error;
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun /*
447*4882a593Smuzhiyun  * To remove an element without calling the free function, call
448*4882a593Smuzhiyun  * xfs_mru_cache_remove() with the data store and the element's key.  On success
449*4882a593Smuzhiyun  * the client data pointer for the removed element is returned, otherwise this
450*4882a593Smuzhiyun  * function will return a NULL pointer.
451*4882a593Smuzhiyun  */
452*4882a593Smuzhiyun struct xfs_mru_cache_elem *
xfs_mru_cache_remove(struct xfs_mru_cache * mru,unsigned long key)453*4882a593Smuzhiyun xfs_mru_cache_remove(
454*4882a593Smuzhiyun 	struct xfs_mru_cache	*mru,
455*4882a593Smuzhiyun 	unsigned long		key)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun 	struct xfs_mru_cache_elem *elem;
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	ASSERT(mru && mru->lists);
460*4882a593Smuzhiyun 	if (!mru || !mru->lists)
461*4882a593Smuzhiyun 		return NULL;
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	spin_lock(&mru->lock);
464*4882a593Smuzhiyun 	elem = radix_tree_delete(&mru->store, key);
465*4882a593Smuzhiyun 	if (elem)
466*4882a593Smuzhiyun 		list_del(&elem->list_node);
467*4882a593Smuzhiyun 	spin_unlock(&mru->lock);
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	return elem;
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun /*
473*4882a593Smuzhiyun  * To remove and element and call the free function, call xfs_mru_cache_delete()
474*4882a593Smuzhiyun  * with the data store and the element's key.
475*4882a593Smuzhiyun  */
476*4882a593Smuzhiyun void
xfs_mru_cache_delete(struct xfs_mru_cache * mru,unsigned long key)477*4882a593Smuzhiyun xfs_mru_cache_delete(
478*4882a593Smuzhiyun 	struct xfs_mru_cache	*mru,
479*4882a593Smuzhiyun 	unsigned long		key)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun 	struct xfs_mru_cache_elem *elem;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	elem = xfs_mru_cache_remove(mru, key);
484*4882a593Smuzhiyun 	if (elem)
485*4882a593Smuzhiyun 		mru->free_func(mru->data, elem);
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun /*
489*4882a593Smuzhiyun  * To look up an element using its key, call xfs_mru_cache_lookup() with the
490*4882a593Smuzhiyun  * data store and the element's key.  If found, the element will be moved to the
491*4882a593Smuzhiyun  * head of the MRU list to indicate that it's been touched.
492*4882a593Smuzhiyun  *
493*4882a593Smuzhiyun  * The internal data structures are protected by a spinlock that is STILL HELD
494*4882a593Smuzhiyun  * when this function returns.  Call xfs_mru_cache_done() to release it.  Note
495*4882a593Smuzhiyun  * that it is not safe to call any function that might sleep in the interim.
496*4882a593Smuzhiyun  *
497*4882a593Smuzhiyun  * The implementation could have used reference counting to avoid this
498*4882a593Smuzhiyun  * restriction, but since most clients simply want to get, set or test a member
499*4882a593Smuzhiyun  * of the returned data structure, the extra per-element memory isn't warranted.
500*4882a593Smuzhiyun  *
501*4882a593Smuzhiyun  * If the element isn't found, this function returns NULL and the spinlock is
502*4882a593Smuzhiyun  * released.  xfs_mru_cache_done() should NOT be called when this occurs.
503*4882a593Smuzhiyun  *
504*4882a593Smuzhiyun  * Because sparse isn't smart enough to know about conditional lock return
505*4882a593Smuzhiyun  * status, we need to help it get it right by annotating the path that does
506*4882a593Smuzhiyun  * not release the lock.
507*4882a593Smuzhiyun  */
508*4882a593Smuzhiyun struct xfs_mru_cache_elem *
xfs_mru_cache_lookup(struct xfs_mru_cache * mru,unsigned long key)509*4882a593Smuzhiyun xfs_mru_cache_lookup(
510*4882a593Smuzhiyun 	struct xfs_mru_cache	*mru,
511*4882a593Smuzhiyun 	unsigned long		key)
512*4882a593Smuzhiyun {
513*4882a593Smuzhiyun 	struct xfs_mru_cache_elem *elem;
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	ASSERT(mru && mru->lists);
516*4882a593Smuzhiyun 	if (!mru || !mru->lists)
517*4882a593Smuzhiyun 		return NULL;
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	spin_lock(&mru->lock);
520*4882a593Smuzhiyun 	elem = radix_tree_lookup(&mru->store, key);
521*4882a593Smuzhiyun 	if (elem) {
522*4882a593Smuzhiyun 		list_del(&elem->list_node);
523*4882a593Smuzhiyun 		_xfs_mru_cache_list_insert(mru, elem);
524*4882a593Smuzhiyun 		__release(mru_lock); /* help sparse not be stupid */
525*4882a593Smuzhiyun 	} else
526*4882a593Smuzhiyun 		spin_unlock(&mru->lock);
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	return elem;
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun /*
532*4882a593Smuzhiyun  * To release the internal data structure spinlock after having performed an
533*4882a593Smuzhiyun  * xfs_mru_cache_lookup() or an xfs_mru_cache_peek(), call xfs_mru_cache_done()
534*4882a593Smuzhiyun  * with the data store pointer.
535*4882a593Smuzhiyun  */
536*4882a593Smuzhiyun void
xfs_mru_cache_done(struct xfs_mru_cache * mru)537*4882a593Smuzhiyun xfs_mru_cache_done(
538*4882a593Smuzhiyun 	struct xfs_mru_cache	*mru)
539*4882a593Smuzhiyun 		__releases(mru->lock)
540*4882a593Smuzhiyun {
541*4882a593Smuzhiyun 	spin_unlock(&mru->lock);
542*4882a593Smuzhiyun }
543