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