Skip to content

Commit fee096d

Browse files
committed
CacheFiles: Catch an overly long wait for an old active object
Catch an overly long wait for an old, dying active object when we want to replace it with a new one. The probability is that all the slow-work threads are hogged, and the delete can't get a look in. What we do instead is: (1) if there's nothing in the slow work queue, we sleep until either the dying object has finished dying or there is something in the slow work queue behind which we can queue our object. (2) if there is something in the slow work queue, we return ETIMEDOUT to fscache_lookup_object(), which then puts us back on the slow work queue, presumably behind the deletion that we're blocked by. We are then deferred for a while until we work our way back through the queue - without blocking a slow-work thread unnecessarily. A backtrace similar to the following may appear in the log without this patch: INFO: task kslowd004:5711 blocked for more than 120 seconds. "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. kslowd004 D 0000000000000000 0 5711 2 0x00000080 ffff88000340bb80 0000000000000046 ffff88002550d000 0000000000000000 ffff88002550d000 0000000000000007 ffff88000340bfd8 ffff88002550d2a8 000000000000ddf0 00000000000118c0 00000000000118c0 ffff88002550d2a8 Call Trace: [<ffffffff81058e21>] ? trace_hardirqs_on+0xd/0xf [<ffffffffa011c4d8>] ? cachefiles_wait_bit+0x0/0xd [cachefiles] [<ffffffffa011c4e1>] cachefiles_wait_bit+0x9/0xd [cachefiles] [<ffffffff81353153>] __wait_on_bit+0x43/0x76 [<ffffffff8111ae39>] ? ext3_xattr_get+0x1ec/0x270 [<ffffffff813531ef>] out_of_line_wait_on_bit+0x69/0x74 [<ffffffffa011c4d8>] ? cachefiles_wait_bit+0x0/0xd [cachefiles] [<ffffffff8104c125>] ? wake_bit_function+0x0/0x2e [<ffffffffa011bc79>] cachefiles_mark_object_active+0x203/0x23b [cachefiles] [<ffffffffa011c209>] cachefiles_walk_to_object+0x558/0x827 [cachefiles] [<ffffffffa011a429>] cachefiles_lookup_object+0xac/0x12a [cachefiles] [<ffffffffa00aa1e9>] fscache_lookup_object+0x1c7/0x214 [fscache] [<ffffffffa00aafc5>] fscache_object_state_machine+0xa5/0x52d [fscache] [<ffffffffa00ab4ac>] fscache_object_slow_work_execute+0x5f/0xa0 [fscache] [<ffffffff81082093>] slow_work_execute+0x18f/0x2d1 [<ffffffff8108239a>] slow_work_thread+0x1c5/0x308 [<ffffffff8104c0f1>] ? autoremove_wake_function+0x0/0x34 [<ffffffff810821d5>] ? slow_work_thread+0x0/0x308 [<ffffffff8104be91>] kthread+0x7a/0x82 [<ffffffff8100beda>] child_rip+0xa/0x20 [<ffffffff8100b87c>] ? restore_args+0x0/0x30 [<ffffffff8104be17>] ? kthread+0x0/0x82 [<ffffffff8100bed0>] ? child_rip+0x0/0x20 1 lock held by kslowd004/5711: #0: (&sb->s_type->i_mutex_key#7/1){+.+.+.}, at: [<ffffffffa011be64>] cachefiles_walk_to_object+0x1b3/0x827 [cachefiles] Signed-off-by: David Howells <dhowells@redhat.com>
1 parent d0e27b7 commit fee096d

7 files changed

Lines changed: 90 additions & 25 deletions

File tree

‎Documentation/filesystems/caching/fscache.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ proc files.
235235
neg=N Number of negative lookups made
236236
pos=N Number of positive lookups made
237237
crt=N Number of objects created by lookup
238+
tmo=N Number of lookups timed out and requeued
238239
Updates n=N Number of update cookie requests seen
239240
nul=N Number of upd reqs given a NULL parent
240241
run=N Number of upd reqs granted CPU time

