rxrpc: Delay terminal ACK transmission on a client call
[linux-block.git] / net / rxrpc / call_object.c
CommitLineData
17926a79
DH
1/* RxRPC individual remote procedure call handling
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
9b6d5398
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
5a0e3ad6 14#include <linux/slab.h>
17926a79
DH
15#include <linux/module.h>
16#include <linux/circ_buf.h>
7727640c 17#include <linux/spinlock_types.h>
17926a79
DH
18#include <net/sock.h>
19#include <net/af_rxrpc.h>
20#include "ar-internal.h"
21
5b8848d1 22const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = {
f5c17aae 23 [RXRPC_CALL_UNINITIALISED] = "Uninit ",
999b69f8 24 [RXRPC_CALL_CLIENT_AWAIT_CONN] = "ClWtConn",
1f8481d1
DH
25 [RXRPC_CALL_CLIENT_SEND_REQUEST] = "ClSndReq",
26 [RXRPC_CALL_CLIENT_AWAIT_REPLY] = "ClAwtRpl",
27 [RXRPC_CALL_CLIENT_RECV_REPLY] = "ClRcvRpl",
00e90712 28 [RXRPC_CALL_SERVER_PREALLOC] = "SvPrealc",
1f8481d1
DH
29 [RXRPC_CALL_SERVER_SECURING] = "SvSecure",
30 [RXRPC_CALL_SERVER_ACCEPTING] = "SvAccept",
31 [RXRPC_CALL_SERVER_RECV_REQUEST] = "SvRcvReq",
32 [RXRPC_CALL_SERVER_ACK_REQUEST] = "SvAckReq",
33 [RXRPC_CALL_SERVER_SEND_REPLY] = "SvSndRpl",
34 [RXRPC_CALL_SERVER_AWAIT_ACK] = "SvAwtACK",
35 [RXRPC_CALL_COMPLETE] = "Complete",
f5c17aae
DH
36};
37
38const char *const rxrpc_call_completions[NR__RXRPC_CALL_COMPLETIONS] = {
39 [RXRPC_CALL_SUCCEEDED] = "Complete",
1f8481d1
DH
40 [RXRPC_CALL_REMOTELY_ABORTED] = "RmtAbort",
41 [RXRPC_CALL_LOCALLY_ABORTED] = "LocAbort",
f5c17aae 42 [RXRPC_CALL_LOCAL_ERROR] = "LocError",
1f8481d1 43 [RXRPC_CALL_NETWORK_ERROR] = "NetError",
1f8481d1
DH
44};
45
17926a79 46struct kmem_cache *rxrpc_call_jar;
17926a79 47
248f219c
DH
48static void rxrpc_call_timer_expired(unsigned long _call)
49{
50 struct rxrpc_call *call = (struct rxrpc_call *)_call;
51
52 _enter("%d", call->debug_id);
53
405dea1d
DH
54 if (call->state < RXRPC_CALL_COMPLETE)
55 rxrpc_set_timer(call, rxrpc_timer_expired, ktime_get_real());
248f219c 56}
17926a79 57
9faaff59
DH
58static struct lock_class_key rxrpc_call_user_mutex_lock_class_key;
59
2341e077
DH
60/*
61 * find an extant server call
62 * - called in process context with IRQs enabled
63 */
64struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx,
65 unsigned long user_call_ID)
66{
67 struct rxrpc_call *call;
68 struct rb_node *p;
69
70 _enter("%p,%lx", rx, user_call_ID);
71
72 read_lock(&rx->call_lock);
73
74 p = rx->calls.rb_node;
75 while (p) {
76 call = rb_entry(p, struct rxrpc_call, sock_node);
77
78 if (user_call_ID < call->user_call_ID)
79 p = p->rb_left;
80 else if (user_call_ID > call->user_call_ID)
81 p = p->rb_right;
82 else
83 goto found_extant_call;
84 }
85
86 read_unlock(&rx->call_lock);
87 _leave(" = NULL");
88 return NULL;
89
90found_extant_call:
fff72429 91 rxrpc_get_call(call, rxrpc_call_got);
2341e077
DH
92 read_unlock(&rx->call_lock);
93 _leave(" = %p [%d]", call, atomic_read(&call->usage));
94 return call;
95}
96
17926a79
DH
97/*
98 * allocate a new call
99 */
9faaff59 100struct rxrpc_call *rxrpc_alloc_call(struct rxrpc_sock *rx, gfp_t gfp)
17926a79
DH
101{
102 struct rxrpc_call *call;
103
104 call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
105 if (!call)
106 return NULL;
107
248f219c
DH
108 call->rxtx_buffer = kcalloc(RXRPC_RXTX_BUFF_SIZE,
109 sizeof(struct sk_buff *),
17926a79 110 gfp);
248f219c
DH
111 if (!call->rxtx_buffer)
112 goto nomem;
17926a79 113
248f219c
DH
114 call->rxtx_annotations = kcalloc(RXRPC_RXTX_BUFF_SIZE, sizeof(u8), gfp);
115 if (!call->rxtx_annotations)
116 goto nomem_2;
117
540b1c48 118 mutex_init(&call->user_mutex);
9faaff59
DH
119
120 /* Prevent lockdep reporting a deadlock false positive between the afs
121 * filesystem and sys_sendmsg() via the mmap sem.
122 */
123 if (rx->sk.sk_kern_sock)
124 lockdep_set_class(&call->user_mutex,
125 &rxrpc_call_user_mutex_lock_class_key);
126
248f219c
DH
127 setup_timer(&call->timer, rxrpc_call_timer_expired,
128 (unsigned long)call);
17926a79 129 INIT_WORK(&call->processor, &rxrpc_process_call);
999b69f8 130 INIT_LIST_HEAD(&call->link);
45025bce 131 INIT_LIST_HEAD(&call->chan_wait_link);
17926a79 132 INIT_LIST_HEAD(&call->accept_link);
248f219c
DH
133 INIT_LIST_HEAD(&call->recvmsg_link);
134 INIT_LIST_HEAD(&call->sock_link);
45025bce 135 init_waitqueue_head(&call->waitq);
17926a79 136 spin_lock_init(&call->lock);
20acbd9a 137 spin_lock_init(&call->notify_lock);
17926a79
DH
138 rwlock_init(&call->state_lock);
139 atomic_set(&call->usage, 1);
140 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
e754eba6 141 call->tx_total_len = -1;
17926a79
DH
142
143 memset(&call->sock_node, 0xed, sizeof(call->sock_node));
144
248f219c 145 /* Leave space in the ring to handle a maxed-out jumbo packet */
75e42126 146 call->rx_winsize = rxrpc_rx_window_size;
248f219c
DH
147 call->tx_winsize = 16;
148 call->rx_expect_next = 1;
57494343 149
f7aec129 150 call->cong_cwnd = 2;
57494343 151 call->cong_ssthresh = RXRPC_RXTX_BUFF_SIZE - 1;
17926a79 152 return call;
248f219c
DH
153
154nomem_2:
155 kfree(call->rxtx_buffer);
156nomem:
157 kmem_cache_free(rxrpc_call_jar, call);
158 return NULL;
17926a79
DH
159}
160
161/*
999b69f8 162 * Allocate a new client call.
17926a79 163 */
9faaff59
DH
164static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
165 struct sockaddr_rxrpc *srx,
aa390bbe 166 gfp_t gfp)
17926a79
DH
167{
168 struct rxrpc_call *call;
57494343 169 ktime_t now;
17926a79
DH
170
171 _enter("");
172
9faaff59 173 call = rxrpc_alloc_call(rx, gfp);
17926a79
DH
174 if (!call)
175 return ERR_PTR(-ENOMEM);
999b69f8 176 call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
999b69f8 177 call->service_id = srx->srx_service;
71f3ca40 178 call->tx_phase = true;
57494343
DH
179 now = ktime_get_real();
180 call->acks_latest_ts = now;
181 call->cong_tstamp = now;
999b69f8
DH
182
183 _leave(" = %p", call);
184 return call;
185}
186
187/*
248f219c 188 * Initiate the call ack/resend/expiry timer.
999b69f8 189 */
248f219c 190static void rxrpc_start_call_timer(struct rxrpc_call *call)
999b69f8 191{
df0adc78 192 ktime_t now = ktime_get_real(), expire_at;
248f219c 193
df0adc78 194 expire_at = ktime_add_ms(now, rxrpc_max_call_lifetime);
248f219c
DH
195 call->expire_at = expire_at;
196 call->ack_at = expire_at;
a5af7e1f 197 call->ping_at = expire_at;
248f219c 198 call->resend_at = expire_at;
df0adc78
DH
199 call->timer.expires = jiffies + LONG_MAX / 2;
200 rxrpc_set_timer(call, rxrpc_timer_begin, now);
17926a79
DH
201}
202
203/*
540b1c48
DH
204 * Set up a call for the given parameters.
205 * - Called with the socket lock held, which it must release.
206 * - If it returns a call, the call's lock will need releasing by the caller.
17926a79 207 */
2341e077 208struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
19ffa01c 209 struct rxrpc_conn_parameters *cp,
999b69f8 210 struct sockaddr_rxrpc *srx,
17926a79 211 unsigned long user_call_ID,
e754eba6 212 s64 tx_total_len,
17926a79 213 gfp_t gfp)
540b1c48 214 __releases(&rx->sk.sk_lock.slock)
17926a79 215{
2341e077 216 struct rxrpc_call *call, *xcall;
2baec2c3 217 struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
2341e077 218 struct rb_node *parent, **pp;
e34d4234 219 const void *here = __builtin_return_address(0);
999b69f8 220 int ret;
17926a79 221
999b69f8 222 _enter("%p,%lx", rx, user_call_ID);
17926a79 223
9faaff59 224 call = rxrpc_alloc_client_call(rx, srx, gfp);
2341e077 225 if (IS_ERR(call)) {
540b1c48 226 release_sock(&rx->sk);
2341e077
DH
227 _leave(" = %ld", PTR_ERR(call));
228 return call;
17926a79
DH
229 }
230
e754eba6 231 call->tx_total_len = tx_total_len;
a84a46d7
DH
232 trace_rxrpc_call(call, rxrpc_call_new_client, atomic_read(&call->usage),
233 here, (const void *)user_call_ID);
e34d4234 234
540b1c48
DH
235 /* We need to protect a partially set up call against the user as we
236 * will be acting outside the socket lock.
237 */
238 mutex_lock(&call->user_mutex);
239
999b69f8 240 /* Publish the call, even though it is incompletely set up as yet */
17926a79
DH
241 write_lock(&rx->call_lock);
242
243 pp = &rx->calls.rb_node;
244 parent = NULL;
245 while (*pp) {
246 parent = *pp;
2341e077 247 xcall = rb_entry(parent, struct rxrpc_call, sock_node);
17926a79 248
2341e077 249 if (user_call_ID < xcall->user_call_ID)
17926a79 250 pp = &(*pp)->rb_left;
2341e077 251 else if (user_call_ID > xcall->user_call_ID)
17926a79
DH
252 pp = &(*pp)->rb_right;
253 else
357f5ef6 254 goto error_dup_user_ID;
17926a79
DH
255 }
256
248f219c 257 rcu_assign_pointer(call->socket, rx);
357f5ef6
DH
258 call->user_call_ID = user_call_ID;
259 __set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
fff72429 260 rxrpc_get_call(call, rxrpc_call_got_userid);
17926a79
DH
261 rb_link_node(&call->sock_node, parent, pp);
262 rb_insert_color(&call->sock_node, &rx->calls);
248f219c
DH
263 list_add(&call->sock_link, &rx->sock_calls);
264
17926a79
DH
265 write_unlock(&rx->call_lock);
266
2baec2c3
DH
267 write_lock(&rxnet->call_lock);
268 list_add_tail(&call->link, &rxnet->calls);
269 write_unlock(&rxnet->call_lock);
17926a79 270
540b1c48
DH
271 /* From this point on, the call is protected by its own lock. */
272 release_sock(&rx->sk);
273
248f219c
DH
274 /* Set up or get a connection record and set the protocol parameters,
275 * including channel number and call ID.
276 */
277 ret = rxrpc_connect_call(call, cp, srx, gfp);
999b69f8
DH
278 if (ret < 0)
279 goto error;
280
a84a46d7 281 trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage),
54fde423 282 here, NULL);
a84a46d7 283
248f219c
DH
284 rxrpc_start_call_timer(call);
285
17926a79
DH
286 _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
287
288 _leave(" = %p [new]", call);
289 return call;
290
2341e077
DH
291 /* We unexpectedly found the user ID in the list after taking
292 * the call_lock. This shouldn't happen unless the user races
293 * with itself and tries to add the same user ID twice at the
294 * same time in different threads.
295 */
357f5ef6 296error_dup_user_ID:
17926a79 297 write_unlock(&rx->call_lock);
540b1c48 298 release_sock(&rx->sk);
8d94aa38 299 ret = -EEXIST;
357f5ef6
DH
300
301error:
302 __rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
303 RX_CALL_DEAD, ret);
a84a46d7
DH
304 trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage),
305 here, ERR_PTR(ret));
357f5ef6 306 rxrpc_release_call(rx, call);
540b1c48 307 mutex_unlock(&call->user_mutex);
357f5ef6
DH
308 rxrpc_put_call(call, rxrpc_call_put);
309 _leave(" = %d", ret);
310 return ERR_PTR(ret);
17926a79
DH
311}
312
c038a58c
DH
313/*
314 * Retry a call to a new address. It is expected that the Tx queue of the call
315 * will contain data previously packaged for an old call.
316 */
317int rxrpc_retry_client_call(struct rxrpc_sock *rx,
318 struct rxrpc_call *call,
319 struct rxrpc_conn_parameters *cp,
320 struct sockaddr_rxrpc *srx,
321 gfp_t gfp)
322{
323 const void *here = __builtin_return_address(0);
324 int ret;
325
326 /* Set up or get a connection record and set the protocol parameters,
327 * including channel number and call ID.
328 */
329 ret = rxrpc_connect_call(call, cp, srx, gfp);
330 if (ret < 0)
331 goto error;
332
333 trace_rxrpc_call(call, rxrpc_call_connected, atomic_read(&call->usage),
334 here, NULL);
335
336 rxrpc_start_call_timer(call);
337
338 _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
339
340 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
341 rxrpc_queue_call(call);
342
343 _leave(" = 0");
344 return 0;
345
346error:
347 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
348 RX_CALL_DEAD, ret);
349 trace_rxrpc_call(call, rxrpc_call_error, atomic_read(&call->usage),
350 here, ERR_PTR(ret));
351 _leave(" = %d", ret);
352 return ret;
353}
354
17926a79 355/*
248f219c
DH
356 * Set up an incoming call. call->conn points to the connection.
357 * This is called in BH context and isn't allowed to fail.
17926a79 358 */
248f219c
DH
359void rxrpc_incoming_call(struct rxrpc_sock *rx,
360 struct rxrpc_call *call,
361 struct sk_buff *skb)
17926a79 362{
248f219c 363 struct rxrpc_connection *conn = call->conn;
42886ffe 364 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
248f219c 365 u32 chan;
17926a79 366
248f219c 367 _enter(",%d", call->conn->debug_id);
e34d4234 368
248f219c
DH
369 rcu_assign_pointer(call->socket, rx);
370 call->call_id = sp->hdr.callNumber;
371 call->service_id = sp->hdr.serviceId;
372 call->cid = sp->hdr.cid;
373 call->state = RXRPC_CALL_SERVER_ACCEPTING;
374 if (sp->hdr.securityIndex > 0)
375 call->state = RXRPC_CALL_SERVER_SECURING;
57494343 376 call->cong_tstamp = skb->tstamp;
248f219c
DH
377
378 /* Set the channel for this call. We don't get channel_lock as we're
379 * only defending against the data_ready handler (which we're called
380 * from) and the RESPONSE packet parser (which is only really
381 * interested in call_counter and can cope with a disagreement with the
382 * call pointer).
a1399f8b 383 */
248f219c
DH
384 chan = sp->hdr.cid & RXRPC_CHANNELMASK;
385 conn->channels[chan].call_counter = call->call_id;
386 conn->channels[chan].call_id = call->call_id;
a1399f8b 387 rcu_assign_pointer(conn->channels[chan].call, call);
17926a79 388
85f32278
DH
389 spin_lock(&conn->params.peer->lock);
390 hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
391 spin_unlock(&conn->params.peer->lock);
17926a79 392
17926a79
DH
393 _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
394
248f219c
DH
395 rxrpc_start_call_timer(call);
396 _leave("");
17926a79
DH
397}
398
8d94aa38
DH
399/*
400 * Queue a call's work processor, getting a ref to pass to the work queue.
401 */
402bool rxrpc_queue_call(struct rxrpc_call *call)
403{
404 const void *here = __builtin_return_address(0);
405 int n = __atomic_add_unless(&call->usage, 1, 0);
8d94aa38
DH
406 if (n == 0)
407 return false;
408 if (rxrpc_queue_work(&call->processor))
2ab27215 409 trace_rxrpc_call(call, rxrpc_call_queued, n + 1, here, NULL);
8d94aa38
DH
410 else
411 rxrpc_put_call(call, rxrpc_call_put_noqueue);
412 return true;
413}
414
415/*
416 * Queue a call's work processor, passing the callers ref to the work queue.
417 */
418bool __rxrpc_queue_call(struct rxrpc_call *call)
419{
420 const void *here = __builtin_return_address(0);
421 int n = atomic_read(&call->usage);
8d94aa38
DH
422 ASSERTCMP(n, >=, 1);
423 if (rxrpc_queue_work(&call->processor))
2ab27215 424 trace_rxrpc_call(call, rxrpc_call_queued_ref, n, here, NULL);
8d94aa38
DH
425 else
426 rxrpc_put_call(call, rxrpc_call_put_noqueue);
427 return true;
428}
429
e34d4234
DH
430/*
431 * Note the re-emergence of a call.
432 */
433void rxrpc_see_call(struct rxrpc_call *call)
434{
435 const void *here = __builtin_return_address(0);
436 if (call) {
437 int n = atomic_read(&call->usage);
e34d4234 438
2ab27215 439 trace_rxrpc_call(call, rxrpc_call_seen, n, here, NULL);
e34d4234
DH
440 }
441}
442
443/*
444 * Note the addition of a ref on a call.
445 */
fff72429 446void rxrpc_get_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
e34d4234
DH
447{
448 const void *here = __builtin_return_address(0);
449 int n = atomic_inc_return(&call->usage);
e34d4234 450
2ab27215 451 trace_rxrpc_call(call, op, n, here, NULL);
e34d4234
DH
452}
453
454/*
248f219c 455 * Detach a call from its owning socket.
e34d4234 456 */
248f219c 457void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
e34d4234 458{
a84a46d7 459 const void *here = __builtin_return_address(0);
248f219c
DH
460 struct rxrpc_connection *conn = call->conn;
461 bool put = false;
462 int i;
e34d4234 463
248f219c 464 _enter("{%d,%d}", call->debug_id, atomic_read(&call->usage));
e34d4234 465
a84a46d7
DH
466 trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage),
467 here, (const void *)call->flags);
17926a79 468
a84a46d7 469 ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
e34d4234 470
17926a79
DH
471 spin_lock_bh(&call->lock);
472 if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
473 BUG();
474 spin_unlock_bh(&call->lock);
475
248f219c 476 del_timer_sync(&call->timer);
17926a79 477
248f219c
DH
478 /* Make sure we don't get any more notifications */
479 write_lock_bh(&rx->recvmsg_lock);
e653cfe4 480
248f219c 481 if (!list_empty(&call->recvmsg_link)) {
17926a79
DH
482 _debug("unlinking once-pending call %p { e=%lx f=%lx }",
483 call, call->events, call->flags);
248f219c
DH
484 list_del(&call->recvmsg_link);
485 put = true;
486 }
487
488 /* list_empty() must return false in rxrpc_notify_socket() */
489 call->recvmsg_link.next = NULL;
490 call->recvmsg_link.prev = NULL;
491
492 write_unlock_bh(&rx->recvmsg_lock);
493 if (put)
494 rxrpc_put_call(call, rxrpc_call_put);
495
496 write_lock(&rx->call_lock);
497
498 if (test_and_clear_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
17926a79
DH
499 rb_erase(&call->sock_node, &rx->calls);
500 memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
8d94aa38 501 rxrpc_put_call(call, rxrpc_call_put_userid);
17926a79 502 }
17926a79 503
248f219c
DH
504 list_del(&call->sock_link);
505 write_unlock(&rx->call_lock);
506
507 _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
508
509 if (conn)
8d94aa38 510 rxrpc_disconnect_call(call);
e653cfe4 511
248f219c 512 for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
71f3ca40
DH
513 rxrpc_free_skb(call->rxtx_buffer[i],
514 (call->tx_phase ? rxrpc_skb_tx_cleaned :
515 rxrpc_skb_rx_cleaned));
248f219c 516 call->rxtx_buffer[i] = NULL;
17926a79 517 }
17926a79
DH
518
519 _leave("");
520}
521
c038a58c
DH
522/*
523 * Prepare a kernel service call for retry.
524 */
525int rxrpc_prepare_call_for_retry(struct rxrpc_sock *rx, struct rxrpc_call *call)
526{
527 const void *here = __builtin_return_address(0);
528 int i;
529 u8 last = 0;
530
531 _enter("{%d,%d}", call->debug_id, atomic_read(&call->usage));
532
533 trace_rxrpc_call(call, rxrpc_call_release, atomic_read(&call->usage),
534 here, (const void *)call->flags);
535
536 ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
537 ASSERTCMP(call->completion, !=, RXRPC_CALL_REMOTELY_ABORTED);
538 ASSERTCMP(call->completion, !=, RXRPC_CALL_LOCALLY_ABORTED);
539 ASSERT(list_empty(&call->recvmsg_link));
540
541 del_timer_sync(&call->timer);
542
543 _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, call->conn);
544
545 if (call->conn)
546 rxrpc_disconnect_call(call);
547
548 if (rxrpc_is_service_call(call) ||
549 !call->tx_phase ||
550 call->tx_hard_ack != 0 ||
551 call->rx_hard_ack != 0 ||
552 call->rx_top != 0)
553 return -EINVAL;
554
555 call->state = RXRPC_CALL_UNINITIALISED;
556 call->completion = RXRPC_CALL_SUCCEEDED;
557 call->call_id = 0;
558 call->cid = 0;
559 call->cong_cwnd = 0;
560 call->cong_extra = 0;
561 call->cong_ssthresh = 0;
562 call->cong_mode = 0;
563 call->cong_dup_acks = 0;
564 call->cong_cumul_acks = 0;
565 call->acks_lowest_nak = 0;
566
567 for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
568 last |= call->rxtx_annotations[i];
569 call->rxtx_annotations[i] &= RXRPC_TX_ANNO_LAST;
570 call->rxtx_annotations[i] |= RXRPC_TX_ANNO_RETRANS;
571 }
572
573 _leave(" = 0");
574 return 0;
575}
576
17926a79
DH
577/*
578 * release all the calls associated with a socket
579 */
580void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
581{
582 struct rxrpc_call *call;
17926a79
DH
583
584 _enter("%p", rx);
585
0360da6d
DH
586 while (!list_empty(&rx->to_be_accepted)) {
587 call = list_entry(rx->to_be_accepted.next,
588 struct rxrpc_call, accept_link);
589 list_del(&call->accept_link);
3a92789a 590 rxrpc_abort_call("SKR", call, 0, RX_CALL_DEAD, -ECONNRESET);
0360da6d
DH
591 rxrpc_put_call(call, rxrpc_call_put);
592 }
593
248f219c
DH
594 while (!list_empty(&rx->sock_calls)) {
595 call = list_entry(rx->sock_calls.next,
596 struct rxrpc_call, sock_link);
597 rxrpc_get_call(call, rxrpc_call_got);
3a92789a 598 rxrpc_abort_call("SKT", call, 0, RX_CALL_DEAD, -ECONNRESET);
26cb02aa 599 rxrpc_send_abort_packet(call);
8d94aa38 600 rxrpc_release_call(rx, call);
248f219c 601 rxrpc_put_call(call, rxrpc_call_put);
f36b5e44
DH
602 }
603
17926a79
DH
604 _leave("");
605}
606
607/*
608 * release a call
609 */
fff72429 610void rxrpc_put_call(struct rxrpc_call *call, enum rxrpc_call_trace op)
17926a79 611{
2baec2c3 612 struct rxrpc_net *rxnet;
e34d4234 613 const void *here = __builtin_return_address(0);
2ab27215 614 int n;
17926a79 615
e34d4234 616 ASSERT(call != NULL);
17926a79 617
e34d4234 618 n = atomic_dec_return(&call->usage);
2ab27215 619 trace_rxrpc_call(call, op, n, here, NULL);
e34d4234
DH
620 ASSERTCMP(n, >=, 0);
621 if (n == 0) {
622 _debug("call %d dead", call->debug_id);
248f219c 623 ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
17926a79 624
2baec2c3
DH
625 if (!list_empty(&call->link)) {
626 rxnet = rxrpc_net(sock_net(&call->socket->sk));
627 write_lock(&rxnet->call_lock);
628 list_del_init(&call->link);
629 write_unlock(&rxnet->call_lock);
630 }
e34d4234 631
8d94aa38 632 rxrpc_cleanup_call(call);
17926a79 633 }
17926a79
DH
634}
635
dee46364
DH
636/*
637 * Final call destruction under RCU.
638 */
639static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
640{
641 struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
642
df5d8bf7 643 rxrpc_put_peer(call->peer);
248f219c
DH
644 kfree(call->rxtx_buffer);
645 kfree(call->rxtx_annotations);
dee46364
DH
646 kmem_cache_free(rxrpc_call_jar, call);
647}
648
17926a79
DH
649/*
650 * clean up a call
651 */
00e90712 652void rxrpc_cleanup_call(struct rxrpc_call *call)
17926a79 653{
248f219c 654 int i;
17926a79 655
248f219c 656 _net("DESTROY CALL %d", call->debug_id);
17926a79
DH
657
658 memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
659
248f219c 660 del_timer_sync(&call->timer);
17926a79 661
8d94aa38 662 ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
17926a79 663 ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
e653cfe4 664 ASSERTCMP(call->conn, ==, NULL);
17926a79 665
248f219c
DH
666 /* Clean up the Rx/Tx buffer */
667 for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++)
71f3ca40
DH
668 rxrpc_free_skb(call->rxtx_buffer[i],
669 (call->tx_phase ? rxrpc_skb_tx_cleaned :
670 rxrpc_skb_rx_cleaned));
17926a79 671
71f3ca40 672 rxrpc_free_skb(call->tx_pending, rxrpc_skb_tx_cleaned);
17926a79 673
dee46364 674 call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
17926a79
DH
675}
676
677/*
2baec2c3
DH
678 * Make sure that all calls are gone from a network namespace. To reach this
679 * point, any open UDP sockets in that namespace must have been closed, so any
680 * outstanding calls cannot be doing I/O.
17926a79 681 */
2baec2c3 682void rxrpc_destroy_all_calls(struct rxrpc_net *rxnet)
17926a79
DH
683{
684 struct rxrpc_call *call;
685
686 _enter("");
8d94aa38 687
2baec2c3 688 if (list_empty(&rxnet->calls))
8d94aa38 689 return;
248f219c 690
2baec2c3 691 write_lock(&rxnet->call_lock);
17926a79 692
2baec2c3
DH
693 while (!list_empty(&rxnet->calls)) {
694 call = list_entry(rxnet->calls.next, struct rxrpc_call, link);
17926a79
DH
695 _debug("Zapping call %p", call);
696
e34d4234 697 rxrpc_see_call(call);
17926a79
DH
698 list_del_init(&call->link);
699
248f219c 700 pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n",
8d94aa38 701 call, atomic_read(&call->usage),
8d94aa38
DH
702 rxrpc_call_states[call->state],
703 call->flags, call->events);
17926a79 704
2baec2c3 705 write_unlock(&rxnet->call_lock);
17926a79 706 cond_resched();
2baec2c3 707 write_lock(&rxnet->call_lock);
17926a79
DH
708 }
709
2baec2c3 710 write_unlock(&rxnet->call_lock);
17926a79 711}