History

fscache: Implement cookie user counting and resource pinning

Provide a pair of functions to count the number of users of a cookie (open files, writeback, invalidation, resizing, reads, writes), to obtain and pin resources for the cookie and to prevent culling for the whilst there are users. The first function marks a cookie as being in use: void fscache_use_cookie(struct fscache_cookie *cookie, bool will_modify); The caller should indicate the cookie to use and whether or not the caller is in a context that may modify the cookie (e.g. a file open O_RDWR). If the cookie is not already resourced, fscache will ask the cache backend in the background to do whatever it needs to look up, create or otherwise obtain the resources necessary to access data. This is pinned to the cookie and may not be culled, though it may be withdrawn if the cache as a whole is withdrawn. The second function removes the in-use mark from a cookie and, optionally, updates the coherency data: void fscache_unuse_cookie(struct fscache_cookie *cookie, const void *aux_data, const loff_t *object_size); If non-NULL, the aux_data buffer and/or the object_size will be saved into the cookie and will be set on the backing store when the object is committed. If this removes the last usage on a cookie, the cookie is placed onto an LRU list from which it will be removed and closed after a couple of seconds if it doesn't get reused. This prevents resource overload in the cache - in particular it prevents it from holding too many files open. Changes ======= ver #2: - Fix fscache_unuse_cookie() to use atomic_dec_and_lock() to avoid a potential race if the cookie gets reused before it completes the unusement. - Added missing transition to LRU_DISCARDING state.

David Howells committed 5 years ago

Commit 12bb21a1 parent 5d00e425 files · +327 −2Browse files

Showing the changes to include (2 of 5 files). Show all

Unified · Split

2 files
  1. include/linux/fscache.h +81 −1
  2. include/trace/events/fscache.h +12 −0
