Skip to content

Commit d3b97ca

Browse files
committed
FS-Cache: The operation cancellation method needs calling in more places
Any time an incomplete operation is cancelled, the operation cancellation function needs to be called to clean up. This is currently being passed directly to some of the functions that might want to call it, but not all. Instead, pass the cancellation method pointer to the fscache_operation_init() and have that cache it in the operation struct. Further, plug in a dummy cancellation handler if the caller declines to set one as this allows us to call the function unconditionally (the extra overhead isn't worth bothering about as we don't expect to be calling this typically). The cancellation method must thence be called everywhere the CANCELLED state is set. Note that we call it *before* setting the CANCELLED state such that the method can use the old state value to guide its operation. fscache_do_cancel_retrieval() needs moving higher up in the sources so that the init function can use it now. Without this, the following oops may be seen: FS-Cache: Assertion failed FS-Cache: 3 == 0 is false ------------[ cut here ]------------ kernel BUG at ../fs/fscache/page.c:261! ... RIP: 0010:[<ffffffffa0089c1b>] fscache_release_retrieval_op+0x77/0x100 [<ffffffffa008853d>] fscache_put_operation+0x114/0x2da [<ffffffffa008b8c2>] __fscache_read_or_alloc_pages+0x358/0x3b3 [<ffffffffa00b761f>] __nfs_readpages_from_fscache+0x59/0xbf [nfs] [<ffffffffa00b06c5>] nfs_readpages+0x10c/0x185 [nfs] [<ffffffff81124925>] ? alloc_pages_current+0x119/0x13e [<ffffffff810ee5fd>] ? __page_cache_alloc+0xfb/0x10a [<ffffffff810f87f8>] __do_page_cache_readahead+0x188/0x22c [<ffffffff810f8b3a>] ondemand_readahead+0x29e/0x2af [<ffffffff810f8c92>] page_cache_sync_readahead+0x38/0x3a [<ffffffff810ef337>] generic_file_read_iter+0x1a2/0x55a [<ffffffffa00a9dff>] ? nfs_revalidate_mapping+0xd6/0x288 [nfs] [<ffffffffa00a6a23>] nfs_file_read+0x49/0x70 [nfs] [<ffffffff811363be>] new_sync_read+0x78/0x9c [<ffffffff81137164>] __vfs_read+0x13/0x38 [<ffffffff8113721e>] vfs_read+0x95/0x121 [<ffffffff811372f6>] SyS_read+0x4c/0x8a [<ffffffff81557a52>] system_call_fastpath+0x12/0x17 The assertion is showing that the remaining number of pages (n_pages) is not 0 when the operation is being released. Signed-off-by: David Howells <dhowells@redhat.com> Reviewed-by: Steve Dickson <steved@redhat.com> Acked-by: Jeff Layton <jeff.layton@primarydata.com>
1 parent a39caad commit d3b97ca

6 files changed

Lines changed: 57 additions & 40 deletions

File tree

‎fs/fscache/cookie.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ int __fscache_check_consistency(struct fscache_cookie *cookie)
672672
if (!op)
673673
return -ENOMEM;
674674

675-
fscache_operation_init(op, NULL, NULL);
675+
fscache_operation_init(op, NULL, NULL, NULL);
676676
op->flags = FSCACHE_OP_MYTHREAD |
677677
(1 << FSCACHE_OP_WAITING) |
678678
(1 << FSCACHE_OP_UNUSE_COOKIE);
@@ -696,8 +696,7 @@ int __fscache_check_consistency(struct fscache_cookie *cookie)
696696
/* the work queue now carries its own ref on the object */
697697
spin_unlock(&cookie->lock);
698698

699-
ret = fscache_wait_for_operation_activation(object, op,
700-
NULL, NULL, NULL);
699+
ret = fscache_wait_for_operation_activation(object, op, NULL, NULL);
701700
if (ret == 0) {
702701
/* ask the cache to honour the operation */
703702
ret = object->cache->ops->check_consistency(op);

‎fs/fscache/internal.h‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ extern int fscache_submit_exclusive_op(struct fscache_object *,
124124
struct fscache_operation *);
125125
extern int fscache_submit_op(struct fscache_object *,
126126
struct fscache_operation *);
127-
extern int fscache_cancel_op(struct fscache_operation *,
128-
void (*)(struct fscache_operation *),
129-
bool);
127+
extern int fscache_cancel_op(struct fscache_operation *, bool);
130128
extern void fscache_cancel_all_ops(struct fscache_object *);
131129
extern void fscache_abort_object(struct fscache_object *);
132130
extern void fscache_start_operations(struct fscache_object *);
@@ -139,8 +137,7 @@ extern int fscache_wait_for_deferred_lookup(struct fscache_cookie *);
139137
extern int fscache_wait_for_operation_activation(struct fscache_object *,
140138
struct fscache_operation *,
141139
atomic_t *,
142-
atomic_t *,
143-
void (*)(struct fscache_operation *));
140+
atomic_t *);
144141
extern void fscache_invalidate_writes(struct fscache_cookie *);
145142

