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