diff options
author | NeilBrown <neilb@suse.de> | 2014-07-07 01:16:04 -0400 |
---|---|---|
committer | Ingo Molnar <mingo@kernel.org> | 2014-07-16 09:10:41 -0400 |
commit | c1221321b7c25b53204447cff9949a6d5a7ddddc (patch) | |
tree | b4dc6613ab838dcd71023f5f018306574fc2985a | |
parent | 743162013d40ca612b4cb53d3a200dff2d9ab26e (diff) |
sched: Allow wait_on_bit_action() functions to support a timeout
It is currently not possible for various wait_on_bit functions
to implement a timeout.
While the "action" function that is called to do the waiting
could certainly use schedule_timeout(), there is no way to carry
forward the remaining timeout after a false wake-up.
As false-wakeups a clearly possible at least due to possible
hash collisions in bit_waitqueue(), this is a real problem.
The 'action' function is currently passed a pointer to the word
containing the bit being waited on. No current action functions
use this pointer. So changing it to something else will be a
little noisy but will have no immediate effect.
This patch changes the 'action' function to take a pointer to
the "struct wait_bit_key", which contains a pointer to the word
containing the bit so nothing is really lost.
It also adds a 'private' field to "struct wait_bit_key", which
is initialized to zero.
An action function can now implement a timeout with something
like
static int timed_out_waiter(struct wait_bit_key *key)
{
unsigned long waited;
if (key->private == 0) {
key->private = jiffies;
if (key->private == 0)
key->private -= 1;
}
waited = jiffies - key->private;
if (waited > 10 * HZ)
return -EAGAIN;
schedule_timeout(waited - 10 * HZ);
return 0;
}
If any other need for context in a waiter were found it would be
easy to use ->private for some other purpose, or even extend
"struct wait_bit_key".
My particular need is to support timeouts in nfs_release_page()
to avoid deadlocks with loopback mounted NFS.
While wait_on_bit_timeout() would be a cleaner interface, it
will not meet my need. I need the timeout to be sensitive to
the state of the connection with the server, which could change.
So I need to use an 'action' interface.
Signed-off-by: NeilBrown <neilb@suse.de>
Acked-by: Peter Zijlstra <peterz@infradead.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Steve French <sfrench@samba.org>
Cc: David Howells <dhowells@redhat.com>
Cc: Steven Whitehouse <swhiteho@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/20140707051604.28027.41257.stgit@notabene.brown
Signed-off-by: Ingo Molnar <mingo@kernel.org>
-rw-r--r-- | fs/cifs/inode.c | 2 | ||||
-rw-r--r-- | fs/nfs/inode.c | 2 | ||||
-rw-r--r-- | fs/nfs/internal.h | 2 | ||||
-rw-r--r-- | fs/nfs/pagelist.c | 2 | ||||
-rw-r--r-- | include/linux/sunrpc/sched.h | 2 | ||||
-rw-r--r-- | include/linux/wait.h | 18 | ||||
-rw-r--r-- | kernel/sched/wait.c | 16 | ||||
-rw-r--r-- | net/sunrpc/sched.c | 4 |
8 files changed, 25 insertions, 23 deletions
diff --git a/fs/cifs/inode.c b/fs/cifs/inode.c index 213c4580b4e3..41de3935caa0 100644 --- a/fs/cifs/inode.c +++ b/fs/cifs/inode.c | |||
@@ -1780,7 +1780,7 @@ cifs_invalidate_mapping(struct inode *inode) | |||
1780 | * @word: long word containing the bit lock | 1780 | * @word: long word containing the bit lock |
1781 | */ | 1781 | */ |
1782 | static int | 1782 | static int |
1783 | cifs_wait_bit_killable(void *word) | 1783 | cifs_wait_bit_killable(struct wait_bit_key *key) |
1784 | { | 1784 | { |
1785 | if (fatal_signal_pending(current)) | 1785 | if (fatal_signal_pending(current)) |
1786 | return -ERESTARTSYS; | 1786 | return -ERESTARTSYS; |
diff --git a/fs/nfs/inode.c b/fs/nfs/inode.c index b7b710e7d08e..abd37a380535 100644 --- a/fs/nfs/inode.c +++ b/fs/nfs/inode.c | |||
@@ -75,7 +75,7 @@ nfs_fattr_to_ino_t(struct nfs_fattr *fattr) | |||
75 | * nfs_wait_bit_killable - helper for functions that are sleeping on bit locks | 75 | * nfs_wait_bit_killable - helper for functions that are sleeping on bit locks |
76 | * @word: long word containing the bit lock | 76 | * @word: long word containing the bit lock |
77 | */ | 77 | */ |
78 | int nfs_wait_bit_killable(void *word) | 78 | int nfs_wait_bit_killable(struct wait_bit_key *key) |
79 | { | 79 | { |
80 | if (fatal_signal_pending(current)) | 80 | if (fatal_signal_pending(current)) |
81 | return -ERESTARTSYS; | 81 | return -ERESTARTSYS; |
diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h index 82ddbf46660e..e0193d63630c 100644 --- a/fs/nfs/internal.h +++ b/fs/nfs/internal.h | |||
@@ -347,7 +347,7 @@ extern int nfs_drop_inode(struct inode *); | |||
347 | extern void nfs_clear_inode(struct inode *); | 347 | extern void nfs_clear_inode(struct inode *); |
348 | extern void nfs_evict_inode(struct inode *); | 348 | extern void nfs_evict_inode(struct inode *); |
349 | void nfs_zap_acl_cache(struct inode *inode); | 349 | void nfs_zap_acl_cache(struct inode *inode); |
350 | extern int nfs_wait_bit_killable(void *word); | 350 | extern int nfs_wait_bit_killable(struct wait_bit_key *key); |
351 | 351 | ||
352 | /* super.c */ | 352 | /* super.c */ |
353 | extern const struct super_operations nfs_sops; | 353 | extern const struct super_operations nfs_sops; |
diff --git a/fs/nfs/pagelist.c b/fs/nfs/pagelist.c index 6104d3500b49..745a612dbe22 100644 --- a/fs/nfs/pagelist.c +++ b/fs/nfs/pagelist.c | |||
@@ -117,7 +117,7 @@ __nfs_iocounter_wait(struct nfs_io_counter *c) | |||
117 | set_bit(NFS_IO_INPROGRESS, &c->flags); | 117 | set_bit(NFS_IO_INPROGRESS, &c->flags); |
118 | if (atomic_read(&c->io_count) == 0) | 118 | if (atomic_read(&c->io_count) == 0) |
119 | break; | 119 | break; |
120 | ret = nfs_wait_bit_killable(&c->flags); | 120 | ret = nfs_wait_bit_killable(&q.key); |
121 | } while (atomic_read(&c->io_count) != 0); | 121 | } while (atomic_read(&c->io_count) != 0); |
122 | finish_wait(wq, &q.wait); | 122 | finish_wait(wq, &q.wait); |
123 | return ret; | 123 | return ret; |
diff --git a/include/linux/sunrpc/sched.h b/include/linux/sunrpc/sched.h index ad7dbe2cfecd..1a8959944c5f 100644 --- a/include/linux/sunrpc/sched.h +++ b/include/linux/sunrpc/sched.h | |||
@@ -236,7 +236,7 @@ void * rpc_malloc(struct rpc_task *, size_t); | |||
236 | void rpc_free(void *); | 236 | void rpc_free(void *); |
237 | int rpciod_up(void); | 237 | int rpciod_up(void); |
238 | void rpciod_down(void); | 238 | void rpciod_down(void); |
239 | int __rpc_wait_for_completion_task(struct rpc_task *task, int (*)(void *)); | 239 | int __rpc_wait_for_completion_task(struct rpc_task *task, wait_bit_action_f *); |
240 | #ifdef RPC_DEBUG | 240 | #ifdef RPC_DEBUG |
241 | struct net; | 241 | struct net; |
242 | void rpc_show_tasks(struct net *); | 242 | void rpc_show_tasks(struct net *); |
diff --git a/include/linux/wait.h b/include/linux/wait.h index 73960ff09e56..6fb1ba5f9b2f 100644 --- a/include/linux/wait.h +++ b/include/linux/wait.h | |||
@@ -25,6 +25,7 @@ struct wait_bit_key { | |||
25 | void *flags; | 25 | void *flags; |
26 | int bit_nr; | 26 | int bit_nr; |
27 | #define WAIT_ATOMIC_T_BIT_NR -1 | 27 | #define WAIT_ATOMIC_T_BIT_NR -1 |
28 | unsigned long private; | ||
28 | }; | 29 | }; |
29 | 30 | ||
30 | struct wait_bit_queue { | 31 | struct wait_bit_queue { |
@@ -141,18 +142,19 @@ __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old) | |||
141 | list_del(&old->task_list); | 142 | list_del(&old->task_list); |
142 | } | 143 | } |
143 | 144 | ||
145 | typedef int wait_bit_action_f(struct wait_bit_key *); | ||
144 | void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key); | 146 | void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key); |
145 | void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key); | 147 | void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key); |
146 | void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key); | 148 | void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key); |
147 | void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr); | 149 | void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr); |
148 | void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr); | 150 | void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr); |
149 | void __wake_up_bit(wait_queue_head_t *, void *, int); | 151 | void __wake_up_bit(wait_queue_head_t *, void *, int); |
150 | int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned); | 152 | int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned); |
151 | int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned); | 153 | int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned); |
152 | void wake_up_bit(void *, int); | 154 | void wake_up_bit(void *, int); |
153 | void wake_up_atomic_t(atomic_t *); | 155 | void wake_up_atomic_t(atomic_t *); |
154 | int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned); | 156 | int out_of_line_wait_on_bit(void *, int, wait_bit_action_f *, unsigned); |
155 | int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned); | 157 | int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned); |
156 | int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned); | 158 | int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned); |
157 | wait_queue_head_t *bit_waitqueue(void *, int); | 159 | wait_queue_head_t *bit_waitqueue(void *, int); |
158 | 160 | ||
@@ -855,8 +857,8 @@ int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key); | |||
855 | } while (0) | 857 | } while (0) |
856 | 858 | ||
857 | 859 | ||
858 | extern int bit_wait(void *); | 860 | extern int bit_wait(struct wait_bit_key *); |
859 | extern int bit_wait_io(void *); | 861 | extern int bit_wait_io(struct wait_bit_key *); |
860 | 862 | ||
861 | /** | 863 | /** |
862 | * wait_on_bit - wait for a bit to be cleared | 864 | * wait_on_bit - wait for a bit to be cleared |
@@ -925,7 +927,7 @@ wait_on_bit_io(void *word, int bit, unsigned mode) | |||
925 | * on that signal. | 927 | * on that signal. |
926 | */ | 928 | */ |
927 | static inline int | 929 | static inline int |
928 | wait_on_bit_action(void *word, int bit, int (*action)(void *), unsigned mode) | 930 | wait_on_bit_action(void *word, int bit, wait_bit_action_f *action, unsigned mode) |
929 | { | 931 | { |
930 | if (!test_bit(bit, word)) | 932 | if (!test_bit(bit, word)) |
931 | return 0; | 933 | return 0; |
@@ -1000,7 +1002,7 @@ wait_on_bit_lock_io(void *word, int bit, unsigned mode) | |||
1000 | * the @mode allows that signal to wake the process. | 1002 | * the @mode allows that signal to wake the process. |
1001 | */ | 1003 | */ |
1002 | static inline int | 1004 | static inline int |
1003 | wait_on_bit_lock_action(void *word, int bit, int (*action)(void *), unsigned mode) | 1005 | wait_on_bit_lock_action(void *word, int bit, wait_bit_action_f *action, unsigned mode) |
1004 | { | 1006 | { |
1005 | if (!test_and_set_bit(bit, word)) | 1007 | if (!test_and_set_bit(bit, word)) |
1006 | return 0; | 1008 | return 0; |
diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c index a104879e88f2..15cab1a4f84e 100644 --- a/kernel/sched/wait.c +++ b/kernel/sched/wait.c | |||
@@ -319,14 +319,14 @@ EXPORT_SYMBOL(wake_bit_function); | |||
319 | */ | 319 | */ |
320 | int __sched | 320 | int __sched |
321 | __wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q, | 321 | __wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q, |
322 | int (*action)(void *), unsigned mode) | 322 | wait_bit_action_f *action, unsigned mode) |
323 | { | 323 | { |
324 | int ret = 0; | 324 | int ret = 0; |
325 | 325 | ||
326 | do { | 326 | do { |
327 | prepare_to_wait(wq, &q->wait, mode); | 327 | prepare_to_wait(wq, &q->wait, mode); |
328 | if (test_bit(q->key.bit_nr, q->key.flags)) | 328 | if (test_bit(q->key.bit_nr, q->key.flags)) |
329 | ret = (*action)(q->key.flags); | 329 | ret = (*action)(&q->key); |
330 | } while (test_bit(q->key.bit_nr, q->key.flags) && !ret); | 330 | } while (test_bit(q->key.bit_nr, q->key.flags) && !ret); |
331 | finish_wait(wq, &q->wait); | 331 | finish_wait(wq, &q->wait); |
332 | return ret; | 332 | return ret; |
@@ -334,7 +334,7 @@ __wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q, | |||
334 | EXPORT_SYMBOL(__wait_on_bit); | 334 | EXPORT_SYMBOL(__wait_on_bit); |
335 | 335 | ||
336 | int __sched out_of_line_wait_on_bit(void *word, int bit, | 336 | int __sched out_of_line_wait_on_bit(void *word, int bit, |
337 | int (*action)(void *), unsigned mode) | 337 | wait_bit_action_f *action, unsigned mode) |
338 | { | 338 | { |
339 | wait_queue_head_t *wq = bit_waitqueue(word, bit); | 339 | wait_queue_head_t *wq = bit_waitqueue(word, bit); |
340 | DEFINE_WAIT_BIT(wait, word, bit); | 340 | DEFINE_WAIT_BIT(wait, word, bit); |
@@ -345,7 +345,7 @@ EXPORT_SYMBOL(out_of_line_wait_on_bit); | |||
345 | 345 | ||
346 | int __sched | 346 | int __sched |
347 | __wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q, | 347 | __wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q, |
348 | int (*action)(void *), unsigned mode) | 348 | wait_bit_action_f *action, unsigned mode) |
349 | { | 349 | { |
350 | do { | 350 | do { |
351 | int ret; | 351 | int ret; |
@@ -353,7 +353,7 @@ __wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q, | |||
353 | prepare_to_wait_exclusive(wq, &q->wait, mode); | 353 | prepare_to_wait_exclusive(wq, &q->wait, mode); |
354 | if (!test_bit(q->key.bit_nr, q->key.flags)) | 354 | if (!test_bit(q->key.bit_nr, q->key.flags)) |
355 | continue; | 355 | continue; |
356 | ret = action(q->key.flags); | 356 | ret = action(&q->key); |
357 | if (!ret) | 357 | if (!ret) |
358 | continue; | 358 | continue; |
359 | abort_exclusive_wait(wq, &q->wait, mode, &q->key); | 359 | abort_exclusive_wait(wq, &q->wait, mode, &q->key); |
@@ -365,7 +365,7 @@ __wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q, | |||
365 | EXPORT_SYMBOL(__wait_on_bit_lock); | 365 | EXPORT_SYMBOL(__wait_on_bit_lock); |
366 | 366 | ||
367 | int __sched out_of_line_wait_on_bit_lock(void *word, int bit, | 367 | int __sched out_of_line_wait_on_bit_lock(void *word, int bit, |
368 | int (*action)(void *), unsigned mode) | 368 | wait_bit_action_f *action, unsigned mode) |
369 | { | 369 | { |
370 | wait_queue_head_t *wq = bit_waitqueue(word, bit); | 370 | wait_queue_head_t *wq = bit_waitqueue(word, bit); |
371 | DEFINE_WAIT_BIT(wait, word, bit); | 371 | DEFINE_WAIT_BIT(wait, word, bit); |
@@ -503,7 +503,7 @@ void wake_up_atomic_t(atomic_t *p) | |||
503 | } | 503 | } |
504 | EXPORT_SYMBOL(wake_up_atomic_t); | 504 | EXPORT_SYMBOL(wake_up_atomic_t); |
505 | 505 | ||
506 | __sched int bit_wait(void *word) | 506 | __sched int bit_wait(struct wait_bit_key *word) |
507 | { | 507 | { |
508 | if (signal_pending_state(current->state, current)) | 508 | if (signal_pending_state(current->state, current)) |
509 | return 1; | 509 | return 1; |
@@ -512,7 +512,7 @@ __sched int bit_wait(void *word) | |||
512 | } | 512 | } |
513 | EXPORT_SYMBOL(bit_wait); | 513 | EXPORT_SYMBOL(bit_wait); |
514 | 514 | ||
515 | __sched int bit_wait_io(void *word) | 515 | __sched int bit_wait_io(struct wait_bit_key *word) |
516 | { | 516 | { |
517 | if (signal_pending_state(current->state, current)) | 517 | if (signal_pending_state(current->state, current)) |
518 | return 1; | 518 | return 1; |
diff --git a/net/sunrpc/sched.c b/net/sunrpc/sched.c index c0365c14b858..9358c79fd589 100644 --- a/net/sunrpc/sched.c +++ b/net/sunrpc/sched.c | |||
@@ -250,7 +250,7 @@ void rpc_destroy_wait_queue(struct rpc_wait_queue *queue) | |||
250 | } | 250 | } |
251 | EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue); | 251 | EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue); |
252 | 252 | ||
253 | static int rpc_wait_bit_killable(void *word) | 253 | static int rpc_wait_bit_killable(struct wait_bit_key *key) |
254 | { | 254 | { |
255 | if (fatal_signal_pending(current)) | 255 | if (fatal_signal_pending(current)) |
256 | return -ERESTARTSYS; | 256 | return -ERESTARTSYS; |
@@ -309,7 +309,7 @@ static int rpc_complete_task(struct rpc_task *task) | |||
309 | * to enforce taking of the wq->lock and hence avoid races with | 309 | * to enforce taking of the wq->lock and hence avoid races with |
310 | * rpc_complete_task(). | 310 | * rpc_complete_task(). |
311 | */ | 311 | */ |
312 | int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *)) | 312 | int __rpc_wait_for_completion_task(struct rpc_task *task, wait_bit_action_f *action) |
313 | { | 313 | { |
314 | if (action == NULL) | 314 | if (action == NULL) |
315 | action = rpc_wait_bit_killable; | 315 | action = rpc_wait_bit_killable; |