146143
/*

‎fs/fscache/object.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,8 @@ static const struct fscache_state *_fscache_invalidate_object(struct fscache_obj
961961
if (!op)
962962
goto nomem;
963963

964-
fscache_operation_init(op, object->cache->ops->invalidate_object, NULL);
964+
fscache_operation_init(op, object->cache->ops->invalidate_object,
965+
NULL, NULL);
965966
op->flags = FSCACHE_OP_ASYNC |
966967
(1 << FSCACHE_OP_EXCLUSIVE) |
967968
(1 << FSCACHE_OP_UNUSE_COOKIE);

‎fs/fscache/operation.c‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
atomic_t fscache_op_debug_id;
2121
EXPORT_SYMBOL(fscache_op_debug_id);
2222

23+
static void fscache_operation_dummy_cancel(struct fscache_operation *op)
24+
{
25+
}
26+
2327
/**
2428
* fscache_operation_init - Do basic initialisation of an operation
2529
* @op: The operation to initialise
@@ -30,13 +34,15 @@ EXPORT_SYMBOL(fscache_op_debug_id);
3034
*/
3135
void fscache_operation_init(struct fscache_operation *op,
3236
fscache_operation_processor_t processor,
37+
fscache_operation_cancel_t cancel,
3338
fscache_operation_release_t release)
3439
{
3540
INIT_WORK(&op->work, fscache_op_work_func);
3641
atomic_set(&op->usage, 1);
3742
op->state = FSCACHE_OP_ST_INITIALISED;
3843
op->debug_id = atomic_inc_return(&fscache_op_debug_id);
3944
op->processor = processor;
45+
op->cancel = cancel ?: fscache_operation_dummy_cancel;
4046
op->release = release;
4147
INIT_LIST_HEAD(&op->pend_link);
4248
fscache_stat(&fscache_n_op_initialised);
@@ -164,9 +170,11 @@ int fscache_submit_exclusive_op(struct fscache_object *object,
164170
flags = READ_ONCE(object->flags);
165171
if (unlikely(!(flags & BIT(FSCACHE_OBJECT_IS_LIVE)))) {
166172
fscache_stat(&fscache_n_op_rejected);
173+
op->cancel(op);
167174
op->state = FSCACHE_OP_ST_CANCELLED;
168175
ret = -ENOBUFS;
169176
} else if (unlikely(fscache_cache_is_broken(object))) {
177+
op->cancel(op);
170178
op->state = FSCACHE_OP_ST_CANCELLED;
171179
ret = -EIO;
172180
} else if (flags & BIT(FSCACHE_OBJECT_IS_AVAILABLE)) {
@@ -200,10 +208,12 @@ int fscache_submit_exclusive_op(struct fscache_object *object,
200208
fscache_stat(&fscache_n_op_pend);
201209
ret = 0;
202210
} else if (flags & BIT(FSCACHE_OBJECT_KILLED_BY_CACHE)) {
211+
op->cancel(op);
203212
op->state = FSCACHE_OP_ST_CANCELLED;
204213
ret = -ENOBUFS;
205214
} else {
206215
fscache_report_unexpected_submission(object, op, ostate);
216+
op->cancel(op);
207217
op->state = FSCACHE_OP_ST_CANCELLED;
208218
ret = -ENOBUFS;
209219
}
@@ -245,9 +255,11 @@ int fscache_submit_op(struct fscache_object *object,
245255
flags = READ_ONCE(object->flags);
246256
if (unlikely(!(flags & BIT(FSCACHE_OBJECT_IS_LIVE)))) {
247257
fscache_stat(&fscache_n_op_rejected);
258+
op->cancel(op);
248259
op->state = FSCACHE_OP_ST_CANCELLED;
249260
ret = -ENOBUFS;
250261
} else if (unlikely(fscache_cache_is_broken(object))) {
262+
op->cancel(op);
251263
op->state = FSCACHE_OP_ST_CANCELLED;
252264
ret = -EIO;
253265
} else if (flags & BIT(FSCACHE_OBJECT_IS_AVAILABLE)) {
@@ -276,11 +288,13 @@ int fscache_submit_op(struct fscache_object *object,
276288
fscache_stat(&fscache_n_op_pend);
277289
ret = 0;
278290
} else if (flags & BIT(FSCACHE_OBJECT_KILLED_BY_CACHE)) {
291+
op->cancel(op);
279292
op->state = FSCACHE_OP_ST_CANCELLED;
280293
ret = -ENOBUFS;
281294
} else {
282295
fscache_report_unexpected_submission(object, op, ostate);
283296
ASSERT(!fscache_object_is_active(object));
297+
op->cancel(op);
284298
op->state = FSCACHE_OP_ST_CANCELLED;
285299
ret = -ENOBUFS;
286300
}
@@ -335,7 +349,6 @@ void fscache_start_operations(struct fscache_object *object)
335349
* cancel an operation that's pending on an object
336350
*/
337351
int fscache_cancel_op(struct fscache_operation *op,
338-
void (*do_cancel)(struct fscache_operation *),
339352
bool cancel_in_progress_op)
340353
{
341354
struct fscache_object *object = op->object;
@@ -355,9 +368,9 @@ int fscache_cancel_op(struct fscache_operation *op,
355368
ASSERT(!list_empty(&op->pend_link));
356369
list_del_init(&op->pend_link);
357370
put = true;
371+
358372
fscache_stat(&fscache_n_op_cancelled);
359-
if (do_cancel)
360-
do_cancel(op);
373+
op->cancel(op);
361374
op->state = FSCACHE_OP_ST_CANCELLED;
362375
if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
363376
object->n_exclusive--;
@@ -373,8 +386,7 @@ int fscache_cancel_op(struct fscache_operation *op,
373386
fscache_start_operations(object);
374387

375388
fscache_stat(&fscache_n_op_cancelled);
376-
if (do_cancel)
377-
do_cancel(op);
389+
op->cancel(op);
378390
op->state = FSCACHE_OP_ST_CANCELLED;
379391
if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
380392
object->n_exclusive--;
@@ -408,6 +420,7 @@ void fscache_cancel_all_ops(struct fscache_object *object)
408420
list_del_init(&op->pend_link);
409421

410422
ASSERTCMP(op->state, ==, FSCACHE_OP_ST_PENDING);
423+
op->cancel(op);
411424
op->state = FSCACHE_OP_ST_CANCELLED;
412425

413426
if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
@@ -440,8 +453,12 @@ void fscache_op_complete(struct fscache_operation *op, bool cancelled)
440453

441454
spin_lock(&object->lock);
442455

443-
op->state = cancelled ?
444-
FSCACHE_OP_ST_CANCELLED : FSCACHE_OP_ST_COMPLETE;
456+
if (!cancelled) {
457+
op->state = FSCACHE_OP_ST_COMPLETE;
458+
} else {
459+
op->cancel(op);
460+
op->state = FSCACHE_OP_ST_CANCELLED;
461+
}
445462

446463
if (test_bit(FSCACHE_OP_EXCLUSIVE, &op->flags))
447464
object->n_exclusive--;

‎fs/fscache/page.c‎

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ int __fscache_attr_changed(struct fscache_cookie *cookie)
213213
return -ENOMEM;
214214
}
215215

216-
fscache_operation_init(op, fscache_attr_changed_op, NULL);
216+
fscache_operation_init(op, fscache_attr_changed_op, NULL, NULL);
217217
op->flags = FSCACHE_OP_ASYNC |
218218
(1 << FSCACHE_OP_EXCLUSIVE) |
219219
(1 << FSCACHE_OP_UNUSE_COOKIE);
@@ -248,6 +248,17 @@ int __fscache_attr_changed(struct fscache_cookie *cookie)
248248
}
249249
EXPORT_SYMBOL(__fscache_attr_changed);
250250

251+
/*
252+
* Handle cancellation of a pending retrieval op
253+
*/
254+
static void fscache_do_cancel_retrieval(struct fscache_operation *_op)
255+
{
256+
struct fscache_retrieval *op =
257+
container_of(_op, struct fscache_retrieval, op);
258+
259+
atomic_set(&op->n_pages, 0);
260+
}
261+
251262
/*
252263
* release a retrieval op reference
253264
*/
@@ -285,7 +296,9 @@ static struct fscache_retrieval *fscache_alloc_retrieval(
285296
return NULL;
286297
}
287298

288-
fscache_operation_init(&op->op, NULL, fscache_release_retrieval_op);
299+
fscache_operation_init(&op->op, NULL,
300+
fscache_do_cancel_retrieval,
301+
fscache_release_retrieval_op);
289302
op->op.flags = FSCACHE_OP_MYTHREAD |
290303
(1UL << FSCACHE_OP_WAITING) |
291304
(1UL << FSCACHE_OP_UNUSE_COOKIE);
@@ -329,25 +342,13 @@ int fscache_wait_for_deferred_lookup(struct fscache_cookie *cookie)
329342
return 0;
330343
}
331344

332-
/*
333-
* Handle cancellation of a pending retrieval op
334-
*/
335-
static void fscache_do_cancel_retrieval(struct fscache_operation *_op)
336-
{
337-
struct fscache_retrieval *op =
338-
container_of(_op, struct fscache_retrieval, op);
339-
340-
atomic_set(&op->n_pages, 0);
341-
}
342-
343345
/*
344346
* wait for an object to become active (or dead)
345347
*/
346348
int fscache_wait_for_operation_activation(struct fscache_object *object,
347349
struct fscache_operation *op,
348350
atomic_t *stat_op_waits,
349-
atomic_t *stat_object_dead,
350-
void (*do_cancel)(struct fscache_operation *))
351+
atomic_t *stat_object_dead)
351352
{
352353
int ret;
353354

@@ -359,7 +360,7 @@ int fscache_wait_for_operation_activation(struct fscache_object *object,
359360
fscache_stat(stat_op_waits);
360361
if (wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
361362
TASK_INTERRUPTIBLE) != 0) {
362-
ret = fscache_cancel_op(op, do_cancel, false);
363+
ret = fscache_cancel_op(op, false);
363364
if (ret == 0)
364365
return -ERESTARTSYS;
365366

@@ -380,7 +381,7 @@ int fscache_wait_for_operation_activation(struct fscache_object *object,
380381
if (unlikely(fscache_object_is_dying(object) ||
381382
fscache_cache_is_broken(object))) {
382383
enum fscache_operation_state state = op->state;
383-
fscache_cancel_op(op, do_cancel, true);
384+
fscache_cancel_op(op, true);
384385
if (stat_object_dead)
385386
fscache_stat(stat_object_dead);
386387
_leave(" = -ENOBUFS [obj dead %d]", state);
@@ -464,8 +465,7 @@ int __fscache_read_or_alloc_page(struct fscache_cookie *cookie,
464465
ret = fscache_wait_for_operation_activation(
465466
object, &op->op,
466467
__fscache_stat(&fscache_n_retrieval_op_waits),
467-
__fscache_stat(&fscache_n_retrievals_object_dead),
468-
fscache_do_cancel_retrieval);
468+
__fscache_stat(&fscache_n_retrievals_object_dead));
469469
if (ret < 0)
470470
goto error;
471471

@@ -595,8 +595,7 @@ int __fscache_read_or_alloc_pages(struct fscache_cookie *cookie,
595595
ret = fscache_wait_for_operation_activation(
596596
object, &op->op,
597597
__fscache_stat(&fscache_n_retrieval_op_waits),
598-
__fscache_stat(&fscache_n_retrievals_object_dead),
599-
fscache_do_cancel_retrieval);
598+
__fscache_stat(&fscache_n_retrievals_object_dead));
600599
if (ret < 0)
601600
goto error;
602601

@@ -702,8 +701,7 @@ int __fscache_alloc_page(struct fscache_cookie *cookie,
702701
ret = fscache_wait_for_operation_activation(
703702
object, &op->op,
704703
__fscache_stat(&fscache_n_alloc_op_waits),
705-
__fscache_stat(&fscache_n_allocs_object_dead),
706-
fscache_do_cancel_retrieval);
704+
__fscache_stat(&fscache_n_allocs_object_dead));
707705
if (ret < 0)
708706
goto error;
709707

@@ -946,7 +944,7 @@ int __fscache_write_page(struct fscache_cookie *cookie,
946944
if (!op)
947945
goto nomem;
948946

949-
fscache_operation_init(&op->op, fscache_write_op,
947+
fscache_operation_init(&op->op, fscache_write_op, NULL,
950948
fscache_release_write_op);
951949
op->op.flags = FSCACHE_OP_ASYNC |
952950
(1 << FSCACHE_OP_WAITING) |

‎include/linux/fscache-cache.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ extern wait_queue_head_t fscache_cache_cleared_wq;
7474
*/
7575
typedef void (*fscache_operation_release_t)(struct fscache_operation *op);
7676
typedef void (*fscache_operation_processor_t)(struct fscache_operation *op);
77+
typedef void (*fscache_operation_cancel_t)(struct fscache_operation *op);
7778

7879
enum fscache_operation_state {
7980
FSCACHE_OP_ST_BLANK, /* Op is not yet submitted */
@@ -109,6 +110,9 @@ struct fscache_operation {
109110
* the op in a non-pool thread */
110111
fscache_operation_processor_t processor;
111112

113+
/* Operation cancellation cleanup (optional) */
114+
fscache_operation_cancel_t cancel;
115+
112116
/* operation releaser */
113117
fscache_operation_release_t release;
114118
};
@@ -121,6 +125,7 @@ extern void fscache_op_complete(struct fscache_operation *, bool);
121125
extern void fscache_put_operation(struct fscache_operation *);
122126
extern void fscache_operation_init(struct fscache_operation *,
123127
fscache_operation_processor_t,
128+
fscache_operation_cancel_t,
124129
fscache_operation_release_t);
125130

126131
/*

0 commit comments

Comments
 (0)