include/linux/fscache.h+81−1
@@ -22,12 +22,14 @@ · 21 unmodified lines
2222 #define fscache_available() (1)
2323 #define fscache_volume_valid(volume) (volume)
2424 #define fscache_cookie_valid(cookie) (cookie)
25−#define fscache_cookie_enabled(cookie) (cookie)
25+#define fscache_resources_valid(cres) ((cres)->cache_priv)
26+#define fscache_cookie_enabled(cookie) (cookie && !test_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags))
2627 #else
2728 #define __fscache_available (0)
2829 #define fscache_available() (0)
2930 #define fscache_volume_valid(volume) (0)
3031 #define fscache_cookie_valid(cookie) (0)
32+#define fscache_resources_valid(cres) (false)
3133 #define fscache_cookie_enabled(cookie) (0)
3234 #endif
3335
@@ -46,6 +48,7 @@ enum fscache_cookie_state { · 12 unmodified lines
4648 FSCACHE_COOKIE_STATE_CREATING, /* The cache object is being created */
4749 FSCACHE_COOKIE_STATE_ACTIVE, /* The cache is active, readable and writable */
4850 FSCACHE_COOKIE_STATE_FAILED, /* The cache failed, withdraw to clear */
51+ FSCACHE_COOKIE_STATE_LRU_DISCARDING, /* The cookie is being discarded by the LRU */
4952 FSCACHE_COOKIE_STATE_WITHDRAWING, /* The cookie is being withdrawn */
5053 FSCACHE_COOKIE_STATE_RELINQUISHING, /* The cookie is being relinquished */
5154 FSCACHE_COOKIE_STATE_DROPPED, /* The cookie has been dropped */
@@ -147,6 +150,8 @@ extern struct fscache_cookie *__fscache_acquire_cookie( · 95 unmodified lines
147150 const void *, size_t,
148151 const void *, size_t,
149152 loff_t);
153+extern void __fscache_use_cookie(struct fscache_cookie *, bool);
154+extern void __fscache_unuse_cookie(struct fscache_cookie *, const void *, const loff_t *);
150155 extern void __fscache_relinquish_cookie(struct fscache_cookie *, bool);
151156
152157 /**
@@ -228,6 +233,39 @@ struct fscache_cookie *fscache_acquire_cookie(struct fscache_volume *volume, · 75 unmodified lines
228233 object_size);
229234 }
230235
236+/**
237+ * fscache_use_cookie - Request usage of cookie attached to an object
238+ * @object: Object description
239+ * @will_modify: If cache is expected to be modified locally
240+ *
241+ * Request usage of the cookie attached to an object. The caller should tell
242+ * the cache if the object's contents are about to be modified locally and then
243+ * the cache can apply the policy that has been set to handle this case.
244+ */
245+static inline void fscache_use_cookie(struct fscache_cookie *cookie,
246+ bool will_modify)
247+{
248+ if (fscache_cookie_valid(cookie))
249+ __fscache_use_cookie(cookie, will_modify);
250+}
251+
252+/**
253+ * fscache_unuse_cookie - Cease usage of cookie attached to an object
254+ * @object: Object description
255+ * @aux_data: Updated auxiliary data (or NULL)
256+ * @object_size: Revised size of the object (or NULL)
257+ *
258+ * Cease usage of the cookie attached to an object. When the users count
259+ * reaches zero then the cookie relinquishment will be permitted to proceed.
260+ */
261+static inline void fscache_unuse_cookie(struct fscache_cookie *cookie,
262+ const void *aux_data,
263+ const loff_t *object_size)
264+{
265+ if (fscache_cookie_valid(cookie))
266+ __fscache_unuse_cookie(cookie, aux_data, object_size);
267+}
268+
231269 /**
232270 * fscache_relinquish_cookie - Return the cookie to the cache, maybe discarding
233271 * it
@@ -247,4 +285,46 @@ void fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire) · 13 unmodified lines
247285 __fscache_relinquish_cookie(cookie, retire);
248286 }
249287
288+/*
289+ * Find the auxiliary data on a cookie.
290+ */
291+static inline void *fscache_get_aux(struct fscache_cookie *cookie)
292+{
293+ if (cookie->aux_len <= sizeof(cookie->inline_aux))
294+ return cookie->inline_aux;
295+ else
296+ return cookie->aux;
297+}
298+
299+/*
300+ * Update the auxiliary data on a cookie.
301+ */
302+static inline
303+void fscache_update_aux(struct fscache_cookie *cookie,
304+ const void *aux_data, const loff_t *object_size)
305+{
306+ void *p = fscache_get_aux(cookie);
307+
308+ if (aux_data && p)
309+ memcpy(p, aux_data, cookie->aux_len);
310+ if (object_size)
311+ cookie->object_size = *object_size;
312+}
313+
314+#ifdef CONFIG_FSCACHE_STATS
315+extern atomic_t fscache_n_updates;
316+#endif
317+
318+static inline
319+void __fscache_update_cookie(struct fscache_cookie *cookie, const void *aux_data,
320+ const loff_t *object_size)
321+{
322+#ifdef CONFIG_FSCACHE_STATS
323+ atomic_inc(&fscache_n_updates);
324+#endif
325+ fscache_update_aux(cookie, aux_data, object_size);
326+ smp_wmb();
327+ set_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &cookie->flags);
328+}
329+
250330 #endif /* _LINUX_FSCACHE_H */
include/trace/events/fscache.h+12−0
@@ -51,13 +51,18 @@ enum fscache_cookie_trace { · 50 unmodified lines
5151 fscache_cookie_discard,
5252 fscache_cookie_get_end_access,
5353 fscache_cookie_get_hash_collision,
54+ fscache_cookie_get_lru,
55+ fscache_cookie_get_use_work,
5456 fscache_cookie_new_acquire,
5557 fscache_cookie_put_hash_collision,
58+ fscache_cookie_put_lru,
5659 fscache_cookie_put_over_queued,
5760 fscache_cookie_put_relinquish,
5861 fscache_cookie_put_withdrawn,
5962 fscache_cookie_put_work,
6063 fscache_cookie_see_active,
64+ fscache_cookie_see_lru_discard,
65+ fscache_cookie_see_lru_do_one,
6166 fscache_cookie_see_relinquish,
6267 fscache_cookie_see_withdraw,
6368 fscache_cookie_see_work,
@@ -68,6 +73,7 @@ enum fscache_access_trace { · 4 unmodified lines
6873 fscache_access_acquire_volume_end,
6974 fscache_access_cache_pin,
7075 fscache_access_cache_unpin,
76+ fscache_access_lookup_cookie,
7177 fscache_access_lookup_cookie_end,
7278 fscache_access_lookup_cookie_end_failed,
7379 fscache_access_relinquish_volume,
@@ -110,13 +116,18 @@ enum fscache_access_trace { · 36 unmodified lines
110116 EM(fscache_cookie_discard, "DISCARD ") \
111117 EM(fscache_cookie_get_hash_collision, "GET hcoll") \
112118 EM(fscache_cookie_get_end_access, "GQ endac") \
119+ EM(fscache_cookie_get_lru, "GET lru ") \
120+ EM(fscache_cookie_get_use_work, "GQ use ") \
113121 EM(fscache_cookie_new_acquire, "NEW acq ") \
114122 EM(fscache_cookie_put_hash_collision, "PUT hcoll") \
123+ EM(fscache_cookie_put_lru, "PUT lru ") \
115124 EM(fscache_cookie_put_over_queued, "PQ overq") \
116125 EM(fscache_cookie_put_relinquish, "PUT relnq") \
117126 EM(fscache_cookie_put_withdrawn, "PUT wthdn") \
118127 EM(fscache_cookie_put_work, "PQ work ") \
119128 EM(fscache_cookie_see_active, "- activ") \
129+ EM(fscache_cookie_see_lru_discard, "- x-lru") \
130+ EM(fscache_cookie_see_lru_do_one, "- lrudo") \
120131 EM(fscache_cookie_see_relinquish, "- x-rlq") \
121132 EM(fscache_cookie_see_withdraw, "- x-wth") \
122133 E_(fscache_cookie_see_work, "- work ")
@@ -126,6 +137,7 @@ enum fscache_access_trace { · 3 unmodified lines
126137 EM(fscache_access_acquire_volume_end, "END acq_vol") \
127138 EM(fscache_access_cache_pin, "PIN cache ") \
128139 EM(fscache_access_cache_unpin, "UNPIN cache ") \
140+ EM(fscache_access_lookup_cookie, "BEGIN lookup ") \
129141 EM(fscache_access_lookup_cookie_end, "END lookup ") \
130142 EM(fscache_access_lookup_cookie_end_failed,"END lookupf") \
131143 EM(fscache_access_relinquish_volume, "BEGIN rlq_vol") \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t