sunrpc/cache: remove races with queuing an upcall.
Subsystems (2)
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 197 lines · On 05 Mar 2013 16:07:39 +0100 Bodo Stroesser <[email protected]> wrote:
Hi,
the patch in my previous mail was a bit too optimized, in that it gets an cache_request* pointer from each entry on the
detail->queue, even if it really is acache_reader.As the resulting pointer is used as a cache_request* after checking
cache_queue->readeronly, this isn't a bug, but it is unclean.Thus I changed the patch below once again to keep the sane handling as it has been implemented.
Bodo
Quoted 47 lines · On 04 Mar 2013 07:10:00 +0100 NeilBrown <[email protected]> wrote:
We currently queue an upcall after setting
CACHE_PENDING, and dequeue after clearingCACHE_PENDING.
So a request should only be present whenCACHE_PENDINGis set.However we don't combine the test and the enqueue/dequeue in a protected region, so it is possible (if unlikely) for a race to result in a request being queued without
CACHE_PENDINGset, or a request to be absent despiteCACHE_PENDING.So: include a test for
CACHE_PENDINGinside the regions of enqueue and dequeue wherequeue_lockis held, and abort the operation if the value is not as expected.Also remove the 'return' from
cache_dequeue()to ensure it always removes all entries: As there is no locking between settingCACHE_PENDINGand callingsunrpc_cache_pipe_upcallit is not inconceivable for some other thread to clearCACHE_PENDINGand then someone else to set it can callsunrpc_cache_pipe_upcall, both before the original thread completed the call.With this, it perfectly safe and correct to:
- callcache_dequeue()if and only if we have just clearedCACHE_PENDING
- callsunrpc_cache_pipe_upcall()(viacache_make_upcall) if and only if we have just setCACHE_PENDING.Reported-by Bodo Stroesser <[email protected]>Signed-off-by NeilBrown <[email protected]>--- net/sunrpc/cache.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)net/sunrpc/cache.c +3
-0@@ -1022,6 +1022,9 @@ static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch) struct cache_request *cr = container_of(cq, struct cache_request, q);if (cr->item != ch)continue ; +if (test_bit (CACHE_PENDING , &ch->flags)) +/* Lost a race and it is pending again */ +break ;if (cr->readers !=0 )continue ;list_del (&cr->q.list);net/sunrpc/cache.c static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch) · line 1032
Quoted 4 lines
net/sunrpc/cache.c +0
-1@@ -1029,7 +1032,6 @@ static void cache_dequeue(struct cache_detail *detail, struct cache_head *ch)cache_put (cr->item, detail);kfree (cr->buf);kfree (cr); -return ;Just removing "return" is not enough. If the loop no longer stops after dequeueing the first entry found, some other changes are necessary also:
1) use
list_for_each_entry_safe()instead oflist_for_each_entry()2) don't call
spin_unlock()in the loop.Thus, at the end of this mail I added a revised patch. With this patch
cache_dequeue()no longer frees the requests in the loop, but moves them to a temporary queue.
After the loop it callsspin_unlock()and does thekfree()andcache_put()in a second loop.The patch is not tested on a mainline kernel. Instead, I ported it to SLES11 SP1 2.6.32.59-0.7.1. On SLES 11 the test is still running fine.
Best Regards,
BodoQuoted 33 lines
net/sunrpc/cache.c +11
-2}spin_unlock (&queue_lock); }@@ -1151,6 +1153,7 @@ int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h,struct cache_request *crq;char *bp;int len; +int ret =0 ;if (!cache_listeners_exist (detail)) {warn_no_listener (detail);@@ -1182,10 +1185,18 @@ int sunrpc_cache_pipe_upcall(struct cache_detail *detail, struct cache_head *h, crq->len =PAGE_SIZE - len; crq->readers =0 ;spin_lock (&queue_lock); -list_add_tail (&crq->q.list, &detail->queue); +if (test_bit (CACHE_PENDING , &h->flags)) +list_add_tail (&crq->q.list, &detail->queue); +else +/* Lost a race, no longer PENDING, so don't enqueue */ + ret = -EAGAIN ;spin_unlock (&queue_lock);wake_up (&queue_wait); -return 0 ; +if (ret == -EAGAIN ) { +kfree (buf); +kfree (crq); + } +return ret; }EXPORT_SYMBOL_GPL (sunrpc_cache_pipe_upcall);.....
Reported-by Bodo Stroesser <[email protected]>Signed-off-by NeilBrown <[email protected]>Signed-off-by Bodo Stroesser <[email protected]>---
net/sunrpc/cache.c +25
-10@@ -1015,23 +1015,33 @@static void cache_dequeue (struct cache_detail *detail,struct cache_head *ch) { -struct cache_queue *cq; +struct cache_queue *cq, *tmp; +struct cache_request *cr; +struct list_head dequeued; + +INIT_LIST_HEAD (&dequeued);spin_lock (&queue_lock); -list_for_each_entry (cq, &detail->queue, list) +list_for_each_entry_safe (cq, tmp, &detail->queue, list)if (!cq->reader) { - struct cache_request *cr = container_of(cq, struct cache_request, q); + cr = container_of(cq, struct cache_request, q);if (cr->item != ch)continue ; +if (test_bit (CACHE_PENDING , &ch->flags)) +/* Lost a race and it is pending again */ +break ;if (cr->readers !=0 )continue ; -list_del (&cr->q.list); -spin_unlock (&queue_lock); -cache_put (cr->item, detail); -kfree (cr->buf); -kfree (cr); -return ; +list_move (&cr->q.list, &dequeued); }spin_unlock (&queue_lock); + +while (!list_empty (&dequeued)) { + cr = list_entry(dequeued.next, struct cache_request, q.list); +list_del (&cr->q.list); +cache_put (cr->item, detail); +kfree (cr->buf); +kfree (cr); + } }/* @@ -1151,6 +1161,7 @@struct cache_request *crq;char *bp;int len; +int ret =0 ;if (!cache_listeners_exist (detail)) {warn_no_listener (detail);@@ -1182,10 +1193,18 @@ crq->len =PAGE_SIZE - len; crq->readers =0 ;spin_lock (&queue_lock); -list_add_tail (&crq->q.list, &detail->queue); +if (test_bit (CACHE_PENDING , &h->flags)) +list_add_tail (&crq->q.list, &detail->queue); +else +/* Lost a race, no longer PENDING, so don't enqueue */ + ret = -EAGAIN ;spin_unlock (&queue_lock);
net/sunrpc/cache.c line 1210
Quoted 10 lines
net/sunrpc/cache.c +5
-1wake_up (&queue_wait); -return 0 ; +if (ret == -EAGAIN ) { +kfree (buf); +kfree (crq); + } +return ret; }EXPORT_SYMBOL_GPL (sunrpc_cache_pipe_upcall);
Thanks for this - that was careless of me. sorry.
Your patch looks good. I'll forward the two to Bruce again shortly, then look at submitting a backport for SLES.
Thanks,
NeilBrown