‎fs/cachefiles/interface.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ static struct fscache_object *cachefiles_alloc_object(
114114

115115
/*
116116
* attempt to look up the nominated node in this cache
117+
* - return -ETIMEDOUT to be scheduled again
117118
*/
118-
static void cachefiles_lookup_object(struct fscache_object *_object)
119+
static int cachefiles_lookup_object(struct fscache_object *_object)
119120
{
120121
struct cachefiles_lookup_data *lookup_data;
121122
struct cachefiles_object *parent, *object;
@@ -145,13 +146,14 @@ static void cachefiles_lookup_object(struct fscache_object *_object)
145146
object->fscache.cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX)
146147
cachefiles_attr_changed(&object->fscache);
147148

148-
if (ret < 0) {
149+
if (ret < 0 && ret != -ETIMEDOUT) {
149150
printk(KERN_WARNING "CacheFiles: Lookup failed error %d\n",
150151
ret);
151152
fscache_object_lookup_error(&object->fscache);
152153
}
153154

154155
_leave(" [%d]", ret);
156+
return ret;
155157
}
156158

157159
/*

‎fs/cachefiles/namei.c‎

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@
2121
#include <linux/security.h>
2222
#include "internal.h"
2323

24-
static int cachefiles_wait_bit(void *flags)
25-
{
26-
schedule();
27-
return 0;
28-
}
29-
3024
#define CACHEFILES_KEYBUF_SIZE 512
3125

3226
/*
@@ -100,8 +94,8 @@ static noinline void cachefiles_printk_object(struct cachefiles_object *object,
10094
/*
10195
* record the fact that an object is now active
10296
*/
103-
static void cachefiles_mark_object_active(struct cachefiles_cache *cache,
104-
struct cachefiles_object *object)
97+
static int cachefiles_mark_object_active(struct cachefiles_cache *cache,
98+
struct cachefiles_object *object)
10599
{
106100
struct cachefiles_object *xobject;
107101
struct rb_node **_p, *_parent = NULL;
@@ -139,8 +133,8 @@ static void cachefiles_mark_object_active(struct cachefiles_cache *cache,
139133
rb_insert_color(&object->active_node, &cache->active_nodes);
140134

141135
write_unlock(&cache->active_lock);
142-
_leave("");
143-
return;
136+
_leave(" = 0");
137+
return 0;
144138

145139
/* an old object from a previous incarnation is hogging the slot - we
146140
* need to wait for it to be destroyed */
@@ -155,13 +149,64 @@ static void cachefiles_mark_object_active(struct cachefiles_cache *cache,
155149
atomic_inc(&xobject->usage);
156150
write_unlock(&cache->active_lock);
157151

158-
_debug(">>> wait");
159-
wait_on_bit(&xobject->flags, CACHEFILES_OBJECT_ACTIVE,
160-
cachefiles_wait_bit, TASK_UNINTERRUPTIBLE);
161-
_debug("<<< waited");
152+
if (test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
153+
wait_queue_head_t *wq;
154+
155+
signed long timeout = 60 * HZ;
156+
wait_queue_t wait;
157+
bool requeue;
158+
159+
/* if the object we're waiting for is queued for processing,
160+
* then just put ourselves on the queue behind it */
161+
if (slow_work_is_queued(&xobject->fscache.work)) {
162+
_debug("queue OBJ%x behind OBJ%x immediately",
163+
object->fscache.debug_id,
164+
xobject->fscache.debug_id);
165+
goto requeue;
166+
}
167+
168+
/* otherwise we sleep until either the object we're waiting for
169+
* is done, or the slow-work facility wants the thread back to
170+
* do other work */
171+
wq = bit_waitqueue(&xobject->flags, CACHEFILES_OBJECT_ACTIVE);
172+
init_wait(&wait);
173+
requeue = false;
174+
do {
175+
prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
176+
if (!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags))
177+
break;
178+
requeue = slow_work_sleep_till_thread_needed(
179+
&object->fscache.work, &timeout);
180+
} while (timeout > 0 && !requeue);
181+
finish_wait(wq, &wait);
182+
183+
if (requeue &&
184+
test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags)) {
185+
_debug("queue OBJ%x behind OBJ%x after wait",
186+
object->fscache.debug_id,
187+
xobject->fscache.debug_id);
188+
goto requeue;
189+
}
190+
191+
if (timeout <= 0) {
192+
printk(KERN_ERR "\n");
193+
printk(KERN_ERR "CacheFiles: Error: Overlong"
194+
" wait for old active object to go away\n");
195+
cachefiles_printk_object(object, xobject);
196+
goto requeue;
197+
}
198+
}
199+
200+
ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &xobject->flags));
162201

163202
cache->cache.ops->put_object(&xobject->fscache);
164203
goto try_again;
204+
205+
requeue:
206+
clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
207+
cache->cache.ops->put_object(&xobject->fscache);
208+
_leave(" = -ETIMEDOUT");
209+
return -ETIMEDOUT;
165210
}
166211

167212
/*
@@ -466,12 +511,15 @@ int cachefiles_walk_to_object(struct cachefiles_object *parent,
466511
}
467512

468513
/* note that we're now using this object */
469-
cachefiles_mark_object_active(cache, object);
514+
ret = cachefiles_mark_object_active(cache, object);
470515

471516
mutex_unlock(&dir->d_inode->i_mutex);
472517
dput(dir);
473518
dir = NULL;
474519

520+
if (ret == -ETIMEDOUT)
521+
goto mark_active_timed_out;
522+
475523
_debug("=== OBTAINED_OBJECT ===");
476524

477525
if (object->new) {
@@ -515,14 +563,18 @@ int cachefiles_walk_to_object(struct cachefiles_object *parent,
515563
cachefiles_io_error(cache, "Create/mkdir failed");
516564
goto error;
517565

566+
mark_active_timed_out:
567+
_debug("mark active timed out");
568+
goto release_dentry;
569+
518570
check_error:
519571
_debug("check error %d", ret);
520572
write_lock(&cache->active_lock);
521573
rb_erase(&object->active_node, &cache->active_nodes);
522574
clear_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags);
523575
wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE);
524576
write_unlock(&cache->active_lock);
525-
577+
release_dentry:
526578
dput(object->dentry);
527579
object->dentry = NULL;
528580
goto error_out;
@@ -543,9 +595,6 @@ int cachefiles_walk_to_object(struct cachefiles_object *parent,
543595
error_out2:
544596
dput(dir);
545597
error_out:
546-
if (ret == -ENOSPC)
547-
ret = -ENOBUFS;
548-
549598
_leave(" = error %d", -ret);
550599
return ret;
551600
}

‎fs/fscache/internal.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ extern atomic_t fscache_n_object_no_alloc;
215215
extern atomic_t fscache_n_object_lookups;
216216
extern atomic_t fscache_n_object_lookups_negative;
217217
extern atomic_t fscache_n_object_lookups_positive;
218+
extern atomic_t fscache_n_object_lookups_timed_out;
218219
extern atomic_t fscache_n_object_created;
219220
extern atomic_t fscache_n_object_avail;
220221
extern atomic_t fscache_n_object_dead;

‎fs/fscache/object.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ static void fscache_lookup_object(struct fscache_object *object)
468468
{
469469
struct fscache_cookie *cookie = object->cookie;
470470
struct fscache_object *parent;
471+
int ret;
471472

472473
_enter("");
473474

@@ -493,12 +494,19 @@ static void fscache_lookup_object(struct fscache_object *object)
493494

494495
fscache_stat(&fscache_n_object_lookups);
495496
fscache_stat(&fscache_n_cop_lookup_object);
496-
object->cache->ops->lookup_object(object);
497+
ret = object->cache->ops->lookup_object(object);
497498
fscache_stat_d(&fscache_n_cop_lookup_object);
498499

499500
if (test_bit(FSCACHE_OBJECT_EV_ERROR, &object->events))
500501
set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
501502

503+
if (ret == -ETIMEDOUT) {
504+
/* probably stuck behind another object, so move this one to
505+
* the back of the queue */
506+
fscache_stat(&fscache_n_object_lookups_timed_out);
507+
set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
508+
}
509+
502510
_leave("");
503511
}
504512

‎fs/fscache/stats.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ atomic_t fscache_n_object_no_alloc;
9898
atomic_t fscache_n_object_lookups;
9999
atomic_t fscache_n_object_lookups_negative;
100100
atomic_t fscache_n_object_lookups_positive;
101+
atomic_t fscache_n_object_lookups_timed_out;
101102
atomic_t fscache_n_object_created;
102103
atomic_t fscache_n_object_avail;
103104
atomic_t fscache_n_object_dead;
@@ -160,10 +161,11 @@ static int fscache_stats_show(struct seq_file *m, void *v)
160161
atomic_read(&fscache_n_acquires_nobufs),
161162
atomic_read(&fscache_n_acquires_oom));
162163

163-
seq_printf(m, "Lookups: n=%u neg=%u pos=%u crt=%u\n",
164+
seq_printf(m, "Lookups: n=%u neg=%u pos=%u crt=%u tmo=%u\n",
164165
atomic_read(&fscache_n_object_lookups),
165166
atomic_read(&fscache_n_object_lookups_negative),
166167
atomic_read(&fscache_n_object_lookups_positive),
168+
atomic_read(&fscache_n_object_lookups_timed_out),
167169
atomic_read(&fscache_n_object_created));
168170

169171
seq_printf(m, "Updates: n=%u nul=%u run=%u\n",

‎include/linux/fscache-cache.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,10 @@ struct fscache_cache_ops {
234234
struct fscache_object *(*alloc_object)(struct fscache_cache *cache,
235235
struct fscache_cookie *cookie);
236236

237-
/* look up the object for a cookie */
238-
void (*lookup_object)(struct fscache_object *object);
237+
/* look up the object for a cookie
238+
* - return -ETIMEDOUT to be requeued
239+
*/
240+
int (*lookup_object)(struct fscache_object *object);
239241

240242
/* finished looking up */
241243
void (*lookup_complete)(struct fscache_object *object);

0 commit comments

Comments
 (0)